[Arduino Mega2560 + ESP8266-01] Stopping after while

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