ESP8266 / ESP01 Working but not with relay module

Dear Arduino and microcontroller enthusiasts,

In my journey to learn more every week I want to learn how to work with the ESP8266 / ESP-01.

What I have done is the following:
I connected an 5v FTDI to my laptop and only connected the Tx and Rx.
Added a led to the GPIO2 port.
Used a separate power source 3v to connect the Vcc+CHPD and GND.

with the following code

 // https://diyhacking.com/esp8266-tutorial/

 // GPIO0 = GPIO0 (need to have pull-up resistors connected to ensure the module starts up correctly)
 // TX    = GPIO1 (used as the Data line, will get some debug output on GPIO1 on power up)
 // GPIO2 = GPIO2 (need to have pull-up resistors connected to ensure the module starts up correctly)
 // RX    = GPIO3 (best practice = input) (will be output HIGH on startup)

#include <ESP8266WiFi.h>
 
const char* ssid = "OhNoYouDidnt";//type your ssid
const char* password = "yesidid";//type your password
 
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port
 
void setup() {
  Serial.begin(115200);
  // Serial.begin(115200,SERIAL_8N1,SERIAL_TX_ONLY);  // needed for to use Rx as input pin
  delay(10);
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
   
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
   
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
   
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
   
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
   
  // Match the request
 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1) {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  } 
  if (request.indexOf("/LED=OFF") != -1){
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
   
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  
  if(value == HIGH) {
    client.println("<a href=\"/LED=OFF\"><button onclick="">On</button></a>"); 
  } else {
    client.println("<a href=\"/LED=ON\"><button onclick="">Off</button></a>");
  }

  
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
}

To upload:
I power the ESP8266 on.
I connect the GP0 to the GND and shortly touch the RST to the GND.
Then set the Arduino IDE to ESP8266 module and hit upload.
Works perfect!
I can go to the IP address in my browser and turn on and off the LED.

Then the problem
I want to connect a relay and the future application must be reliable.
So from 'the internet' I recover the best practice and add 2 pull-up resistors 47k from GP2 and GP0.
So I boot again, everything works can still flip the LED.
So now I connect the relay to a 5v power source and also join the GND.
I remove the LED and plug the Relay module signal on the place of the LED.
It still works.....

But then It will not reboot properly.
What I now have:

I have the feeling that I miss something...
What am I doing wrong?

Thanks in advance!

Picture of the breadboard:

For the ESP-01 to boot and run code, GPIO0 and GPIO2 must be high.

Yes, so that is why I have added the pull-up resistors from GPIO0 and GPIO2

NicoleClicquot:
Yes, so that is why I have added the pull-up resistors from GPIO0 and GPIO2

I bet if you measure the voltage at GPIO2, it is LOW.

mmm.. true or my LED would light up right!

I got the pull-up from the following tutorial, under heading "Using the GPIO0 /GPIO2 for OUTPUT and RX for INPUT":
ESP8266 01 pin magic

It shows the following:

I now see that I also used 3k3 resistors, not 47k.

Still puzzled here.
So there is something wrong with my pull-up resistors, but what...

What I also do not understand is that:

  1. I want HIGH on those two pins to Run code.
  2. Then I want to be able to change the output through the code, but they are HIGH for the Boot, so How can I use them.... I have the feeling I miss some basic understanding here.... But what...

NicoleClicquot:
mmm.. true or my LED would light up right!

I got the pull-up from the following tutorial, under heading "Using the GPIO0 /GPIO2 for OUTPUT and RX for INPUT":
ESP8266 01 pin magic

It shows the following:

I now see that I also used 3k3 resistors, not 47k.

Still puzzled here.
So there is something wrong with my pull-up resistors, but what...

What I also do not understand is that:

  1. I want HIGH on those two pins to Run code.
  2. Then I want to be able to change the output through the code, but they are HIGH for the Boot, so How can I use them.... I have the feeling I miss some basic understanding here.... But what...

You didn't follow that circuit exactly, did you?

First: Thanks for helping me out and pushing me to find the solution myself :wink:

I thought it would not matter, so I did not add the other resistors.
Oke, So I have added:

  • CH_PD - 3K3 - Vcc
  • RST - 3K3 - Vcc
  • Rx - GND

Still won't boot normally. (It however boots normally if I remove the relay during boot)
I read 0v on GPIO0 and 1.2v on GPIO2

So from the schematic I only did not connect:

  • the Rx resistor, as I do not have an input yet
  • the Active relay driver to GPIO0, as I want to use only one relay.

What am I still missing here?
Is the relay module pulling it down to LOW, thereby initiating the UART mode?

What kind of relay driver you use? Driver or module? What is there in the other end GPI00 lead? A relay driver should not load much but you never know (especially when we don't know). A schematic of the driver/module? If you remove all power supplies, you can measure your self how much resistance there is between driver input and ground.

I use ESP01 WIFI Relay Module. To drive the relay I wrote a little scketch that I use in my Christmas Tree lamps. The module use only serial line to send commands to internal STC15F104 chip.
As you can see must be set Serial(9600) and send 4 bytes to switch on or off teh relay: see the ON and OFF defines in the code.

/* Author Enrico Taormina : ESP01 Relay Module : Simple Christmas Tree Lights : 2017 12 21 */
int larrayidx=0;
int laststart=0;
//typedef unsigned long uint;

typedef struct {int times;uint t_on;uint t_off;int next;} cycle ;
typedef struct {int times;uint t_on;uint t_off;int next;uint starttime;} cycle_run ;

#define FACTOR 4

#define OFF Serial.write(0xA0);Serial.write(0x01);Serial.write(0x01);Serial.write(0xA2);
#define ON Serial.write(0xA0);Serial.write(0x01);Serial.write(0x00);Serial.write(0xA1);

cycle larray[]=
{
{5,100,100,1},
{5,500,500,2},
{5,200,100,3},
{5,100,200,4},
{20,200,120,5},
{50,150,250,6},
{10,500,500,7},
{1,5000,5000,8},
{30,210,310,9},
{10,100,100,10},
{20,100,210,11},
{5,200,120,12},
{20,200,210,13},
{10,220,310,14},
{20,320,110,15},
{1,1000,100,0}
};
cycle_run run_cycle;

void newrun(int i)
{
run_cycle.times=larray*.times;*
run_cycle.t_on=larray.t_onFACTOR;
run_cycle.t_off=larray.t_offFACTOR;

run_cycle.next=larray*.next;
run_cycle.starttime=millis();
_}
void loops()
{
unsigned long mil=millis();_

unsigned long elp=mil-run_cycle.starttime;
if ( elp < run_cycle.t_on ) {ON; return;}
if ( (elp < (run_cycle.t_on + run_cycle.t_off)) ) {OFF ; return;}
if ( run_cycle.times-- > 0 )
_ {_
run_cycle.starttime=mil;
_ }else{_
newrun( run_cycle.next );
_ }
return;
}
void setup() {
Serial.begin(9600);
delay(10);
newrun(0);
}
void loop() {
// put your main code here, to run repeatedly:
loops();
}*_

Totally unrelated to the topic, but oke nice... Who can be against Christmas lights this time a year?

However, how is this sketch connected to the wifi?
Why run loops() in loop()

And
On topic: I never got the ESP-01 stable to reboot into the main program. I used the Wemos ESP-12 which is very easy to work with, however a bit bigger. Aliexpress also sells nice shields for this modules.

Wemos:
https://www.aliexpress.com/item/NodeMCU-Lua-ESP8266-ESP-12-WeMos-D1-Mini-WIFI-4M-Bytes-Development-Board-Module/32703511086.html

Shield:
https://www.aliexpress.com/item/ProtoBoard-Shield-for-WeMos-D1-mini-double-sided-perf-board-Compatible/32713427652.html