Blynk.begin and WiFi timeout

Hello there,

I have been designed a pool system managing the filtration time during the day depending on the water temperature.

Basically it is an arduino MEGA a DS3231 RTC, a temperature probe, some relay shield, and an ESP8266 (ESP-05) to have the information displayed on the app and to be able to start the heat pump remotely.

I had a skecth working correctly without wifi connection. Then I tried to implement blynk, Based on the exemple with ESP8266 shield.

So I use:

Libraries: blynk 0.4.8

ESP8266_Lib.h
BlynkSimpleShieldEsp8266.h

Code in setup:

Blynk.begin

Code in loop

Blynk.run base on a condition on blynk.connected

My issue is that on startup (in setup blynk.begin) if the wifi is not available the code is stuck forever.
That is not a robust design for my requirement to filter the pool.

I will put the code at the end (search for “blynk” ) as it is long.

I have been trying the idea proposed by @Costas on: Code isn't working without connected Blynk with no success.

I have also tried to handle wifi conneciton separately and use blynk.config but it is not working.

If someone knows how to manage this issue with the libraries I am using it would help.

thanks a lot!

Antoine

The code (too long to fit in the forum)

@furby_goulag take a look at the code provided by @mars in that thread as it’s supposed to cover Mega’s.

I generally only use ESP’s connected directly to Blynk.

What you need to do with the code provided by @mars is just build a small sketch, test it thoroughly and then add to your 2000 line sketch.

You need to use blynk.config(), however the issue seems to be that blynk.config() will not play nicely with the libraries required for ESP as Shield modes… I have been reading up on others attempts, and experimenting with various ways, but no positive results yet.

Ah yes the code by @mars is for Mega with Ethernet.

If you really need a Mega then for full Blynk functionality a decent Ethernet connection is essential.
There are some Blynk features not available when using an ESP as a shield for an Arduino.
You can still use ESP’s with Arduino’s but consider using ESP as the master rather than the slave.

Aside from the aforementioned blynk.config() everything else with Blynk works just fine, including reconnection after signal loss (based on my testing so far)… but oh my!! that massive I/O of the MEGA combined with WiFi is very nice to have :wink:

How are you handling reconnection?

How are you doing OTA?

Blynk.connect(); Granted, I haven’t tried every disconnection scenario yet… I am sure that lack of blynk.config() will byte back eventually :stuck_out_tongue:

That’s not a Blynk specific feature… besides, who is going to hang a MEGA in a tree :wink:

Blynk is constantly developing so the ability to perform remote updates is very useful for many IOT projects.

True… but this OP and I am referring to the MEGA here… like a cargo van compared to a sports car… Different applications need different features. Having a fully reconnectable MEGA/ESP WiFi is more important for reliable project autonomy with massive I/O requirements, than remote updatability.

But done correctly you can have both.

Not disagreeing, but until I have a spare, I am not risking my ESP-01 for a Standalone ESP-Master, Arduino-slave experiment :wink: and I will let the OP decide on his own hardware options :smiley:

But if anyone can assist with this ESP as WiFi adapter - blynk.config() incompatibility, then I am also interested.

1 Like

As @Gunner is saying everything works fine with blynk and mega . If my WiFi is available on startup it is working as a charm.

From my point of view on “big” installations with mega, OTA is not critical. But WiFi timeout is.

If someone as a trick for that, it would be great.

Hello there,

After a few more tests I managed to have an accpetable situation for me:

I am using another library (ITEADLIB_Arduino_WeeESP8266-master) to test the availability of the connection (at least access point). It is better than nothing, and it allows quite à good behaviour.

The sketch is below.

The only thing observed is that the “blynk.run” command can take up to 3s long which is huge and I don’t know why.

Antoine

> // ------------------------------------------------------------------------------------
> // Test wifi with blynk v1 based on pool AS V023
> // ------------------------------------------------------------------------------------
>  
> //inclure la bibliothèques Wire
> #include <Wire.h>
> // Inclure la bibliothèque LCD I2C
> #include <LiquidCrystal_I2C.h>
> // Inclusion de la librairie OneWire pour la sonde de température
> // détails sur le protocole http://playground.arduino.cc/Learning/OneWire
> #include <OneWire.h> 
> //Librairies BLynk et ESP8266
> #define BLYNK_PRINT Serial
> #include <BlynkSimpleShieldEsp8266.h>
> #include <WiFi.h>

> //definition de l'adresse du module LCD I2C
> // Si le jumper A0 est soudé, l'adresse I2C est 0x3E, sinon utiliser un scanner I2C
> //LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
> 	LiquidCrystal_I2C lcd(0x3E, 16,2);

> // Variables de température
> static float temp = -10;
> static float day_maxtemp = 10;
> static boolean blynkbeginpass = 0;

> //

> // Variable pour la connexion WIFI
> 	//Variable de dernière connexion wifi
> static unsigned long last_wifi_millis = 0;
> 	// variable de dispo de la connexion
> static boolean result_wifi;


> // Pin de signe de vie du WIFI
> const int pin_wifi = 10;


> // WIFI paramètres de connexion


> // Set password to "" for open networks.
> char ssid[] = "Mikado";
> char pass[] = "41414141";
> char auth[] = "7e4ab6dd49f24e11d38759b00320";
> BlynkTimer timer; // Create a Timer object called "timer"! 

> // Ouverture de la connexion série de l'ESP sur le port série 2: pins 17/18

> #define EspSerial Serial2

> // Your ESP8266 baud rate:
> #define ESP8266_BAUD 115200

> ESP8266 wifi(&EspSerial);

> // Fin WIFI paramètres de connexion
>  
>  
>  /*/////////////////////////////////////////////////////////
>  
>  SETUP
>  
>  //////////////////////////////////////////////////////////*/
>  
>  
>  
> void setup()
> {
> 	delay(10);
> 	Serial.begin(115200);
> 	delay(10);	

> 	Serial.println("Entrée dans le SETUP");
> 	Serial.println("Setting up Wire and LCD libs");
> 	
> 	Wire.begin();

> 	lcd.begin(16,2); //(colonne, ligne)
> 	lcd.backlight();	
> 	clearLCD();
> 		// on initialise un timer pour la data blynk que toutes les 1 secondes
> 	timer.setInterval(1000L, SendToBlynk); 
> 	

> 	// SETUP DU WIFI
> 	
> 	delay(1000);
> 	
> 	// Set ESP8266 baud rate
> 	EspSerial.begin(ESP8266_BAUD);
> 	delay(10);

> 	// Blynk.begin ne sert qu'à configurer la connexion. Pas de connexion à ce stade
>    
>    
>    if (wifi.setOprToStation()) {
>        Serial.print("to station ok\r\n");
>    } else {
>        Serial.print("to station err\r\n");
>    }

>    if (wifi.joinAP(ssid,pass)) {
>        Serial.print("Join AP success\r\n");
>        Serial.print("IP: ");       
>        Serial.println(wifi.getLocalIP().c_str());
> 	   
> 	   delay(10);
> 	
> 		// Blynk.begin ne sert qu'à configurer la connexion. Pas de connexion à ce stade
> 		Blynk.begin(auth, wifi, ssid, pass);
> 		Serial.println("blynk begin passed");
> 		result_wifi = Blynk.connect(8000); 
> 		blynkbeginpass = true;
> 		
> 	   
> 	   
> 	   
>    } else {
>        Serial.print("Join AP failure\r\n");
>    }

> 	
> 	clearLCD();
> 	lcd.setCursor(0,0);
> 	lcd.print("    Test WIFI   ");
> 	lcd.setCursor(0,1);
> 	if (result_wifi == true) {
> 		lcd.print(" WIFI connected ");
> 		digitalWrite(pin_wifi, HIGH);
> 	}
> 	else if (result_wifi == false) {
> 		lcd.print("  WIFI broken   ");
> 		digitalWrite(pin_wifi, LOW);		
> 	}
> 	
> 	delay(2000);
> 	

> 	// FIN SETUP DU WIFI	
> } // Fin SETUP
>  
> void loop()
> {
> 	
> 	
> 	// WIFI LOOP
> 	
> 	// on vérifie la connexion toutes les loop
> 	
> 	// Si le wifi est connecté on run blynk
> 	// Sinon on run un blynk.connect (timeout de 30s) et on ne repassera que dans 5min
> 	result_wifi = Blynk.connected();
> 	if (result_wifi == true) {
> 		// debug blynk
> 		Serial.print("[");
> 		Serial.print(millis());
> 		Serial.print("]");
> 		Serial.println("je run Blynk");
> 		Blynk.run();
> 		timer.run();
> 		timer.setInterval(1000L, SendToBlynk);
> 		//timer.setInterval(1000L, Blynk.run); 		
> 		last_wifi_millis = millis();
> 		digitalWrite(pin_wifi,HIGH);
> 	}
> 	else if (result_wifi == false) {
> 		Serial.println("result_wifi= false-loop");
> 		digitalWrite(pin_wifi,LOW);
> 	}
> 	if (result_wifi == false && millis() > (last_wifi_millis + 30000)) {
> 		last_wifi_millis = millis();
> 		if (blynkbeginpass == true){
> 				Serial.println("Je run blynk.connect");
> 				Blynk.connect();
> 		}
> 		else if (blynkbeginpass == false){
> 		   if (wifi.setOprToStation()) {
> 			   Serial.print("to station ok\r\n");
> 		   } else {
> 			   Serial.print("to station err\r\n");
> 		   }

> 		   if (wifi.joinAP(ssid,pass)) {
> 			   Serial.print("Join AP success\r\n");
> 			   Serial.print("IP: ");       
> 			   Serial.println(wifi.getLocalIP().c_str());
> 			   
> 			   delay(10);
> 			
> 				// Blynk.begin ne sert qu'à configurer la connexion. Pas de connexion à ce stade
> 				Blynk.begin(auth, wifi, ssid, pass);
> 				Serial.println("blynk begin passed");
> 				result_wifi = Blynk.connect(8000); 
> 				blynkbeginpass = true;
> 			   
> 		   } else {
> 			   Serial.print("Join AP failure\r\n");
> 		   }	
> 		}
> 	}
> 	
> } // end of Main Loop

> // Début de la fonction blynk pour pusher les données toutes les secondes

> void SendToBlynk()
> {
>   // This function sends Arduino up time every 1 second to Virtual Pin (V5, V6, V7)


>   Blynk.virtualWrite(V5,temp);
>   //Blynk.virtualWrite(V6, millis() / 1000);
>   //Blynk.virtualWrite(V7, millis() / 1000);
> }


> // Fin de la fonction blynk pour pusher les données toutes les secondes

> // Fonction de RAM dispo

> int freeRam ()
> {
> 	extern int __heap_start, *__brkval;
> 	int v;
> 	return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);

> }

> //Mise à zéro brutale du LCD
> void clearLCD ()
> {
>   lcd.setCursor(0,0);
>   lcd.print("                ");
>   lcd.setCursor(0,1);
>   lcd.print("                ");
>   lcd.setCursor(0,0);
> }

Hi! I head the same problem with UBIDOTS, and found for my self next solution- I separated main part of scetch and ubidots (for your cause blynk). It’s mean, main code run as Void loop, but in Void setup is also timer which run Void ubidots (or blynk) 6times/hour. In Void blynk wifi started and client blynk begin, if wifi connection is not available the wifi off and return to main loop. Also used eeprom.put command to store data from server (for examle target temp) and eeprom.read to get and than send stored in Void loop data and send it to blynk. (Sorry for my english)

#include <SimpleTimer.h>
#include <time.h>
#include <OneButton.h>
#include "UbidotsMicroESP8266.h"
#include "ESP8266WiFi.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include "DHT.h"
#include <SoftwareSerial.h>
#define DHTPIN D6
#define DHTTYPE DHT11
#define RELEY D3
#define ONE_WIRE_BUS D7
#define TOKEN  "************************"  // Put here your Ubidots TOKEN
#define ID_1 "*******************" // Tin
#define ID_2 "****************" // Hin
#define ID_3 "******************" // Tout
#define ID_4 "******************" // Reley
#define ID_5 "**********************" // Ttarget
#define WIFISSID "**************" // Put here your Wi-Fi SSID
#define PASSWORD "*********************" // Put here your Wi-Fi password
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET D4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire (ONE_WIRE_BUS);
DallasTemperature sensors (&oneWire);
float Hin = dht.readHumidity();
float Tin = dht.readTemperature();
float Tout = sensors.getTempCByIndex(0);
float valBase = 7;
boolean  value4;
int delta = 2;
int addr_targ = 101;
int addr_reley = 102;
int addr_last = 103;
int addr_Time = 104;
Ubidots client(TOKEN);
int value5;
long Time = getVarTimestamp(TOKEN);
unsigned long millis();
unsigned long micros();
SimpleTimer timer;

void setup(){
  Serial.begin(57600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.display();
 unsigned long millis(2000);
  display.clearDisplay();
  sensors.begin();
  dht.begin();
  pinMode (RELEY, OUTPUT);
EEPROM.begin(512);
   int value5 = valBase;
   EEPROM.put(addr_targ, value5);
timer.setInterval(600000L, ubidots);
    }
    
  
void loop(){
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
    timer.run();
    sensors.requestTemperatures();
    float value1 = dht.readTemperature();
    display.println("TEMP.inside: "+String(value1));
    float value2 = dht.readHumidity();
    display.println("Hymid.inside: "+String(value2));
    float value3 = sensors.getTempCByIndex(0);
    display.println("TEMP.out: "+String(value3));
       int value5 = EEPROM.read(addr_targ);
       EEPROM.put(addr_last, value5);
     //  Serial.println("temperature_target: "+String(value5)); 
       display.println("TEMP.targrt: "+String(value5));
       
        if (value1 <= value5-delta)digitalWrite(RELEY, HIGH); 
     else if (value1 >= value5) digitalWrite(RELEY, LOW);
       
//       if (digitalRead(RELEY) == LOW) value4 =0;
//     else value4 = 1;
//  EEPROM.put(addr_reley, value4);
  
unsigned long millis(20000);    
 
 display.display();
 unsigned long micros(200);
 display.clearDisplay();
  }

void ubidots() { 
  client.wifiConnection(WIFISSID, PASSWORD);
   if (WiFi.status() != WL_CONNECTED){
   unsigned long millis(120000); 
   int value5 = valBase;
   EEPROM.put(addr_targ, value5);
    WiFi.disconnect();}
  else {int value5 = client.getValue(ID_5);
  if(value5 <= 5){ value5 = EEPROM.read(addr_last);
  EEPROM.put(addr_targ, value5);}
  else EEPROM.put(addr_targ, value5);
   Serial.println("temperature_target: "+String(value5)); 
     sensors.requestTemperatures();
    float value1 = dht.readTemperature();
    float value2 = dht.readHumidity();
    float value3 = sensors.getTempCByIndex(0); 
    long Time = getVarTimestamp(TOKEN);
  if (digitalRead(RELEY) == LOW) value4 =0;
     else value4 = 1;
    Serial.println("RELEY: "+String(value4));
    client.add(ID_1, value1);
    client.add(ID_2, value2);
    client.add(ID_3, value3);
    client.add(ID_4, value4);
    unsigned long millis(20000); 
    client.sendAll();
   WiFi.disconnect(); }
}

A post was split to a new topic: Mega+ESP will freeze if there is a connection problem