ESP32-Ethernet-Kit_A_V1.1 WEB server

dh_esp32
Posts: 1
Joined: Wed Jan 20, 2021 1:56 am

ESP32-Ethernet-Kit_A_V1.1 WEB server

Postby dh_esp32 » Wed Jan 20, 2021 2:14 am

Environment:
  • Windows 10
  • 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
create_project.png
create_project.png (26.6 KiB) Viewed 1456 times
  • Refer to the official ESP example, copy the following code to the main.cpp file
  1.    #include <WiFi.h>
  2.    #include <WiFiClient.h>
  3.    #include <WebServer.h>
  4.    #include <ESPmDNS.h>
  5.    
  6.    const char* ssid = "your_wifi_ssid";
  7.    const char* password = "your_wifi_passwd";
  8.    
  9.    WebServer server(80);
  10.    
  11.    const int led = 13;
  12.    
  13.    void handleRoot()
  14.    {
  15.        digitalWrite(led, 1);
  16.        server.send(200, "text/plain", "hello from esp8266!");
  17.        digitalWrite(led, 0);
  18.    }
  19.    
  20.    void handleNotFound()
  21.    {
  22.        digitalWrite(led, 1);
  23.        String message = "File Not Found\n\n";
  24.        message += "URI: ";
  25.        message += server.uri();
  26.        message += "\nMethod: ";
  27.        message += (server.method() == HTTP_GET) ? "GET" : "POST";
  28.        message += "\nArguments: ";
  29.        message += server.args();
  30.        message += "\n";
  31.        for (uint8_t i = 0; i < server.args(); i++)
  32.        {
  33.            message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  34.        }
  35.        server.send(404, "text/plain", message);
  36.        digitalWrite(led, 0);
  37.    }
  38.    
  39.    void setup(void)
  40.    {
  41.        pinMode(led, OUTPUT);
  42.        digitalWrite(led, 0);
  43.        Serial.begin(115200);
  44.        WiFi.mode(WIFI_STA);
  45.        WiFi.begin(ssid, password);
  46.        Serial.println("");
  47.    
  48.        // Wait for connection
  49.        while (WiFi.status() != WL_CONNECTED)
  50.        {
  51.            delay(500);
  52.            Serial.print(".");
  53.        }
  54.        Serial.println("");
  55.        Serial.print("Connected to ");
  56.        Serial.println(ssid);
  57.        Serial.print("IP address: ");
  58.        Serial.println(WiFi.localIP());
  59.    
  60.        if (MDNS.begin("esp32"))
  61.        {
  62.            Serial.println("MDNS responder started");
  63.        }
  64.    
  65.        server.on("/", handleRoot);
  66.    
  67.        server.on("/inline", []()
  68.        {
  69.            server.send(200, "text/plain", "this works as well");
  70.        });
  71.    
  72.        server.onNotFound(handleNotFound);
  73.    
  74.        server.begin();
  75.        Serial.println("HTTP server started");
  76.    }
  77.    
  78.    void loop(void)
  79.    {
  80.        server.handleClient();
  81.    }
[/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!"
  1.      void handleRoot()
  2.      {
  3.          digitalWrite(led, 1);
  4.          server.send(200, "text/plain", "hello from esp8266!");
  5.          digitalWrite(led, 0);
  6.      }

Receive a request for "ip:80/inline", return a 200 status code and the text "this works as well"
  1.      server.on("/inline", []()
  2.                {
  3.                    server.send(200, "text/plain", "this works as well");
  4.                });

Other resource requests are received, processed by handleNotFound, and 404 is returned
  1.      void handleNotFound()
  2.      {
  3.          digitalWrite(led, 1);
  4.          String message = "File Not Found\n\n";
  5.          message += "URI: ";
  6.          message += server.uri();
  7.          message += "\nMethod: ";
  8.          message += (server.method() == HTTP_GET) ? "GET" : "POST";
  9.          message += "\nArguments: ";
  10.          message += server.args();
  11.          message += "\n";
  12.          for (uint8_t i = 0; i < server.args(); i++)
  13.          {
  14.              message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  15.          }
  16.          server.send(404, "text/plain", message);
  17.          digitalWrite(led, 0);
  18.      }
  • Modify the wifi account and password to the wifi configuration of your environment hotspot, so that the module can connect to the hotspot
  1.    const char* ssid = "your_wifi_ssid";
  2.    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
esp32_ip.png
esp32_ip.png (28.09 KiB) Viewed 1456 times
http://172.30.200.121/
index.png
index.png (7.08 KiB) Viewed 1456 times

Who is online

Users browsing this forum: Bing [Bot], elDi@mond and 124 guests