Automatic reset if Blynk not connected

I use Arduino uno’s with Ethernet sheild and run a local Blynk server. Every now and then, my arduino’s seem to stop connecting with the server and I get them going again with a press of the reset button on the Ethernet sheild.

I’m looking for a solution that would detect the loss of connection to the Blynk server and then automatically reset the Arduino/Ethernet sheild.

Can it be done? Does anyone else have similar issues or a solution to the disconnections.

2 Likes

I think there is something called BLYNK_DISCONNECTED which you can probably use the set the reset pin of the board high. That would be the obvious solution I think. Otherwise, there is also a method to reset your whole Arduino, but I’m not sure that is what you want (or need).

There certainly is a BLYNK_CONNECTED, with some logic you can achieve the same. You can use them like BLYNK_WRITE etc.

Or use Blynk.disconnected == 1 with an if statement in the loop. Not sure you want that, but it’ll probably do the trick too :slight_smile:

1 Like

@NickMurray in the good old days when I was using those Arduino things I had sketches like the one below to cover your requirement :smile:
Change Mega bits to Uno bits and ESP bit to Ethernet etc.

/* Code extract to reset Arduino (Nano's require optiboot to fix WDT bug)
 * You need to add all the regular Blynk libraries for your specific system (sketch is for Mega with ESP8266 shield)
 * Sketch will not compile for Uno until you modify for that hardware i.e. just one serial port etc
 * Sketch shows Watchdog Timer option for general Arduino lock ups and resetFunc for disconnects with Blynk.
 */
//#define BLYNK_DEBUG         // Comment this out to disable prints and save space
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>

#define EspSerial Serial1   // Set ESP8266 Serial object

char auth[] = "YourAuthToken";

ESP8266 wifi(EspSerial);
#include <avr/wdt.h> // watchdog timer, maybe set at 8 seconds in the sketch
#include <SimpleTimer.h>
SimpleTimer timer;

int disconnects; // number of times disconnected from server
byte pinV11 = 0;  // flag to ensure LED on Blynk V11 is only lit once for all disconnects.

void(* resetFunc) (void) = 0; //declare reset function @ address 0 THIS IS VERY USEFUL

void checkBlynk(){  // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false){
    disconnects++;     
  }
  if(disconnects > 0){
     if (pinV11 == 0){  // this stops the sketch sending 255 to V11 over and over again
       Blynk.virtualWrite(V11, 255);
       pinV11 = 1;
       resetFunc();
     }
  }
}

void setup(){
    
  Serial.begin(9600); // Set console baud rate
  delay(10);
  EspSerial.begin(115200); // Set ESP8266 baud rate
  delay(10);

  Blynk.begin(auth, wifi, "ssid", "pass");
  wdt_enable(WDTO_8S);  // set WDT to 8 seconds
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  wdt_reset(); // if we get here turn off 8 second WDT  
}
1 Like

@NickMurray it would be also nice to get more information about disconnects. Did you try to investigate why that happens? What conditions lead to that? Maybe some debug, logs output? We would love to fix issue if problem on our side. But we need more info from you. For instance my arduino UNO if up for few weeks already.

Correction to the sketch above for lighting the red LED disconnected (at some point) notice.

if((disconnects > 0){

needs to be:

if((disconnects > 0) && (isconnected == true)){ // need to be connected to be able to do the virtualWrite

Thank you for the responses. I appreciate you all taking the time to share your ideas. I’m currently distracted with other priorities for the next few days but I’ll report back here after that with how I move forward.

Hello everyone,

I am using Arduino Mega and Wifi shield. I am using CC3000 Adafruit example from the blynk examples and everything works perfect.

Sometimes Arduino gets offline (it simply hangs). If the connection is out - for more than 30 secs- , I need to program Arduino to be able to reset itself.

I knew that one rigid way of doing this is through Watchdog timer. I also read through Arduino forums, how to get the wdt to work for more than 8 seconds. but due to my limited programming capabilities, I don’t know where to integrate this in the blynk example.

If you please could guide me, how to program my Arduino correctly (What am I missing in the code? What to add?), I would be so grateful,

Here is my best code:
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example shows how to use Adafruit CC3000 breakout
* to connect your project to Blynk.
* Feel free to apply it to any other example. It’s simple!
*
* For this example you need Adafruit_CC3000_Library library:
* https://github.com/adafruit/Adafruit_CC3000_Library
*
* Note: Firmware version 1.14 or later is preferred.
*
* 1. Update pin definitions according to your setup.
* 2. Change WiFi ssid, pass, and Blynk auth token
* 3. Run :slight_smile:
*
**************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

// These are the interrupt and control pins for СС3000
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <BlynkSimpleCC3000.h>
#include <avr/wdt.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "blabla";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "bla", "bla", WLAN_SEC_WPA2);
}

void loop()
{
  if (Blynk.connected()){
  Blynk.run();
}
else {
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
  myWatchDogEnable(0b100001); //8 seconds
}
}

void myWatchDogEnable(const byte interval){
  MCUSR=0;
  WDTCSR|=0b00011000;                               //set WDCE, WDE
  WDTCSR=0b01000000|interval;                       //set WDIE & delay
  wdt_reset();
  Blynk.run();
}
ISR(WDT_vect){
  wdt_disable();
}

Hi @Costas,

i’m interested on this item, only (sorry I’m a begginer on C++) I’ve not well understand checkBlynk function.
What is the function of pinV11 ? You set it to 1 just before reset; why?
Sorry again for so simple questions.
My Mega+CC30000 project sometime losts communication and it seems blocked. Only by hardware reset it start to work again. So I’m looking for a automatic reset. Watchdog 8s results too short because of my system needs more than 10secs to recover the communication.
rgds

@zetalif Ok I will see if I can remember what the sketch does.

pinV11 was simply a variable that I used against a Blynk LED (on virtual Pin 11) to indicate if my Arduino has disconnected at any point. I think it was an attempt to ensure I didn’t flood the Blynk server, with Blynk.virtualWrite(V11, 255), but probably not important for 2 reasons:

A. The time interval was set at 3 seconds and doing Blynk.virtualWrite(V11, 255) every 3 seconds wouldn’t flood the server.

B. If my internet connection was down for a significant amount of time it wouldn’t be able to write to the server in any case.

The benefit of the resetFunc() is that you don’t need any special bootloader on the Arduino. If your Mega is less than 2 years old you might have the correct bootloader for WDT but if not you would need to install it to ensure WDT works correctly. The downside with resetFunc() is that it might not fully reset your Mega.

The variable disconnects could be used to only call the resetFunc() if say 5 disconnections have been made, either in total or over a given period.

ok @Costas,

thank you very much for your suggestions.

rgds

Дмитрий, проблема с UNO + Ethernet shield проявляется в моменты выключения общего питания 220 вольт в квартире, и вероятнее всего причина в том что UNO поднимается раньше чем появляется интернет, т.к. мой Keenetiс перезагружается около 3 минут! В моменты зависания на Ethernet shield не горят светодиоды 100М и FULLD которые обычно горят постоянно. Надеюсь это поможет в решении проблемы!

Юрий Поляков

Дмитрий, добрый день,
если нужна отладочная информация сообщите, пожалуйста, какие инструкции нужно задействовать в коде, я готов повторить эксперимент с выводом лога, т.к. проблема с ethernet интерфейсом очень популярна на форумах

Юра