I am running a nodemcu with 2 relay with 2 push button to turn them on and off and also via phone (Blynk app). This is works fine . And am running a local server (for ref.) . Once the nodemcu reboots due to power failure, it turns back all the relay untill the Blynk.virtual sync All gets in contact with the local server to get the previous state. Because this takes almost a min or so for the loacl sever and the wifi router to come to life. So till then all the relays will be in on condition. And later sets itself to normal state.
So my doubt is can we for example say the nodemcu to stay off until it syncs with the server and only then turn on or off the relay according to the previous state, Instead of turning all the relays and then turning back off. Because this causes unwanted appliances to turn even though it was not on before power failure.
Your best/easiest bet is some form of battery backup on your node/router/network… otherwise, I for one do not see any easy way to NOT turn on the NodeMCU until it detects all conditions are perfect… Although, perhaps yet another MCU monitoring network conditions and activating the primary when all is good.
Yeah ! Battery does work. But again a separate circuit for battery management system is not good. it becomes bulkier, And there is no much room to stuff all these into a tight space such as wall board.
This may work. But i feel this will stop the nodemcu from booting up normally. Because the pin needs to pulled up high and low for few pins. By defining pinmode and digital write it may prevent the nodemcu to boot up.
Can you please provide a link that may help me in trying this idea ?
Or a ex. line of code that i can insert in and check. Because doing wrong may not solve the problem even thought the given solution is right ! i will definitely try myself and let you know if that works.
Not true… because the Node MCU will have already booted before getting to these commands. Besides, you shouldn’t be using special pins anyhow, else the sensors/controllers hooked up to them could influence the boot.
Back to the pinMode() commands, these commands would run after boot, but before the network setup… so you could preset the safest defaults until such time the connection is made and the last Blynk settings synced.
Just pick up an ESP8266 (or ESP32) with built in 18650 module… it will contain all the necessary battery management in as small a package as you are likely to get.
My mistake, I should have said to put it in the void setup, not loop. I’ve edited my post to correct this, so as not to confuse anyone else who reads this.
Right now the pinmode is set as output and the digitalwrite is all set to low. As per your idea i need to set these High. Or another function for this ?
I just ran across this issue with a garage door opener project. The problem was that the power would fail, the relay drive pin would startup as high and the garage door would open when the power would return. This would happen prior to my notification text being sent so I could end up with an open door and not know it! That would have been bad.
The fix I discovered was that not all the pins on the nodemcu come on as high, some com on as low!
You have to test your pinouts to see which boot up hot (prior to the bootloader even getting to the code!) and which boot up low. I simply switched my output relay driver pin from D5 to D6 and the problem was solved. Not all boards are the same, so you have to test the pins at startup.
Hope this helps,
Scott