# ESP8266_AT_WebServer **Repository Path**: yy800119/ESP8266_AT_WebServer ## Basic Information - **Project Name**: ESP8266_AT_WebServer - **Description**: This is simple yet complete WebServer library for AVR, Teensy, SAM DUE, SAMD, STM32, etc. boards running ESP8266 AT-command shields. The functions are similar and compatible to ESP8266/ESP32 WebServer libraries to make life much easier to port sketches from ESP8266/ESP32. - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-12 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## ESP8266_AT_WebServer [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP8266_AT_WebServer.svg?)](https://www.ardu-badge.com/ESP8266_AT_WebServer) [![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP8266_AT_WebServer.svg)](#releases) [![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ESP8266_AT_WebServer/blob/master/LICENSE) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP8266_AT_WebServer.svg)](http://github.com/khoih-prog/ESP8266_AT_WebServer/issues) ### New Version v1.0.5 1. Add support to ***SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.) and SAM DUE***. ### New Version v1.0.4 1. Sync with ESP8266WebServer library of ESP8266 core v2.6.3 2. Fix bug. ### New Version v1.0.3 1. Add support to STM32 (STM32F0, F1, F2, F3, F4, F7, etc) boards ### New Version v1.0.2 1. Add support to SAMD (ZERO, MKR, ***NANO_33_IOT***, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.) boards This is simple yet complete WebServer library for `AVR, Teensy, SAM DUE, SAMD, STM32, etc.` boards running `ESP8266 AT-command` shields. The functions are similar and compatible to ESP8266/ESP32 WebServer libraries to make life much easier to port sketches from ESP8266/ESP32. The library supports 1. WiFi Client, STA and AP mode 2. TCP Server and Client 3. UDP Server and Client 4. HTTP Server and Client 5. HTTP GET and POST requests, provides argument parsing, handles one client at a time. Library is based on and modified from: 1. [Ivan Grokhotkov's ESP8266WebServer](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer) 2. [bportaluri's WiFiEsp library](https://github.com/bportaluri/WiFiEsp) The ESP8266_AT_Web_Server class found in `ESP8266_AT_Web_Server.h` header, is a simple web server that knows how to handle HTTP requests such as GET and POST and can only support one simultaneous client. ## Prerequisite 1. [`Arduino IDE 1.8.12 or later` for Arduino](https://www.arduino.cc/en/Main/Software) 2. [`Arduino Core for STM32 v1.8.0 or later`](https://github.com/khoih-prog/Arduino_Core_STM32) for STM32 boards 3. [`Teensy core 1.51 or later`](https://www.pjrc.com/teensy/td_download.html) for Teensy (4.0, 3.6, 3.5, 3,2, 3.1, 3.0, LC) boards 4. [`Arduino SAM DUE core 1.6.12 or later`](https://www.arduino.cc/en/Guide/ArduinoDue) for SAM DUE ARM Cortex-M3 boards 5. [`Arduino SAMD core 1.8.5 or later`](https://www.arduino.cc/en/Guide/ArduinoM0) for SAMD ARM Cortex-M0+ boards 6. [`Adafruit SAMD core 1.5.11 or later`](https://www.adafruit.com/) for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.) 7. [`Functional-VLPP library`](https://github.com/khoih-prog/functional-vlpp) to use server's lambda function. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/Functional-Vlpp.svg?)](https://www.ardu-badge.com/Functional-Vlpp) ## Installation ### Use Arduino Library Manager The best and easiest way is to use `Arduino Library Manager`. Search for `ESP8266_AT_Web_Server`, then select / install the latest version. You can also use this link [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP8266_AT_WebServer.svg?)](https://www.ardu-badge.com/ESP8266_AT_WebServer) for more detailed instructions. ### Manual Install 1. Navigate to [ESP8266_AT_WebServer](https://github.com/khoih-prog/ESP8266_AT_WebServer) page. 2. Download the latest release `ESP8266_AT_WebServer-master.zip`. 3. Extract the zip file to `ESP8266_AT_WebServer-master` directory 4. Copy whole - `ESP8266_AT_WebServer-master` folder to Arduino libraries' directory such as `~/Arduino/libraries/`. #### Usage #### Class Constructor ```cpp ESP8266_AT_WebServer server(80); ``` Creates the ESP8266_AT_WebServer class object. *Parameters:* host port number: ``int port`` (default is the standard HTTP port 80) #### Basic Operations ***Starting the server*** ```cpp void begin(); ``` ***Handling incoming client requests*** ```cpp void handleClient(); ``` ***Disabling the server*** ```cpp void close(); void stop(); ``` Both methods function the same ***Client request handlers*** ```cpp void on(); void addHandler(); void onNotFound(); void onFileUpload(); ``` Example:* ```cpp server.on("/", handlerFunction); server.onNotFound(handlerFunction); // called when handler is not assigned server.onFileUpload(handlerFunction); // handle file uploads ``` ***Sending responses to the client*** ```cpp void send(); void send_P(); ``` `Parameters:` `code` - HTTP response code, can be `200` or `404`, etc. `content_type` - HTTP content type, like `"text/plain"` or `"image/png"`, etc. `content` - actual content body #### Advanced Options ***Getting information about request arguments*** ```cpp const String & arg(); const String & argName(); int args(); bool hasArg(); ``` `Function usage:` `arg` - get request argument value, use `arg("plain")` to get POST body `argName` - get request argument name `args` - get arguments count `hasArg` - check if argument exist ***Getting information about request headers*** ```cpp const String & header(); const String & headerName(); const String & hostHeader(); int headers(); bool hasHeader(); ``` `Function usage:` `header` - get request header value `headerName` - get request header name `hostHeader` - get request host header if available, else empty string `headers` - get header count `hasHeader` - check if header exist ***Authentication*** ```cpp bool authenticate(); void requestAuthentication(); ``` `Function usage:` `authenticate` - server authentication, returns true if client is authenticated else false `requestAuthentication` - sends authentication failure response to the client `Example Usage:` ```cpp if(!server.authenticate(username, password)){ server.requestAuthentication(); } ``` #### Other Function Calls ```cpp const String & uri(); // get the current uri HTTPMethod method(); // get the current method WiFiClient client(); // get the current client HTTPUpload & upload(); // get the current upload void setContentLength(); // set content length void sendHeader(); // send HTTP header void sendContent(); // send content void sendContent_P(); void collectHeaders(); // set the request headers to collect void serveStatic(); size_t streamFile(); ``` Also see examples: 1. [HelloServer](examples/HelloServer) 2. [HelloServer2](examples/HelloServer2) 3. [AdvancedWebServer](examples/AdvancedWebServer) 4. [HttpBasicAuth](examples/HttpBasicAuth) 5. [PostServer](examples/PostServer) 6. [SimpleAuthentication](examples/SimpleAuthentication) 7. [ConnectWPA](examples/ConnectWPA) 8. [ScanNetworks](examples/ScanNetworks) 9. [UdpNTPClient](examples/UdpNTPClient) 10. [UdpSendReceive](examples/UdpSendReceive) 11. [WebClient](examples/WebClient) 12. [WebClientRepeating](examples/WebClientRepeating) 13. [WebServer](examples/WebServer) 14. [WebServerAP](examples/WebServerAP) ## Example [HelloServer](examples/HelloServer) Please take a look at other examples, as well. ```cpp #define DEBUG_ESP8266_AT_WEBSERVER_PORT Serial #if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \ || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) \ || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) \ || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(__SAMD21E18A__) || defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) \ || defined(__SAMD51G19A__) || defined(__SAMD21G18A__) ) #if defined(ESP8266_AT_USE_SAMD) #undef ESP8266_AT_USE_SAMD #endif #define ESP8266_AT_USE_SAMD true #endif #if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) ) #if defined(ESP8266_AT_USE_SAM_DUE) #undef ESP8266_AT_USE_SAM_DUE #endif #define ESP8266_AT_USE_SAM_DUE true #endif #if ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) ) #if defined(ESP8266_AT_USE_STM32) #undef ESP8266_AT_USE_STM32 #endif #define ESP8266_AT_USE_STM32 true #endif #ifdef CORE_TEENSY // For Teensy 4.0 #define EspSerial Serial2 //Serial2, Pin RX2 : 7, TX2 : 8 #if defined(__IMXRT1062__) #define BOARD_TYPE "TEENSY 4.0" #elif ( defined(__MKL26Z64__) || defined(ARDUINO_ARCH_AVR) ) #define BOARD_TYPE "TEENSY LC or 2.0" #else #define BOARD_TYPE "TEENSY 3.X" #endif #elif defined(ESP8266_AT_USE_SAMD) // For SAMD #define EspSerial Serial1 #if defined(ARDUINO_SAMD_ZERO) #define BOARD_TYPE "SAMD Zero" #elif defined(ARDUINO_SAMD_MKR1000) #define BOARD_TYPE "SAMD MKR1000" #elif defined(ARDUINO_SAMD_MKRWIFI1010) #define BOARD_TYPE "SAMD MKRWIFI1010" #elif defined(ARDUINO_SAMD_NANO_33_IOT) #define BOARD_TYPE "SAMD NANO_33_IOT" #elif defined(ARDUINO_SAMD_MKRFox1200) #define BOARD_TYPE "SAMD MKRFox1200" #elif ( defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) ) #define BOARD_TYPE "SAMD MKRWAN13X0" #elif defined(ARDUINO_SAMD_MKRGSM1400) #define BOARD_TYPE "SAMD MKRGSM1400" #elif defined(ARDUINO_SAMD_MKRNB1500) #define BOARD_TYPE "SAMD MKRNB1500" #elif defined(ARDUINO_SAMD_MKRVIDOR4000) #define BOARD_TYPE "SAMD MKRVIDOR4000" #elif defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) #define BOARD_TYPE "SAMD ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS" #elif defined(ADAFRUIT_ITSYBITSY_M4_EXPRESS) #define BOARD_TYPE "SAMD ADAFRUIT_ITSYBITSY_M4_EXPRESS" #elif defined(__SAMD21E18A__) #define BOARD_TYPE "SAMD21E18A" #elif defined(__SAMD21G18A__) #define BOARD_TYPE "SAMD21G18A" #elif defined(__SAMD51G19A__) #define BOARD_TYPE "SAMD51G19A" #elif defined(__SAMD51J19A__) #define BOARD_TYPE "SAMD51J19A" #elif defined(__SAMD51J20A__) #define BOARD_TYPE "SAMD51J20A" #elif defined(__SAM3X8E__) #define BOARD_TYPE "SAM3X8E" #elif defined(__CPU_ARC__) #define BOARD_TYPE "CPU_ARC" #elif defined(__SAMD51__) #define BOARD_TYPE "SAMD51" #else #define BOARD_TYPE "SAMD Unknown" #endif #elif defined(ESP8266_AT_USE_SAM_DUE) // For SAM DUE #define EspSerial Serial1 #define BOARD_TYPE "SAM DUE" #elif defined(ESP8266_AT_USE_STM32) // For STM32 #define EspSerial Serial1 #if defined(STM32F0) #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) #define BOARD_TYPE "STM32F7" #else #warning STM32 unknown board selected #define BOARD_TYPE "STM32 Unknown" #endif #else // For Mega #define EspSerial Serial3 #define BOARD_TYPE "AVR Mega" #endif #include char ssid[] = "****"; // your network SSID (name) char pass[] = "****"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status int reqCount = 0; // number of requests received ESP8266_AT_WebServer server(80); const int led = 13; void handleRoot() { server.send(200, "text/plain", "Hello from ESP8266_AT_WebServer!"); } void handleNotFound() { 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) { // Open serial communications and wait for port to open: Serial.begin(115200); // initialize serial for ESP module EspSerial.begin(115200); // initialize ESP module WiFi.init(&EspSerial); Serial.println("\nStarting HelloServer on " + String(BOARD_TYPE)); // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { Serial.println(F("WiFi shield not present")); // don't continue while (true); } // attempt to connect to WiFi network while ( status != WL_CONNECTED) { Serial.print(F("Connecting to WPA SSID: ")); Serial.println(ssid); // Connect to WPA/WPA2 network status = WiFi.begin(ssid, pass); } server.begin(); Serial.print("WebServer is @ "); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.on("/inline", []() { server.send(200, "text/plain", "This works as well"); }); server.onNotFound(handleNotFound); server.begin(); Serial.print(F("HTTP server started @ ")); Serial.println(WiFi.localIP()); } void loop(void) { server.handleClient(); } ``` The following are debug terminal output and screen shot when running example [AdvancedWebServer](examples/AdvancedWebServer) on Teensy 4.0

``` Starting AdvancedServer on TEENSY 4.0 Connecting to WPA SSID: **** HTTP server started @ 192.168.2.107 [ESP_AT] send1: len = 289 [ESP_AT] content = ESP8266 Demo

Hello from ESP8266!

Uptime: 00:00:27

[ESP_AT] send1: len = 1946 [ESP_AT] content = ``` ### New Version v1.0.5 1. Add support to ***SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.)and SAM DUE***. ### New Version v1.0.4 1. Sync with ESP8266WebServer library of ESP8266 core v2.6.3 2. Fix bug. ### New Version v1.0.3 1. Add support to STM32 (STM32F0, F1, F2, F3, F4, F7, etc) boards ### Version v1.0.2 1. Add support to SAMD (ZERO, MKR, ***NANO_33_IOT***, M0, Mo Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.) boards ### Version v1.0.1 1. Add support to server's lambda function calls with dependency on [`Functional-VLPP library`](https://github.com/khoih-prog/functional-vlpp) ### Initial Version v1.0.0 This is simple yet complete WebServer library for `AVR, Teensy, etc.` boards running `ESP8266 AT-command` shields. ***The functions are similar and compatible to ESP8266/ESP32 WebServer libraries*** to make life much easier to port sketches from ESP8266/ESP32. The library supports 1. WiFi Client, STA and AP mode 2. TCP Server and Client 3. UDP Server and Client 4. HTTP Server and Client 5. HTTP GET and POST requests, provides argument parsing, handles one client at a time. ## TO DO 1. Bug Searching and Killing 2. Add SSL/TLS Client and Server support 3. Support more types of boards using ESP8266 AT-command shields. ### Contributions and thanks 1. Based on and modified [Ivan Grokhotkov's ESP8266WebServer](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer) 2. Based on and modified from [bportaluri's WiFiEsp library](https://github.com/bportaluri/WiFiEsp) ## Contributing If you want to contribute to this project: - Report bugs and errors - Ask for enhancements - Create issues and pull requests - Tell other people about this library ## Copyright Copyright 2020- Khoi Hoang