This circuit is the following:
**ESP8266-01:**
VCC <--> 3V3 power supply
GND <--> GND (power supply)
CH_PD <--> 3V3 power supply
GPIO2 <--> IN (relay)
**3V relay:**
VCC <--> 3V3 power supply
GND <--> GND (power supply)
IN <--> GPIO2 (ESP-01)
The relay is mounted on a power extension cord, on which I can connect various devices.
Here is my circuit diagram (the breadboard is not represented):
My sketch is the following:
#include <ESP8266WiFi.h>
const int switch_pin = 2;
int state = 0;
void setup() {
Serial.begin(115200);
WiFi.forceSleepBegin();
pinMode(switch_pin, OUTPUT);
}
void loop() {
if (state == 0){
digitalWrite(switch_pin, HIGH);
state = 1;
Serial.println("1");
}
else{
digitalWrite(switch_pin, LOW);
state = 0;
Serial.println("0");
}
delay(1000);
}
which produces, on the serial:
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
ets Jan 8 2013,rst cause:4, boot mode:(3,7)
wdt reset
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
ets Jan 8 2013,rst cause:4, boot mode:(3,7)
wdt reset
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld
Thus, sometimes, the Esp-01 resets and, sometimes it cannot "recover" from such resets.
However, when I disconnect the extension cord (from the electrical outlet in the wall), the relay switches indefinitely as expected (I do not know if it's a clue to solve my problem).
I tried with 2 relays and 2 Esp-01 and always got the same behaviour.
Would you have any idea about the origin of such a problem?