ESP32 + espalexa + OTA + Blynk = Issues with conectivity

Hi All,
I´m quite new in IOT projects, and I really appreciate if somebody can help me with a issue.

I create a project combining Blynk + OTA + espalexa using a NodeMcu ESP32S.
When ESP32 starts, everything runs perfectly (comand though Alexa or Blynk), but ramdonmly the blynk stop to work.

The interesting thing is that Internet conexion is ok and also Blynk does not lose the conection (Terminal keeps working ok).

Another issue is that, when I press the button (any virtual Pin) lot of times quickly, the ESP32 lost the blynk conection.

Here is my Code (just in case).
Thanks in advance.

Central da area da churrasqueira que controla:
  Luzes fundo Jardim PIN 18
  LEDS Churrasqueira PIN 19
  Geladeira PIN 21
  Luzes da Grelha PIN 22
  Rega da Horta PIN 23 - com temporizador de segurança
*/

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxx"

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";

#include <Ticker.h>
Ticker TerminalBlynk;
Ticker TimerSegurancaRegaHorta;
Ticker MedicaoHumidade;
Ticker TimerReset;

#define AOUT_PIN 36  //Sensor Humidade
const int AirValue = 3600;   //you need to replace this value with Value_1
const int WaterValue = 1523;  //you need to replace this value with Value_2
const int SensorPin = 15;
int soilMoistureValue = 0;
int soilmoisturepercent=0;

WidgetTerminal terminal(V1);
BLYNK_WRITE(V23) {  //Rega da Horta
  int x = param.asInt();
  if (x == 1) {
    digitalWrite(23, HIGH);
    TimerSegurancaRegaHorta.attach(1200, TimerSeguranca);
  }

  if (x == 0) {
    digitalWrite(23, LOW);
    TimerSegurancaRegaHorta.detach();
  }
}

BLYNK_WRITE(V22) {  //Grelha
  digitalWrite(22, param.asInt());
}

BLYNK_WRITE(V21) {  //Geladeira
  digitalWrite(21, param.asInt());
}

BLYNK_WRITE(V19) {  //Leds Club Casablanca
  digitalWrite(19, param.asInt());
}

BLYNK_WRITE(V18) {  // Luzes Fundo Jardim
  digitalWrite(18, param.asInt());
}

#include <ESPmDNS.h>
#include <WiFiUdp.h>

#include <ArduinoOTA.h>

#include <Espalexa.h>
void ReleRegaHorta(uint8_t brightness);
void ReleGrelha(uint8_t brightness);
void ReleGeladeira(uint8_t brightness);
void ReleLeds(uint8_t brightness);
void ReleLuzFundo(uint8_t brightness);
Espalexa espalexa;

void atualiza() {
  int sinal = (WiFi.RSSI() * -1);
  terminal.clear();
  terminal.println(WiFi.SSID());
  terminal.println(WiFi.localIP());
  terminal.print("Sinal:: ");
  terminal.print(sinal * -1);
  if (sinal <= 67) {
    terminal.println("  Excelente!");
  } else if (sinal > 67 && sinal <= 70) {
    terminal.println("  Bom!");
  } else if (sinal > 70 && sinal <= 80) {
    terminal.println("  Ruim!!");
  } else if (sinal > 80) {
    terminal.println("  Péssimo!!!");
  }
  terminal.flush();
}

void setup() {

  Serial.begin(115200);
  delay(10);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  TerminalBlynk.attach(5, atualiza);

  pinMode(23, OUTPUT);  //Rega da Horta
  pinMode(22, OUTPUT);  //Luzes Grelha
  pinMode(21, OUTPUT);  //Geladeira
  pinMode(19, OUTPUT);  //Leds
  pinMode(18, OUTPUT);  //Luz Fundo Jardim
  pinMode(2, OUTPUT);
  digitalWrite(23, LOW);
  digitalWrite(22, LOW);
  digitalWrite(21, LOW);
  digitalWrite(19, LOW);
  digitalWrite(18, LOW);
  digitalWrite(2, LOW);

  //OTA=======================================================

  ArduinoOTA.setHostname("ESP32_Central_Club_Casablanca");
  ArduinoOTA.setPassword("3289328800");
  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else  // U_SPIFFS
        type = "filesystem";
      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      Serial.println("Start updating " + type);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .onError([](ota_error_t error) {
      Serial.printf("Error[%u]: ", error);
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });
  ArduinoOTA.begin();

  espalexa.addDevice("Rega da Horta", ReleRegaHorta);
  espalexa.addDevice("Luz da Grelha", ReleGrelha);
  espalexa.addDevice("Geladeira", ReleGeladeira);
  espalexa.addDevice("Leds", ReleLeds);
  espalexa.addDevice("Luzes do Fundo do Jardim", ReleLuzFundo);
  espalexa.begin();

  MedicaoHumidade.attach(1800, MedirHumidade);

}

void loop() {
  Blynk.run();
  espalexa.loop();
  ArduinoOTA.handle();
  //digitalWrite(2, HIGH);
  //if the connection to the stongest hotstop is lost, it will connect to the next network on the list
   

}

//=================================FUNCAO TIMER SEGURANCA REGA DA HORTA=====================================

void TimerSeguranca() {
  digitalWrite(23, LOW);
  Blynk.virtualWrite(V23, 0);
}

//==================================FUNCAO LIGAR RELE (Rega da Horta) ALEXA==================================

void ReleRegaHorta(uint8_t brightness) {
  Serial.print("Device 1 changed to ");
  if (brightness) {
    Serial.print("ON, brightness ");
    digitalWrite(23, HIGH);
    Blynk.virtualWrite(V23, 1);
    TimerSegurancaRegaHorta.attach(1200, TimerSeguranca);
  } else {
    Serial.println("OFF");
    digitalWrite(23, LOW);
    Blynk.virtualWrite(V23, 0);
    TimerSegurancaRegaHorta.detach();
  }
}

//==================================FUNCAO LIGAR RELE (Grelha) ALEXA==================================

void ReleGrelha(uint8_t brightness) {
  Serial.print("Device 1 changed to ");
  if (brightness) {
    Serial.print("ON, brightness ");
    digitalWrite(22, HIGH);
    Blynk.virtualWrite(V22, 1);
  } else {
    Serial.println("OFF");
    digitalWrite(22, LOW);
    Blynk.virtualWrite(V22, 0);
  }
}

//==================================FUNCAO LIGAR RELE (Geladeira) ALEXA==================================

void ReleGeladeira(uint8_t brightness) {
  Serial.print("Device 1 changed to ");
  if (brightness) {
    Serial.print("ON, brightness ");
    digitalWrite(21, HIGH);
    Blynk.virtualWrite(V21, 1);
  } else {
    Serial.println("OFF");
    digitalWrite(21, LOW);
    Blynk.virtualWrite(V21, 0);
  }
}

//==================================FUNCAO LIGAR RELE (Leds) ALEXA==================================

void ReleLeds(uint8_t brightness) {
  Serial.print("Device 1 changed to ");
  if (brightness) {
    Serial.print("ON, brightness ");
    digitalWrite(19, HIGH);
    Blynk.virtualWrite(V19, 1);
  } else {
    Serial.println("OFF");
    digitalWrite(19, LOW);
    Blynk.virtualWrite(V19, 0);
  }
}

//==================================FUNCAO LIGAR RELE (Luzes Fundo Jardim) ALEXA==================================

void ReleLuzFundo(uint8_t brightness) {
  Serial.print("Device 1 changed to ");
  if (brightness) {
    Serial.print("ON, brightness ");
    digitalWrite(18, HIGH);
    Blynk.virtualWrite(V18, 1);
  } else {
    Serial.println("OFF");
    digitalWrite(18, LOW);
    Blynk.virtualWrite(V18, 0);
  }
}

//====================================FUNCAO MEDIR HUMIDADE SOLO=================================================

void MedirHumidade() {
 soilMoistureValue = analogRead(AOUT_PIN);  //put Sensor insert into soil
  Serial.println(soilMoistureValue);
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
    if (soilmoisturepercent > 100){
      soilmoisturepercent = 100;
    }
    if (soilmoisturepercent < 0){
      soilmoisturepercent = 0;
    }
    Blynk.virtualWrite(V36, soilmoisturepercent);
}

//======================================FUNCAO RESET ESP32=======================================================
void Reset(){
  ESP.restart();
}´´´

@Francisco_Vital Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thanks Pete,
Just done.

What EXACTLY does this mean?

Pete.

After I start to run the program, everything is working ok, but sometimes, when I press the button (blynk app), the esp32 is not following the command, i.e,. is not change the status of the virtual pin.
If I turn on by Alexa, works perfectly.

When you say “button” do you mean button widgets attached to virtual pins 18-23?

If so, how are these button widgets configured? (Push/Switch)

Have you tried adding serial print commands into your corresoinding BLYNK_WRITE(vPin) statements to confirm that the button presses are being received?

Pete.

Yes. Button widgets attached to virtual pins and also configured as Switch.
Yes, I tried to see using Serial.print, but the issue is that ESP32 does not receive the command from Blynk server even online.

Show me a snippet of code that you used for the serial print commands.

Did this code produce the correct output when everything was working okay, then stopped producing a serial output when the buttons stopped responding?

Pete.

BLYNK_WRITE(V22) {  //Grelha

  digitalWrite(22, param.asInt());

  Serial.println("Mudanca de status");

}```

Yes. When starts everything is ok. When stopped, Serial.print also stoped.

Your triple backticks need to be in a separate line for them to work correctly.

I’ve never come across an issue like this before, so not sure what to suggest next.

It might be worth using one of the basic sketch builder examples to do some testing. If that’s okay then my only thought would be to replace the ticker timers with BlynkTimers instead.

Pete.

Many Thanks for your attention.

It might also be worth you updating to the latest Blynk library version, but top do that you’ll need to move these three lines to the top of your sketch…

You should also ensure that you’re running the latest app version too.

You could also create a web dashboard and add buttons attached to the same virtual pins, and see what happens with those (Note that you add the buttons in Template view but need to be in Device view to use them and observe how they change in sync with the app buttons).

Pete.

Hi Pete,
I changed tickers (Ticker.h) to BlynkTimers and looks like that the issue was fixed.
Probably an incompatibility with tickers.h.
Many thanks for your time.

2 Likes