Check connection status in loop and reconect

@Costas actually I don’t know how to telnet inside the router thru an ESP 8266. Consider that my network is composed only by an AT mega 328, an ESP 8266 and this router, because it is a monitoring system implemented for a remote site this Blog mentions a Telnet server on the ESP, but it could be a start. I’m not sure how much Blynk will be “fubar” if you start running your own AT commands while blynk is running, but its worth trying in a local setup I guess.

@Lichtsignaal Thanks, I’ll try !! :slight_smile:

What state is the router in when it “stops working”?
Is the 3G failing but the router still running perhaps?
Your manual indicates telnet access is available. In theory a simple shell script could be put on the router and called at intervals with crontab to reboot it if fails to ping google DNS. Obviously this would only work if some parts of the router are still functioning correctly.

@Xandr has just posted a link to a script that would do the job for you.

@Costas Hi Costas, sorry for the delay in my answer but in this period I stay on holidays :sunglasses:
I don’t remember which is the connection status of the router when the issue occours, anyway I strongly believe that the problem is relevant to the SIM Card because changing the type of SIM Card the frequency of the issue varies.
As regards the router, it is still working because it is enough to press the power switch for 1 second to restabilish the connection (when the router is completely down it is necessary to press the power button for 5 seconds).
Anyway the systen that I’ve implemented is very effective but the problem is the wire bonding on the terminals of the power switch that are located on a tiny flexible connector. For this reason I’m still interested to a software solution.
I have had a look at Xandr’s script but I don’t know how to launch it because I don’t have a PC within the system. The system is composed only by an AT Mega 328, an ESP 8266 and this 3G router. Nothing else. The script should be inserted in the sketch of the AT Mega as:
if (!Blynk.connected())
script code …

But I don’t know how to do it (I’m more an hardware guy than a software person). Could you help??

@Gianca my TPLink routers only have Telnet up until the first bootup and then it is disabled but I believe your router may have a permanent Telnet connection. With Gargoyle I am supposed to be able to enable Telnet permanently but it didn’t work when I tried.

I have tried a Telnet session from the ESP to my Pi and a Telnet server in the US ( but with limited success. I am struggling with the correct parameters to send from the ESP but it should be doable.

Similarly you should be able to do it via http from the ESP to with admin username and admin password (unless you have changed them) and then to the reboot url. I haven’t tried this yet with my router and I again I am a bit ropey with the commands I need to send from the ESP.

Presumably you have a Smartphone, is it Android or iOS?

On my Android Smartphone I have this simple Telnet Client

Can you try to Telnet into your router from your Smartphone:

Just type telnet in the Smartphone console and then it should ask for username and password. If we know Telnet is available then we should be able to give you a simple script to put on the router until we are able to give you and ESP Telnet / http fix.

My routers are all unlocked with the Gargoyle firmware but it might be that you can’t write anything to your filesystem even when you make a Telnet connection. That said it if allows Telnet then maybe you can.

@Costas Thanks indeed for your attention !!

@Gianca you can also try to SSH from your Smartphone to the router. I’m using this app as I can’t activate Telnet on my router.

@Costas Uhmm !! I’ll try !! Thanks

i own an uno and an esp8266 connected via softserial. all works great with blynk except when the connection drops either to blynk server either to the router or to the internet. in that case the blynk function just retries to connect to server no matter if the wifi connection is down. so i would like the code that is descriped in this tthread writen for my hardware (uno+esp8266 via softserial). thats because i think that my hardware does not support MyWifi function just AT commands.
Thanks in advance and sorry for my English.

@liq you want your Uno to continue running if the internet is down, right?

Blynk have made changes recently regarding the connection process but in loop() you should be ok with:

  if (Blynk.connected()) { // is only called if connected to the server;

Costas , thanks for the reply. i think that something is wrong with the Blynk.begin function.
i dont Know how to post my entire sketch, anyway
when Blynk.begin (auth,ssid,psw) is called in void setup if the wifi is not available at this moment this function goes into an infinite loop and the code does not advance further. Here is what esp sends to uno when (Blynk.begin(auth, wifi, ssid, pass, “”):wink: when wifi not found:


so the code hangs there…
as soon as wifi becomes available it does not unstuck because the “AT+CWJAP=“XXXXX”,“XXXXXX”” is need in order to connect to wifi and then the
“AT+CIPSTART=1,“TCP”,“”,8442” inorder to connect to blynk cloud
that is the no1 bug that i spotted
i could run blynk.begin in a timed loop but the “at+cipstart…” continues forever so the code hungs.
no2 is that sometimes the “AT+CIPSTART=1,“TCP”,“”,8442” command is send but because “mux=0” , the command fails. (i ll write details in future post)

i find blynk a very cool , unique , powerful and simplified project that if some stability isues were resolved it would be the best available out there.
thanks in advance

In my opinion it already is :slight_smile:

In the last week or so Blynk introduced a blocking routine for Blynk.begin() and generally most users will benefit form it. Without the blocking some of the code directly after Blynk.begin() wasn’t being processed correctly.

As you have found the blocking can also be a problem but there are ways to overcome it.

Even though I have used AT commands for around 30 years it’s something I try to avoid i.e. I have ESP’s that can be used as a shield for my Arduino’s but I almost never use them as working with ESP’s in standalone mode is my preferred set up.

What you need to do is make a WiFi connection with the regular Arduino functions before connecting to Blynk. Then instead of using Blynk.begin() you use Blynk.config() which circumnavigates the new blocking routine.

@Liq as an aside I stumbled across “mux” in some code I was looking at a few days ago. The code was “AT based” and I was looking to convert it to “ESP standalone format”. Are you familiar with “mux”?

when i use Blynk.config(auth); function “BlynkSimpleShieldEsp8266.h:160:10: note: void BlynkWifi::config(ESP8266&, const char*, const char*, uint16_t)” so no solution… :frowning:
When i say mux=0 i mean esp is running in single connection mode
see: “AT+CIPMUX - Enable multiple connections or not”
as you can see i am new to all this and i don’t know if i can help you somehow
anyway i ve order new wemos boards so if there is no solution for my hardware i ll move on…

@Liq thanks, I’ll dig into “mux” when I have a minute.

Notice an error in my last post. It should have said was make an ethernet, not WiFi connection.

The compiler error is telling you that you now need 2 variables for the Blynk.config() function. I think it was just the token variable previously. Let me check here if Blynk.config() will work with ENC.

I am going mad!!! In your case it is WiFi not Ethernet and nothing to do with ENC (that is the ENC28J60 thread).

For the second variable in Blynk.config() after token just try “” (assuming you are using Blynk’s cloud server).


no known conversion for argument 1 from ‘char [33]’ to ‘ESP8266&’
BlynkSimpleShieldEsp8266.h:160:10: note: void BlynkWifi::config(ESP8266&, const char*, const char*, uint16_t)
void config(ESP8266& esp8266,
exit status 1
no matching function for call to ‘BlynkWifi::config(char [33], const char [16])’

try to relax i am newbie to all these. maybe i am making an rookie mistake such as “turn the power switch on”…

@Liq paste your sketch as follows:

enter 3 backticks and cpp
paste sketch on a new line
enter 3 more backticks on a new line after the end of the sketch

 * 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:
 *   Blynk community:  
 *   Social networks:  
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 * This example shows how to use ESP8266 Shield (with AT commands)
 * to connect your project to Blynk.
 * Note: Ensure a stable serial connection to ESP8266!
 *       Firmware version 1.0.0 (AT v0.22) is needed.
 *       You can change ESP baud rate. Connect to AT console and call:
 *           AT+UART_DEF=9600,8,1,0,0
 *       In general, Soft Serial may be unstable.
 *       It is highly recommended to switch to Hard Serial.
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 * Feel free to apply it to any other example. It's simple!
#include <SimpleTimer.h>
#include <ESP8266wifi.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DHT.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <BlynkSimpleShieldEsp8266.h>
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "XXX";
char ssid[] = "XXX";
char pass[] = "XXX";

#define DHTPIN 7
#define DHTTYPE DHT22 

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

int ledPin = 13; // LED connected to digital pin 13
int inPin = 12;   // pushbutton connected to digital pin 12
int buttonState = 0 ;   
float atemperature;
bool Connected2Blynk = false;
bool wifi_started;
SimpleTimer timer;

void setup()
  // Set console baud rate
  // Set ESP8266 baud rate

 Blynk.begin(auth, wifi, ssid, pass, "");
  pinMode(ledPin, OUTPUT);     
 pinMode(inPin, INPUT);  
 timer.setInterval(12000L, tempcheck);
// timer.setInterval(15000L, checkconnectiontorouter);

void loop()

buttonState = digitalRead(inPin);

  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);


void tempcheck()
 Blynk.virtualWrite(1, (sensors.getTempCByIndex(0)));
 //Blynk.virtualWrite(2, (sensors.getTempCByIndex(0)));
 //atemperature= sensors.getTempCByIndex(0) - 0.630;
 Blynk.virtualWrite(3, atemperature);
 Blynk.virtualWrite(4, atemperature);
Serial.println("read dallas");
 float h = dht.readHumidity();
 float t = dht.readTemperature();
//Blynk.virtualWrite(5, (myDHT22.getHumidity()));
//Blynk.virtualWrite(6, (myDHT22.getHumidity()));
Blynk.virtualWrite(7, h);
Blynk.virtualWrite(8, t);
 Serial.println("read dht");

//void checkconnectiontorouter()
//  delay(1000);
//  Connected2Blynk = Blynk.connected();
 // if (Connected2Blynk){
 //   Serial.println("CONNECTED blynk");
//  delay(1000);
//  }
 // else{
 //   Serial.println("not CONNECTED blynk");
 //   Blynk.connect();
  //  delay(500);
//  }
1 Like