Dear all, the ESP32S is driving me crazy, over 2 weeks the ESP32S i have connected it to some lighting and control them by Blynk & physical switch, but many times per day the ESP32 disconnect from internet and reconnect again which results in making all my light go from off to on, and i have to make them off again, am not able to maintain a reliable wifi connection.
By the way my sketch contain 2 modes as shown below, 1st mode to use blynk + physical switch in case of having internet connection & have actual feedback on mobile application from the physical button, and 2nd mode to use only physical switch in case of no internet connection.
i have been searching all over internet more than 2 weeks and i have done every thing but with out solution for example:
1- i have used different power source 2A with regulator 5V to VIN for powering the ESP32 and another source for my output relays with common ground
2- i have put capacitor 47mF on the EN to ground to smooth the volt 3.3 on the internal regulator and avoid any noise
3- i have changed all my GPIO & removed some outputs to avoid using ADC2 pins which is used during wifi as per the ESP32 data sheet
4- My router connection is very good as all my other devices PC and mobile working without any issue, even i was having internet calls on my mobile during the ESP32 wifi disconnect/reconnect issue happens without any problem in my mobile call + the ESP32 is very near to the router less than 5 meters without walls
please can u help me to find a solution for such frustrating issue really i do not know the reason for this…
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_PRINT Serial
#define DEBUG_SW 0
#define S1 23
#define R1 19 // old 15
#define S2 17
#define R2 16
#define S3 34
#define R3 21 // old 4
#define S4 35
#define R4 22
#define S5 32
#define R5 18
#define S6 33
#define R6 5
int MODE = 0;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxxxx";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
BlynkTimer timer;
int switch_ON_Flag1_previous_I = 0;
int switch_ON_Flag2_previous_I = 0;
int switch_ON_Flag3_previous_I = 0;
int switch_ON_Flag4_previous_I = 0;
int switch_ON_Flag5_previous_I = 0;
int switch_ON_Flag6_previous_I = 0;
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
digitalWrite(R1, pinValue);
// process received value
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
digitalWrite(R2, pinValue);
// process received value
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
digitalWrite(R3, pinValue);
// process received value
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
digitalWrite(R4, pinValue);
// process received value
}
BLYNK_WRITE(V5)
{
int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
digitalWrite(R5, pinValue);
// process received value
}
BLYNK_WRITE(V6)
{
int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
digitalWrite(R6, pinValue);
// process received value
}
BLYNK_WRITE(V30)
{
digitalWrite(R1, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V1, param.asInt()); // Send timer state back to button to update it.
digitalWrite(R2, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V2, param.asInt()); // Send timer state back to button to update it.
digitalWrite(R3, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V3, param.asInt()); // Send timer state back to button to update it.
digitalWrite(R4, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V4, param.asInt()); // Send timer state back to button to update it.
digitalWrite(R5, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V5, param.asInt()); // Send timer state back to button to update it.
digitalWrite(R6, param.asInt()); // take the state of the Timer and send it to the pin
Blynk.virtualWrite(V6, param.asInt()); // Send timer state back to button to update it.
}
void with_internet()
{
if (digitalRead(S1) == LOW)
{
if (switch_ON_Flag1_previous_I == 0 )
{
digitalWrite(R1, LOW);
if (DEBUG_SW) Serial.println("Relay1- ON");
Blynk.virtualWrite(V1, 0);
switch_ON_Flag1_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch1 -ON");
}
if (digitalRead(S1) == HIGH )
{
if (switch_ON_Flag1_previous_I == 1)
{
digitalWrite(R1, HIGH);
if (DEBUG_SW) Serial.println("Relay1 OFF");
Blynk.virtualWrite(V1, 1);
switch_ON_Flag1_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch1 OFF");
}
if (digitalRead(S2) == LOW)
{
if (switch_ON_Flag2_previous_I == 0 )
{
digitalWrite(R2, LOW);
if (DEBUG_SW) Serial.println("Relay2- ON");
Blynk.virtualWrite(V2, 0);
switch_ON_Flag2_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch2 -ON");
}
if (digitalRead(S2) == HIGH )
{
if (switch_ON_Flag2_previous_I == 1)
{
digitalWrite(R2, HIGH);
if (DEBUG_SW) Serial.println("Relay2 OFF");
Blynk.virtualWrite(V2, 1);
switch_ON_Flag2_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch2 OFF");
//delay(200);
}
if (digitalRead(S3) == LOW)
{
if (switch_ON_Flag3_previous_I == 0 )
{
digitalWrite(R3, LOW);
if (DEBUG_SW) Serial.println("Relay3- ON");
Blynk.virtualWrite(V3, 0);
switch_ON_Flag3_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch3 -ON");
}
if (digitalRead(S3) == HIGH )
{
if (switch_ON_Flag3_previous_I == 1)
{
digitalWrite(R3, HIGH);
if (DEBUG_SW) Serial.println("Relay3 OFF");
Blynk.virtualWrite(V3, 1);
switch_ON_Flag3_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch3 OFF");
//delay(200);
}
if (digitalRead(S4) == LOW)
{
if (switch_ON_Flag4_previous_I == 0 )
{
digitalWrite(R4, LOW);
if (DEBUG_SW) Serial.println("Relay4- ON");
Blynk.virtualWrite(V4, 0);
switch_ON_Flag4_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch4 -ON");
}
if (digitalRead(S4) == HIGH )
{
if (switch_ON_Flag4_previous_I == 1)
{
digitalWrite(R4, HIGH);
if (DEBUG_SW) Serial.println("Relay4 OFF");
Blynk.virtualWrite(V4, 1);
switch_ON_Flag4_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch4 OFF");
//delay(200);
}
if (digitalRead(S5) == LOW)
{
if (switch_ON_Flag5_previous_I == 0 )
{
digitalWrite(R5, LOW);
if (DEBUG_SW) Serial.println("Relay5- ON");
Blynk.virtualWrite(V5, 0);
switch_ON_Flag5_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch5 -ON");
}
if (digitalRead(S5) == HIGH )
{
if (switch_ON_Flag5_previous_I == 1)
{
digitalWrite(R5, HIGH);
if (DEBUG_SW) Serial.println("Relay5 OFF");
Blynk.virtualWrite(V5, 1);
switch_ON_Flag5_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch5 OFF");
//delay(200);
}
if (digitalRead(S6) == LOW)
{
if (switch_ON_Flag6_previous_I == 0 )
{
digitalWrite(R6, LOW);
if (DEBUG_SW) Serial.println("Relay6- ON");
Blynk.virtualWrite(V6, 0);
switch_ON_Flag6_previous_I = 1;
}
if (DEBUG_SW) Serial.println("Switch6 -ON");
}
if (digitalRead(S6) == HIGH )
{
if (switch_ON_Flag6_previous_I == 1)
{
digitalWrite(R6, HIGH);
if (DEBUG_SW) Serial.println("Relay6 OFF");
Blynk.virtualWrite(V6, 1);
switch_ON_Flag6_previous_I = 0;
}
if (DEBUG_SW)Serial.println("Switch6 OFF");
//delay(200);
}
}
void without_internet()
{
digitalWrite(R1, digitalRead(S1));
digitalWrite(R2, digitalRead(S2));
digitalWrite(R3, digitalRead(S3));
digitalWrite(R4, digitalRead(S4));
digitalWrite(R5, digitalRead(S5));
digitalWrite(R6, digitalRead(S6));
}
void checkBlynk() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
MODE = 1;
}
if (isconnected == true) {
MODE = 0;
}
}
void setup()
{
// Debug console
if (DEBUG_SW) Serial.begin(9600);
pinMode(S1, INPUT);
pinMode(R1, OUTPUT);
pinMode(S2, INPUT);
pinMode(R2, OUTPUT);
pinMode(S3, INPUT);
pinMode(R3, OUTPUT);
pinMode(S4, INPUT);
pinMode(R4, OUTPUT);
pinMode(S5, INPUT);
pinMode(R5, OUTPUT);
pinMode(S6, INPUT);
pinMode(R6, OUTPUT);
digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
digitalWrite(R3, HIGH);
digitalWrite(R4, HIGH);
digitalWrite(R5, HIGH);
digitalWrite(R6, HIGH);
//pinMode(MODE, INPUT);
WiFi.begin(ssid, pass);
timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
Blynk.config(auth);//, ssid, pass);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
if (DEBUG_SW) Serial.println("Not Connected");
}
else
{
if (DEBUG_SW) Serial.println(" Connected");
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
if (MODE == 0)
with_internet();
else
without_internet();
}