AC-dimmer with Blynk - cause ESP crash

Hi, again.
I still aim for a stable AC-dimmer using Blynk, but it seems a difficult way.
First I created a code as below, just for testing my hardware:

#include <ESP8266WiFi.h>
#include <Ticker.h>

 // Interupt PIN = 12
 // Dim PIN      = 13
 
Ticker blinker;

volatile int dimTime;
int LED = 2;
volatile int dimVal = 20;

void ICACHE_RAM_ATTR onTimerISR()
{
  digitalWrite(LED,!(digitalRead(LED)));  //Toggle LED Pin
  timer1_write(12800000);
  dimVal = dimVal + 10;
  if(dimVal > 130)
  {
    dimVal = 20;
  }
}

// *******************************************************************************************
void setup()
{
  Serial.begin(115200);
  delay(10);
  pinMode(13, OUTPUT);                                    // Set AC Load pin as output
  pinMode(2,OUTPUT);
  timer1_attachInterrupt(onTimerISR);
  timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
  timer1_write(12800000); //60000 = 120000 us
  attachInterrupt(12, zero_crosss_int, RISING);           // Choose the zero cross interrupt # from the table above
  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
 }

// *******************************************************************************************
void zero_crosss_int()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimVal);                                  // 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()  
{
  ESP.wdtFeed();
}

This code works stable for over two days.

Then I add BLYNK to control dimming in a new code as below:

#include <BlynkSimpleEsp8266.h>

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

int dimS;
volatile int dimVal = 20;
volatile int dimTime;

// *******************************************************************************************
void setup() 
{
  Serial.begin(115200);
  delay(10);
  pinMode(13, OUTPUT);                                    // Set AC Load pin as output
  Blynk.begin(auth, ssid, pass); 
  attachInterrupt(12, zero_crosss_int, RISING);           // Choose the zero cross interrupt # from the table above
  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
}

// *******************************************************************************************
void zero_crosss_int()                                    //function to be fired at the zero crossing to dim the light
{
  dimTime = (75*dimVal);                                  // 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();
  ESP.wdtFeed();
}

// *******************************************************************************************
BLYNK_WRITE(V1)                                           // Slider
{
  dimS = param.asInt();
  dimVal = dimS;
}

Then troubles starts. ESP crash after 10-30 min. with error message as below:


// ---------------------------------------------------------  ESP ERROR  -------------------------------------------------------------

14:33:54.877 -> Exception (0):
14:33:54.877 -> epc1=0x4020210c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
14:33:54.877 -> 
14:33:54.877 -> ctx: sys 
14:33:54.877 -> sp: 3ffffc30 end: 3fffffb0 offset: 01a0
14:33:54.877 -> 
14:33:54.877 -> >>>stack>>>
14:33:54.877 -> 3ffffdd0:  40106b58 00000000 3ffeb986 000000f7  
14:33:54.877 -> 3ffffde0:  ffffffff 00000020 00000001 00000000  
14:33:54.877 -> 3ffffdf0:  00000000 40100fab 00000000 00000022  
14:33:54.877 -> 3ffffe00:  3fffc200 40106b20 3fffc258 4000050c  
14:33:54.877 -> 3ffffe10:  4000437d 00000030 00000019 ffffffff  
14:33:54.924 -> 3ffffe20:  60000200 00000008 ffffffff 80000000  
14:33:54.924 -> 3ffffe30:  20000000 3fff1630 80000000 2007c100  
14:33:54.924 -> 3ffffe40:  80000000 3fffc6fc 3ffee308 3fff1634  
14:33:54.924 -> 3ffffe50:  000001f4 0007c100 60000600 00000030  
14:33:54.924 -> 3ffffe60:  00000128 40103891 00040000 40202148  
14:33:54.924 -> 3ffffe70:  40106bbc 00080000 00000000 40106ba6  
14:33:54.924 -> 3ffffe80:  ffffffff 00000020 00000000 4000050c  
14:33:54.971 -> 3ffffe90:  00000000 00000000 0000001f 40105139  
14:33:54.971 -> 3ffffea0:  4000050c 40106b20 3fffc258 4000050c  
14:33:54.971 -> 3ffffeb0:  40000f68 00000030 0000001a ffffffff  
14:33:54.971 -> 3ffffec0:  40000f58 00000000 00000020 00000000  
14:33:54.971 -> 3ffffed0:  3fff04dc 40208f24 00000000 00000000  
14:33:54.971 -> 3ffffee0:  ffffffff 3ffe8f64 3ffee308 3fffdab0  
14:33:54.971 -> 3ffffef0:  00000000 3fffdcb0 3ffee348 00000030  
14:33:55.018 -> 3fffff00:  00000000 400042db 00000064 60000600  
14:33:55.018 -> 3fffff10:  40004b31 3fff1534 000002f4 0007c000  
14:33:55.018 -> 3fffff20:  4010569e 3ffee330 3ffed3d0 40107118  
14:33:55.018 -> 3fffff30:  402089f5 3ffed3d0 3ffee330 1dfbe4e0  
14:33:55.018 -> 3fffff40:  3fff1534 00001000 40208e8a 00000008  
14:33:55.018 -> 3fffff50:  40105e5c 00000000 40208f37 3ffed484  
14:33:55.018 -> 3fffff60:  3ffee330 08ea9ad5 3ffee330 60000600  
14:33:55.065 -> 3fffff70:  4021a619 3ffed484 3ffee330 1dfbe24e  
14:33:55.065 -> 3fffff80:  4021a65e 3fffdab0 00000000 3fffdcb0  
14:33:55.065 -> 3fffff90:  3ffee350 00000000 40000f65 3fffdab0  
14:33:55.065 -> 3fffffa0:  40000f49 0002f684 3fffdab0 40000f49  
14:33:55.065 -> <<<stack<<<

14:33:55.111 ->  ets Jan  8 2013,rst cause:2, boot mode:(1,7)
14:33:55.111 -> 

14:34:02.939 ->  ets Jan  8 2013,rst cause:4, boot mode:(1,7)
14:34:02.939 -> 
14:34:02.939 -> wdt reset

I’m using an ESP-07 module.
Newest Arduino version.
Latest Blynk lib.
Hardware works 100%

Can anyone tell me what’s wrong when adding Blynk?

So… what is your question?

Hi,
Please go to Blynk issues & Errors area. I have open a new topic, re. AC-Dimmer. I think Blynk has an error in lib.
My Test code still working.

There is only one forum and we see all at the same time, even if differing topic category. I merged both of your topics here already.

Since you haven’t backed that up with any form of qualifiable or quantifiable evidence… or for that matter even a clearly defined description of the issue :stuck_out_tongue_winking_eye: no one will take your issue seriously

My question is: What cause ESP module crash, when adding Blynk lib.?
Test code works great, without Blynk.

Bad code? bad power? bad wiring? bad day? Too may variables, not enough info.

You may look at the code. One without Blynk (works great) and one with Blynk, (cause ESP error).

Not my job to look at (in depth) or fix your code, only to advise and suggest (and actually that is just volunteer, not a job :stuck_out_tongue_winking_eye: )

Blynk is not just a drop in magic pill… it needs to be integrated and has it’s own coding/timing rules… I recommend you learn how those rules work on smaller, simpler projects before attempting on larger, more complicated code.

PS, I had to edit your posted code to show the separate sketches & comments… if you are unable to grasp the correct syntax on formatting code (Backticks, NOT commas), then perhaps you also need more coding practice to integrate Blynk?

Perhaps timing conflict?? I have not used ESP.wdtFeed() (part of Ticker?) so I do not know. Blynk works well with the SimpleTimer based BlynkTimer for all timing purposes.

What do the errors translate out to? Google for ways to translate ESP errors

ESP.wdtFeed() is just to keep ESP WatchDog alive. I have also removed ESP WatchDog line in Blynk code.
Same issue. It works OK for 10-30 minutes. Code without Blynk works for days.

I think it’s fair to say that your two pieces of code are very different. You haven’t just added-in a few lines of Blynk code. You’ve removed some timers, that appear to be using the Ticker library, but haven’t replaced them with anything else.

I’ve not taken the time to delve too deeply into your code, one because I don’t have the time or inclination, but two because I don’t have your hardware to test-out any changes.

What I would say is that any code that has this as a void loop:

has some issues with timeouts. Blynk has very strict rules around timeouts and ensuring that the Blynk background processes receive plenty of processor time.
As A result, I’d start by looking again at your original code and what you’ve changes, and possibly add-in some Blynk.run(); commands into your void zero_crosss_int() function to feed Blynk, as well as feeding the watchdog.

Pete.

Well, as I said… Blynk is not just some simple drop-in… it is rather complex library. Many others have Blynkified code running for weeks, months, years without issue (not including partial update conflicts), so you will have to learn over time how to integrate it with other timing sensitive code. This is a common issue when merging libraries that tend to compete for system resources.

1 Like

The only Timing I use is delayMicroseconds in the ISR.
The only Library I add is #include <BlynkSimpleEsp8266.h>.
I don’t see how I need other timers and Libraries. It works, but only for 10 minutes.

FYI, I have also done some test using MCU AT328, it was very stable (without using Blynk).

At the end Hardware seems stable.

Have you tried searching this forum. There is an example of some working code for an AC dimmer using Zero-Cross detection. Although I personally have not tried this project.

Yes, I have searched and also tried some code. Mostly the code using TimerOne.h lib. TimerOne.h does not work with ESP8266.Therefore I tried a code ex.using an MCU AT328 board. Dimming works very good.
Now my problem is for ESP and Blynk. ESP as generic module without Blynk lib. works same way as for AT328, very stable.

Try disabling Blynk’s internal GPIO control… unnecessary when using virtual pins anyhow. This might alleviate some timing issue?

#define BLYNK_NO_BUILTIN // Disable built-in analog & digital pin operations

The point I’m making is that your original “works perfectly” code contains libraries and timers that you’ve dispensed with in your Blynk “cashes after 10 minutes code”

As I said, I’ve not unpicked the details of your code and what this stuff is all about, but you’ve clearly made more changes than simply adding in the Blynk stuff.

If you want to bury your head in the sand and insist that it’s a Blynk library issue then go ahead, I’m done with my input to this issue.

Pete.

1 Like

Hi,
After adding line as suggested, #define BLYNK_NO_BUILTIN // Disable built-in analog & digital pin operations.
Code become more stable. No crash so fare.

in the BlynkConfig.h I uncomment the line too, maybe it’s cover all future code. I normally only use Virtual Pins.

thank guys, I appreciate your help :+1:

Case solved.

Final test code as below:

#include <BlynkSimpleEsp8266.h>
#define BLYNK_NO_BUILTIN // Disable built-in analog and digital operations

char auth[] = “”;
char ssid[] = “”;
char pass[] = “”;

int dimS;
volatile int dimVal = 20;
volatile int dimTime;

// *******************************************************************************************
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(13, OUTPUT); // Set AC Load pin as output
Blynk.begin(auth, ssid, pass);
attachInterrupt(12, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
Blynk.syncAll();
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
}

// *******************************************************************************************
void zero_crosss_int() //function to be fired at the zero crossing to dim the light
{
dimTime = (75*dimVal); // 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();
ESP.wdtFeed();
}

// *******************************************************************************************

BLYNK_WRITE(V1) // Slider
{
dimS = param.asInt();
dimVal = dimS;
}

Uncommenting in the library or adding the define in the sketch does the same thing. The developers have put that and many other features in the library as it is very versatile.

Glad your system is running more stable… unfortunately you still didn’t read up on how to properly format your code when posting such here :frowning:

2 Likes

still after using your code i face the problem