BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Pausing Blynk re-connecting to handle button press event

Hello !
@khoih @PeteKnight @guidotillema
i was going through the forum and found this interesting thread !!
After following the exact steps as you guys have mentioned above i still find that the button stops responding when connecting to the server !!

Scene 1
Device turns on
No Router available (button responds no lag)

Scene 2
Device turns on
router available but no internet

Now after the device is connected to wifi router it fails to connect to the server as there is no internet connection available the device keep trying to connect at set intervals… during this time the code as blocked !!
This is the code i am using

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define DEBOUNCE_TIME               25
#define LONG_BUTTON_PRESS_TIME_MS   10
#define DEBUG_ISR                   0
#define VPIN1 V1
volatile unsigned long  lastDebounceTime  = 0;
volatile bool           buttonPressed     = false;
bool                    alreadyTriggered  = false;

void lightOn1();
void lightOff1();

boolean LampState1 = 0;
boolean SwitchReset1 = true;

const int RelayPin1 = D2;
const int buttonPin = D1;


// Initialize Blynk
char authD1[]         = "";
char ssid[]           = ""; 
char pass[]           = ""; 
IPAddress serverIP    (00,000,000,000);
uint16_t serverPort   = 8080;


BlynkTimer Timer;
unsigned int myServerTimeout      =  3500L;  //  3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout        =  3200L;  //  3.2s WiFi connection timeout   (WCT)
unsigned int blynkInterval        = 25000L;  // 25.0s check server frequency    (CSF)
unsigned int buttonInterval       =   100L;  //  0.1s check button state


void ICACHE_RAM_ATTR Falling();
void ICACHE_RAM_ATTR Rising();



void setup() {
  Serial.begin(115200);
    pinMode(RelayPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);


  Serial.println("Initialize Blynk");
  if(WiFi.status() == 6){
    Serial.println("WiFi not connected yet.");
  }
  Timer.setInterval(blynkInterval, checkBlynk);
  unsigned long startWiFi = millis();
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    if(millis() > startWiFi + myWiFiTimeout){
      Serial.println("Check the WiFi router. ");
      break;
    }       
  }
  Blynk.config(authD1, serverIP, 8080);
  Serial.println("This is HomeControl-TEST"); 
  checkBlynk();
  Timer.setInterval(buttonInterval, checkButton);  
}

void checkBlynk() {
  if (WiFi.status() == WL_CONNECTED)  
  {
    unsigned long startConnecting = millis();    
    while(!Blynk.connected()){
      Blynk.connect();  
      if(millis() > startConnecting + myServerTimeout){
        Serial.print("Unable to connect to server. ");
        break;
      }
    }
  }
  if (WiFi.status() != 3) {
    Serial.print("No WiFi. ");
  } 
  Serial.printf("Checking again in %is.\n", blynkInterval / 1000);
}

void Rising()
{
  unsigned long currentTime  = millis();
  unsigned long TimeDiff;
  #if (DEBUG_ISR > 0)  
  Serial.println("rising");
  #endif  
  TimeDiff = currentTime - lastDebounceTime;
  if ( digitalRead(buttonPin) && (TimeDiff > DEBOUNCE_TIME) )
  {
    #if (DEBUG_ISR > 0)     
    Serial.println("Button Released");
    #endif
    buttonPressed = false;
          
    if (TimeDiff > LONG_BUTTON_PRESS_TIME_MS)
    {
     #if (DEBUG_ISR > 0)
        Serial.printf("TimeDiff = %d ms => long Button Press (with release)\n", TimeDiff );
       #endif      
      
    } else {
      #if (DEBUG_ISR > 0)
        Serial.println("Short Button Press");
      #endif
    }
    lastDebounceTime = currentTime;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);    
  }
}

void  Falling()
{
  unsigned long currentTime  = millis();
  #if (DEBUG_ISR > 0)  
  Serial.println("falling");
  #endif  
     
  if ( !digitalRead(buttonPin) && (currentTime > lastDebounceTime + DEBOUNCE_TIME))
  {
    lastDebounceTime = currentTime;

     #if (DEBUG_ISR > 0)     
    Serial.println("Button Pressed");
    #endif   
    buttonPressed = true;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Rising, RISING);                                                
  }     
}

void loop() {
  if (Blynk.connected()) {
    Blynk.run();
    }
  Timer.run();
}


void checkButton()
{
  if (!alreadyTriggered && buttonPressed && (millis() - lastDebounceTime > LONG_BUTTON_PRESS_TIME_MS))
  {
    alreadyTriggered = true;
    #if (DEBUG_ISR > 0)
      Serial.println("Long Button Press");
    #endif
    
    // Doing something here without waiting for button released (long press)
   ButtonCheck1();
  
  }
  else if (!buttonPressed)
  {
    // Reset flag when button released to avoid triggered repeatedly
    alreadyTriggered = false;
    ButtonCheck1();
  }
}

void ButtonCheck1() {
  boolean SwitchState1 = (digitalRead(buttonPin));
  if (!SwitchState1 && SwitchReset1 == true) {
if (LampState1) {
  lightOff1();
} else {
  lightOn1();
}
SwitchReset1 = false;
delay(50);
  }
  else if (SwitchState1) {
SwitchReset1 = true;
  }
}

void ToggleRelay1() {
  LampState1 = !LampState1;
  if (LampState1) {
   lightOn1();
  }
  else lightOff1();
}

void lightOn1() {
digitalWrite(RelayPin1, LOW);
LampState1 = 1;
Blynk.virtualWrite(VPIN1, HIGH); 
}

void lightOff1() {
digitalWrite(RelayPin1, HIGH);
LampState1 = 0;
Blynk.virtualWrite(VPIN1, LOW); 
}

BLYNK_WRITE(VPIN1) {
  int SwitchStatus1 = param.asInt();
if (SwitchStatus1 == 2){
ToggleRelay1();
  }
  else if (SwitchStatus1){
lightOn1();
  }
  else lightOff1();
}

The whole purpose was to eliminate the blockage of the code during reconnection !! But the problem still exists !! MAY BE I AM GOING WRONG SOMEWHERE !!

Is there any thing missing in the code or is this the way it is supposed to work ??

EDITING TO ADD SERIAL MONITOR OUTPUT

WiFi not connected yet.
Check the WiFi router. 
[3558] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.0 on NodeMCU

This is HomeControl-TEST
No WiFi. Checking again in 25s.            *Button works without any lag [No router found]*
No WiFi. Checking again in 25s.            *Button works without any lag [No router found]*
[50057] Connecting to 40.200.200.000  *Button stops working [router available, No internet]*
[55058] Connecting to 40.200.20.000    *Button stops working [router available, No internet]*
Unable to connect to server. Checking again in 25s. *Button works without any lag*
[75057] Connecting to 40.200.200.00
[75896] Ready (ping: 208ms).
Checking again in 25s.                            *Starts to work normally !!!!!!!*

That’s the way Blynk library was written to block everything while connecting to WiFi (unless you use ISR directly) . You can see the code in function void connectWiFi(const char* ssid, const char* pass) of BlynkSimpleEsp8266.h

while (WiFi.status() != WL_CONNECTED) {
            BlynkDelay(500);
        }

At that time, nothing else can execute but ISR. And you’re relying on function checkButton(), which in turn relies on Software Timer to run. But nothing can run then !!!
The solution, if absolutely necessary, is

  1. to use Falling/Rising ISR and call your lean/mean function directly
  2. to use ESP8266TimerInterrupt ( https://github.com/khoih-prog/ESP8266TimerInterrupt ) to call checkButton() and your lean/mean function directly
  3. modify the BlynkSimpleEsp8266.h code like the following to reduce blocking time to max 10s
int i = 0;
while ( (i++ < 40) && (WiFi.status() != WL_CONNECTED) )
{
  delay(250);
}

The other reason is the way Blynk.connect() was written in BlynkProtocol.h can block up to 9s while connecting to Blynk server:

while ((state != CONNECTED) && (BlynkMillis() - started < timeout))
{
   run();
}

I installed the lib. And under USAGE section i found this code

#define TIMER_INTERRUPT_DEBUG      1

#include "ESP8266TimerInterrupt.h"

#ifndef LED_BUILTIN
#define LED_BUILTIN       2         // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#endif

volatile uint32_t lastMillis = 0;

void IRAM_ATTR TimerHandler(void)
{
  static bool toggle = false;
  static bool started = false;

  if (!started)
  {
    started = true;
    pinMode(LED_BUILTIN, OUTPUT);
  }

  #if (TIMER_INTERRUPT_DEBUG > 0)
  if (lastMillis != 0)
    Serial.println("Delta ms = " + String(millis() - lastMillis));
  lastMillis = millis();
  #endif
  
  //timer interrupt toggles pin LED_BUILTIN
  digitalWrite(LED_BUILTIN, toggle);
  toggle = !toggle;
}

#define TIMER_INTERVAL_MS        1000 

// Init ESP8266 timer 0
ESP8266Timer ITimer;


void setup()
{
  Serial.begin(115200);
  Serial.println("\nStarting");
  
  // Interval in microsecs
  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
    Serial.println("Starting  ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer correctly. Select another freq. or interval");

}

void loop()
{
  
}

But the code is not compiling

sketch_nov25c:11:16: error: expected initializer before 'TimerHandler'

 void IRAM_ATTR TimerHandler(void)

                ^

sketch_nov25c:11:16: error: expected initializer before 'TimerHandler'

 void IRAM_ATTR TimerHandler(void)

                ^

exit status 1
expected initializer before 'TimerHandler'


Can you please tell me whether the lib has been installed properly ?

Can you please shed more light on this ?? I am finding it difficult to understand the examples you have provided. And even the SwitchDebounce example from your lib is not compiling.
Can you please help me out ?

Sorry to know that you have the problem with installing library.
Can you check that you

  1. Already copy the whole directory ESP8266TimerInterrupt-master into libraries directory, normally ~/Arduino/libraries/ for Linux/Ubuntu
  2. Use the file in the examples subdirectory under ESP8266TimerInterrupt-master to avoid copying mistakes. Just a missing or extra {, } or ; can create issue.
  3. Check if you have the latest Arduino 1.8.10. I haven’t checked the earlier versions.
  4. Check if you have ESP8266 newer library version, such as 2.6.0 or 2.6.1. I haven’t checked the earlier version.

I tested again and again your posted code as well as files in the library without any compiling error, using Arduino 1.8.10 and ESP8266 library v2.6.1.

I’ll find out if the earlier libraries or Arduino versions can create this kind of error.

The lean/mean function to use in ISR (Interrupt Service Routine) means that it must be short, not using uncontrolled while, for, etc. loop that takes lot of processing time, and especially don’t use any delay() or call any function doing so.
Generally, this function is part of ISR, and must follow guidelines to write ISRs.
That’s the catch in order to bypass being blocked.

Update:
Just find out Arduino 1.8.9 has that compiling problem. I’ll find out why. Update to 1.8.10 now.

I am using 1.8.7 …
May be thats the issue !

Should i update the arduino build ?
So with your lib i should be getting the buttons working even if there is connection happening ?

Can you please provide any example sketch ?

Can you try removing IRAM_ATTR at TimerHandler(void)

void /*IRAM_ATTR*/ TimerHandler(void)

It’s better you update the Arduino to 1.8.10 and ESP8266 library to 2.6.1 as they’re stable and better now.

So with your lib i should be getting the buttons working even if there is connection happening ?

Yes, that’s the power of ISR. Certainly. it also depends on how you write your function. Anyway, I’ll use your application as an example to use either ISR directly or to use Hardware Timer to poll.
This is something I plan to do to add more examples showing the benefit of ISR. I will finish it shortly and let you know then.

I will do that !

Thank you for taking my request. Please keep us updated on the example sketch.

This is the first version using ISR directly to detect the SW and control lamp relay. It won’t stop functioning even if some other function is blocking. I tested and it’s working OK on Arduino1.8.10 and esp8266 2.6.1

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG true

#include <ESP8266WiFi.h>

//#define USE_BLYNK_WM   true
#define USE_BLYNK_WM   false

#define USE_SSL     false

#if USE_BLYNK_WM
  #if USE_SSL
    #include <BlynkSimpleEsp8266_SSL_WM.h>        //https://github.com/khoih-prog/Blynk_WM
  #else
    #include <BlynkSimpleEsp8266_WM.h>            //https://github.com/khoih-prog/Blynk_WM
  #endif
#else
  #if USE_SSL
    #include <BlynkSimpleEsp8266_SSL.h>
    #define BLYNK_HARDWARE_PORT     9443
  #else
    #include <BlynkSimpleEsp8266.h>
    #define BLYNK_HARDWARE_PORT     8080   
  #endif
#endif

#if !USE_BLYNK_WM
  #define USE_LOCAL_SERVER    true
  
  // If local server
  #if USE_LOCAL_SERVER
    char blynk_server[]   = "yourname.duckdns.org";
  #else
    char blynk_server[]   = "";
  #endif

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

#endif

#define DEBOUNCE_TIME               25
#define LONG_BUTTON_PRESS_TIME_MS   10
#define DEBUG_ISR                   0
#define VPIN1                       V1

volatile unsigned long  lastDebounceTime  = 0;
volatile bool           buttonPressed     = false;
bool                    alreadyTriggered  = false;

void lightOn1();
void lightOff1();

boolean LampState1 = 0;
boolean SwitchReset1 = true;

const int RelayPin1 = D1;     //D2;
const int buttonPin = D3;     //D1;

BlynkTimer Timer;
unsigned int myServerTimeout      =  3500L;  //  3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout        =  3200L;  //  3.2s WiFi connection timeout   (WCT)

unsigned int buttonInterval       =   500L;  //  0.5s check button state

void ICACHE_RAM_ATTR Falling();
void ICACHE_RAM_ATTR Rising();

void setup() 
{
  Serial.begin(115200);
  
  pinMode(RelayPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);

  Serial.println("Initialize Blynk");

  #if USE_BLYNK_WM
    Blynk.begin();
  #else
    unsigned long startWiFi = millis();
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      if(millis() > startWiFi + myWiFiTimeout)
      {
        Serial.println("Check the WiFi router. ");
        break;
      }       
    }
    Blynk.config(auth, blynk_server, BLYNK_HARDWARE_PORT);
    Serial.println("This is HomeControl-TEST"); 
    Blynk.connect();  
    
  #endif

  Timer.setInterval(buttonInterval, checkButton);  
}

void Rising()
{
  unsigned long currentTime  = millis();
  unsigned long TimeDiff;
  #if (DEBUG_ISR > 0)  
  Serial.println("rising");
  #endif  
  TimeDiff = currentTime - lastDebounceTime;
  if ( digitalRead(buttonPin) && (TimeDiff > DEBOUNCE_TIME) )
  {
    #if (DEBUG_ISR > 0)     
    Serial.println("Button Released");
    #endif
    buttonPressed = false;

    ButtonCheck1();
          
    if (TimeDiff > LONG_BUTTON_PRESS_TIME_MS)
    {
     #if (DEBUG_ISR > 0)
        Serial.printf("TimeDiff = %d ms => long Button Press (with release)\n", TimeDiff );
       #endif      
      
    } else {
      #if (DEBUG_ISR > 0)
        Serial.println("Short Button Press");
      #endif
    }
    lastDebounceTime = currentTime;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);    
  }
}

void  Falling()
{
  unsigned long currentTime  = millis();
  #if (DEBUG_ISR > 0)  
  Serial.println("falling");
  #endif  
     
  if ( !digitalRead(buttonPin) && (currentTime > lastDebounceTime + DEBOUNCE_TIME))
  {
    lastDebounceTime = currentTime;

     #if (DEBUG_ISR > 0)     
    Serial.println("Button Pressed");
    #endif

    ButtonCheck1();
    
    buttonPressed = true;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Rising, RISING);                                                
  }     
}

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

void checkButton()
{
  Blynk.virtualWrite(VPIN1, LampState1);   
}

void ButtonCheck1() 
{
  boolean SwitchState1 = (digitalRead(buttonPin));
  
  if (!SwitchState1 && SwitchReset1) 
  {
    if (LampState1) 
    {
      lightOff1();
    } 
    else 
    {
      lightOn1();
    }
    
    SwitchReset1 = false;
  }
  else if (SwitchState1) 
  {
    SwitchReset1 = true;
  }
}

void ToggleRelay1() 
{
  LampState1 = !LampState1;
  
  if (LampState1) 
  {
    lightOn1();
  }
  else 
    lightOff1();
}

void lightOn1() 
{
  digitalWrite(RelayPin1, LOW);
  LampState1 = 1;
}

void lightOff1() 
{
  digitalWrite(RelayPin1, HIGH);
  LampState1 = 0;
}

BLYNK_WRITE(VPIN1) 
{
  int SwitchStatus1 = param.asInt();
  
  if (SwitchStatus1 == 2)
  {
    ToggleRelay1();
  }
  else if (SwitchStatus1)
  {
    lightOn1();
  }
  else 
    lightOff1();
}

Thank you for the quick update. I will try it shortly and update you on this.
Thank you once again.

Here is the version using ISR to detect the SW press and the Hardware Timer to poll and make decision.
Be noted that all the unnecessary calls, such as delay() or

Blynk.virtualWrite(VPIN1, LampState1);

are either deleted or pushed out to Blynk Software Timer.

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG true

#include <ESP8266WiFi.h>

//#define USE_BLYNK_WM   true
#define USE_BLYNK_WM   false

#define USE_SSL     false

#if USE_BLYNK_WM
  #if USE_SSL
    #include <BlynkSimpleEsp8266_SSL_WM.h>        //https://github.com/khoih-prog/Blynk_WM
  #else
    #include <BlynkSimpleEsp8266_WM.h>            //https://github.com/khoih-prog/Blynk_WM
  #endif
#else
  #if USE_SSL
    #include <BlynkSimpleEsp8266_SSL.h>
    #define BLYNK_HARDWARE_PORT     9443
  #else
    #include <BlynkSimpleEsp8266.h>
    #define BLYNK_HARDWARE_PORT     8080   
  #endif
#endif

#define TIMER_INTERRUPT_DEBUG      1

#include "ESP8266TimerInterrupt.h"

// Init ESP8266 Hardware timer
ESP8266Timer ITimer;
#define TIMER_INTERVAL_MS         100

#if !USE_BLYNK_WM
  #define USE_LOCAL_SERVER    true
  
  // If local server
  #if USE_LOCAL_SERVER
    char blynk_server[]   = "yourname.duckdns.org";
  #else
    char blynk_server[]   = "";
  #endif

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

#endif

#define DEBOUNCE_TIME               25
#define LONG_BUTTON_PRESS_TIME_MS   10
#define DEBUG_ISR                   0
#define VPIN1                       V1

volatile unsigned long  lastDebounceTime  = 0;
volatile bool           buttonPressed     = false;
bool                    alreadyTriggered  = false;

void lightOn1();
void lightOff1();

boolean LampState1 = 0;
boolean SwitchReset1 = true;

const int RelayPin1 = D1;     //D2;
const int buttonPin = D3;     //D1;

BlynkTimer Timer;
unsigned int myServerTimeout      =  3500L;  //  3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout        =  3200L;  //  3.2s WiFi connection timeout   (WCT)

unsigned int buttonInterval       =  2000L;  //  2.0s update button state

void ICACHE_RAM_ATTR Falling();
void ICACHE_RAM_ATTR Rising();

void setup() 
{
  Serial.begin(115200);
  
  pinMode(RelayPin1, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);

  // Interval in microsecs, so MS to multiply by 1000
  // Be sure to place this HW Timer well ahead blocking calls, because it needs to be initialized.
  if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, HWCheckButton))
    Serial.println("Starting  ITimer OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer. Select another freq. or interval");

  Serial.println("Initialize Blynk");

  #if USE_BLYNK_WM
    Blynk.begin();
  #else
    unsigned long startWiFi = millis();
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      if(millis() > startWiFi + myWiFiTimeout)
      {
        Serial.println("Check the WiFi router. ");
        break;
      }       
    }
    Blynk.config(auth, blynk_server, BLYNK_HARDWARE_PORT);
    Serial.println("This is HomeControl-TEST"); 
    Blynk.connect();  
    
  #endif

  Timer.setInterval(buttonInterval, checkButton);  
}

void Rising()
{
  unsigned long currentTime  = millis();
  unsigned long TimeDiff;
  #if (DEBUG_ISR > 0)  
  Serial.println("rising");
  #endif  
  TimeDiff = currentTime - lastDebounceTime;
  if ( digitalRead(buttonPin) && (TimeDiff > DEBOUNCE_TIME) )
  {
    #if (DEBUG_ISR > 0)     
    Serial.println("Button Released");
    #endif
    buttonPressed = false;

    //ButtonCheck1();
          
    if (TimeDiff > LONG_BUTTON_PRESS_TIME_MS)
    {
     #if (DEBUG_ISR > 0)
        Serial.printf("TimeDiff = %d ms => long Button Press (with release)\n", TimeDiff );
       #endif      
      
    } else {
      #if (DEBUG_ISR > 0)
        Serial.println("Short Button Press");
      #endif
    }
    lastDebounceTime = currentTime;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Falling, FALLING);    
  }
}

void  Falling()
{
  unsigned long currentTime  = millis();
  #if (DEBUG_ISR > 0)  
  Serial.println("falling");
  #endif  
     
  if ( !digitalRead(buttonPin) && (currentTime > lastDebounceTime + DEBOUNCE_TIME))
  {
    lastDebounceTime = currentTime;

     #if (DEBUG_ISR > 0)     
    Serial.println("Button Pressed");
    #endif

    //ButtonCheck1();
    
    buttonPressed = true;
    attachInterrupt(digitalPinToInterrupt(buttonPin), Rising, RISING);                                                
  }     
}

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

void HWCheckButton()
{
  if (!alreadyTriggered && buttonPressed)
  {
    alreadyTriggered = true;
    #if (DEBUG_ISR > 0)
      Serial.println("Long Button Press");
    #endif
    
    // Doing something here without waiting for button released (long press)
   ButtonCheck1();
  
  }
  else if (!buttonPressed)
  {
    // Reset flag when button released to avoid triggered repeatedly
    alreadyTriggered = false;
    ButtonCheck1();
  }
}

void checkButton()
{
  Blynk.virtualWrite(VPIN1, LampState1);   
}

void ButtonCheck1() 
{
  boolean SwitchState1 = (digitalRead(buttonPin));
  
  if (!SwitchState1 && SwitchReset1) 
  {
    if (LampState1) 
    {
      lightOff1();
    } 
    else 
    {
      lightOn1();
    }
    
    SwitchReset1 = false;
  }
  else if (SwitchState1) 
  {
    SwitchReset1 = true;
  }
}

void ToggleRelay1() 
{
  LampState1 = !LampState1;
  
  if (LampState1) 
  {
    lightOn1();
  }
  else 
    lightOff1();
}

void lightOn1() 
{
  digitalWrite(RelayPin1, LOW);
  LampState1 = 1;
}

void lightOff1() 
{
  digitalWrite(RelayPin1, HIGH);
  LampState1 = 0;
}

BLYNK_WRITE(VPIN1) 
{
  int SwitchStatus1 = param.asInt();
  
  if (SwitchStatus1 == 2)
  {
    ToggleRelay1();
  }
  else if (SwitchStatus1)
  {
    lightOn1();
  }
  else 
    lightOff1();
}

Tried it just now. Seems to work ok. But once in a while the ESP is crashing. And dumping
something like this

ctx: sys 
sp: 3ffffd90 end: 3fffffb0 offset: 01a0

>>>stack>>>
3fffff30:  00000014 00000210 00000210 3fff1418  
3fffff40:  4023920a 3ffe9f14 3ffef398 4021a955  
3fffff50:  4023930c 3ffe9f14 3ffef3c0 009c8f9c  
3fffff60:  40104d7e 0030c940 3ffef3c0 60000600  
3fffff70:  40240a9d 3ffe9f14 3ffef3c0 009c82fc  
3fffff80:  40240ae2 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffef3d0 3fffdad0 3ffefdb8 4021164f  
3fffffa0:  40000f49 3fffdab0 3fffdab0 40000f49  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

May be the problem is with ESP core and Arduino build…
I will try it out with your version of CORE and arduino build.

I did not get this !! :cold_sweat:

@khoih
I tried to upgrade to arduino version to 1.8.10 and updated esp8266 core to 2.6.1
Now none of my previous codes are compiling…

It says multiple libraries were found… I tried deleting all the user files from previous build and reinstalled !! But no use. Should i change back to the older version ?? Sure its not a blynk related issue. But want to know whether you faced the same issue earlier when you upgraded…

EDIT

But the code compiles with ESP core 2.5.2
Gives no error…

Sorry to know that you got problem arising from updating.
Normally, multiple libraries were found warning is OK, provided the compiler uses the correct intended library. So you have to know what the real error is.

If you can use 2.5.2 without problem, then use it for now. But which Arduino version? 1.8.10 or 1.8.7?

I haven’t experienced that kind of problem and don’t have direct knowledge of what the issue is. Which OS are you running (Win/Ubuntu/Linux)??
Anyway, it’s better to go to Arduino forum to search or ask for advice. You can also go to my GitHub

and post Issues there, and I’ll try my best to help you out. I’m afraid here is not a good place to deal with that problem.

RE: random reset of the board:
ets Jan 8 2013,rst cause:2, boot mode:(3,6)

Selection_343

The reset came from your reset pin. This means you might use the wrong pins, or some other external devices pulse the RST pin. Did you change the pins in the code?

const int RelayPin1 = D1;     //D2;
const int buttonPin = D3;     //D1;

I’ve been running several boards continuously from the time I posted the code until now without any issue.

I found out the compiling error problem with Arduino before 1.8.10.
The version before 1.8.10 uses

ICACHE_RAM_ATTR

from 1.8.10, we can use

either ICACHE_RAM_ATTR or IRAM_ATTR

I’ll change all IRAM_ATTR to ICACHE_RAM_ATTR for better compatibility.

I tested with Arduino 1.8.9 and ESP8266 2.5.2 and it’s working OK.

Sorry. If you still have problems with updating, keep using the old version now.

Updated:

Just post new release v1.0.1 to fix those compiler errors.

1 Like

This is the build i have installed.

need to check whether this core will solve the problem or not.

i am on win8.1

same here @PeteKnight i guess Pete has not come across this thread or may be busy :wink::stuck_out_tongue_winking_eye:

I guess the problem is, i am using wifimanager to input the credentials !! May that is taking up a lot of memory !
Any ways i will check it out with core 2.5.2 and let you know !!

The new release v1.0.1 of ESP8266TimerInterrupt with new Blynk examples (inspired from your code) has been published.
It fixes all the pre-1.8.10-Arduino and pre-2.6.0-ESP8266-core compiler error issues as well as start-up and random resets you’ve experienced.

Please go to GitHub to download and test in examples subdirectory. They are too long to post here.

You can test using 1.8.10 and 2.5.2. I tested with 1.8.9 amd 2.5.2 OK.

1 Like

wooooow !! thats amazing !! I will try that shortly !!

@khoih
Hello !!
Sorry for the late update.
I tried your new example ! But it still crashes while trying to connect to the server. But works fine when both wifi and internet is up. If only wifi is available without the internet it crashes when i push the button… I tried changing the pins, changed the nodemcu module !! But still the same.
Is there any other way out ? :thinking:

The issue has been moved to

1 Like

Hello @khoih
Problem with the crash was
i had Blynk.virtualWrite on the Hardware Timer. So as soon as the connection drops(internet) it was crashing…

My bad i did not pay attention to this… Sorry !!