I’ve built an ESP8266 using a WEMOS D1 Mini Pro and relay that I need to trigger a momentary connection.
This is being accessed as a web service using ‘ESPAsyncWebServer’. I’m happy with the aspects of using the web service, setting up the config over WiFi, saving it to SPIFFS etc. but the frustration comes when trying to trigger the momentary relay connection.
const int relayPin = D1;
const long interval = 500;
void togglePower() {
Serial.println("Relay triggered!");
digitalWrite(relayPin, HIGH);
digitalWrite(D4, LOW);
delay(interval);
digitalWrite(relayPin, LOW);
digitalWrite(D4, HIGH);
}
What happens is the relay gets triggered, but then sticks in that triggered state.
I think it’s because the Async web service is not waiting (as per) for the relay to toggle back after the delay. At the point of delay() we get no further in the code.
My main thrust is to act on an api call:
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/api", [](AsyncWebServerRequest *request, JsonVariant &json) {
togglePower();
}
There’s some more clever stuff around it that checks the authenticity of the call etc, but as the togglePower() is called Asynchronously it fails as control is returned to the loop I guess.
In the worlds of of JavaScript I’d use Async/Await or promises. I tried to look at using future and promises, but all the c++ reference material I found is a different syntax and compiling my code fails.
Can someone point me to a source where I can get a working example of future/promises on Arduino?
I’ve attached the full .ino if any one is interested. It’s going to end up public on Github when I get it working anyhow.
Many thanks for looking.
scorpion-lite.ino (7.4 KB)