Project going off-line

Hi guys. I just finished my home automation project and well, I’m having a hard time with connection problems. My home automation keeps going offline everyday. One or two times a day. Then I have to press the reset button on my ESP32 to make it connect again with the router.

A little bit about the project:

I’m using a NodeMCU ESP32 (over wifi) to control 8 lights. I am also using a 16 relays board (which I’m using just 8 relays out of it), and 8 AC Voltage Sensors. The AC Voltage Sensor are used to know when a light is ON or OFF. That’s because I’m also controlling the lights with wall switches and Google Home, so the AC Voltage Sensors could provide me a virtual feedback within the Blynk App.

Everything is working fine. I can control my lights with Blynk App, with Google Home + IFTTT and through physical wall switches.

The code was build based on a code provided by one of our members, @newdos

I’ve tested two different routers as well as trying a fixed IP for the ESP32 but it keeps goings offline. Sometimes it connects again right away but mostly between 2am and 5am (-3GMT) it disconnects and doesn’t connect again, so I have to press the reset button as I said before.

When it goes offline my relays keep the state, so I know the ESP32 is not reseting, just the Wi-Fi connection that keeps going off.


#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
SimpleTimer timer;

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx"; 
char pass[] = "xxxxxxxxxx"; 


int relay_visita = 16;  // QUARTO VISITA
int sensor_visita = 13; 
int feedback;
int feedback1;
 
int relay_entrada = 17;  // ENTRADA
int sensor_entrada = 27;
int feedback2;
int feedback3;

int relay_casal = 5;   // Quarto Casal
int sensor_casal = 12; 
int feedback4;
int feedback5;

int relay_mesa = 18; // MESA
int sensor_mesa = 14;
int feedback6;
int feedback7;

int relay_sofa = 19;  // SOFÁ                     
int sensor_sofa = 35; // 
int feedback8;
int feedback9;
    
int relay_cozinha = 21;  // COZINHA               
int sensor_cozinha = 34; // 
int feedback10;
int feedback11;

int relay_tv = 22;  // TV 
int sensor_tv = 26;
int feedback12;
int feedback13;

int relay_pendente = 23;  // PENDENTE
int sensor_pendente = 25;
int feedback14;
int feedback15;


bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    isFirstConnect = false;
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  // Setup all of the pins and default ALL relays to OFF before reinstating stored states
  
  //LUZ 1
  pinMode(relay_visita, OUTPUT);
  pinMode(sensor_visita, INPUT);
  digitalWrite(relay_visita, HIGH);
  
  //LUZ 2
  pinMode(relay_entrada, OUTPUT);
  pinMode(sensor_entrada, INPUT);
  digitalWrite(relay_entrada, HIGH);
  
  //LUZ 3
  pinMode(relay_casal, OUTPUT);
  pinMode(sensor_casal, INPUT);
  digitalWrite(relay_casal, HIGH);
  
  //LUZ 4
  pinMode(relay_mesa, OUTPUT);
  pinMode(sensor_mesa, INPUT);
  digitalWrite(relay_mesa, HIGH);
  
  //LUZ 5
  pinMode(relay_sofa, OUTPUT);
  pinMode(sensor_sofa, INPUT);
  digitalWrite(relay_sofa, HIGH);
  
  //LUZ 6
  pinMode(relay_cozinha, OUTPUT);
  pinMode(sensor_cozinha, INPUT);
  digitalWrite(relay_cozinha, HIGH);
  
  //LUZ 7
  pinMode(relay_tv, OUTPUT);
  pinMode(sensor_tv, INPUT);
  digitalWrite(relay_tv, HIGH);
  
  //LUZ 8
  pinMode(relay_pendente, OUTPUT);
  pinMode(sensor_pendente, INPUT);
  digitalWrite(relay_pendente, HIGH);


  //Lets sync up all the store states for the V pins and load the appropriate variables listed below
  Blynk.syncVirtual(V3);             // Light 1 Relay state
  Blynk.syncVirtual(V5);             // Light 2 Relay state
  Blynk.syncVirtual(V7);             // Light 3 Relay state
  Blynk.syncVirtual(V9);             // Light 4 Relay state
  Blynk.syncVirtual(V11);            // Light 5 Relay state
  Blynk.syncVirtual(V13);            // Light 6 Relay state
  Blynk.syncVirtual(V15);            // Light 7 Relay state
  Blynk.syncVirtual(V17);            // Light 8 Relay state


  timer.setInterval(200, AC_detect); // Check AC_detect for AC presence every 50 milliseconds
}


void AC_detect()
{
  
  feedback1 = digitalRead(sensor_visita);
  feedback3 = digitalRead(sensor_entrada);
  feedback5 = digitalRead(sensor_casal);
  feedback7 = digitalRead(sensor_mesa);
  feedback9 = digitalRead(sensor_sofa);
  feedback11 = digitalRead(sensor_cozinha);
  feedback13 = digitalRead(sensor_tv);
  feedback15 = digitalRead(sensor_pendente);

  if (feedback != feedback1)
  { //state has changed
    Blynk.virtualWrite(V2, feedback1); // update SWITCH widget state from AC input only if it has changed
    feedback = feedback1;
    Serial.println("Estado da luz quarto de visita foi alterado."); // just monitoring through serial 
    Blynk.virtualWrite(V0, "Estado da luz quarto de visita foi alterado.\n"); // just monitoring terminal widget
  }
  
  if (feedback2 != feedback3)
  { //state has changed
    Blynk.virtualWrite(V4, feedback3);
    feedback2 = feedback3;
    Serial.println("Estado da luz da entrada foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz da entrada foi alterado.\n");
  }

  if (feedback4 != feedback5)
  { //state has changed
    Blynk.virtualWrite(V6, feedback5);
    feedback4 = feedback5;
    Serial.println("Estado da luz quarto de casal foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz quarto de casal foi alterado.\n");
  }

  if (feedback6 != feedback7)
  { //state has changed
    Blynk.virtualWrite(V8, feedback7);
    feedback6 = feedback7;
    Serial.println("Estado da luz da mesa foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz da mesa foi alterado.\n");
  }

  if (feedback8 != feedback9)
  { //state has changed
    Blynk.virtualWrite(V10, feedback9);
    feedback8 = feedback9;
    Serial.println("Estado da luz do sofá foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz do sofá foi alterado.\n");
  }

  if (feedback10 != feedback11)
  { //state has changed
    Blynk.virtualWrite(V12, feedback11);
    feedback10 = feedback11;
    Serial.println("Estado da luz da cozinha foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz da cozinha foi alterado.\n");
  }

  if (feedback12 != feedback13)
  { //state has changed
    Blynk.virtualWrite(V14, feedback13);
    feedback12 = feedback13;
    Serial.println("Estado da luz da TV foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz da TV foi alterado.\n");
  }

  if (feedback14 != feedback15)
  { //state has changed
    Blynk.virtualWrite(V16, feedback15);
    feedback14 = feedback15;
    Serial.println("Estado da luz dos pendentes foi alterado.");
    Blynk.virtualWrite(V0, "Estado da luz dos pendentes foi alterado.\n");
  }
}


// Sync up all of the virtual pin data


// -------------------------------------------------- Luz Quarto Visita

BLYNK_WRITE(V2)  // Runs every-time switch widget is toggled - For Light 1
{
  Serial.println("Botão da luz do quarto de visita pressionado."); // just monitoring through serial 
  digitalWrite(relay_visita, !digitalRead(relay_visita)); // Toggle relay 1
  Blynk.virtualWrite(V3, digitalRead(relay_visita));    // Store Light 1 relay state in V3
}

BLYNK_WRITE(V3)
{
  int pinData1 = param.asInt();       // Relay 1 state stored in V3
  digitalWrite(relay_visita, pinData1); // and reinstate relay state
}

// -------------------------------------------------- Luz Entrada

BLYNK_WRITE(V4)  // Runs every-time switch widget is toggled - For Light 2
{
  Serial.println("Botão da luz da entrada pressionado.");
  digitalWrite(relay_entrada, !digitalRead(relay_entrada)); // Toggle relay 2
  Blynk.virtualWrite(V5, digitalRead(relay_entrada));    // Store Light 2 relay state in V5
}

BLYNK_WRITE(V5)
{
  int pinData2 = param.asInt();       // Relay 2 state stored in V5
  digitalWrite(relay_entrada, pinData2); // and reinstate relay state
}

// -------------------------------------------------- Luz Quarto Casal

BLYNK_WRITE(V6)  
{
  Serial.println("Botão da luz do quarto de casal pressionado.");
  digitalWrite(relay_casal, !digitalRead(relay_casal)); 
  Blynk.virtualWrite(V7, digitalRead(relay_casal));    
}

BLYNK_WRITE(V7)
{
  int pinData3 = param.asInt();       
  digitalWrite(relay_casal, pinData3); 
}


// -------------------------------------------------- Luz Mesa

BLYNK_WRITE(V8)  
{
  Serial.println("Botão da luz da mesa pressionado.");
  digitalWrite(relay_mesa, !digitalRead(relay_mesa)); 
  Blynk.virtualWrite(V9, digitalRead(relay_mesa));    
}

BLYNK_WRITE(V9)
{
  int pinData4 = param.asInt();       
  digitalWrite(relay_mesa, pinData4); 
}

// -------------------------------------------------- Luz Sofá

BLYNK_WRITE(V10)  
{
  Serial.println("Botão da luz do sofá pressionado.");
  digitalWrite(relay_sofa, !digitalRead(relay_sofa));
  Blynk.virtualWrite(V11, digitalRead(relay_sofa));    
}

BLYNK_WRITE(V11)
{
  int pinData5 = param.asInt();       
  digitalWrite(relay_sofa, pinData5); 
}

// -------------------------------------------------- Luz Cozinha

BLYNK_WRITE(V12)  
{
  Serial.println("Botão da luz da cozinha pressionado.");
  digitalWrite(relay_cozinha, !digitalRead(relay_cozinha)); 
  Blynk.virtualWrite(V13, digitalRead(relay_cozinha));    
}

BLYNK_WRITE(V13)
{
  int pinData6 = param.asInt();       
  digitalWrite(relay_cozinha, pinData6); 
}

// -------------------------------------------------- Luz TV

BLYNK_WRITE(V14)  
{
  Serial.println("Botão da luz da TV pressionado.");
  digitalWrite(relay_tv, !digitalRead(relay_tv)); 
  Blynk.virtualWrite(V15, digitalRead(relay_tv));    
}

BLYNK_WRITE(V15)
{
  int pinData7 = param.asInt();       
  digitalWrite(relay_tv, pinData7); 
}

// -------------------------------------------------- Luz Pendente

BLYNK_WRITE(V16)  
{
  Serial.println("Botão da luz do pendente pressionado.");
  digitalWrite(relay_pendente, !digitalRead(relay_pendente)); 
  Blynk.virtualWrite(V17, digitalRead(relay_pendente));    
}

BLYNK_WRITE(V17)
{
  int pinData8 = param.asInt();       
  digitalWrite(relay_pendente, pinData8); 
  
}

void loop()
{
  Blynk.run();
  timer.run();
}


So I don’t know what is probably going wrong.

I found this AWESOME post from @wolph42 and I will try to apply his “tips”.

thank you for the compliment. I’m not sure that article will help in your case. First off, there are currently still issues with the esp32 its rather new and has ‘kinks’.

however what you can try is adding a reconnect timer something like this:

in setup

and this

void reconnectBlynk() {

  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
} 

another thing you can do as a ‘hack’ is put this on a 12h timer:

void resetESP(){
    toTerminal(String("ESP RESET"));
    delay(100);
    ESP.restart();
    delay(1000);
}

or perhaps if you loose wifi connection for >5 minutes then run the restart routine.

1 Like

Glad you now have it all working - sure you will get lots of help with the wifi issues on here as well

Cheers

kev

Awesome. I’m definitely trying these methods.

Yeah. I’m already trying to make it right.

Thanks guys.

Ok guys, after a long time not touching my ESP32, I started searching for solutions again. Unfortunately the tips that @wolph42 gave my did not work alone. What really worked for me, was found in this topic:

So basically use the first tip that @wolph42 gave us, but add a void setup_wifi() and that’s it. I ran some tests and everything is working like a charm now.