So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Brandon
#77132 Hi there, I don't know if this question has been asked but I couldn't seem to find it. I am creating a remote light switch using the ESP8266 and a relay. I have followed this tutorial to the exact specs only difference is the code and the safeties used before the AC 120V to DC 5V conversion. Here is the link to the tutorial:

http://www.instructables.com/id/ESP8266-Wifi-Switch/

The problem I am having is that everything works fine when I don't have the 120V connected to the relay. I have left it running for 2 days straight without issues. However, when I connect the 120V to the relay. It'll work for maybe a minute or two sometimes longer but then it will fault and just stop working. I heard about Isolating the relay but removing the jumper between JD-VCC and VCC and giving it its own 5V but that did not work. I also read about using a diode in after the 2N2222 Transistor. But still the issue is still there. I am almost certain it is not my code becuase again it works fine all the way until the 120 is put through the relay. Anyone have any ideas? My code basically reads from a php file in my server which connects to a database
and reads the value. Here is my code:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <TextFinder.h>

 
// 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);
}


Thanks again for whoever helps as I have been working on this for months now and cant figure this part out.