Blynk and Interupt cause my ESP8266 crash

I kindly asking for help to solve an issue when using AttachInterupt with Blynk.
I’m working with an AC dimmer which works fine until I add Blynk, to control by Android App.
The ESP8266 crach immediately after uploading the code.
I have tried compile, choos different ESP boards (NodeMCU, WeMos, Generic). same issue.
Following error occurs:

10:46:18.084 ->  ets Jan  8 2013,rst cause:4, boot mode:(1,7)
10:46:18.084 -> 
10:46:18.084 -> wdt reset

FYI, I have updated to latest Blynk lib. ver.0.5.4 and Arduino too.
I think the new Blynk version, has a timing issue.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Pin 12 = INT
// Pin 13 = DIM

#define TCHPIN 14
#define LEDPIN 2

char auth[] = "xx";        
char ssid[] = "yy";                                
char pass[] = "zz";                              

int dim;
volatile int dimming = 127;                     // Startup value for max dimming
volatile int dimTime; 

void setup()
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);                          // Set AC Load pin as output
  pinMode(14, INPUT);                            
  attachInterrupt(digitalPinToInterrupt(12), zcint, RISING);           // Zero cross interrupt 
  Blynk.begin(auth, ssid, pass); 
}

// *******************************************************************************************
void zcint()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimming);                       // Value set for 50Hz    
  delayMicroseconds(dimTime);                   // Wait till firing the TRIAC
  digitalWrite(13, HIGH);                       // Fire the TRIAC
  delayMicroseconds(10);                        // triac On propogation delay 
  digitalWrite(13, LOW);                        // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

// *******************************************************************************************
void loop()  
{  
  Blynk.run();
}

// *******************************************************************************************
BLYNK_WRITE(V1)                                           // slider 
{
 dim = param.asInt();
 dimming = dim;
}

If I remove Blynk lines and use my button version code, it works perfect and very stable.

----------- CODE FOR BUTTON VERSION ---------


// This works fine !
// Pin 12 = INT
// Pin 13 = DIM

#define TCHPIN 14
#define LEDPIN 2

int tchState;
volatile int dimTime;
volatile int dimming = 127;                     // Startup value for max dimming
int count = 0;
int period = 200;                               // Time for debunch when touch
unsigned long time_now = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);                          // Set AC Load pin as output
  pinMode(14, INPUT);                            
  attachInterrupt(12, zcint, RISING);           // Zero cross interrupt 
  dimming = count;
  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
 }

// *******************************************************************************************
void zcint()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimming);                       // Value set for 50Hz    
  delayMicroseconds(dimTime);                   // Wait till firing the TRIAC
  digitalWrite(13, HIGH);                       // Fire the TRIAC
  delayMicroseconds(10);                        // triac On propogation delay 
  digitalWrite(13, LOW);                        // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

// *******************************************************************************************
void loop()  
{  
  time_now = millis();                          // Initial timer
  tchState = digitalRead(TCHPIN);
  if (tchState == HIGH)
    {
      if(count == 120)
      {
        digitalWrite(2, LOW);                   // Turn LED on
      }
      else
      {
        digitalWrite(2, HIGH);
      }
      if (count < 100)
       { 
        count = count + 15;                     // Dimmer is not liniar
       }
      else 
       {
        count = count + 5;
       }
      if(count > 125)                           // Value 125 set max. dimming
       {
        count = 10;                             // Reset to full light
       }
      
      while(millis() < time_now + period)       // Here is the delay for debunch
        {
         dimming = count;
        }
  }
   ESP.wdtFeed();
}

Hello… firstly… I fixed your post formatting…

Blynk%20-%20FTFC

No, I think you are just using it incorrectly :wink: Have you searched and read others attempts with AC dimmers and Blynk?

This means whatever your code is doing, not only stalled out Blynk, but the ESP’s internal firmware as well… causing the WDT reset.

This explains that better…

Hi, sorry I really try do formatting text using the ‘’ but seem not working.
Next, yes I have study most of the AC dimmers. Most of them is related to AT328 type MCU’s, only few ESP8266. I have previously tried some code with old version of Blynk (before timer was built-in to lib.) and this works quite well, but still unstable.

Ihave a very stable version running right now, using a Touch function which step up-down the dimming. Is has been working 24-7 for over a week. It proves the hardware works.
Now I want to combine the AC dimmer with Blynk adding a simple slider .

I have read a lots of how Interupt works when using ESP8266.
No matter how I add Blynk, It goes into a WDT crash. I have spend lots of time and almost give up.
My last hope is getteing some help or idea how to get Blynk working.

Below my well function code for AC-dimmer using a touch button.


/ This works fine !
// Pin 12 = INT
// Pin 13 = DIM

#define TCHPIN 14
#define LEDPIN 2
#define INTPIN 12

int tchState;
volatile int dimTime;
volatile int dimming = 127;                     // Startup value for max dimming
int count = 0;
int period = 200;                               // Time for debunch when touch
unsigned long time_now = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);                          // Set AC Load pin as output
  pinMode(14, INPUT);                            
  attachInterrupt(digitalPinToInterrupt(INTPIN), zcint, RISING);
  dimming = count;
  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
 }

// *******************************************************************************************
void zcint()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimming);                       // Value set for 50Hz    
  delayMicroseconds(dimTime);                   // Wait till firing the TRIAC
  digitalWrite(13, HIGH);                       // Fire the TRIAC
  delayMicroseconds(10);                        // triac On propogation delay 
  digitalWrite(13, LOW);                        // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

// *******************************************************************************************
void loop()  
{  
  time_now = millis();                          // Initial timer
  tchState = digitalRead(TCHPIN);
  if (tchState == HIGH)
    {
      if(count == 120)
      {
        digitalWrite(2, LOW);                   // Turn LED on
      }
      else
      {
        digitalWrite(2, HIGH);
      }
      if (count < 100)
       { 
        count = count + 15;                     // Dimmer is not liniar
       }
      else 
       {
        count = count + 5;
       }
      if(count > 125)                           // Value 125 set max. dimming
       {
        count = 10;                             // Reset to full light
       }
      
      while(millis() < time_now + period)       // Here is the delay for debunch
        {
         dimming = count;
        }
  }
   ESP.wdtFeed();
}

Here is my Blynk Code which works few minutes:

#include <BlynkSimpleEsp8266.h>

#define INTPIN 12                                         // Interupt Pin (D6)
#define TCHPIN 14
#define DIMPIN 13
#define LEDPIN 2

int count = 0;
int dim = 125;      
volatile int dimming = 125;                                // Dimming level (0-128)  0 = ON, 128 = OFF
volatile int dimTime;

char auth[] = "xx";        
char ssid[] = "yy";                               
char pass[] = "zz";                               

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass); 
  delay(10);
  pinMode(DIMPIN, OUTPUT);                               // Set AC Load pin as output
  pinMode(2, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(INTPIN), zcint, RISING);
  attachInterrupt(digitalPinToInterrupt(TCHPIN), tchon, RISING);
  Blynk.syncAll();
}

// *******************************************************************************************
BLYNK_WRITE(V1)                                           // slider 
{
 dim = param.asInt();
 dimming = dim;
  if(dim >= 125)
   {
     digitalWrite(2, LOW);                   // Turn LED on
   }
  else
   {
    digitalWrite(2, HIGH);
   }
}

// *******************************************************************************************
void zcint()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimming);                                 // For 60Hz =>65    
  delayMicroseconds(dimTime);                             // Wait till firing the TRIAC
  digitalWrite(DIMPIN, HIGH);                            // Fire the TRIAC
  delayMicroseconds(10);                                  // triac On propogation delay (for 60Hz use 8.33)
  digitalWrite(DIMPIN, LOW);                             // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

// *******************************************************************************************
void loop()  
{
  if(Blynk.connected())                                   // Check if still connected
   {
     Blynk.run();
   }
}
// *******************************************************************************************
void tchon()
{
  Serial.println("TOUCH ACTIVE");
  if(count >= 125)
      {
        digitalWrite(2, LOW);                   // Turn LED on
      }
      else
      {
        digitalWrite(2, HIGH);
      }
      if (count < 100)
       { 
        count = count + 15;                     // Dimmer is not liniar to eye
        dimming = count;
       }
      else 
       {
        count = count + 5;
        dimming = count;
       }
      if(count > 125)                           // Value 125 set max. dimming
       {
        count = 10;                             // Reset to full light
        dimming = count;
       }
     Blynk.virtualWrite(V1, dimming);
     Blynk.syncVirtual(V1);
}

I have seen a lot of posts with issues using Zero-cross detect for AC Dimming.

There is some code that works (as shown in the video), but it uses a different timer. Maybe give that a try.

Backtick key… three times… not comma, apostrophe, etc. And don’t forget the cpp on the preceding set.

```cpp
void loop()

Another option is to use one that controls using i2c

HI,
Hardware is not the issue. I have not design my hardware using Raspberry, it won’t fit into the project, too big.
I have tried code with ATMEGA nano board using TimerOne lib and Blynk, it works.
Still it seems to be a timing problem using Blynk. I’m not an expert in Blynk, but something is wrong when using ESP8266 and time critical functions. TimerOne.h get benefit of ATMEGA internal timers. For ESP they have similar Timers (Ticker.h), but I missing the commands:

Timer1.initialize(freqStep);                    
Timer1.attachInterrupt(dim_check, freqStep);

I am not saying it is a hardware issue, but rather a possible timing issue with the code needed for that hardware and BLYNK. I think it does cause some timing conflict as the interrupt routine would be triggered around 100 times a second, continuously. From What I have seen here on this forum, AC dimmer projects using Zero-Cross Detection on ESP Boards seem to give problems with BLYNK. Thus, why i suggested the board that uses i2c.

One way you may be able to check, is to disconnect the AC lines and see if the connection is stable. That is, no interrupt being triggered.

Your “i2c” board have attiny chip, and you can just use arduino nano for this.

My recommendation for your project is this https://www.aliexpress.com/item/UNO-WiFi-R3-ATmega328P-ESP8266-32Mb-memory-USB-TTL-CH340G-Compatible-for-Arduino-Uno-NodeMCU-WeMos/32806748844.html?spm=2114.search0104.3.87.6e0b2a86dqNiVB&ws_ab_test=searchweb0_0,searchweb201602_4_10065_10068_319_317_5733915_10696_10924_10084_453_454_10083_10618_5733715_10920_10921_10304_10922_10307_10820_10821_537_10302_536_5733815_10059_10884_5734015_10887_100031_321_322_10103,searchweb201603_51,ppcSwitch_0&algo_expid=f43d795e-31b0-4bed-a307-1ff2cb28984f-12&algo_pvid=f43d795e-31b0-4bed-a307-1ff2cb28984f
esp8266 have only 2 timers, one for Wifi and one for other staff, if you try setup timer0 this is bad idea.

As with most problems, there is usually more than one solution.

While I do not know too much about the board you linked to, it appears that it just an Arduino, or an ESP8266, or an Arduino with a ESP Shield. In this particular case, it would probably be used as an Arduino with ESP Shield. If so, the OP could then refer back to my first post which has some example code for this type of set-up. Using the TimerOne library. Although he seems to have already gotten this working with an ATMEGA nano.

The i2c boards that linked to, while having an attiny chip, are easily compatible with the ESP Boards mentioned in the opening post.

Another Option is to use one of these, which are discussed here:

yes, i2c can be use on any board that have support and library. MPDMv4 is good dimmer, but expensive and not repeatable for makers. Also have only 1 channel. Any body can do it on arduino nano /mega /… at home. Like me a have dimmer running on nano and he receive messages via serial from esp8266. I think this is the best solution.