I'm trying to publish to the mosquitto test server when there's a button press in the Photon. However, it only runs once even though there's a loop present.
if(buttonState == LOW)
{
// if light is currently off, switch to on
if(ledStatus == "off")
{
ledStatus = "on";
if (client.isConnected())
{
digitalWrite(led7, HIGH);
client.publish("doorOpen", "Door Opened");
//client.publish("timeStamp", Time.timeStr());
}
delay(20000);
digitalWrite(led, HIGH);
digitalWrite(led7, LOW);
delay(5000);
ledStatus = "off";
digitalWrite(led, LOW);
}
// else light must be on, so switch to off
else
{
}
}
// wait 0.2 seconds before checking button again
delay(200);
client.loop();
Not too sure what I'm doing wrong. I also noticed that it won't publish at all if the if(client.isConnected()) is located after the delay(20000);
Would the delay be a reason to not publish at all?
I’ll add more functionality for the if client isn’t connected. Thanks !
As for the context, that is pretty much the main body of the code. I just skipped copying the setup and the initializing parts of the code. ledstatus is originally set as off. As for the project, it’s sort of a concept demo I’m working on for proper hand sanatization timer. So I can’t take the delays out. Or rather I need some method to create that 25 second gap.
You can, you just need to be clever about it
For simple time stuff like that you can use millis() as a base and measure the time between the last state to the next.
For more elaborate stuff FSM would be the paradigm to follow.