I'm using ESP8266 as a web server with Arduino IDE. ESP8266 provides an HTML web page and change GPIO output when receiving POST request. The code below works fine.
p.s. index.h is just a file containing PAGE_INDEX
which is the string of the HTML page.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "index.h"
#define OUT_PIN1 2
#define OUT_PIN2 0
const char ssid[] = "********";
const char pass[] = "********";
ESP8266WebServer server(80); // Run a web server on port 80
// handaling the request of main page
void rootRouter() {
server.send (200, "text/html", PAGE_INDEX );
}
void setup() {
pinMode(OUT_PIN1, OUTPUT);
pinMode(OUT_PIN2, OUTPUT);
Serial.begin(115200);
// Connecting to wifi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP: ");
Serial.println(WiFi.localIP()); // print the ip
// the main page
server.on ( "/", rootRouter);
// page for switch 1
server.on ("/sw1", []() {
String state = server.arg("led");
if (state == "ON") {
digitalWrite(OUT_PIN1, HIGH);
} else if (state == "OFF") {
digitalWrite(OUT_PIN1, LOW);
}
Serial.print("OUT_PIN1: ");
Serial.println(state);
});
// page for switch 2
server.on ("/sw2", []() {
String state = server.arg("led");
if (state == "ON") {
digitalWrite(OUT_PIN2, HIGH);
} else if (state == "OFF") {
digitalWrite(OUT_PIN2, LOW);
}
Serial.print("OUT_PIN2: ");
Serial.println(state);
});
// For page not found
server.onNotFound([](){
server.send(404, "text/plain", "404 NOT found!");
});
server.begin();
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient(); // handaling requests
}
However, when I modify some part of the code. I got the exception (3). What I modified in the code below:
/sw
to change both GPIO's output.Serial.print()
The code I modified:
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include "index.h"
#define OUT_PIN1 2
#define OUT_PIN2 0
const char ssid[] = "********";
const char pass[] = "********";
ESP8266WebServer server(80); // Run a web server on port 80
// handaling the request of main page
void rootRouter() { server.send(200, "text/html", PAGE_INDEX); }
void setup() {
pinMode(OUT_PIN1, OUTPUT);
pinMode(OUT_PIN2, OUTPUT);
Serial.begin(115200);
// Connecting to wifi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP: ");
Serial.println(WiFi.localIP()); // print the ip
// the main page
server.on("/", rootRouter);
// This is the page I add
server.on("/sw", []() {
String state = server.arg("led");
if (state == "ON") {
digitalWrite(OUT_PIN1, HIGH);
digitalWrite(OUT_PIN2, HIGH);
} else if (state == "OFF") {
digitalWrite(OUT_PIN1, LOW);
digitalWrite(OUT_PIN2, LOW);
}
server.send(204);
});
// page for switch 1
server.on("/sw1", []() {
String state = server.arg("led");
if (state == "ON") {
digitalWrite(OUT_PIN1, HIGH);
} else if (state == "OFF") {
digitalWrite(OUT_PIN1, LOW);
}
server.send(204);
});
// page for switch 2
server.on("/sw2", []() {
String state = server.arg("led");
if (state == "ON") {
digitalWrite(OUT_PIN2, HIGH);
} else if (state == "OFF") {
digitalWrite(OUT_PIN2, LOW);
}
server.send(204);
});
// For page not found
server.onNotFound([]() { server.send(404, "text/plain", "404 NOT found!"); });
server.begin();
Serial.println("HTTP server started.");
}
void loop() {
server.handleClient(); // handaling requests
}
The error:
Exception (3):
epc1=0x40100794 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4006ead9 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffb50 end: 3fffffc0 offset: 01a0
3ffffcf0: 00000000 feefeffe feefeffe feefeffe
3ffffd00: 401027d4 00080000 feefeffe 3ffffeb0
3ffffd10: 0000049c 0000049c 00000020 40100984
3ffffd20: feefeffe 2c9f0300 4000050c 3fffc278
3ffffd30: 00000000 400042db 000000fe 40100bdc
3ffffd40: 40004b31 00001000 000000fe 401002f8
3ffffd50: 40105c74 00000001 3ffef850 40232b29
3ffffd60: 40105e31 40232c13 3ffef854 0000049c
3ffffd70: 000000fd 3ffffeb0 3ffef854 40232bf6
3ffffd80: ffffff01 55aa55aa 00000011 00000020
3ffffd90: 00000020 0000006d 0000006d aa55aa55
3ffffda0: 000000ff 402330f6 3ffef854 3ffef854
3ffffdb0: 000000ff 000001ae 000001ae 401006c4
3ffffdc0: 40105e31 00000001 3ffef864 40233316
3ffffdd0: 00000005 3ffef854 000000ff 3ffffeb0
3ffffde0: 3ffffed0 3ffef88b 00000011 00000020
3ffffdf0: 3ffef914 3fffff11 00000001 402333c6
3ffffe00: 3ffffeb0 4023f750 00000000 00000008
3ffffe10: 3ffefc54 3ffffed0 3fff5b8c 40233395
3ffffe20: 3ffef854 402333fc 3ffe84cc 3ffe868e
3ffffe30: 40204302 3ffe868e 3ffe8685 40204257
3ffffe40: 69676e65 645f656e 5f6d726f 47342e32
3ffffe50: 40007a48 feefeffe feefeffe feefeffe
3ffffe60: 30323230 24234021 3ffef700 40207a07
3ffffe70: 0000001c 0001c200 00000000 00000000
3ffffe80: 00000003 40207bc9 ffffffff 00000001
3ffffe90: feefeffe 00000001 3ffee4a4 3ffee520
3ffffea0: 1b327800 fea4bec5 feefeffe 00000100
3ffffeb0: 69676e65 645f656e 5f6d726f 47342e32
3ffffec0: 40007a48 feefeffe feefeffe feefeffe
3ffffed0: 30323230 24234021 3ffef700 40207afb
3ffffee0: 0000001c 0001c200 00000000 00000000
3ffffef0: 00000003 40207cbd ffffffff 00000001
3fffff00: feefeffe 00000001 3ffee494 3ffee510
3fffff10: 00000000 00000001 3ffee481 00000002
3fffff20: 00000004 00000000 3ffee45c 00000001
3fffff30: 0001c200 0000001c 00000000 3ffee510
3fffff40: 3ffee494 00000000 3ffee45c 40201b1c
3fffff50: feefeffe feefeffe feefeffe feefeffe
3fffff60: feefeffe feefeffe feefeffe feefeffe
3fffff70: feefeffe feefeffe feefeffe feefeffe
3fffff80: feefeffe feefeffe feefeffe feefeffe
3fffff90: feefeffe feefeffe feefeffe 3ffee510
3fffffa0: 3fffdad0 00000000 3ffee4d0 40206a00
3fffffb0: feefeffe feefeffe 3ffe84e8 40100c11
<<<stack<<<
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
I also tried to add only one feature I just mention once a time, but always get the exception (3) error.
A general walk through for debugging:
Replace your String class with fixed char arrays.
Send a simple 200 response back to the browser - this works best for testing
server.send(200,"text/plain","");
add Serial prints for debugging and
if you want only a POST to be processed ensure it with
server.on("/sw", HTTP_POST, []()
Serial.println("Debug: /sw started");
... do something ...
Serial.println("Debug: /sw finished");
)
analyze what is send and what - if at all- is received in the browser via web developer console.
For further assistance please list your environment ArduinoIDE 1.8.12 and the version of esp8266 core (latest is as of 20.3.2020 version 2.6.2), and the request headers sent and received.
One more tip: Exception 3 is an out of bound write, there is up to now (see issue #1997 - closed but not completely solved - in esp8266 repo) So mentioning you store your index page in flash try a complete erease:
In Arduino IDE:
Change tools/Erase flash to "All flash content" once
flash a sketch.
And then set it back to "only sketch"
Read the issue for more causes and possible solutions.
To down voters - leave - at least - a comment what you would do instead.