ESP32 needs to be re-programmed after internet is disconnected

elinor.g
Posts: 1
Joined: Sun Sep 27, 2020 4:55 pm

ESP32 needs to be re-programmed after internet is disconnected

Postby elinor.g » Sun Sep 27, 2020 5:03 pm

Hello,

I've programmed an DOIT ESP32 devkit V1 board to turn on and off an LED. The board is controlled by Blynk app via Wifi. Today Wifi in my apartment got disconnected and I wasn't able to control the board after it was connected again, I had to connect the board to my computer and to re-upload the program to the board in order to make it respond to my app again. I think the problem was that it disconnected from the Wifi, and didn't try to re-establish the connection. How can I make it re-connect to the Wifi in such cases?

Here's my code:

Code: Select all

#define BLYNK_PRINT Serial
#define RELAY_PIN 32
#define TEMP_PIN V5
#define MSG_PIN V6
// Initialize Telegram BOT
#define BOTtoken "1200001400:AAF6RKXn_FjMHjWh4p1CqrCe7Tmc9P3AFQA"  // your Bot Token (Get from Botfather)


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Global variables
bool was_overheated;

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
String chat_id = "1225422883";


int botRequestDelay = 1000;

// Sends log every 15 minutes.
// int botRequestDelay = 900000;
unsigned long lastTimeBotRan;
char ledState;

// 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[] = "...";


#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

BlynkTimer timer;


void createLOG(){
  // Send LOG
  bot.sendMessage(chat_id, "Temperature: ", "");
  String celsius = String((temprature_sens_read() - 32) / 1.8) + " C";
  bot.sendMessage(chat_id, celsius, "");
  bot.sendMessage(chat_id, "Light State: ", "");
  if (digitalRead(RELAY_PIN)){
        bot.sendMessage(chat_id, "LED is ON", "");
  }
  else{
    bot.sendMessage(chat_id, "LED is OFF", "");
  } 
}

void setup() {  
  pinMode(RELAY_PIN, OUTPUT); 
  pinMode(RELAY_PIN, HIGH);

  was_overheated = false;
  
  Serial.begin(115200);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected"); 

  Blynk.begin("...", ssid, pass); 

  timer.setInterval(1000L, myTimerEvent);
}

void myTimerEvent()
{
  
  // Conver raw temp in F to Celsius degrees
  Blynk.virtualWrite(TEMP_PIN, (temprature_sens_read() - 32) / 1.8);
  if(((temprature_sens_read() - 32) / 1.8) <= 85){
    Blynk.virtualWrite(MSG_PIN, "NO MESSAGES");
    if(was_overheated == true){
      digitalWrite(RELAY_PIN, HIGH);
      was_overheated = false;
    }
  }

  if((((temprature_sens_read() - 32) / 1.8) > 85) && (was_overheated == false) ){
      was_overheated = true;
      digitalWrite(RELAY_PIN, LOW);
      Blynk.virtualWrite(MSG_PIN, "OVERHEATED!!");       
    }
}

void loop(){
    Blynk.run();
    timer.run();
            
    if (millis() > lastTimeBotRan + botRequestDelay)  {
      createLOG();
      lastTimeBotRan = millis();
    }
}
Thank you.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: ESP32 needs to be re-programmed after internet is disconnected

Postby PeterR » Sun Sep 27, 2020 10:27 pm

As a guess:
I think you need to shut down services on loss of IP and start on gain.
I am not very familiar with Arduino & how you hook to IP events however.

I use IDF and most IDF services are quite happy & recover when IP address is lost on their own. Some do not not however e.g. mDNS. I am guessing that your Wifi library needs to be stopped & started after disconnect.
Its an IT thing - did you switch it off and on again? ;)
The key thing is how you get IP events (which IJDK), the rest should be simple.
& I also believe that IDF CAN should be fixed.

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: ESP32 needs to be re-programmed after internet is disconnected

Postby username » Wed Sep 30, 2020 3:20 am

You are not checking Wifi status in your loop. If wifi gets lost, you need to detect that and reconnect.

techstudycell
Posts: 1
Joined: Mon Jul 19, 2021 3:00 am

Re: ESP32 needs to be re-programmed after internet is disconnected

Postby techstudycell » Mon Jul 19, 2021 4:07 am

Hi,
Please ad the following function to check the Blynk status

Code: Select all

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, LOW); //Turn on WiFi LED
  }
}
Then add the following line in the void setup() to call the function after every 3 second

Code: Select all

timer.setInterval(3000L, checkBlynkStatus);
For the complete code, you can also visit the following article
https://iotcircuithub.com/nodemcu-esp8 ... tomation/

Who is online

Users browsing this forum: Bing [Bot] and 183 guests