blynkkk / blynk_Issues

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ticker is not working with Arduino core for the ESP32 release v2.0.0

whogarden opened this issue · comments

I reuse an old project that was compiled with Arduino core for the ESP32 release v1.0.6,
but now with release 2.0.0 ticker doesn't work anymore.

Hardware:

ESP32 16MB (dev board)
4 relay boards

Sketch:

/*
 Roller shutter ESP32 
 ESP32 serial(115200 bauds) sending Hex command to  '4 relays board'
 to know when a relay is on/off test gpio x ( x defined in 'senseRelay')
 Drive one relay at a time and prevent the relay from beeing on a long time by using 'Ticker' 
 (I use 'Ticker' because a few times the order 'relay off' was lost and the relay stayed on many hours ...)
*/
#include <SoftwareSerial.h>

SoftwareSerial SerialA(4, 5) ;  //  rx unused, tx connected to rx 1st 4 relays board
SoftwareSerial SerialB(12, 14) ; // rx unused, tx connected to rx 2nd 4 relays board
SoftwareSerial SerialC(25, 26) ; // rx unused, tx connected to rx 3rd 4 relays board
SoftwareSerial SerialD(18, 19) ; // rx unused, tx connected to rx 4th 4 relays board

byte relON[] = {0xA0, 0x01, 0x01, 0xA2};  //Hex command to send to serial to close relay1
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial to open relay1
byte re2ON[] = {0xA0, 0x02, 0x01, 0xA3};  //Hex command to send to serial to close relay2
byte re2OFF[] = {0xA0, 0x02, 0x00, 0xA2}; //Hex command to send to serial to open relay2
byte re3ON[] = {0xA0, 0x03, 0x01, 0xA4};  //Hex command to send to serial to close relay3
byte re3OFF[] = {0xA0, 0x03, 0x00, 0xA3}; //Hex command to send to serial to open relay3
byte re4ON[] = {0xA0, 0x04, 0x01, 0xA5};  //Hex command to send to serial to close relay4
byte re4OFF[] = {0xA0, 0x04, 0x00, 0xA4}; //Hex command to send to serial to open relay4

#include <Ticker.h>
Ticker RelayRelease;
int Time_On = 1;// maximum time relay is on

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "yyyyyyyyyyyyyyyy"

#define BLYNK_FIRMWARE_VERSION        "0.2.4"

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7

#include "BlynkEdgent.h"

#define senseRelay 13
int prevState = -1;
int currState = -1;
long lastChangeTime = 0;
int Vcase = 1;

// Release every relay
void SecureRelay1() {
  SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay2() {
  SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay3() {
  SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay4() {
  SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF

  delay(30);
  //SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay5() {
  SerialB.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay6() {
  SerialB.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay7() {
  SerialB.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay8() {
  SerialB.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay9() {
  SerialC.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay10() {
  SerialC.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay11() {
  SerialC.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay12() {
  SerialC.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay13() {
  SerialD.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay14() {
  SerialD.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay15() {
  SerialD.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void SecureRelay16() {
  SerialD.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  delay(30);
  //SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
  RelayRelease.detach();
}
void checkPin()
{
  // Invert state, since button is "Active LOW"
  int state = !digitalRead(senseRelay);
//  Serial.print("senseRelay value read : ");
//  Serial.println(state);
  // Debounce mechanism
  long t = millis();
  if (state != prevState) {
    lastChangeTime = t;
  }
  if (t - lastChangeTime > 5) {
    if (state != currState) {
      currState = state;
      Blynk.virtualWrite(V101, (state));
      Serial.print("GPIO 2 value to write : ");
      Serial.println(state);
      switch (Vcase) {
        case 1:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V11, (state));
          break;
        case 2:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V21, (state));
          break;
        case 3:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V31, (state));
          break;
        case 4:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V41, (state));
          break;
        case 5:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V51, (state));
          break;
        case 6:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V61, (state));
          break;
        case 7:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V71, (state));
          break;
        case 8:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V81, (state));
          break;
        case 9:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V14, (state));
          break;
        case 10:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V24, (state));
          break;
        case 11:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V34, (state));
          break;
        case 12:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V44, (state));
          break;
        case 13:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V54, (state));
          break;
        case 14:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V64, (state));
          break;
        case 15:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V74, (state));
          break;
        case 16:
          // block of code 1: do something when var == label_1
          Blynk.virtualWrite(V84, (state));
          break;
      }
    }
  }
  prevState = state;
}

WidgetLED led10(V100);
//BlynkTimer timer;
// V100 LED Widget is blinking
void blinkLedWidget()
{
  if (led10.getValue()) {
    led10.off();
    //Serial.println("LED on V1: off");
  } else {
    led10.on();
    //Blynk.virtualWrite(V20, 255);
    }
}

BLYNK_WRITE(V11) // Executes when the value of virtual pin V11 changes
{
  RelayRelease.detach();
  Vcase = 1;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialA.write(relON, sizeof(relON));     // turns the relay ON
    delay(30);
    SerialA.write(relON, sizeof(relON));     // turns the relay ON
    Serial.println("Relay V11 ON");
    RelayRelease.attach(Time_On, SecureRelay1);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    delay(30);
    SerialA.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    Serial.println("Relay V11 OFF");
  }
}

BLYNK_WRITE(V21) // Executes when the value of virtual pin V21 changes
{
  RelayRelease.detach();
  Vcase = 2;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialA.write(re2ON, sizeof(re2ON));     // turns the relay ON
    delay(30);
    SerialA.write(re2ON, sizeof(re2ON));     // turns the relay ON
    Serial.println("Relay V21 ON");
    RelayRelease.attach(Time_On, SecureRelay2);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    delay(30);
    SerialA.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    Serial.println("Relay V21 OFF");
  }
}
BLYNK_WRITE(V31) // Executes when the value of virtual pin V31 changes
{
  RelayRelease.detach();
  Vcase = 3;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialA.write(re3ON, sizeof(re3ON));     // turns the relay ON
    delay(30);
    SerialA.write(re3ON, sizeof(re3ON));     // turns the relay ON
    Serial.println("Relay V31 ON");
    RelayRelease.attach(Time_On, SecureRelay3);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    delay(30);
    SerialA.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    Serial.println("Relay V31 OFF");
  }
}
BLYNK_WRITE(V41) // Executes when the value of virtual pin V41 changes
{
  RelayRelease.detach();
  Vcase = 4;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialA.write(re4ON, sizeof(re4ON));     // turns the relay ON
    delay(30);
    SerialA.write(re4ON, sizeof(re4ON));     // turns the relay ON
    Serial.println("Relay V41 ON");
    RelayRelease.attach(Time_On, SecureRelay4);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    delay(30);
    SerialA.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    Serial.println("Relay V41 OFF");
  }
}

BLYNK_WRITE(V14) // Executes when the value of virtual pin V14 changes
{
  RelayRelease.detach();
  Vcase = 9;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialB.write(relON, sizeof(relON));     // turns the relay ON
    delay(30);
    SerialB.write(relON, sizeof(relON));     // turns the relay ON
    Serial.println("Relay V14 ON");
    RelayRelease.attach(Time_On, SecureRelay5);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialB.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    delay(30);
    SerialB.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    Serial.println("Relay V14 OFF");
  }
}
BLYNK_WRITE(V24) // Executes when the value of virtual pin V24 changes
{
  RelayRelease.detach();
  Vcase = 10;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialB.write(re2ON, sizeof(re2ON));     // turns the relay ON
    delay(30);
    SerialB.write(re2ON, sizeof(re2ON));     // turns the relay ON
    Serial.println("Relay V24 ON");
    RelayRelease.attach(Time_On, SecureRelay6);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialB.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    delay(30);
    SerialB.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    Serial.println("Relay V24 OFF");
  }
}
BLYNK_WRITE(V34) // Executes when the value of virtual pin V34 changes
{
  RelayRelease.detach();
  Vcase = 11;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialB.write(re3ON, sizeof(re3ON));     // turns the relay ON
    delay(30);
    SerialB.write(re3ON, sizeof(re3ON));     // turns the relay ON
    Serial.println("Relay V34 ON");
    RelayRelease.attach(Time_On, SecureRelay7);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialB.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    delay(30);
    SerialB.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    Serial.println("Relay V34 OFF");
  }
}
BLYNK_WRITE(V44) // Executes when the value of virtual pin V44 changes
{
  RelayRelease.detach();
  Vcase = 12;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialB.write(re4ON, sizeof(re4ON));     // turns the relay ON
    delay(30);
    SerialB.write(re4ON, sizeof(re4ON));     // turns the relay ON
    Serial.println("Relay V44 ON");
    RelayRelease.attach(Time_On, SecureRelay8);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialB.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    delay(30);
    SerialB.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    Serial.println("Relay V44OFF");
  }
}

//--------------------------------------------------------------------//
BLYNK_WRITE(V51) // Executes when the value of virtual pin V51 changes
{
  RelayRelease.detach();
  Vcase = 5;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialC.write(relON, sizeof(relON));     // turns the relay ON
    delay(30);
    SerialC.write(relON, sizeof(relON));     // turns the relay ON
    Serial.println("Relay V51 ON");
    RelayRelease.attach(Time_On, SecureRelay9);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialC.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    delay(30);
    SerialC.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    Serial.println("Relay V51 OFF");
  }
}
BLYNK_WRITE(V61) // Executes when the value of virtual pin V61 changes
{
  RelayRelease.detach();
  Vcase = 6;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialC.write(re2ON, sizeof(re2ON));     // turns the relay ON
    delay(30);
    SerialC.write(re2ON, sizeof(re2ON));     // turns the relay ON
    Serial.println("Relay V61 ON");
    RelayRelease.attach(Time_On, SecureRelay10);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialC.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    delay(30);
    SerialC.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    Serial.println("Relay V61 OFF");
  }
}
BLYNK_WRITE(V71) // Executes when the value of virtual pin V71 changes
{
  RelayRelease.detach();
  Vcase = 7;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialC.write(re3ON, sizeof(re3ON));     // turns the relay ON
    delay(30);
    SerialC.write(re3ON, sizeof(re3ON));     // turns the relay ON
    Serial.println("Relay V71 ON");
    RelayRelease.attach(Time_On, SecureRelay11);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialC.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    delay(30);
    SerialC.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    Serial.println("Relay V71 OFF");
  }
}
BLYNK_WRITE(V81) // Executes when the value of virtual pin V81 changes
{
  RelayRelease.detach();
  Vcase = 8;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialC.write(re4ON, sizeof(re4ON));     // turns the relay ON
    delay(30);
    SerialC.write(re4ON, sizeof(re4ON));     // turns the relay ON
    Serial.println("Relay V81 ON");
    RelayRelease.attach(Time_On, SecureRelay12);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialC.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    delay(30);
    SerialC.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    Serial.println("Relay V81OFF");
  }
}

BLYNK_WRITE(V54) // Executes when the value of virtual pin V54 changes
{
  RelayRelease.detach();
  Vcase = 13;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialD.write(relON, sizeof(relON));     // turns the relay ON
    delay(30);
    SerialD.write(relON, sizeof(relON));     // turns the relay ON
    Serial.println("Relay V54 ON");
    RelayRelease.attach(Time_On, SecureRelay13);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialD.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    delay(30);
    SerialD.write(relOFF, sizeof(relOFF));     // turns the relay OFF
    Serial.println("Relay V54 OFF");
  }
}
BLYNK_WRITE(V64) // Executes when the value of virtual pin V64 changes
{
  RelayRelease.detach();
  Vcase = 14;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialD.write(re2ON, sizeof(re2ON));     // turns the relay ON
    delay(30);
    SerialD.write(re2ON, sizeof(re2ON));     // turns the relay ON
    Serial.println("Relay V64 ON");
    RelayRelease.attach(Time_On, SecureRelay14);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialD.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    delay(30);
    SerialD.write(re2OFF, sizeof(re2OFF));     // turns the relay OFF
    Serial.println("Relay V64 OFF");
  }
}
BLYNK_WRITE(V74) // Executes when the value of virtual pin V74 changes
{
  RelayRelease.detach();
  Vcase = 15;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialD.write(re3ON, sizeof(re3ON));     // turns the relay ON
    delay(30);
    SerialD.write(re3ON, sizeof(re3ON));     // turns the relay ON
    Serial.println("Relay V74 ON");
    RelayRelease.attach(Time_On, SecureRelay15);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialD.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    delay(30);
    SerialD.write(re3OFF, sizeof(re3OFF));     // turns the relay OFF
    Serial.println("Relay V74 OFF");
  }
}
BLYNK_WRITE(V84) // Executes when the value of virtual pin V84 changes
{
  RelayRelease.detach();
  Vcase = 16;
  int pin_value = param.asInt();
  if (pin_value == 1)
  {
    // execute this code if the switch widget is now ON
    SerialD.write(re4ON, sizeof(re4ON));     // turns the relay ON
    delay(30);
    SerialD.write(re4ON, sizeof(re4ON));     // turns the relay ON
    Serial.println("Relay V84 ON");
    RelayRelease.attach(Time_On, SecureRelay16);
  }
  else
  {
    // execute this code if the switch widget is now OFF
    SerialD.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    delay(30);
    SerialD.write(re4OFF, sizeof(re4OFF));     // turns the relay OFF
    Serial.println("Relay V84 OFF");
  }
}

void setup()
{
  pinMode(senseRelay, INPUT);
  Serial.begin(115200);
  delay(100);
  Serial.println("\n\nFile : Roller-Shutter_ESP32-001.ino\n");
  SerialA.begin(115200);
  SerialB.begin(115200);
  SerialC.begin(115200);
  SerialD.begin(115200);

  BlynkEdgent.begin();
  timer.setInterval(1000L, blinkLedWidget);
}

void loop() {
  BlynkEdgent.run();
  checkPin();
}

Not working is very abstract. Not compiling? Not connecting? Etc.

First of all, these 2 lines should be always at the top of the code

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "yyyyyyyyyyyyyyyy" 

Hello,
Thank you for this response.
In that case, ticker is used to secure a relay that can be activated with a virtual push button.
Sometimes, the relay stays on (I suppose, that the order sent by blynk when I release the virtual push button is lost somewhere...)
If after some delay the relay stays on, ticker is used to set the relay off.
I have just updated the device (compiled with the new arduino core 2.0.1) with Blynk.Air and the same sketch where I put the 2 define at the top of the code.
Tests are now in progress.

Hello,
With the 2 lines at the top of the sketch there is nothing new...
(Relays are not set off)
I restored Arduino core for ESP32 V1.0.6 and everything is working good.
(Return to ESP32 V2.0.1 and hello problems).
Any idea?
Thank you.

Hello,
To help someone with same pb,
I found the error, it's a bad update of Blynk.
The file indicator.h was not updated.