Problem with ESP8266 01 and a Relay.

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:


Here is the code i am running on the 8266:

#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:


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 :).

The 3.3V regulator of the Uno can barely supply enough current for the ESP, let alone with a relay that draws quite a lot of current on top of that.

PieterP:
The 3.3V regulator of the Uno can barely supply enough current for the ESP, let alone with a relay that draws quite a lot of current on top of that.

First of all thanks for your time again.
Ai said i tried with the power to the light bulb off (Like on the scheme 2) and the relay powered on and off without any problem.
I also tried with a diode connected directly to the relay and worked, the problem is when i use the light bulb.

Hi, i'm new to this forum.

I have a smiliar configuration.
In my case, arduino gives 5v to a "2 relay module" and i use ESP8266 GPIOS to signal the relay.
It works fine, except when i connect it to eletricity. It restarts, kind of a interferance.

Did you solve your problem?