-->
Page 1 of 1

ESP8266 01 Relay problems

PostPosted: Sat Nov 18, 2017 4:44 pm
by fer112233
First of all thanks about Reading my post.
I am new to this things of arduino and 8266. I am triying to power on a light bulb over the internet using an 8266 01, a 1 channel 5V relay, a light bulb and an arduino uno rev3.
This is my actual scheme:
Image
Here is the code i am running on the 8266:
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char *ssid =   "MY SSID";      
const char *pass =   "My Pass";   

IPAddress server(192, 168, 1, 73); // Raspberry IP

const int relay = 2;

#define BUFFER_SIZE 100

void callback(const MQTT::Publish& pub) {
  // handle message arrived
  Serial.print(pub.topic());
  Serial.print(" => ");

    Serial.println(pub.payload_string());

    if(pub.payload_string() == "on")
    {
      Serial.println(pub.payload_string());
      digitalWrite(relay, LOW); // Should turn off the relay.
    }
    else
    {
      Serial.println(pub.payload_string());
      digitalWrite(relay, HIGH); // Should turn on the relay.
    }

}

WiFiClient wclient;
PubSubClient client(wclient, server);

void setup() {

  pinMode(relay, OUTPUT);

  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  digitalWrite(relay, HIGH);
  client.set_callback(callback);
}



void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, pass);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      if (client.connect("ESP8266: FlexoF")) {
   client.publish("outTopic","hello world");
   client.subscribe("FlexoF");
      }
    }

    if (client.connected())
      client.loop();
  }
}

Everithing Works fine, when i send to the mqtt channel to turn on, i see the blue light on the 8266 blink.
The problem is that when i connect the GPIO2 to the relay and i turn ON the signal, the light bulb turns on but the 8266 crash and it restarts itself.
When i turn OFF the power directly to the Light bulb from my house like this scheme:
Image
The esp Works perfectly i mean, i can see that when i send the command to turn on the blue light of the 8266 blinks with no problem and no crash, i hear too that the relay turns on and off without problems.

Things i already tried:
- Replace the relay with a diode: it worked.
- Replace the light bulb with a diode: it worked.
- Try with a different ESP8266 01: same problem.
- Try with a different Relay: same problem.
- Try with another power supply: same problem.
- Connect the IN1 from the relay directly to the ground of the protoboard: the light bulb turned on without problems.

Thanks for your time again, I hope I can find a solution soon :).