Stability problems/connection to blynk server is lost

Hello, I have a problem with my blynk projects:

Everything works as expected but after a while (most of the times after a few hours) the connection to the blynk server is lost and reconnection seems not to work.
In my recent project I am using an Arduino MEGA with a Sparkfun ESP 8266 WifiShield but I had the same problem with an UNO and connection via USB/Serial.
I reduced the size of the project for the upload, now only one sensor (DHT22) is connected, but still the same issue.

I tried to include a blynk-reconnect-function someone posted in the forum but it seems not to work all the time:

void reconnectBlynk() {
  if (!Blynk.connected()) {
    digitalWrite(LED_PIN2, LOW);
    digitalWrite(LED_PIN, LOW);
    BLYNK_LOG("not connected");
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }

}

When I disconnect the power of the Wifi-Shield (or the Arduino) and reconnect everything works fine, but independt temperature logging for a few days (for example) is not possible.

Here is the complete project:

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char auth[] = "xxxx";

char ssid[] = "xxxx";
char pass[] = "xxxx";

#define EspSerial Serial1

#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

#define DHTPIN 31
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

#define LED_PIN 22
#define LED_PIN2 24

SimpleTimer timer;

unsigned long currentMillis;
float t, h;
int t1 = 20;


void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature(); 
  

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V0, t);
  Serial.println("timestamp:");
  Serial.println(currentMillis / 1000);
  Serial.println(t);
  Serial.println(h);

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print(" C");
  lcd.setCursor(0, 1);
  lcd.print("Hum : ");
  lcd.print(h);
  lcd.print(" %");
}
  

void reconnectBlynk() {
  if (!Blynk.connected()) {
    digitalWrite(LED_PIN2, LOW);
    digitalWrite(LED_PIN, LOW);
    BLYNK_LOG("not connected");
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }

}


void setup()
{
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED_PIN2, OUTPUT);

  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);

  timer.setInterval(3000, sendSensor);

  timer.setInterval(5000, reconnectBlynk);
}

void loop()
{
  if (Blynk.connected())
  {
    digitalWrite(LED_PIN2, HIGH);
    Blynk.run();
  }
  timer.run(); // Initiates SimpleTimer
  currentMillis = millis();

}


It would be really great if someone could help with this, besides this stability problems blynk is awesome! :grin:

try instead

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  currentMillis = millis();

}

then add digitalWrite(LED_PIN2, HIGH); below BLYNK_LOG("Reconnected");

1 Like

Okay, I changed the code as suggested. I wanted the LED to show me if a connection to the blynk server is established, what is not exactly what it does now (only after the first reconnection).

But I noticed some strange behavior in the SerialMonitor:

connection lost for 1st time:

[156960] Not reconnected
[156987] Ready (ping: 27ms).

so no “reconnected” message is transmitted (and the LED stayed off) but the connection was nevertheless established!

A few seconds later:

[311493] not connected
[321845] Ready (ping: 27ms).
[322421] Reconnected

Now the LED is turned on…

I tested it a few days and it seems to run stable :slight_smile:.

But now I switched to the UNO with network connection over USB (connected to a Raspberry Pi). And unfortunately the connection is not reestablished when I disconnect the ethernet cable of the pi for a short time as a test.
Only unplugging the USB-Cable or holding the reset button for a longer time works.

Any idea how to build this more “fail safe”?

Add a true ethernet port to the Uno (ENC28J60, really easy to get going) or get a Wemos for Wifi connection. USB via Pi will never be really stable and certainly not production-ready :slight_smile:

I had the setup running since yesterday (~20h) but now no more data is logged and the hardware LED on the Arduino which is programmed to show the state of Blynk.Connected() is off. The strange thing is that in the Blynk-App no “your Arduino UNO is not in network” is shown, on my other (not connected) projects this message appears. I waited over an hour but still no connection. In the terminal of the Pi nothing is logged. Only a ~30sec long press on the reset button solved the problem :worried:

I use the Pi because it has built in ethernet and WiFi which works without any problems and…I already bought it.

My project is lot bigger than this example and I think not all of my librarys will work with the Wemos/ESP8266.
So I would really appreciate any hint to solve this, maybe this “hard reset” can be implemented in software?!

I’m getting confused here, how do you want to run your stuff, on the Pi or AVR hardware? If your libraries are compatible with the Arduino IDE, they will run on the ESP/Wemos. Are there any libraries in particular about which you are worried? The Wemos is a way more capable platform than actual Arduino AVR hardware, just a hint :slight_smile:

There is something of a reset function possible:

void(* resetFunc) (void) = 0;

BLYNK_WRITE(V0)  {  if(param.asInt()) { resetFunc(); } }          // Reset function

This will reset your arduino when you press a button attached to V0.

UPDATE:

Temperature-/Humidity-logging stopped again, Blynk.Connected() seems to be FALSE because the attached LED is off. Reading an analog value via a widget in the blynk app doesn’t work too. Nevertheless the connection is not completly lost, when I add a button in the blynk app and attach it to Pin D13, the RX LED soldered on the UNO board flashes when I press the button and the LED on pin 13 turns on (although not very reliable). So there is still a kind of connection to the blynk server but unfortunately no more readings of the inputs are possible :sob:.

@Lichtsignaal:

I’m getting confused here, how do you want to run your stuff, on the Pi or AVR hardware?

Of course on the AVR, see code above. The Pi just acts as a gateway because of its built-in ethernet/wifi.

Are there any libraries in particular about which you are worried? The Wemos is a way more capable platform than actual Arduino AVR hardware, just a hint

I have to admit that I didn’t check the compability with the WEMOS for every sensor, but the thing is that there is already a complete (2nd) setup “in the field” which I can not simply change to the wemos. For future projects I think I will have a closer look at the ESP8266.

This will reset your arduino when you press a button attached to V0.

But not if I have no connection?! :grinning: But yeah of course I could attach it to the Blynk.Connected() function. I will try it with this construction, but it seems as if this is not a very clean way to do this :unamused:

It sounds like a complicated solution to me. You can buy a ENC28J60 ethernet port for about 3 bucks. Much more stable to directly attach your Arduino to the network. Or get the Wemos if you need Wifi. Only thing with the Wemos is, it has only one analogue port, so you need some kind of multiplexing if you attach more than one analog sensor.

I think it behaves the way it does because of the Pi in between. I suggest trying to eliminate that via forementioned Ethernet port or Wifi directly on the Wemos :slight_smile:

In the project which is already in use I definitely need (stable!) WiFi, there is no ethernet available. And as I said I can’t simply switch to another platform because I am not on location. :unamused: I would really appreciate a software solution.

Well, I think you have a hardware issue and that cannot be solved in software. Question: how do you power your ESP board? Because it looks like it doesn’t get enough power.

The Blynk.run() should take care of all reconnecting. There is, as far as I know, no need to do it “manual” in the code.

The problem with Blynk.run() alone is that it can stop all the rest of your code running (when there is a network problem) so it really should be enclosed by the Blynk.connected() if statement. This then leads on to requiring a SimpleTimer to try to make a connection to Blynk at intervals etc.

1 Like

True, that is why I said “should” :wink:

It can never really hurt to double check things, but it can lead to more complex code. Basically there should be a good stable hardware solution so as to have the software optimized for doing it’s thing without too much error handling.

You mean the UNO Board?! (No ESP is involved). At the moment it is powered over the USB Ports on the Pi. The Pi is powered by the official Raspberry 2A power supply.

So you recommend to change my code back to the version in my first post?!

I am not saying your code is bug free or that it doesn’t need modification if you are not using ESP in standalone mode.

What I am saying is that all networks go down at some point and Blynk.run() alone will stop the rest of your sketch from running as it will spend all it’s time trying to reconnect. For some people if the network is down then their sketch doesn’t need to run but if it does use the if statement in loop() and produce bug free way of checking if the network is back up and reconnect etc.

From my experience ESP in standalone mode can reliably reconnect but most of the other methods can’t. For these methods, which I no longer use, I would also be looking to incorporate the WDT to reset an Arduino when the network fails.

Sorry, I read ESP and my mind went wandering off, lol.

The UNO doesn’t take a lot of power, but it could be worth a try with a separate power supply to the UNO.

The UNO doesn’t take a lot of power, but it could be worth a try with a separate power supply to the UNO

I tried it but was not successfull. Because everything worked very unstable I switched again to connection with the ESP8266 wifi module.

Unfortunately even with the recommended if statement

  {
  Blynk.run();
  }```

the rest of the code is not working when connection is lost