ESP32 wifi getting disconnected once within 3-4 days

sd.hrishi
Posts: 1
Joined: Tue Jan 17, 2023 5:06 am

ESP32 wifi getting disconnected once within 3-4 days

Postby sd.hrishi » Tue Jan 17, 2023 5:30 am

I integrated ESP32 (ESP32 DEVKITC V4) with the Blynk IoT platform. I observed that my ESP32 is getting disconnected once in a while from Wifi. I added a patch to restart ESP (ESP.restart()) if it is not connected to Wifi for the last 30 minutes. Sometimes this works but not always. I have to turn off the power supply and then turn it on. And then it gets to connect to Wifi within a few seconds.

I tried with esps, but no luck.
I'm not sure what is the reason.

Here is the program:

[Codebox]//#define BLYNK_DEBUG // Optional, this enables more detailed prints
//#define BLYNK_PRINT Serial // Defines the object that is used for printing
#define BLYNK_TEMPLATE_ID "<<Replace-me>>"
#define BLYNK_DEVICE_NAME "<<Replace-me>>"
#define BLYNK_AUTH_TOKEN "<<Replace-me>>"
#define BLYNK_FIRMWARE_VERSION "0.1.2"


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


#define WIFI_LED 26
#define SENSOR_PIN 36
#define VPIN_0 V0
#define RELAY_PIN 5
#define PUMP_IDEAL 0
#define PUMP_RUNNING 1
#define PUMP_FORCE_KILL_IN_MINS 15 // IN MINS
#define HARD_RESTART_AFTER 30 // IN MINS

char auth[] = BLYNK_AUTH_TOKEN;
int wifiFlag = 0;
int pumpState =0; //PUMP_IDEAL, PUMP_RUNNING
int pumpStartTime =0; //In millis

int lastConnected = 0;

bool forceKilled = false;
int pumpRunDurationInMinsInFroceKill = 0;

char ssid[] = "<<Replace-me>>";
char pass[] = "<<Replace-me>>";


BlynkTimer timer;


BLYNK_WRITE(VPIN_0)
{

int val = param.asInt();

if(val ==1){

pumpState=PUMP_RUNNING;
pumpStartTime=millis();

Serial.println("Pump Started ");
Blynk.logEvent("pump_start", String("Pump Started."));

digitalWrite(RELAY_PIN, LOW);

}else {

digitalWrite(RELAY_PIN, HIGH);

double totalTimePumpRun = (double) (millis() - pumpStartTime)/(1000.0*60.0); //mins
pumpStartTime = 0;

if(pumpState == PUMP_RUNNING) {
Serial.println(String("Pump runs (Mins) : ") + totalTimePumpRun);
Blynk.logEvent("pump_stopped", String("Pump runs (in Mins): ") + totalTimePumpRun) ;
Blynk.virtualWrite(V1, totalTimePumpRun);
}else {
Serial.println(String("Pump runs (Mins) : ") + totalTimePumpRun);
Blynk.logEvent("pump_stopped", String("[Without Start Data] Pump runs (in Mins): ") + totalTimePumpRun) ;
}

pumpState=PUMP_IDEAL;
}
}

void checkBlynkStatus() {
// HARD RESTART
if(millis() - lastConnected > HARD_RESTART_AFTER*60*1000){
lastConnected = millis();
ESP.restart();

}

// FORCE KILL;
if(pumpState == PUMP_RUNNING){
pumpRunDurationInMinsInFroceKill = (millis() - pumpStartTime) /(1000*60) ;

if(pumpRunDurationInMinsInFroceKill >= PUMP_FORCE_KILL_IN_MINS){
digitalWrite(RELAY_PIN, HIGH);
pumpState=PUMP_IDEAL;
forceKilled = true;

Serial.println("Force Killed Pump after (in mins) : " + pumpRunDurationInMinsInFroceKill);
}
}

float sensorValue = analogRead(SENSOR_PIN);
int moisture = (int) (100 -(sensorValue/4095)*100);

bool isconnected = Blynk.connected();

if (isconnected == false) {
wifiFlag = 1;
WiFi.reconnect();
digitalWrite(WIFI_LED, LOW);

} else {
wifiFlag = 0;
lastConnected = millis();

digitalWrite(WIFI_LED, HIGH);
Blynk.virtualWrite(V2, moisture);

if(forceKilled){
Blynk.virtualWrite(VPIN_0, 0);

pumpRunDurationInMinsInFroceKill = (pumpRunDurationInMinsInFroceKill <= 0)?1:pumpRunDurationInMinsInFroceKill;
Blynk.virtualWrite(V1, pumpRunDurationInMinsInFroceKill);

Blynk.logEvent("force_kill", String("Foce Killed pump after (in mis)" )+ pumpRunDurationInMinsInFroceKill);

pumpRunDurationInMinsInFroceKill = 0;
forceKilled = false;
}
}
}

BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_0);
}

void setup(){
Serial.begin(9600);
Serial.println("Planto-Hydra is starting.");

lastConnected = millis();

pinMode(RELAY_PIN, OUTPUT);
pinMode(WIFI_LED, OUTPUT);

timer.setInterval(2000L, checkBlynkStatus);

digitalWrite(WIFI_LED, LOW);
digitalWrite(RELAY_PIN, HIGH);

Blynk.begin(auth, ssid, pass);
Serial.println("Planto-Hydra Setup completed.");

}

void loop()
{
Blynk.run();
timer.run();
}[/Codebox]

esp-at
Posts: 200
Joined: Mon May 09, 2022 3:00 am

Re: ESP32 wifi getting disconnected once within 3-4 days

Postby esp-at » Mon Jan 30, 2023 3:48 am

Here is the forum for esp-at project: https://github.com/espressif/esp-at, i am not sure if you post a wrong place for your issue.

if you use esp-at firmware, please provide more details, such as AT+GMR, and AT logs.

Who is online

Users browsing this forum: No registered users and 41 guests