Interruption & Web Server

Hi everyone !

I’ve tried to create a connected dimmer for my lamp and I’ve too problem : Sometime the microcontroller (esp8266) resets itself and wake me up a 5am and when the lamp is on the light level is making some… wave ? .
I think the problem is due to delay() in my program , but what do you think ?
There is a web server running meanwhile the AC signal is modulated and I think my poor esp doesn’t like it…

Here’s my code :

/*

AC Voltage dimmer with Zero cross detection
Author: Charith Fernanado Adapted by DIY_bloke
License: Creative Commons Attribution Share-Alike 3.0 License.
Attach the Zero cross pin of the module to Arduino External Interrupt pin
Select the correct Interrupt # from the below table 
(the Pin numbers are digital pins, NOT physical pins: 
digital pin 2 [INT0]=physical pin 4 and digital pin 3 [INT1]= physical pin 5)
check: <a href="http://arduino.cc/en/Reference/attachInterrupt">interrupts</a>

Pin    |  Interrrupt # | Arduino Platform
---------------------------------------
2      |  0            |  All -But it is INT1 on the Leonardo
3      |  1            |  All -But it is INT0 on the Leonardo
18     |  5            |  Arduino Mega Only
19     |  4            |  Arduino Mega Only
20     |  3            |  Arduino Mega Only
21     |  2            |  Arduino Mega Only
0      |  0            |  Leonardo
1      |  3            |  Leonardo
7      |  4            |  Leonardo
The Arduino Due has no standard interrupt pins as an iterrupt can be attached to almosty any pin. 

In the program pin 2 is chosen
*/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "UPCFA39CA8";
const char* password = "sC8Fjzwdevce";
bool flag = true;

ESP8266WebServer server(80);

int level = 0;
int AC_LOAD = 16;    // Output to Opto Triac pin
int dimming =  128;  // Dimming level (0-128)  0 = ON, 128 = OFF

void setup()
{
  Serial.begin(9600);
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(digitalPinToInterrupt(13), zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above 
  WiFi.begin(ssid, password);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/", handleRoot);
  server.on("/switch", Switch);
  server.on("/On", On);
  server.on("/Off", Off);
  server.on("/BrightnessStatus", BrightnessStatus);
  server.on("/Status", Status);
  

  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void Switch(){
  dimming = map(server.arg("level").toInt(), 0, 100, 128, 8);
  level = server.arg("level").toInt();
  server.send(200, "text/plain", "Level set to " + String(level) + "%");
}

void Off(){
  flag = false;
}
void On(){
  flag = true;
}
void BrightnessStatus() {
  server.send(200, "text/plain", String(level));
}
void Status() {
  if (level != 0){
    server.send(200, "text/plain", "1");
  }
  else {
    server.send(200, "text/plain", "0");
  }
}
//the interrupt function must take no parameters and return nothing
void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation : 1 full 50Hz wave =1/50=20ms 
  // Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle) 
  // For 60Hz => 8.33ms (10.000/120)
  // 10ms=10000us
  // (10000us - 10us) / 128 = 75 (Approx) For 60Hz =>65
  if (flag == true){
  int dimtime = (75*dimming);    // For 60Hz =>65    
  delayMicroseconds(dimtime);    // Wait till firing the TRIAC    
  digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  delayMicroseconds(40);         // triac On propogation delay 
         // (for 60Hz use 8.33) Some Triacs need a longer period
  digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
  }
}

void handleRoot() {
  server.send(200, "text/plain", "hello from esp8266!");
}
void handleNotFound(){
  server.send(404, "text/plain", "Page not found");
}
void loop()  {
  server.handleClient();
}

Thank you for reading :smiley:

Do you have a Real Time Clock attached so you can recover from a reset? Then the processor can check the time (or time range) and do what it should be doing.

No, that's only the esp and the dimmer system, but for the reset that's just that the default state is 'On', I'll switch that . And do you have an idea for the instability ?
Thanks, tellierflexus

Nothing obvious in that code that could cause resets.

Keep it running with the Serial monitor attached, see if you get a stack trace. That'd give you some more information if it's a software glitch.

Otherwise it's likely to be lack of power. A common problem for the ESP8266.

As you have a connection to the Internet, get the time using NTP. No need for an RTC. Default your light to off, only switch it on after you have received the correct time and you determined it's time to wake up.

Thank you for your answer !
When you say lack of power, that's mean that I should switch to another micro controller ? Like Esp32 or other ?
And I have the feeling that the electrical circuit is unstable as if something were misconnected ... I will try to check that

tellierflexus:
When you say lack of power, that’s mean that I should switch to another micro controller ?

No, he means lack of power. Electrical power! What you get from a “Power point” (or “Power outlet”) in your home.

You have given some code, now give a full description/ schematic/ properly taken photographs in full daylight - of how you have it wired and what is powering it.

Indeed, electric power. Those modules draw peaks of 200 mA, your power supply must be able to deliver that easily (i.e. sufficient current available; proper wires; etc - sometimes a 100 µF cap close to the module helps).

Ahah i was hesitating between the two meaning ! (Sorry English isn't my usual language :wink: )
I can't take picture of the system until next week, so I'll come back when I've taken them.
But I know that I use a power supply which can deliver 750mA max .
For the capacitor, between Vcc and Ground ?

tellierflexus:
But I know that I use a power supply which can deliver 750mA max .
For the capacitor, between Vcc and Ground ?

Yes - place it right at the ESP module for maximum effect.