esp8266 + octocoupler relay: WDT reset when using AC current I'm trying to switch on bulb via 4 channel 5V optocoupler relay controller from ESP8266 using WIFI Access point. All my connections looking fine and i can able to connect WIFI SoftAP and send signal to ESP8266 01. Even i can able to switch the Relay from NO to NC or other way around. Only when a connect 240V AC current to relay for powering the bulb, i'm getting following error from ESP

21:11:34.312 -&gt;  ets Jan  8 2013,rst cause:4, boot mode:(1,7) 
21:11:34.312 -&gt;  
21:11:34.312 -&gt; wdt reset


If i use 12V DC power to light the led via relay its working fine. if again use 240V power to relay to light bulb i'm get same error again.

I have already tried using ceramic capacitor for decoupling to avoid noise from AC. Still no luck. Please advice. I'm very new and naive at handling electronics.

Adding the circuit as well.



Update: After further testing, I can see following behaviour,
If i connect AC bulb to relay and turn it on. Then by provide a long gap before turning it off makes the ESP stable for longer time.

Even better, if i turn off/on my mobile wifi between each time i turn on or off the AC bulb then ESP is always stable.

Adding my code as well.

#include &lt;ESP8266WiFi.h&gt;
#include &lt;WiFiClient.h&gt; 
#include &lt;ESP8266WebServer.h&gt;

/* Set these to your desired credentials. */
const char *ssid = "*****";
const char *password = "*********";

ESP8266WebServer server(80);

const int led = 2;  //GPIO2

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
 * connected to this access point to see it.
 */
void handleRoot() {

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"&lt;body&gt;\  
    &lt;p&gt;Status: Light ON&lt;/h1&gt;&lt;/p&gt;\
    &lt;p&gt;&lt;a href=\"http://192.168.4.1/on\"&gt;&lt;h1&gt;Turn ON&lt;/h1&gt;&lt;/a&gt;&lt;/p&gt;\
    &lt;p&gt;&lt;/p&gt;\
    &lt;p&gt;&lt;a href=\"http://192.168.4.1/off\"&gt;&lt;h1&gt;Turn OFF&lt;/h1&gt;&lt;/a&gt;&lt;/p&gt;\           
  &lt;/body&gt;\
  ",

    hr, min % 60, sec % 60
  );
  server.send ( 200, "text/html", temp );  

}

void setup() {
    delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");

    /* You can remove the password parameter if you want the AP to be open. */
    WiFi.softAP(ssid, password);

    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);

  pinMode(led, OUTPUT);
  digitalWrite ( led, HIGH );

  //URLs available to query
    server.on("/", handleRoot);
  server.on ( "/on", turnON );    
  server.on ( "/off", turnOFF );
    server.begin();
    Serial.println("HTTP server started");

}

void turnON(){

  digitalWrite ( led, HIGH );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"&lt;body&gt;&lt;p&gt;&lt;a href=\"http://192.168.4.1/off\"&gt;&lt;h1&gt;Turn OFF&lt;/h1&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);

}

void turnOFF(){

  digitalWrite ( led, LOW );

  int size=1000;
  char temp[size];

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, size,

"&lt;body&gt;&lt;p&gt;&lt;a href=\"http://192.168.4.1/on\"&gt;&lt;h1&gt;Turn ON&lt;/h1&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;",

    hr, min % 60, sec % 60
  );

  server.send ( 200, "text/html", temp);

}

void loop() {
    server.handleClient();
}
To prevent EMI and to save the relay contacts, you could switch the relays at AC zero-crossing. Add some kind of zero-crossing detection to you project and before switching a relay, wait for a zero-crossing and then wait just before the next zero crossing (about 9500 microseconds at 50 Hz AC). 

Simple is to use a current sensor to detect zero-crossing. Current AC wave should be aligned with the voltage wave, if there is no shift in power factor.