Controlling a sonoff S20 device
I've just finished writing an instruction sheet, for my IoT students, on how to control a SonOff S20 mains switch.
Note: This exercise uses a WeMos D1 Mini, Node-RED and MQTT (mosquitto) and of course a SonOff S20.
I hope you enjoy reading this as much as I enjoyed writing it.
Regards from David
PS: The document, in the above link, has been updated to implement the comments from @zenofmud.
3 Likes
Nice and a coulple comments
The word âflowâ is over used since everything on every tab is your âflowsâ while a series of nodes connecter together is a flow, and all the nodes on a tab make up a flow. I'd say:
The âflow-variableâ is usable by all nodes in a flow tab.
This is a nitpick but I think it helps reduce confusion.
In the âPass repeated signal ONCEâ node you have
if ( (msg.payload == 1) && (sonoff_s29_status == "off") )
{
msg.payload = 1;
sonoff_s29_status = "on";
flow.set("sonoff_s20_status",sonoff_s29_status);
return msg;
}
Since msg.payload is already 1 this could be written
if ( (msg.payload == 1) && (sonoff_s29_status == "off") )
{
flow.set("sonoff_s20_status","on");
return msg;
}
same for the second âifâ.
Another case that is not handled - what if the device it 'on' when the flow starts. The 'Initial conditionsâ function will set âsonoff_s20_statusâ to âoffâ even though it is on. If you try to turn it off, the code will think it is off so it wonât turn it off and the light stays on. Might want to check the status of the light when the flow starts OR use this as a test case for the kids to figure out how to handle it.
Thanks for your inputs Paul. I'll include your improvements later today.
I meant to include a wire from the 'initial_conditions' to turn the SonOff off at the start - for some reason this got omitted. I'll put that right later today.