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.
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
Sample Code
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetWebServer.h>
#ifdef 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
// 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, 100);
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);
delay(1000);
Serial.println("\nStarting HelloServer on " + String(BOARD_TYPE));
// start the ethernet connection and the server:
Ethernet.begin(mac, ip);
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();
}
Haven't got a chance to do that as I have only W5x00 Ethernet shields and am using standard Ethernet library.
I just found out about your UIPEthernet library, and already forked and installed it. Will have a look and test very soon.
Just bought an ENC28J60 shield, and have some tests with UIPEthernet on Mega2560.
Very good result, just after some very minor mods in EthernetWebServer library, all the examples are running perfectly with your UIPEthernet library.
I will make more tests on NodeMCU, Teensy 4.0, ESP32. etc. but don't expect any real issue.
I'll make some final mods, then release new version to make EthernetWebServer support both Ethernet and UIPEthernet libraries.
Just bought an ENC28J60 shield, and have some tests with UIPEthernet on Mega2560.
Very good result, just after some very minor mods in EthernetWebServer library, all the examples are running perfectly with your UIPEthernet library.
I will make more tests on NodeMCU, Teensy 4.0, ESP32. etc. but don't expect any real issue.
I'll make some final mods, then release new version to make EthernetWebServer support both Ethernet and UIPEthernet libraries.
Cheers,
you could make one WebServer library for all arduino networking libraries as I recommended in the thread about the esp8266 AT version
khoih-prog:
That's the ideal goal, but it'll take some time to be sure all the libraries follow certain de-facto interface.
Currently, while no standard interface has been agreed upon, I prefer the ESP8266 / ESP32.
That's why I tailored the libs to imitate the Ivan Grokhotkov's ESP8266WebServer.
yes, that was a good decision.
all arduino networking libraries implement the Client base class which derives from Stream class.
the server classes have a common base class too, but that is useless. In my ArduinoOTA library I use template class and ifdefs to work with server classes of different libraries
Do you think about merging UIPEthernet with all the remaining Ethernet libs (Ethernet, Ethernet,2, Ethernet3, EtherCard, Ethersia and UIPEthernet) into one super Ethernet lib? There are so many and confusing.
khoih-prog:
Do you think about merging UIPEthernet with all the remaining Ethernet libs (Ethernet, Ethernet,2, Ethernet3, EtherCard, Ethersia and UIPEthernet) into one super Ethernet lib? There are so many and confusing.
I meant merging into one superlib, not using wrapper to select which one.
This way, we can include all the best features of each lib, and also have one standard interface.
Certainly, wrapper class is still the best for now, until superlib done.
Add EthernetWrapper.h for easier W5x00 support as well as more Ethernet libs in the future.
Add default SS/CS pin for ESP8266 and ESP32
Increase default clock speed for W5x00 in Ethernet and EthernetLarge libraries to 25MHz from 14MHz. Tested OK on W5100 as well as W5500.
New in v1.0.8
Fix W5x00 support for ESP8266 and many more boards.
New in v1.0.7
Add ENC28J60 support to ESP32 and ESP8266 boards.
New in v1.0.6
Add W5x00 support to ESP32 and ESP8266 boards.
New in v1.0.5
Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox, etc.
Support any future custom Ethernet library that meets the no-compiling-error requirements. Currently, Ethernet2, Ethernet3, EthernetLarge libraries are supported. Ethernet_Shield_W5200, EtherCard, EtherSia libraries are not supported now.
New in v1.0.4
Add support to Adafruit SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).