0

I have connected my ESP32 board to a 12V LED using a MOSFET transistor and is operated using a touchPin. The idea is to have 2 identical boards and when I turn on the lamp on one board it should also light up on the other using Wifi (as the boards will not be located in the same physical location and will be connected to different networks).

On their own they do work like a charm but as soon as I want to retrieve the other boards transistor state via a GET request it does not work as intended, meaning the transistor state is not passed to the other board and thus doesnt affect the LED. I suspect the error might be in my void setup() see below

void setup() {
  Serial.begin(1155200);
  delay(1000);
  // Initialise the LED Pin as an output
  pinMode(transistor, OUTPUT);

  // Send a GET request to <IP>/led/<number>/state/<0 or 1>
  server.on("^\\/led\\/([0-9]+)\\/state\\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String ledNumber = request->pathArg(0); // currently unused - we use only a predefined LED number
    String state = request->pathArg(1);

    request->send(200, "text/plain", "LED: " + ledNumber + ", with state: " + state);
  });

Or in the for-loop in the void loop() as shown below:

void loop() {
  while (1) {
    
  touchValue = touchRead(touchPin);

  //Serial.print(touchValue);

  if (millis() - lastTimeButtonStateChanged > debounceDuration) {
  // check if the touchValue is below the threshold
  // if it is, set ledPin to HIGH

    buttonState = (touchValue < threshold) ? HIGH: LOW;

    if (buttonState != lastButtonState) {
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
      if (buttonState == LOW) {
        transistorState = (transistorState == HIGH) ? LOW: HIGH;
        digitalWrite(transistor, transistorState);
        if(transistorState == LOW){
          transistorStateInt = 0;
        } else{
          transistorState = 1;
        }
      }
    }
  }
  for (auto const &host : Husarnet.listPeers()) {
    IPv6Address peerAddr = host.first;
    if(host.second == "master") {
      ;
    } else {
      AsyncClient* client_tcp = new AsyncClient;
      
      client_tcp->onConnect([](void *arg, AsyncClient *client) {
        String requestURL = "/led/1/state/" + String(transistorStateInt);
        String GETreq = String("GET ") + requestURL + " HTTP/1.1\r\n" + "Host: esp32\r\n" + "Connection: close\r\n\r\n";

        if ( client->canSend() && (client->space() > GETreq.length())){
          client->add(GETreq.c_str(), strlen(GETreq.c_str()));
            client->send();
        } else {
          Serial.printf("\r\nSENDING ERROR!\r\n");
        }
      }, client_tcp);

  }
}
CatiaV5
  • 168
  • 1
  • 1
  • 10
  • 1
    Nobody can guess "does not work as intended" means, if you put up so much code, you should be able to do some debug on the request you received or explain it better. – hcheung Sep 16 '21 at 01:38
  • Break down your question and code to *minimal* example. It looks like you just have a problem sending/requesting data using GET, so ask it that way and don't explain how you actually generate the signal, if this is not part of the problem. – Marc Sep 16 '21 at 04:48
  • Are you sure that 3.3V is enough to open/close MOSFET? Which transistor are you using? I would recommend to play with transistor with minimal code - just send HIGH level and check that it is working. – Aleksej Vasinov Sep 16 '21 at 05:07
  • @Marc thank you and apologies. I have changed it now. I suspect the error is in the GET request. But again I am not sure – CatiaV5 Sep 16 '21 at 12:06

0 Answers0