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);
}
}