ESP32-Ethernet-Kit_A_V1.1 WEB server
Posted: Wed Jan 20, 2021 2:14 am
Environment:
Experimental steps:
The ESP32-Ethernet-Kit_A_V1.1 evaluation board has the ESP32-WROVER-B module onboard, so we chose ESP32-WROVER to create a project based on the PlatformIO with Arduino framework
Here we use RT-Thread Studio as the IDE, this IDE can easily create PlatformIO projects
[/code]
In this code, a web server is started and monitored on port 80, and the following routing processing is performed on the received http request:
The request for "ip:80/" is received, processed by the handleRoot function, and the http status code 200 is returned, and the peaceful text string "hello from esp8266!"
Receive a request for "ip:80/inline", return a 200 status code and the text "this works as well"
Other resource requests are received, processed by handleNotFound, and 404 is returned
- Windows 10
- RT-Thread Studio 2.0.0(https://www.rt-thread.io/studio.html)
- ESP32-Ethernet-Kit_A_V1.1
Experimental steps:
The ESP32-Ethernet-Kit_A_V1.1 evaluation board has the ESP32-WROVER-B module onboard, so we chose ESP32-WROVER to create a project based on the PlatformIO with Arduino framework
Here we use RT-Thread Studio as the IDE, this IDE can easily create PlatformIO projects
- Create project
- Refer to the official ESP example, copy the following code to the main.cpp file
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <ESPmDNS.h>
- const char* ssid = "your_wifi_ssid";
- const char* password = "your_wifi_passwd";
- WebServer server(80);
- const int led = 13;
- void handleRoot()
- {
- digitalWrite(led, 1);
- server.send(200, "text/plain", "hello from esp8266!");
- digitalWrite(led, 0);
- }
- void handleNotFound()
- {
- digitalWrite(led, 1);
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++)
- {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- digitalWrite(led, 0);
- }
- void setup(void)
- {
- pinMode(led, OUTPUT);
- digitalWrite(led, 0);
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- 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("esp32"))
- {
- Serial.println("MDNS responder started");
- }
- server.on("/", handleRoot);
- server.on("/inline", []()
- {
- server.send(200, "text/plain", "this works as well");
- });
- server.onNotFound(handleNotFound);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop(void)
- {
- server.handleClient();
- }
In this code, a web server is started and monitored on port 80, and the following routing processing is performed on the received http request:
The request for "ip:80/" is received, processed by the handleRoot function, and the http status code 200 is returned, and the peaceful text string "hello from esp8266!"
- void handleRoot()
- {
- digitalWrite(led, 1);
- server.send(200, "text/plain", "hello from esp8266!");
- digitalWrite(led, 0);
- }
Receive a request for "ip:80/inline", return a 200 status code and the text "this works as well"
- server.on("/inline", []()
- {
- server.send(200, "text/plain", "this works as well");
- });
Other resource requests are received, processed by handleNotFound, and 404 is returned
- void handleNotFound()
- {
- digitalWrite(led, 1);
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++)
- {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- digitalWrite(led, 0);
- }
- Modify the wifi account and password to the wifi configuration of your environment hotspot, so that the module can connect to the hotspot
- const char* ssid = "your_wifi_ssid";
- const char* password = "your_wifi_passwd";
- Click the small hammer icon in the upper left corner of the IDE to compile the entire project
- Connect the micro-usb cable and click the download button to download the program to the evaluation board
- After the download is complete, connect to the serial terminal, you can see that the module has been successfully started, the information is as follows
- Open chrome and visit http://172.30.200.121/, http://172.30.200.121/inline http://172.30.200.121/xxx to get the response of the ESP web server to different resources