Hi to everybody,
I've gone through the thread and I haven't been able to find a solution to my problem. I've implemented, with some modifications, a project taken from Random Nerd Tutorials aimed to control with Alexa the electric roller shutter, at home. The project works fine but what drives me mad is the following:
I can voice activate two relays without any issue but I also need to keep them excited for just 30 seconds. Consequently I added, in the void loop() the following piece of code
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
// check if delay has timed out after Roll Up Time
static unsigned long last = millis();
Serial.println(last);
delay(500);
if (millis() - last > RollUpTime) { // RollUpTime = 42000
last = millis();
Serial.println(millis());
delay(500);
digitalWrite(RELAY_PIN_1, HIGH);
digitalWrite(RELAY_PIN_2, HIGH);
}
}
Now, I had to put 42 seconds as Roll Up Time in order to achieve 30 and, as a matter of fact, the 30 seconds is not repetitive. It varies by 3-4 seconds around its target value.
I'm sure I'm overlooking something simple, stupid, I'd say, because of my lack of experience.
Can anybody help me? I don't want to revert to using Delay(). I'd like a more elegant solution
Many thanks in advance
Many thanks for the prompt reply.
Actually I'm using an ESP8266-01S module, fairly cheap. It could be the xtal but, then, why it is not repetitive?. As I wrote, the time interval isn't always 30 sec. Sometimes it's 27, other times it's 33.
I'm using an accurate electronic chronograph to measure the time the two relays are on and I may be slow or fast by a sec, not more.
To clear the table from the doubts on the xtal I will repeat the experiment on an ESP32 WROOM module that looks a bit better but the question mark about the lack of repetitiveness remains. Could it be the fauxmo.handle()? I know that on the ESP8266-01S the CPU resources are divided between the WiFi and the rest. Might it influence, somehow, the mills count?
I run a quick check replacing > with >= and, actually, it seems to have gained in repetitiveness. What still remains weird is the time, now it is 35 sec instead of 42. I've to run it on a different module, ESP32 based, as I wrote
Yes Coding Badly, and I got the message. The title of the thread, and many of the posts, were perfectly describing my problem and I jumped into it.
My apologies.
I'm afraid you're right. The fauxmo.handle() and the call back fauxmo.onSetState, in the setup section, are interacting in a way that is a bit obscure to me.
I've to find another way to set a timer for resetting the relays once they have been activated by a voice command. I'm even considering an HW based solution (NE555 triggered by the microcontroller)