Wifi Continuously Connect and Reconnect on mobile device

Hello!
I am trying to get a stable connection between my android phone and the Blynk app. But when I add more void loops, the app reconnects and connects over and over. Is there a code that will keep the wifi connection when more void loops are added? Or is there something I need to do within my loops?

I am using an arduino mega and an ESP8266 wifi module.

I would also like to display my values on an lcd screen, that may be the problem but I am not sure how to fix that.
Thank you!


#define BLYNK_DEBUG
#define BLYNK_PRINT Serial

//#if defined(ESP8266)
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <max6675.h>

int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds

//Motor
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display


//thermo
// creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO1   48
#define MAXCS1   46
#define MAXCLK1  44

// Initialize the Thermocouple

MAX6675 thermocouple1(MAXCLK1, MAXCS1, MAXDO1);
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

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

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

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, millis() / 1000);
}

BLYNK_APP_CONNECTED() {
  Serial.println("App Connected.");
}

// This is called when Smartphone App is closed
BLYNK_APP_DISCONNECTED() {
  Serial.println("App Disconnected.");
}

void getTemp()
{
 double t1 = thermocouple1.readCelsius();
   //delay(500);
  t1= (((t1*9))/5)+32;
   
     Serial.print("Thermocouple Temp1 = *");
     Serial.println(t1);
 
lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp1 = ");
  lcd.print(t1);   
  lcd.print(" F ");
   
    Blynk.virtualWrite(V10, t1);

}

void setup()
{
   
  Serial.begin(9600);//initialize the serial

  lcd.init();
  lcd.backlight(); //op
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(6000L, getTemp);

    while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
  #endif

}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

}

Your code is OK, except in the setup(). Your loop() is perfect.
You also have to check if ESP-01 (ESP8266 WiFi module) can handle baud rate 115200. Try to reduce ESP8266_BAUD down gradually to 56.8K, 38.4K, 19.2K, 9.6K etc. to see if it’s better.
Sometimes, you have to update ESP-01 AT firmware to get a reliable AT firmware and therefore connection. Please search in this forum or Google for this issue.

void setup()
{
   
  //Serial.begin(9600);//initialize the serial
  // Modify terminal speed to 115200 for better performance as Mega HardwareSerial can handle
  Serial.begin(115200);//initialize the serial

  lcd.init();
  lcd.backlight(); //op
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  // A little delay() here so that ESP-01 is ready before calling Blynk.begin() 
  delay(2000);
  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(6000L, getTemp);
  // Remove this
  //  while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
  //#endif
}

1 Like

Thank you, I’ll try that.

This implies that you’re adding more void loops into your code. Do you know that there can only be one void setup, and one void loop in your code?
Void setup runs once at startup, and void loop executes continually after that.

You can add more functions of your own, but you can’t call them void loop.

Pete.

I’m sorry about that. To clarify, I mean void getTemp(), or void getvolt() loops, I should’ve said functions. I know there should only be one main void loop().

1 Like

Hello, and thank you for your suggestions. I’ve tried both changes but I get the “packet too big” line in the serial window.




[1999] 
    ___[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to "wifi"
[5557] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
[10622] +CIFSR:STAIP,"192.168.0.112"
+CIFSR:STAMAC,"bc:dd:c2:2d:fe:b9"
[10624] Connected to WiFi
[20776] Ready (ping: 11ms).
[30784] Packet too big: 2603
[36142] Ready (ping: 200ms).
[39833] Packet too big: 2603''

You have to update the AT-firmware to v1.3.0.2_AT_Firmware.bin. This version works reliably at 115200 baud. All other version is not working OK at 115.2 KBaud and especially with Mega.

My test results:

1) ai-thinker-v1.1.1.bin (AT command works with 115200 baud), try with MEGA using 9600 - 115200 not working.
2) ai-thinker-0.9.5.2.bin (AT command totally not working with any baud), try with MEGA using 9600 - 115200 not working.
3) ai-thinker-0.9.5.bin (AT command totally not working with any baud), try with MEGA using 9600 - 115200 not working.

Get the bin file at

https://github.com/mlwmlw/esp8266-workshop/blob/master/firmware/v1.3.0.2%20AT%20Firmware.bin

then update AT-firmware. If you don;t know how to update, Google research.

If you still have problem, post your ful code along with Serial Window.

2 Likes

Thank you for your help. I used an esp8266 flasher but I get the same response.

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <max6675.h>
LiquidCrystal_I2C lcd(0x27,20,4);

#define MAXDO1   48
#define MAXCS1   46
#define MAXCLK1  44

MAX6675 thermocouple1(MAXCLK1, MAXCS1, MAXDO1);

//volts
float volt1 = 2;
float volt2 = 3;
float volt3 = 4;

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif


//buttons
int pin_switch1 = 50;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

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

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);


BlynkTimer timer;

void getVolt()
{
volt1 = analogRead(A1);
  volt1 = volt1/1024*5.0;// Convert the data to the corresponding voltage value in a math way
  delay(200); //Wait for 200ms
///volt2
    volt2 = analogRead(A2);
    volt2 = volt2/1024*5.0;// Convert the data to the corresponding voltage value in a math way
delay(200); //Wait for 200ms
//volt3
   volt3 = analogRead(A3);
  volt3 = volt3/1024*5.0;// Convert the data to the corresponding voltage value in a math way
delay(200); //Wait for 200ms
   
   if (digitalRead(pin_switch1) == LOW) {
    {
  volt1 = analogRead(A1);
  volt1 = volt1/1024*5.0;// Convert the data to the corresponding voltage value in a math way
  delay(200); //Wait for 200ms
///volt2
    volt2 = analogRead(A2);
    volt2 = volt2/1024*5.0;// Convert the data to the corresponding voltage value in a math way
delay(200); //Wait for 200ms
//volt3
   volt3 = analogRead(A3);
  volt3 = volt3/1024*5.0;// Convert the data to the corresponding voltage value in a math way
delay(200); //Wait for 200ms
   
 lcd.clear();
  lcd.setCursor(0,0);
  //lcd.autoscroll();
  lcd.print("V1: ");
  lcd.print(volt1);
  lcd.print("V2: ");
  lcd.print(volt2);
  lcd.setCursor(0,1);
  lcd.print("V3: ");
  lcd.print(volt3);
   delay(100);
 }

  }


Blynk.virtualWrite(V1, volt1);
Blynk.virtualWrite(V2, volt2);
Blynk.virtualWrite(V3, volt3);
    
  delay(100);

}
void getTemp()
{
 double t1 = thermocouple1.readCelsius();
   //delay(500);
  t1= (((t1*9))/5)+32;
   
     Serial.print("Thermocouple Temp1 = *");
     Serial.println(t1);
 
lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp1 = ");
  lcd.print(t1);   
  lcd.print(" F ");
   
    Blynk.virtualWrite(V10, t1);

}
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V60, millis() / 1000);
}
 

void setup()
{
  // Debug console
  Serial.begin(115200);
 
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(2000);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
 timer.setInterval(5000L, getVolt);
  timer.setInterval(5000L, getTemp);
 

}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}


[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to 
[5558] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[10654] +CIFSR:STAIP,"192.168.0.112"
+CIFSR:STAMAC,"bc:dd:c2:2d:fe:b9"
[10655] Connected to WiFi
[22152] Ready (ping: 11ms).
[45851] Packet too big: 2603


Wow. You’re so fast I can’t keep up with.

I’m afraid you hardware + network + code combination somehow triggers the infamous bug Packet too big of the combination AT-firmwame + ESP8266_Lib + BlynkSimpleShieldEsp8266.h.
I saw and experienced this random bug quite a while and am trying to go to the root cause of it, but not yet successful and had to defer.

I currently have just a quick-dirty fix, but didn’t find out the root cause and solve yet.

Anyway, I need some time to duplicate this and let you know later.

2 Likes

:grin: I’ve spent a lot of time with this.
Thank you so much for your help!

@khoih, you might find this useful. It’s obviously related to different hardware, but might give a different perspective on the issue.

Pete.

Dear @PeteKnight:
Many thanks for your insight on a lot of different issues and fields.
This is an interesting bug worth for us to spend time. Hopefully it’s not inside out-of-reach AT-firmware and/or BLE AT-firmware.
Best Regards,

Thank to @PeteKnight pointing out similar problem in BLE, it’s possible just the small buffer size creating this issue.
In the mean time, you can try this test by modifying these line in

./Arduino/libraries/Blynk/src/Blynk/BlynkConfig.h

from

  // Limit the incoming command length.
  #ifndef BLYNK_MAX_READBYTES
  #define BLYNK_MAX_READBYTES  256
  #endif

  // Limit the outgoing command length.
  #ifndef BLYNK_MAX_SENDBYTES
  #define BLYNK_MAX_SENDBYTES  128
  #endif  

to

  // Limit the incoming command length.
  #ifdef BLYNK_MAX_READBYTES
  #undef BLYNK_MAX_READBYTES
  #endif

  #define BLYNK_MAX_READBYTES  1024
  
  // Limit the outgoing command length.
  #ifdef BLYNK_MAX_SENDBYTES
  #undef BLYNK_MAX_SENDBYTES
  #endif

  #define BLYNK_MAX_SENDBYTES  512

and let us know the result. I still haven’t got time to setup Mega+ESP to test.

1 Like

Thank you. I changed that line but I get the same result.

There’s more than one line that needs to be changed.

Pete.

1 Like

Did you change all those lines?
I set up and test the MegaWiFi R3 (Mega2560 + ESP01) similar to your config and still had no issue yet, after change those lines. I’m connecting to Local Blynk Server.
The terminal output:

[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to ****
[5598] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[10802] +CIFSR:STAIP,"192.168.2.79"
+CIFSR:STAMAC,"5c:cf:7f:78:28:1f"
[10803] Connected to WiFi
[20956] Ready (ping: 25ms).
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00

and this is the result connecting to Local Blynk Server, but via DDNS (*.duckdns.org). Still OK.

[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to HueNet
[15668] WIFI CONNECTED
WIFI GOT IP
AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd
Bi:...Sp1211:84


[20797] +CIFSR:STAIP,"192.168.2.79"
+CIFSR:STAMAC,"5c:cf:7f:78:28:1f"
[20798] Connected to WiFi
[31034] Ready (ping: 21ms).
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00

Can you move the ESPSerial port to Serial3 (and change the ESP-01 wire correspondingly) and test again to see if there was any conflict.

Also you can remove all unnecessary device (LCD, etc), just bare board + ESP-01 and test again if problem persists.

Yes, I don’t know why I said “that line” instead of “those lines”. :sweat_smile:

I made the changes and removed other items, but the problem persisted. I am just connected to my wifi. I can try to connect to the local Blynk server.


[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to 
[5558] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[10644] +CIFSR:STAIP,"192.168.0.112"
+CIFSR:STAMAC,"bc:dd:c2:2d:fe:b9"
[10645] Connected to WiFi
[20803] Ready (ping: 11ms).
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
Thermocouple Temp1 = *32.00
[87567] Packet too big: 2603
Thermocouple Temp1 = *32.00

UPDATE:
I didn’t have the app on my screen and the code ran for much longer. It wasn’t until I opened the app that the error persisted.

Can you try with the following bare-bone code and post the result:

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//Blynk Cloud Server
char auth[] = "***";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial3

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

void setup()
{
  // Debug console
  Serial.begin(115200);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(2000);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
}

void loop()
{
  Blynk.run();
}

@khoih

I’ve noticed that the Blynk examples work fine, I think it’s when functions are added.
Also, I didn’t have the app on my screen and the code ran for much longer. It wasn’t until I opened the app that the error persisted.

[12445] +CIFS192.168.0.112"
+CIFSR:STAMAC,"bc:dd:c2:2d:fe:b9"
[12445] Connected to WiFi
[1999] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[2506] Connecting to 
[5558] AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04
OK
[10645] +CIFSR:STAIP,"192.168.0.112"
+CIFSR:STAMAC,"bc:dd:c2:2d:fe:b9"
[10646] Connected to WiFi
[20867] Ready (ping: 12ms).

If so, the other library and/or cell network speed / latency could be the culprit. Using the Local Blynk Server can eliminate the network part.
It’s possibly the Wire and LiquidCrystal_I2C libraries are using too much MCU time in the slow Mega (16MHz MCU clock, etc.), not playing nice, monopolize and prevent other code portions to run when necessary. This will lead to buffer overflow => Packet too Big problem.

Try to use another similar library if possible to test.

Now you’re expert in how to debug by eliminating one-by-one and achieving the good results.

1 Like