ESP8266-01 Web Server

Hi,

I am bulding a swing gate opener system and I would like the door to be controlled using Wi-Fi.

This is the topic where I talk about the system itself:https://forum.arduino.cc/index.php?topic=586973.0

I am very confused with the connections of the ESP8266-01 and the programming. I have an Arduino MEGA to program the Wi-Fi Module.

Basically my idea is to create an application or a web server with buttons for controlling the door.

I think I managed to connect the ESP8266-01 to the router but the Arduino showed this message or something similar

Leaving...
Hard resetting via RST pin...

This is the sketch I use and at the bottom is the error message.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(81);

const char* ssid = "Name"; //Enter Wi-Fi SSID
const char* password =  "Password"; //Enter Wi-Fi Password
 
void setup() {
  Serial.begin(115200); //Begin Serial at 115200 Baud
  WiFi.begin(ssid, password);  //Connect to the WiFi network
  
  while (WiFi.status() != WL_CONNECTED) {  //Wait for connection
      delay(500);
      Serial.println("Waiting to connect...");
  }
  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //Print the local IP
  
  server.on("/", handle_index); //Handle Index page
  
  server.begin(); //Start the server
  Serial.println("Server listening");
}

void loop() {
  server.handleClient(); //Handling of incoming client requests
}

void handle_index() {
  //Print Hello at opening homepage
  server.send(200, "text/plain", "Hello! This is an index page.");
}

Error Message

esptool.py v2.6
2.6
esptool.py v2.6
Serial port COM3
Connecting........_____....._____....._____....._____....._____....._____.....____Traceback (most recent call last):
  File "C:\Users\SkyMaster\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2/tools/upload.py", line 25, in <module>
    esptool.main(fakeargs)
  File "C:/Users/SkyMaster/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py", line 2653, in main
    esp.connect(args.before)
  File "C:/Users/SkyMaster/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool\esptool.py", line 468, in connect
    raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

The sketch took more than a half of the memory of the Wi-Fi Module which is a bit weird for me, since my sketch for the swing gate opener system is quite a lot of code as well. I am not sure if the memory will be enough.

Is there a way to programm the Arduino MEGA and just use the Wi-Fi Module as a module to connect to the router instead of programming the Wi-Fi Module itself?

your esp-01 pinout is from the bottom side?

Juraj:
your esp-01 pinout is from the bottom side?

That is correct, the pinout is when you look the ESP8266 from the bottom side and the pins are on the left side.

in esp8266 the WiFi is handled by software. the Espressif SDK is linked to your sketch.

did you reset the esp8266 before upload? io 0 LOW will put it in bootloader mode at reset

Juraj:
in esp8266 the WiFi is handled by software. the Espressif SDK is linked to your sketch.

did you reset the esp8266 before upload? io 0 LOW will put it in bootloader mode at reset

No, I did not reset the ESP8266. Could you tell me how to do it? Is it via the RESET pin the on the Module?

KonstantinBG:
No, I did not reset the ESP8266. Could you tell me how to do it? Is it via the RESET pin the on the Module?

disconnect ch-pd (enable pin) for a moment

Juraj:
disconnect ch-pd (enable pin) for a moment

When should I remove the pin before pressing the button or after?

I have read on the Internet about a flasher for the ESP8266 Module, I did not manage to get it working, do I need to do that first?

KonstantinBG:
When should I remove the pin before pressing the button or after?

I have read on the Internet about a flasher for the ESP8266 Module, I did not manage to get it working, do I need to do that first?

'flasher'? what is that? you want to upload a sketch to the esp8266. the esp8266 will receive it only in bootloader mode.

the rest is not time critical. the esp8266 will stay in bootloader mode after reset with io 0 LOW.

Juraj:
'flasher'? what is that? you want to upload a sketch to the esp8266. the esp8266 will receive it only in bootloader mode.

the rest is not time critical. the esp8266 will stay in bootloader mode after reset with io 0 LOW.

I did the reset when the sketch was almost compiled and I got this message.

esptool.py v2.6
2.6
esptool.py v2.6
Serial port COM3
Connecting....
Chip is ESP8266EX
Features: WiFi
MAC: 84:0d:8e:a4:4f:22
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 1MB
Flash params set to 0x0320
Compressed 293072 bytes to 210624...

Writing at 0x00000000... (7 %)
Writing at 0x00004000... (15 %)
Writing at 0x00008000... (23 %)
Writing at 0x0000c000... (30 %)
Writing at 0x00010000... (38 %)
Writing at 0x00014000... (46 %)
Writing at 0x00018000... (53 %)
Writing at 0x0001c000... (61 %)
Writing at 0x00020000... (69 %)
Writing at 0x00024000... (76 %)
Writing at 0x00028000... (84 %)
Writing at 0x0002c000... (92 %)
Writing at 0x00030000... (100 %)
Wrote 293072 bytes (210624 compressed) at 0x00000000 in 18.7 seconds (effective 125.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

But there is nothing in the Serial Monitor and it looks like it stuck at leaving.

I think I managed make it work, when I saw the message for Leaving, I removed the GPIO0 pin from GND and I reset the ESP8266 by removing the CH_PD pin for a moment.

Then I got a message in the Serial Monitor with the IP address of the Module.

KonstantinBG:
I did the reset when the sketch was almost compiled and I got this message.

esptool.py v2.6

2.6
esptool.py v2.6
Serial port COM3
Connecting....
Chip is ESP8266EX
Features: WiFi
MAC: 84:0d:8e:a4:4f:22
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 1MB
Flash params set to 0x0320
Compressed 293072 bytes to 210624...

Writing at 0x00000000... (7 %)
Writing at 0x00004000... (15 %)
Writing at 0x00008000... (23 %)
Writing at 0x0000c000... (30 %)
Writing at 0x00010000... (38 %)
Writing at 0x00014000... (46 %)
Writing at 0x00018000... (53 %)
Writing at 0x0001c000... (61 %)
Writing at 0x00020000... (69 %)
Writing at 0x00024000... (76 %)
Writing at 0x00028000... (84 %)
Writing at 0x0002c000... (92 %)
Writing at 0x00030000... (100 %)
Wrote 293072 bytes (210624 compressed) at 0x00000000 in 18.7 seconds (effective 125.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...




But there is nothing in the Serial Monitor and it looks like it stuck at leaving.

the esptool.py does a hard reset so the esp8266 returns to bootloader mode because io 0 is still LOW. set io 0 HGH with a pull-up resistor and reset the esp8266. your sketch will start

Juraj:
the esptool.py does a hard reset so the esp8266 returns to bootloader mode because io 0 is still LOW. set io 0 HGH with a pull-up resistor and reset the esp8266. your sketch will start

You mean after the sketch is uploaded to the ESP8266 to connect a resistor the CH_PD pin and then to 3.3V.

What do you think about what I said in the previous post?

Do I need to follow this procedure every time I want to program the ESP8266 Module?

KonstantinBG:
You mean after the sketch is uploaded to the ESP8266 to connect a resistor the CH_PD pin and then to 3.3V.

What do you think about what I said in the previous post?

Do I need to follow this procedure every time I want to program the ESP8266 Module?

I wrote my comment before I read yours. You did it right.
Now I realized that the esptool doesn't really reset your module, because the module doesn't have autoreset circuit.
Dev boards with USB connector like Wemos or NodeMcu have auto-reset circuit to set io 0 LOW and resets the board.

The upload tool changed in new versions (2.5) of the esp8266 arduino core. With the old tool you could leave io 0 LOW. The sketch run after upload. So one could upload and test the sketch and only reset before next upload. The new tool uses an upload stub and it changes things and I don't know how because I didn't use it yet.

You can wire buttons to io 0 and ch_pd. Or buy an USB-to-TTL-Serial adapter with DTR and RTS lines used for auto-reset. Or you could control the esp-01 with the Mega and upload to it over Mega (Arduino + ESP8266 with EspProxy (develop without rewiring) - Project Guidance - Arduino Forum).

Hi,

If I understand correctly, I actually program the ESP8266 itself not the Arduino MEGA. The Arduino MEGA is just a tool that is utilized to program the ESP8266. Are my thoughts correct?

I tried to implement my code for the swing gate opener in the same web server sketch but I found out that the ESP8266 does not support, for example, uint_8t data type. This is a bit dissaipointing since I use a DS3231 Real Time Clock.

My next idea to fix the problem is to use na I2C communication between the ESP8266 and the Arduino MEGA where the ESP8266 will be the Master. It receives request from the web server and then the Arduino MEGA, depending on the request that the ESP8266 has received, does the elbow grease. The main code for the swing gate system is on the Arduino MEGA while the ESP8266 only cares about the web server and the requests.

I am not sure if that is possible and if it is too complicated.

Another solution to the problem is to buy an ESP development board with the capabilities of an Arduino MEGA.

Which is the better solution in your opinion?

KonstantinBG:
Hi,

If I understand correctly, I actually program the ESP8266 itself not the Arduino MEGA. The Arduino MEGA is just a tool that is utilized to program the ESP8266. Are my thoughts correct?

reset to ground holds the ATmega 2560 in reset

KonstantinBG:
I tried to implement my code for the swing gate opener in the same web server sketch but I found out that the ESP8266 does not support, for example, uint_8t data type. This is a bit dissaipointing since I use a DS3231 Real Time Clock.

uint8_t is standard C/C++ data type and of course the used gcc compiler has it

KonstantinBG:
My next idea to fix the problem is to use na I2C communication between the ESP8266 and the Arduino MEGA where the ESP8266 will be the Master. It receives request from the web server and then the Arduino MEGA, depending on the request that the ESP8266 has received, does the elbow grease. The main code for the swing gate system is on the Arduino MEGA while the ESP8266 only cares about the web server and the requests.

why I2C? use Seral and esp8266 should be slave. you could use some existing firmware in esp8266 and a corresponding WiFi library in Mega

KonstantinBG:
Another solution to the problem is to buy an ESP development board with the capabilities of an Arduino MEGA.
Which is the better solution in your opinion?

use esp8266 as network adapter with firmware and WiFi library in Mega or
the esp8266 dev boards small or Uno sized can be used, but have restrictions like 3.3 V, less pins, some of pins have pull-up or pull-down, some of pins must have required state at boot

most used with esp-01 is AT firmware with WiFiEsp library

Juraj:
uint8_t is standard C/C++ data type and of course the used gcc compiler has it

I think I found out why it gives me the error.

'uint8_t' does not name a type

It is because in the DS3231.h file or in the TouchScreen.h file the ESP8266 is not defined. I tried to add it but unsuccessfully.

Juraj:
why I2C? use Seral and esp8266 should be slave. you could use some existing firmware in esp8266 and a corresponding WiFi library in Mega

It is because the I2C communication was the first thing on my mind. I guess the Serial communication is easier but I do not quite understand it.

I can explain what I am thinking. I will programm the ESP8266 via the Arduino MEGA. Basically in the code I will have the following algorithm:

If the OPEN button(on the web server or on an android app) is pressed, I will send, for example, a 1 on the Serial. The Arduino MEGA will be programmed with the swing gate system code and therefore if there is a 1 on the Serial then do this.

If the CLOSE button(on the web server or on an android app) is pressed, I will send, for example, a 0 on the Serial. Hence, the same thing happens in the Arduino MEGA as before.

Is this the correct way to do it? Accroding to me it is too simple to be the correct way.

Another question, can I send data from the Arduino MEGA to the ESP8266 and print it on the web server, for example, the Time from the Real Time Clock?

EDIT:

I wrote the following sketches:

The ESP8266-01 sketch.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>



ESP8266WebServer Webserver(81);

// Replace with your network credentials
const char* ssid = "";
const char* password = "";

String HTMLpage = "";

void setup(void)
{
  HTMLpage += "<head><title>Name</title></head><h3>Welcome to Name!</h3><p>DOOR <a href=\"DOOROPENING\"><button>OPEN</button></a>&nbsp;<a href=\"DOORCLOSING\"><button>CLOSE</button></a></p>";

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266", WiFi.localIP()))
  {
    Serial.println("MDNS responder started");
  }

  Webserver.on("/", [](){
    Webserver.send(200, "text/html", HTMLpage);
  });
  
  Webserver.on("/DOOROPENING", [](){
    Webserver.send(200, "text/html", HTMLpage+"<p>DOOR OPENING</p>");
    Serial.write("1");
  });
  
  Webserver.on("/DOORCLOSING", [](){
    Webserver.send(200, "text/html", HTMLpage+"<p>DOOR CLOSING</p>");
    Serial.write("0");
  });

  Webserver.begin();
  Serial.println("HTTP Webserver started");
}

void loop(void)
{
  Webserver.handleClient();
}

The Arduino MEGA sketch.

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if(Serial.available())
  {
    int x = Serial.read();
    if(x == 1)
    {
      Serial.print("OPEN");
      Serial.println("/n");
    }
    
    if(x == 0)
    {
      Serial.print("CLOSE");
      Serial.println("/n");
    }
  }
}

In the web server interface I think everything works. If I press the OPEN button it shows the message DOOR OPENING below the buttons as it is supposed to. The same happens if I press the CLOSE button.

But on the Serial Monitor it does not print anything.

why do you attached the esp8266 to Serial of Mega?. put it on Serial1 2 or 3. Serial is attached to USB

Juraj:
why do you attached the esp8266 to Serial of Mega?. put it on Serial1 2 or 3. Serial is attached to USB

I was trying to use Serial Port1 but in my opinion something happened.

I could not detect the Arduino with the USB cable I used before. Now I have another cable and everything is fine except that even if I reset the CH_PD pin I cannot connect to the ESP8266.

EDIT:

I figured something out.

Generally, when I reset the CH_PD pin the RX or TX LED blinks.

If I connect the TX and RX to TX1 and RX1(also on TX,RX2 or TX,RX3) on the Arduino MEGA I cannot upload the sketch to the ESP8266. I also add Serial1.begin(115200) in the ESP8266 sketch. I am not sure if I need to have it but if I write Serial3.begin(115200) the Wi-Fi Module does not recognize it. The RX or TX LED does not blink.

If I connect the TX and RX to TX0 and RX0 on the Arduino MEGA I can upload the sketch to the ESP8266. I can also see the number which I send to the Serial depending on the button that was pressed. Then I tried to just move the pins to the TX1 and RX1 on the Arduino MEGA but I cannot see what is supposed to be printed(OPEN or CLOSE).

Could it be that I somehow burned the other TX and RX pins except the ones on the 0 channel?

When I tried before(when my first cable was working), I am not completely sure, I cannot remember exactly, I could have uploaded the sketch with TX and RX connected to TX1 and RX1 on the Arduino MEGA.

use the SerialPassthrough example sketch to let data go over the ATmega.
set 115200 as baudrate

advanced version of SerialPassthrough is my EspProxy sketch Arduino + ESP8266 with EspProxy (develop without rewiring) - Project Guidance - Arduino Forum

Juraj:
use the SerialPassthrough example sketch to let data go over the ATmega.
set 115200 as baudrate

advanced version of SerialPassthrough is my EspProxy sketch https://forum.arduino.cc/index.php?topic=519181.0

I managed to receive the correct data but there is another problem.

I defined that if I receive “1” on the Serial1 it prints “OPEN” and if I receive “2” it prints “CLOSE”. For some reason it prints always "OPEN"no matter what number I have received on the Serial1.

In the Serial Monitor Tab I have chosen " Both “NL” and “CR” "

I attached a picture of what I receive on the Serial Monitor Tab.

By the way I connected the ESP8266 ti TX0 and RX0 and I read that it may cause demage to the MEGA or the Wi-Fi Module, could that be the reason for the wrong readings?

I also tried to connect TX of the ESP8266 to RX of the MEGA and RX of the ESP8266 to the TX of the MEGA. I do not know if that is correct.

This is the code for the Arduino MEGA:

int received;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop()
{
  if(Serial1.available())
  {
    received = Serial.write(Serial1.read());

    if(received == 1)
    {
      Serial.println("OPEN");
    }

    if(received == 2)
    {
      Serial.println("CLOSE");
    }
  }
}

ESP8266 code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>



ESP8266WebServer Webserver(81);

// Replace with your network credentials
const char* ssid = "";
const char* password = "";

String HTMLpage = "";

void setup(void)
{
  HTMLpage += "<head><title>Gate Opener</title></head><h3>Welcome!</h3><p>DOOR <a href=\"DOOROPENING\"><button>OPEN</button></a>&nbsp;<a href=\"DOORCLOSING\"><button>CLOSE</button></a></p>";

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266", WiFi.localIP()))
  {
    Serial.println("MDNS responder started");
  }

  Webserver.on("/", [](){
    Webserver.send(200, "text/html", HTMLpage);
  });
  
  Webserver.on("/DOOROPENING", [](){
    Webserver.send(200, "text/html", HTMLpage+"<p>DOOR OPENING</p>");
    Serial.write("1");
  });
  
  Webserver.on("/DOORCLOSING", [](){
    Webserver.send(200, "text/html", HTMLpage+"<p>DOOR CLOSING</p>");
    Serial.write("2");
  });

  Webserver.begin();
  Serial.println("HTTP Webserver started");
}

void loop(void)
{
  Webserver.handleClient();
}

Serial Monitor Tab.jpg