Invalid auth token on Reconnection Wifi and Blynk

Hi,

I’m trying to handle WiFi / Blynk reconnection when a disconnection happens.

My problem is that first connection to Blynk goes OK, if happens a disconnection I get following error (Invalid auth token) at reconnection.

Follows log retrieved in my attempts:

…
[2216] Connecting to xxxxxxxxxxxxxxxxxxxxx
[5250] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[10313] +CIFSR:STAIP,“xxxxxxxxxxxxxxxxxxxxx”
+CIFSR:STAMAC,“xxxxxxxxxxxxxxxxxxxxx”
[10314] Connected to WiFi
[10809] Ready (ping: 12ms).
[20170331_213237][11555][4934]BLYNK - BLYNK_CONNECTED - Prima connessione
[20170331_213237][14373][5021]BLYNK - Arduino connesso
[14449] Time sync: OK
…
[20170331_221707][2639471][5021]BLYNK - Arduino disconnesso
…
[20170331_221853][2746112][5021]WIFI - Riavvio Esp8266
[2750141] Connecting to FASTWEB-1-5E6535
[2753176] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[2763214] Failed to connect WiFi
[20170331_221922][2774772][4989]TEMPERATURA - ARDUINO - T.: 21.00 U.: 42.00 %
[20170331_221922][2775317][4989]TEMPERATURA - ESTERNA - T.: 22.50 U.: 45.70 %
[20170331_221922][2775321][5021]WIFI - Riavvio Esp8266
[2779350] Connecting to xxxxxxxxxxxxxxxxxxxxx
[2782385] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[2787447] +CIFSR:STAIP,“xxxxxxxxxxxxxxxxxxxxx”
+CIFSR:STAMAC,“xxxxxxxxxxxxxxxxxxxxx”
[2787448] Connected to WiFi
[2787628] Invalid auth token
[2788275] Invalid auth token
[2788919] Invalid auth token
[2789880] Invalid auth token
[2790218] Invalid auth token
[2790866] Invalid auth token
[2791511] Invalid auth token
[2792440] Invalid auth token
[2792889] Invalid auth token

In this case connection fails for a router restart (as you can see from “Failed to connect WiFi”), but happens also without a router restart (for example a simple connection fall)

Code that handle the first connection (in setup) and reconnection (in loop) is:

void setup() {
	previousConnected = false;

	EspSerial.begin(ESP8266_BAUD);

	delay(100);

	Blynk.config(wifi, BLYNK_AUTH);
	Blynk.connectWiFi(BLYNK_SSID, BLYNK_PWD);
	Blynk.connect();
	
	timer.setInterval(1000, primaSincronizzazione);
	timer.setInterval(2000, primaSincronizzazioneLow);
}
void loop() {
	if (Blynk.connected()) {
		Blynk.run();

		timeToRetry = T_RETRY;
		nRetry = 0;

		if (!previousConnected) {
			previousConnected = true;
			disconnectionTime = 0;

			Log::stampa(F("BLYNK - Arduino connesso"));

			rtc.begin();
			setSyncInterval(900);
		}
		timer.run();
	}
	else {
		if (nRetry >= MAX_RETRY) 
			timeToRetry = EXTENDED_T_RETRY;
			
		if (disconnectionTime != 0 &&  millis() - disconnectionTime > timeToRetry) {
			Log::stampa(F("WIFI - Riavvio Esp8266"));

			disableWDT();

			wifi.restart();
			Blynk.connectWiFi(BLYNK_SSID, BLYNK_PWD);
			Blynk.connect();

			nRetry++;
			enableWDT();
		}

		if (previousConnected) {
			previousConnected = false;
			disconnectionTime = millis();
			Log::stampa(F("BLYNK - Arduino disconnesso"));
		}
	}
	.....
}	

I’m on Library v0.4.4 and Server Blynk Cloud.
Hardware: Arduino Mega + ESP8266.

Where I’m wrong in reconnection handling?

Thanks.
Regards

1 Like

I’m sorry for unnecessary post.

I solved my problem adding:

Blynk.config(wifi, BLYNK_AUTH);

after Wifi restart, as follow:

			wifi.restart();
			Blynk.config(wifi, BLYNK_AUTH);
			Blynk.connectWiFi(BLYNK_SSID, BLYNK_PWD);
			Blynk.connect();