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!