Hi there,
I am trying to use an ESP8266 to control a light switch wirelessly connected to a relay. I am able to get everything working but when I connect the 120V to the relay side my ESP will stop working after a minute or 2 sometimes longer or shorter. If I do not connect that part then the relay and ESP will working properly. I have left it running for 2 days striaght without any issues. I followed this tutorial exactly only difference is the code. I have read that you need to isolate the Relay by taking the jumper off between VCC and JD-VCC and giving it its own 5V however, that still causes the same issue. I also read that you should put a 4001 Diode in after the 2N2222 Transistor however, that still has not fixed my issue. I am almost certain it is not the code as it works 100% when the 120V AC is not connected to the Relay. When I plug that in the ESP will work a couple of times and then just stop working.
Here is the tutorial I followed:
And here is my code:
#include
#include
#include
// WiFi information
const char WIFI_SSID[] = "SHAW-789830";
const char WIFI_PSK[] = "251169111284";
// Remote site information
const char http_site[] = "192.168.0.25";
const int http_port = 80;
//TextFinder finder( client);
// Pin definitions
const int LED_PIN = 5;
// Global variables
WiFiClient client;
TextFinder finder( client);
int result;
void setup()
{
pinMode(2, OUTPUT);
connectWiFi();
Serial.begin(9600);
delay(2000);
Serial.println("connecting...");
}
void loop()
{
if (client.connect(http_site, 80)) {
Serial.print("connected... ");
client.println("GET /projects/test.php HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
if (client.connected()) {
if(finder.find("test")){
result = finder.getValue();
Serial.println(result);
if (result == 0){
digitalWrite(2, LOW);
}
else{
digitalWrite(2, HIGH);
}
}
else
Serial.println("result not found");
client.stop();
delay(1000); // check again in 10 seconds
}
else {
Serial.println();
Serial.println("not connected");
connectWiFi();
client.stop();
delay(1000);
}
}
// Attempt to connect to WiFi
void connectWiFi() {
byte led_status = 0;
// Set WiFi mode to station (client)
WiFi.mode(WIFI_STA);
// Initiate connection with SSID and PSK
WiFi.begin(WIFI_SSID, WIFI_PSK);
// Blink LED while we wait for WiFi connection
while ( WiFi.status() != WL_CONNECTED ) {
digitalWrite(LED_PIN, led_status);
led_status ^= 0x01;
delay(100);
}
// Turn LED on when we are connected
digitalWrite(LED_PIN, HIGH);
}
If anyone has any thoughts please let me know.