[Arduino Mega2560 + ESP8266-01] Stopping after while

Hello,

I have a problem with Arduino Mega2560 + ESP8266 with Blynk.

After connection between Arduino and ESP8266-01 with Blynk app, suddenly connection failed.

I want to debug what is the problem caused. I have a doubt problem on “blynk to auduino mega(with esp8266) connection”. It means that if response from the blynk or arduino have a problem then stop the any action on system.(Becuase I inserted “Softare Reset code” every 10minute, but this code not execute when problem occured.)

  1. What is best approach to debug blynk.(I want to know exactly why loop() is not executed).
  2. What is the best guessing problem in my project.

I tried reading blynk source and searching this forum topic. Unfortunately, this forum not many post Arduino Mega2560 + ESP8266 combination and does not exists post relate in my problem.

I found “Arduino/libraries/Blynk/src/BlynkSimpleShieldEsp8266.h” file below codes.

   void begin(const char* auth,**
             ESP8266&    esp8266,**
               const char* ssid,**
               const char* pass,**
               const char* domain = BLYNK_DEFAULT_DOMAIN,**
               uint16_t    port   = BLYNK_DEFAULT_PORT)**
     {
        config(esp8266, auth, domain, port);**
        connectWiFi(ssid, pass);**
        while(this->connect() != true) {} 
    }

"while(this->connect() != true) {} => In my understanding, If connection is not work, arduino will stop. I don’t want stop my arduino. For that reason I modified blynk library like belows.

void begin(const char* auth,
           ESP8266&    esp8266,
           const char* ssid,
           const char* pass,
           const char* domain = BLYNK_DEFAULT_DOMAIN,
           uint16_t    port   = BLYNK_DEFAULT_PORT)
{
    config(esp8266, auth, domain, port);
    connectWiFi(ssid, pass);
//        while(this->connect() != true) {} //Original
    while(this->connect() != true) {
        BLYNK_LOG1(BLYNK_F("[connectWifi Fail] It would try softwarereset"));
        delay(1000);
        wdt_enable(WDTO_60MS);
        while(1) {};
    }
}

I guess like this codes, Blynk codes if some problem caused, not execute any code in the loop.

I want to solve this problem. I want if some problem caused in blynk codes, then execute software reset.

Help me please.

There’re many posts around talking about a blocking/non-blocking connection routine.

You can also have a look at: http://docs.blynk.cc/#blynk-firmware-configuration-blynkbegin

1 Like

https://community.blynk.cc/search?q=Arduino%20mega%20ESP-01%20connection%20order%3Alatest

1 Like

Thank you!

I found the valuable post “How to make ESP8266 Shield work full automatically without any effect by router’s interruptions”!

I will try, and report on my result in this post.
Update.

  1. Added Connection Management (http://docs.blynk.cc/#blynk-firmware-connection-management)
  2. Added coded like a “How to make ESP8266 Shield work full automatically without any effect by router’s interruptions

It seems good, but after 2-3hours my code blocking.(Doesn’t reset.)

#define BLYNK_PRINT Serial // Blynk Print Serial. This "define" should place on top of sketch
//#define BLYNK_DEBUG_ALL Serial
#define BLYNK_MAX_SENDBYTES 2048 // set Limit Blynk Symbol Number(include Subject + body)
#define BLYNK_MAX_READBYTES  4096
#define BLYNK_MSG_LIMIT 300
#include <DHT.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <avr/wdt.h>
#include <EEPROM.h>
#include <EEPROMAnything.h>
#include <SPI.h>
#include <SD.h>

#define WRITESD_DEBUG 0

//EEPROM Settings
#define EEPROM_RESET_ADDR 0x58
struct config_reset_cnt_struct
{
  unsigned int reset_cnt;
} eeprom_reset_struct;



#define ESP_SERIAL Serial2
#define ESP_BAUD 115200


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

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


ESP8266 wifi(&ESP_SERIAL);

BlynkTimer bl_timer;
int count_blynk_fail = 0;
float discon_msec = 0.0;
int last_connect_start = 0;

unsigned long arduino_smsec;


//SD card
int sd_card_pin = 53;
boolean is_sd_card_init = false;
#define SD_LOG_FILE  "pfclog.txt"
File sd_file;



BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  Serial.println("[Function Call]Called BLYNK_CONNECTED");
  count_blynk_fail = (unsigned long)0;


  int virtual_relay[] = {V26,V27,V28,V29,V30,V31,V32,V33,V34,V35,V36,V37,V38,V39,V40,V41};
  for(int i=0; i< (sizeof(virtual_relay) / sizeof(int)); i++)
  {
    Blynk.syncVirtual(virtual_relay[i]);
    delay(10);
  }


  String log_data = String("[BLYNK Connected]") + String(millis());
  writeSD(log_data);


}

BLYNK_APP_CONNECTED()
{
  Serial.println("[BLYNK] BLYNK APP CONNECTED");
}
BLYNK_APP_DISCONNECTED()
{
  Serial.println("[BLYNK] BLYNK APP DISCONNECTED");
}


void setup() {
  

  arduino_smsec = millis();

  Serial.begin(9600);
  delay(10);
  ESP_SERIAL.begin(ESP_BAUD);
  delay(10);

  
  Serial.println(">>>>>>>>>>>>");
  Serial.println("[Arduino Mega] Start");
  Serial.println(">>>>>>>>>>>>");
  EEPROM_readAnything(EEPROM_RESET_ADDR,eeprom_reset_struct);
  if (eeprom_reset_struct.reset_cnt > 60,000)
  {
    eeprom_reset_struct.reset_cnt = 0;
  }
  eeprom_reset_struct.reset_cnt += 1;
  EEPROM_writeAnything(EEPROM_RESET_ADDR, eeprom_reset_struct);
  Serial.print("[EEPROM RESET CNT] on EEPROM MEMORY : ");
  Serial.println(eeprom_reset_struct.reset_cnt);

  if (!SD.begin(sd_card_pin))
  {
    Serial.println("[SD Card]Initialization Failed"); 
  }
  else
  {

    Serial.println("[SD Card]Initialization Success");
 
  }


  // Blynk Start(Manually)
  Blynk.config(wifi, auth,BLYNK_DEFAULT_DOMAIN,BLYNK_DEFAULT_PORT);  // Attempt general connection to network
  if (Blynk.connectWiFi(ssid, pass)) {  // If connected to WiFi...
    Blynk.connect();  // ...connect to Server
  }
  else
  {
    Serial.println("[Blynk.connecteWiFi(ssid,pass)] Failed");
    delay(5000);
    wdt_enable(WDTO_1S);
  }


  // Blynk Interval Event Attach
  bl_timer.setInterval(5000L, checkBlynk);
  bl_timer.setInterval(3000L,sendMillis);
  bl_timer.setInterval(30000L, sendDhtSensor);

  Serial.println("[Setup] Blynk Timer setted");
  pinMode(3, OUTPUT);

  
}

unsigned long last_msec = millis();

void loop() {
    Blynk.run();
    bl_timer.run();
 
  if(millis() - last_msec > 30000)
  {
    Serial.println("[Elapsed Time in Loop()]" +  String(millis() - last_msec / 1000));
    last_msec =millis();
  }


}


void checkBlynk(){  
// Serial.println("[CheckBlynk()]" + String(millis()));
 if(!Blynk.connected()){
    Serial.println("[CheckBlynk()] Not connected to Blynk server"); 
    wdt_enable(WDTO_1S);
  }
} 

Above is my Arduino Code.

Below code is BlynkSimpleShieldEsp8266.h (customized)

    bool connectWiFi(const char* ssid, const char* pass)
    {
        BlynkDelay(500);
        BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
        /*if (!wifi->restart()) {
            BLYNK_LOG1(BLYNK_F("Failed to restart"));
            return false;
        }*/
        if (!wifi->kick()) {
             BLYNK_LOG1(BLYNK_F("ESP is not responding"));
             wdt_enable(WDTO_1S);
             //TODO: BLYNK_LOG_TROUBLE(BLYNK_F("esp8266-not-responding"));
             return false;
        }
        if (!wifi->setEcho(0)) {
            BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
            return false;
        }
        String ver = wifi->ESP8266::getVersion();
        BLYNK_LOG1(ver);
        if (!wifi->enableMUX()) {
            BLYNK_LOG1(BLYNK_F("Failed to enable MUX"));
        }
        if (!wifi->setOprToStation()) {
            BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
            wdt_enable(WDTO_1S);
            return false;
        }
        if (wifi->joinAP(ssid, pass)) {
            String my_ip = wifi->getLocalIP();
            BLYNK_LOG1(my_ip);
        } else {
            BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
            wdt_enable(WDTO_1S);
            return false;
        }
        BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
        return true;
    }

I don’t know how can i debugging to find what parts caused blocking! :disappointed_relieved::disappointed_relieved:

Give me some any hint @Gunner

1 Like

@Kwang-Hee_Han

I have a project running now that uses the same combination of Arduino Mega + ESP8266-01 and in the beginning I had a lot of problems maintaining a stable connection with Blynk. Now my air data instrument is running very well as a stand alone instrument and with a Blynk UI with only one “hang up” in the last 5 weeks (due to a Blynk information overload!).

I’m not an expert programmer by any means but I have learn’t a lot from the guys and posts on this forum, one very important thing I learn’t the hard way is that: -

DO NOT POWER THE ESP8266 DIRECT FROM THE ARDUINO

Use a separate 3.3Vdc power supply that will provide at least 200ma or you will have intermittent connection issues.

I live in a densely populated area of China and the Internet can sometimes be rubbish, usually in the evening when everyone is surfing or watching TV and I had to find a solution to automatically reconnecting. The Arduino can only do one thing at a time so whilst it’s trying to reconnect everything else is on hold. The last thing I wanted to do was a Software Reset as then my nice glcd graph data would be lost.

I use some code supplied by @Gunner and modified for my own situation: -


 void startBlynk() {
  if (linkState == 1) {                               //WiFi connect button pressed
    linkState = 2;                                    //Start Blynk
    wifi.setDHCP(1, 1, 1);                            //Enable dhcp in station mode and save in flash of esp8266
    Blynk.config(wifi, auth, "blynk-cloud.com", 8080);
    if (Blynk.connectWiFi(ssid, pass)) {
      Blynk.connect();
    }
    linkState = 3;                                   //Blynk startup done
  }
}

void loop()
{
  if (linkState == 1) {
    startBlynk();
  }
  if (Blynk.connected()) {                                //If connected run as normal
    Blynk.run();
  }
  if (!Blynk.connected()) {                               //If Blynk is not connected
    bnkServCon = 0;
    linkState = 1;
    startBlynk();
  }
  timer.run();
}

This code works very well for me, I have considered including a counter so after say 5 unsuccessful attempts at connecting it gives up, forgets the Internet connection and carries on with the main program, but so far everything is good.

I hope this is of some help, sorry if I’ve miss-interpreted your post/problem.

Richard

Edit… The comment in the above code : -

//WiFi connect button pressed

should be deleted as it is no longer used, on start up linkState is set to 1 and if the connection is lost it returns to 1.

1 Like

Dear @rha10 (Richard), Thank you your information.

I fight with this problem during the 4weeks… I supply external power supply to ESP8266-01(maximum supply 700mA model). Between Arduino Mega 2560 - ESP8266-01, I placed Logic Level Shifter(5v - 3.3v).

My experiment is simple.

  1. Reboot Arduino Mega2560, write EEPROM increase number(Reset Count) - in Setup() function.
  2. For this reason, every reset will be increase EEPROM “reset count number”.
  3. After arduino start, 1~2hours fine(Even I tested WIFI turn on/turn off, ESP8266-01 turn on/turn off, All of this experiment is fine=> wdt_reset() is executed well!). But, After 1-2 hours, connection out… And not executed “wdt_reset” function.
  4. I checked “EEPROM” reset count number is not increased. (It means that reset is not fired. Probably, It blocked in the blynk codes)
  5. I don’t know exactly what blynk code blocking… Or what is problem relates.

@Kwang-Hee_Han

Like I said I’m not an expert programmer but I don’t understand why you need to do a Software Reset just because your connection is lost.

The one and only time when I had to physically power down and reset was when I accidentally flooded Blynk with data and then was “locked out / blocked” until I reset.

Have you tried running a simple program?

Richard

Dear All,

I find some hint to start.

@rha10 mentioned “wdt_reset” would not need If I set callback function when connection failed.
(I will refactoring after solve the problem this blocking).

In my recent guessing is “Webhook”(VirtualWrite function ) will blocking or invoke any wating something.

I blocked, and last message is “[WebHook]http://210.92.91~” and not any refresh or new message.
I reset only ESP-8266(External power supply turn on/off).
Suddenly “[2116442]\ >[14|03]x[00]9” message displayed and arduino mega reseted.

I use “virtualwrite” function to send sensor data to my local server.

http://210.92.91.225:4000/v1/ffa88c6b0efe838a005d57bdc709/insert/?co2=503.13&ldr=72
(this url is webhook url).

Until now, my recent opinion “VirtualWrite” affect blocking codes. I will start “Does possible virtualwrite affect blocking?”.

From KgwangHee Han.