Loss of wifi connection, stop code

I have to clarify a doubt:

if a device in the field like Wemos D1 mini “loses” the communication with the server (for example) because of a failure to the access point, with which function could I stop the operations that happen automatically on Wemos?
A code could be this:

bool connect = Blynk.connected();
if(connect != true){
    //stop code for loss of connection to the wifi network
}else{
    //normal operation when connected device
}

Can this function not only stop the code but also reboot on reconnection?
What other solutions (perhaps with some examples) could be useful for this purpose?

thank you so much

Stopping the code when disconnected is usually as simple as using Blynk.begin() as it is a blocking command… no connection no run.

Most want to do the opposite, keep code running and attempt reconnection… that uses Blynk.config() instead, as well as other various commands. There a many ways of doing such, search this forum, but here is one.

1 Like

In fact, I need a sketch in the sketch, which is updated through the use of “bridge”, steps to 0 if the connection is missing. This variable dynamically controls the temperature control of a boiler and I need the boiler to switch off (variable = 0) if there is no control (connection with the server).
What do you think about it?

I think you need to learn how a single sketch with OTA for updating works and that Bridge is for sharing data and controls from one MCU to a totally different MCU :stuck_out_tongue_winking_eye:

Otherwise, yep, no problem (well, coding skills depending)… Although one would normally make the sketch have fully autonomous control of the boiler anyhow, with fall back contingencies for things like loss of WiFi, power backup, reboots, etc.

The App is better meant for monitoring and adjustments, not actual 24/7 control.

Unfortunately I can not explain how my simple system works, I try again:

master A (Arduino Mega + eth shield) >>> via Bridge >>> slave B (wemosD1)

master sends values ​​to the slave via Bridge

the values ​​sent every two seconds to the slave regulate the times, variables, on / off the boiler (a sort of PWM control).

if the value received by the slave is 0, the boiler is off, if it receives the value 198 the boiler is always on. If the value sent is, for example, 99, the boiler is on at 50%. If at this point there is a loss of wifi connection (or for any other reason connected to the network) the boiler remains on at 50% even if the master is sent the value 0. This is not good. The OTA you mentioned for now is not implemented. I need, on the slave side, that if a connection loss occurs (of any kind, due to access point failure, to the server crash that I have at home then not cluod, etc. etc.) the value that is expected to arrive from the master goes to 0 (and therefore turns off the boiler) until a subsequent connection reset. In another way I do not know how to explain :slight_smile:

Quite unusual setup, I must say., but if you want it that way…

You actually answered yourself:

if(Blynk.connected()){
    //run normal stuff
}else{
    //set the PWM to '0', eventually try to reconnect or reset 
}

I think I’d use a timer on the slave side, (set to say 10 seconds - depending in your needs).
Every time a message arrives from the master (should be every 2 seconds) then the timer is reset to zero. If you miss a number of consecutive messages - in this case 5 - then you turn your boiler off.

This has the advantage of not only monitoring the Wi-Fi and Blynk connection functionality, but also ensuring that your master device is online and functioning. Using Blynk.connected won’t necessarily give you the same level of end-to-end protection.

Pete.

2 Likes

what you have proposed to me is an excellent solution. I thought of using a function already present on Blynk not to burden the code but in fact the timer is safer.

1 Like

Ah, yes… had you mentioned this earlier it would have made more sense :stuck_out_tongue:

As mentioned before, the best way is to make your Wemos code run autonomously and be able to make its own determination to shut off the boiler under preset circumstances, time, loss of server connection, overheating, etc.

Then as long as the link is up and the temp is OK, it will keep going as normal, but if the link goes down, then you could have have a timer kick in to wait as required before shutting things down, in case the link reestablishes sooner than later.

BTW, Blynk Timer does NOT require a Server or internet link to work.