I don’t know exactly how it’s work te missing parameter
but if I take a look to the messages using MQTT.fx software I see all messages with QoS = 0
and the pollaio/switchStatus/ topic have a retained status.
Let’s take a scenario…
The actual door satus is “UP” and the NodeMCU is reseting for various reason.
The default of internal variable “doorSwitch” when power on is set to “down”
Here is a log of what is happen when I power on the NodeMCU.
connected with DaVinci17Mesh, channel 6
dhcp client start...
IP address: 192.168.1.152
Attempting MQTT connection...
MQTT Callback Topic: pollaio/switchCMD/ <<< here is the first callback routine at the connection
MQTT Callback Payload = 1 (go UP) <<< here arrive the retained message cause the last msg was "1"
MQTT ACTUAL doorSwitch = DOWN <<< the default variable at power on is DOWN
MQTT > door go UP
motorUP FINISH ACTUAL doorSwitch = UP
Sabato, 9:26:58 L O O P > doorSwitch = UP
The logic is:
- if arrive cmd UP and the internal doorSwitch variable is DOWN, the motor will go UP
- if arrive cmd UP and the internal doorSwitch variable is already UP, the motor will do nothing
- and viceversa for cmd DOWN
I use the internal variable because I have also a push button on the chicken coop
that toggle the open/close door when I’m near the chicken coop.
But… now I have another idea…
I’ll insert a new internal variable, for example PowerON
The default of PowerON variable at power on will be “0”
At the callback routine will do:
-if PowerON == “0” > set the doorSwitch == retain msg that arrive, and set the PowerON to “1”
-if PowerON == “1” > do the logic and go and do the motor routine
Is not so nice, but I think will work
In order to thanks here is a pic of chicken coop that I made
Thanks a lot for your help