I try to upload a script which starts a webserver on the ESP8266 with a simple web GUI where the user can enter his WLAN credentials.
/* ============================================================================================================
Sample code to create a setup web portal using a soft access point on an ESP8266.
For more info, please watch my video at https://youtu.be/1VW-z4ibMXI
MrDIY.ca
============================================================================================================== */
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
ESP8266WebServer server(80);
struct settings {
char ssid[30];
char password[30];
} user_wifi = {};
void setup() {
EEPROM.begin(sizeof(struct settings) );
EEPROM.get( 0, user_wifi );
WiFi.mode(WIFI_STA);
WiFi.begin(user_wifi.ssid, user_wifi.password);
byte tries = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
if (tries++ > 30) {
WiFi.mode(WIFI_AP);
WiFi.softAP("Setup Portal", "mrdiy.ca");
break;
}
}
server.on("/", handlePortal);
server.begin();
}
void loop() {
server.handleClient();
}
void handlePortal() {
if (server.method() == HTTP_POST) {
strncpy(user_wifi.ssid, server.arg("ssid").c_str(), sizeof(user_wifi.ssid) );
strncpy(user_wifi.password, server.arg("password").c_str(), sizeof(user_wifi.password) );
user_wifi.ssid[server.arg("ssid").length()] = user_wifi.password[server.arg("password").length()] = '\0';
EEPROM.put(0, user_wifi);
EEPROM.commit();
server.send(200, "text/html", "<!doctype html><html lang='en'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'><title>Wifi Setup</title><style>*,::after,::before{box-sizing:border-box;}body{margin:0;font-family:'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans','Liberation Sans';font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#f5f5f5;}.form-control{display:block;width:100%;height:calc(1.5em + .75rem + 2px);border:1px solid #ced4da;}button{border:1px solid transparent;color:#fff;background-color:#007bff;border-color:#007bff;padding:.5rem 1rem;font-size:1.25rem;line-height:1.5;border-radius:.3rem;width:100%}.form-signin{width:100%;max-width:400px;padding:15px;margin:auto;}h1,p{text-align: center}</style> </head> <body><main class='form-signin'> <h1>Wifi Setup</h1> <br/> <p>Your settings have been saved successfully!<br />Please restart the device.</p></main></body></html>" );
} else {
server.send(200, "text/html", "<!doctype html><html lang='en'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'><title>Wifi Setup</title> <style>*,::after,::before{box-sizing:border-box;}body{margin:0;font-family:'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans','Liberation Sans';font-size:1rem;font-weight:400;line-height:1.5;color:#212529;background-color:#f5f5f5;}.form-control{display:block;width:100%;height:calc(1.5em + .75rem + 2px);border:1px solid #ced4da;}button{cursor: pointer;border:1px solid transparent;color:#fff;background-color:#007bff;border-color:#007bff;padding:.5rem 1rem;font-size:1.25rem;line-height:1.5;border-radius:.3rem;width:100%}.form-signin{width:100%;max-width:400px;padding:15px;margin:auto;}h1{text-align: center}</style> </head> <body><main class='form-signin'> <form action='/' method='post'> <h1 class=''>Wifi Setup</h1><br/><div class='form-floating'><label>SSID</label><input type='text' class='form-control' name='ssid'> </div><div class='form-floating'><br/><label>Password</label><input type='password' class='form-control' name='password'></div><br/><br/><button type='submit'>Save</button><p style='text-align: right'><a href='https://www.mrdiy.ca' style='color: #32C5FF'>mrdiy.ca</a></p></form></main> </body></html>" );
}
}
I try to figure out how I need to connect the ESP8266 WLAN module with my Arduino Uno in order to be able to upload that script. I've searched the documentation, but I can't find much helpful.
The module has these pins:
3V3 | RX
RST | IO0
EN | IO2
TX | GND
But in the documentation there are other pins which I don't have and the pin EN is not even mentioned in the documentation.
I connected it like this (i did not used pull up resistors yet):
[ESP8266 -> Arduino Uno]
3V3 -> 3V3 (bread board)
EN -> 3V3 (bread board)
RX -> RX
TX -> TX
GND -> GND
The connection works and it responds to AT commands.
But if I try to upload a script, then it fails with:
. Variables and constants in RAM (global, static), used 28236 / 80192 bytes (35%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1504 initialized variables
╠══ RODATA 1020 constants
╚══ BSS 25712 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 59535 / 65536 bytes (90%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 26767 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 239164 / 1048576 bytes (22%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 239164 code in flash
esptool.py v3.0
Serial port COM4
Connecting........_____....._____....._____....._____....._____....._____....._____
A fatal esptool.py error occurred: Failed to connect to ESP8266: Invalid head of packet (0xF0)
How do I need to connect my module in order to upload code?
I am using this chip. I also tested another chip (i got 4) to check if mine is broken, but I get the same error.