- Add support to SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
From v1.0.3+, the library supports more Arduino boards ( SAM DUE, SAMD21: ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.)
From v1.0.2+, the library supports many more Arduino boards (Atmel AVR-s, Atmel SAM3X8E ARM Cortex-M3, STM32F series, ESP8266, Intel ARC32(Genuino101), Nordic nRF51(RFduino), Teensy boards, Realtek Ameba(RTL8195A,RTL8710)) using Wiznet W5x00 or ENC28J60 EThernet shields by using UIPEthernet library besides standard Ethernet library.
This is simple yet complete WebServer library for AVR, Teensy,SAM, STM32F, Intel, etc.
boards running Ethernet 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
- HTTP Server and Client
- HTTP GET and POST requests, provides argument parsing, handles one client at a time.
Library is based on and modified from:
The EthernetWebServer class found in EthernetWebServer.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.
Arduino IDE 1.8.11 or later
for ArduinoArduino AVR core 1.8.2 or later
for Arduino (Use Arduino Board Manager)- Depending on which Ethernet card you're using:
- Ethernet library for W5100, W5200 and W5500
- UIPEthernet for ENC28J60
Functional-VLPP library
to use lambda function
Another way is to use Arduino Library Manager
or . Search for
EthernetWebServer
, then select / install the latest version.
- Navigate to EthernetWebServer page.
- Download the latest release
EthernetWebServer-master.zip
. - Extract the zip file to
EthernetWebServer-master
directory - Copy whole
EthernetWebServer-master
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- If your application requires 2K+ HTML page, the current
Ethernet library
must be modified if you are using W5200/W5500 Ethernet shields. W5100 is not supported for 2K+ buffer. - To fix
Ethernet library
, just copy these following files into theEthernet library
directory to overwrite the old files:
EthernetWebServer server(80);
Creates the EthernetWebServer class object.
Parameters:
host port number: int port
(default is the standard HTTP port 80)
Starting the server
void begin();
Handling incoming client requests
void handleClient();
Disabling the server
void close();
void stop();
Both methods function the same
Client request handlers
void on();
void addHandler();
void onNotFound();
void onFileUpload();
Example:*
server.on("/", handlerFunction);
server.onNotFound(handlerFunction); // called when handler is not assigned
server.onFileUpload(handlerFunction); // handle file uploads
Sending responses to the client
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
Getting information about request arguments
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
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
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:
if(!server.authenticate(username, password)){
server.requestAuthentication();
}
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:
Example HelloServer
Please take a look at other examples as well.
#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__) || defined(__SAM3X8E__) || defined(__CPU_ARC__) )
#if defined(ETHERNET_USE_SAMD)
#undef ETHERNET_USE_SAMD
#endif
#define ETHERNET_USE_SAMD true
#endif
#if defined(ETHERNET_USE_SAMD)
// For SAMD
#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(CORE_TEENSY) )
// For Teensy 4.0
#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
#else
// For Mega
#define BOARD_TYPE "AVR Mega"
#endif
#include <SPI.h>
// Use true for ENC28J60 and UIPEthernet library (https://github.com/UIPEthernet/UIPEthernet)
// Use false for W5x00 and Ethernetx library (https://www.arduino.cc/en/Reference/Ethernet)
//#define USE_UIP_ETHERNET true
#include <EthernetWebServer.h>
// Enter a MAC address and IP address for your controller below.
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Select the IP address according to your local network
IPAddress ip(192, 168, 2, 200);
EthernetWebServer server(80);
const int led = 13;
void handleRoot()
{
server.send(200, "text/plain", "Hello from EthernetWebServer");
}
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);
while (!Serial);
//delay(1000);
Serial.println("\nStarting HelloServer on " + String(BOARD_TYPE));
// start the ethernet connection and the server:
// Use Static IP
//Ethernet.begin(mac, ip);
// Use DHCP dynamic IP
Ethernet.begin(mac);
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "This works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(Ethernet.localIP());
}
void loop(void)
{
server.handleClient();
}
The following are debug terminal output and screen shot when running example AdvancedWebServer on Teensy 4.0
Starting AdvancedServer on TEENSY 4.0
HTTP EthernetWebServer is @ IP : 192.168.2.100
[ETHERNET_WEBSERVER] send1: len = 289
[ETHERNET_WEBSERVER] content = <html><head><meta http-equiv='refresh' content='5'/><title>ESP8266 Demo</title><style>body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style></head><body><h1>Hello from ESP8266!</h1><p>Uptime: 00:00:27</p><img src="/test.svg" /></body></html>
[ETHERNET_WEBSERVER] send1: len = 1946
[ETHERNET_WEBSERVER] content = <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="310" height="150">
<rect width="310" height="150" fill="rgb(250, 230, 210)" stroke-width="1" stroke="rgb(0, 0, 0)" />
<g stroke="black">
<line x1="10" y1="77" x2="20" y2="67" stroke-width="1" />
<line x1="20" y1="67" x2="30" y2="98" stroke-width="1" />
<line x1="30" y1="98" x2="40" y2="111" stroke-width="1" />
<line x1="40" y1="111" x2="50" y2="90" stroke-width="1" />
<line x1="50" y1="90" x2="60" y2="22" stroke-width="1" />
<line x1="60" y1="22" x2="70" y2="98" stroke-width="1" />
<line x1="70" y1="98" x2="80" y2="64" stroke-width="1" />
<line x1="80" y1="64" x2="90" y2="104" stroke-width="1" />
<line x1="90" y1="104" x2="100" y2="31" stroke-width="1" />
<line x1="100" y1="31" x2="110" y2="59" stroke-width="1" />
<line x1="110" y1="59" x2="120" y2="139" stroke-width="1" />
<line x1="120" y1="139" x2="130" y2="117" stroke-width="1" />
<line x1="130" y1="117" x2="140" y2="75" stroke-width="1" />
<line x1="140" y1="75" x2="150" y2="72" stroke-width="1" />
<line x1="150" y1="72" x2="160" y2="137" stroke-width="1" />
<line x1="160" y1="137" x2="170" y2="20" stroke-width="1" />
<line x1="170" y1="20" x2="180" y2="94" stroke-width="1" />
<line x1="180" y1="94" x2="190" y2="81" stroke-width="1" />
<line x1="190" y1="81" x2="200" y2="38" stroke-width="1" />
<line x1="200" y1="38" x2="210" y2="33" stroke-width="1" />
<line x1="210" y1="33" x2="220" y2="53" stroke-width="1" />
<line x1="220" y1="53" x2="230" y2="88" stroke-width="1" />
<line x1="230" y1="88" x2="240" y2="32" stroke-width="1" />
<line x1="240" y1="32" x2="250" y2="110" stroke-width="1" />
<line x1="250" y1="110" x2="260" y2="87" stroke-width="1" />
<line x1="260" y1="87" x2="270" y2="11" stroke-width="1" />
<line x1="270" y1="11" x2="280" y2="98" stroke-width="1" />
<line x1="280" y1="98" x2="290" y2="76" stroke-width="1" />
<line x1="290" y1="76" x2="300" y2="121" stroke-width="1" />
</g>
</svg>
- Add support to SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
- From v1.0.3+, the library supports many more Arduino boards ( SAM DUE, SAMD: ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.)
- From v1.0.2+, the library supports many more Arduino boards (Atmel AVR-s, Atmel SAM3X8E ARM Cortex-M3, STM32F series, ESP8266, Intel ARC32(Genuino101), Nordic nRF51(RFduino), Teensy boards, Realtek Ameba(RTL8195A,RTL8710))
- Support Wiznet W5x00 or ENC28J60 EThernet shields by using UIPEthernet library besides standard Ethernet, Ethernet2, Ethernet3 libraries.
- Add support to Server's lambda function calls
This is simple yet complete WebServer library for AVR, Teensy, etc. boards running Ethernet 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
- HTTP Server and Client
- HTTP GET and POST requests, provides argument parsing, handles one client at a time.
- Bug Searching and Killing
- Add SSL/TLS Client and Server support
- Support more types of boards using Ethernet shields.
- Forked from Ivan Grokhotkov's ESP8266WebServer
- jandrassy for UIPEthernet library
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 2020- Khoi Hoang