What you’re doing here is setting the digital pins on the NodeMCU LOW by writing to them directly using a digitalWrite
command:
BLYNK_WRITE(V0) // switch which makes all loads off by single push button
{
if (param.asInt())
{
digitalWrite(D0, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}
}
Instead, you should be setting the switch widgets in the app to Off, using the Blynk.virtualWrite
command, like this:
{
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
Blynk.virtualWrite(V4, 0);
}
However, your initial code has some problems…
The switch widget attached to V0 will (by default) send a “0” when it’s turned off, and a “1” when it’s turned on.
The code:
if (param.asInt())
is effectively saying “if the value that comes back from the switch widget on V0 is true, then do this…”
In C++ terms, true equals 1 and false equals zero, so your code would perform these digital writes (or virtual writes if you changed the code) when the switch on V0 is turned On.
Another way of writing this line of code is:
if (param.asInt()==1)
I suspect also that your relay is probably an active LOW device, in which case setting the pin to LOW will actuate the relay rather than de-actuate it.
The final issue is the pins that you’ve chosen to use, and how you’re referring to them in your code. You’re using the “D” numbers that are screen-printed on to the NodeMCU, rather than referencing the GPIO numbers of the pins. This help with the wiring, but makes it more difficult to use the same code on other devices, and also makes it more difficult o work-out which pins you shouldn’t be using.
If you look at this diagram…
you’ll see that you’ve chosen to use GPIO16. 5, 4 and 0.
If you look at this table…
you’ll see that GPIO0 needs to be HIGH at boot. connecting a relay to it could prevent the NodeMCU from booting, so you should avoid that pin.
Pete.