Go Down

Topic: EthernetWebServer library for AVR, Teensy, SAM, SAMD, ... & Ethernet shields (Read 2348 times) previous topic - next topic

khoih-prog

https://github.com/khoih-prog/EthernetWebServer


How To Install Using Arduino Library Manager


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
1. HTTP Server and Client
2. HTTP GET and POST requests, provides argument parsing, handles one client at a time.

Library is based on and modified from:

Ivan Grokhotkov's ESP8266WebServer


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

Code: [Select]

#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();
}

Juraj

did you test it with UIPEthernet library? at least compile?
I maintain the UIPEthernet library.

khoih-prog

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.

khoih-prog

Hi @Juraj

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,

Juraj

Hi @Juraj

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.

Juraj

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

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.

Juraj

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.
https://github.com/jandrassy/ArduinoOTA/blob/e66d83a865617b6b6280c6fdaf49aab28eba64ac/src/ArduinoOTA.h#L107

khoih-prog

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.

khoih-prog

Update Feb 20th 2020

Version v1.0.2

1. 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))
2. Support Wiznet W5x00 or ENC28J60 Ethernet shields by using UIPEthernet library besides standard Ethernet library thanks to jandrassy

khoih-prog

Update Feb 23rd 2020

Version v1.0.3

EthernetWebServer Release v1.0.3

From v1.0.3+, the library supports more Arduino boards ( SAM DUE, SAMD: ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.)

khoih-prog

New in v1.0.10

1. Fix bug not closing client and releasing socket.
2. Merge new features from latest ESP8266WebServer
3. Add and enhance examples.

New in v1.0.9

1. Add EthernetWrapper.h for easier W5x00 support as well as more Ethernet libs in the future.
2. Add default SS/CS pin for ESP8266 and ESP32
3. 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

1. Fix W5x00 support for ESP8266 and many more boards.

New in v1.0.7

1. Add ENC28J60 support to ESP32 and ESP8266 boards.

New in v1.0.6

1. Add W5x00 support to ESP32 and ESP8266 boards.

New in v1.0.5

1. 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.
2. 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

1. Add support to Adafruit SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).

khoih-prog

New in v1.0.12

1. Add support to new EthernetENC library for ENC28J60.
2. Add debug feature. Clean up code.

New in v1.0.11

1. Add support to Seeeduino SAMD21/SAMD51 boards (LoRaWAN, Zero, Femto M0, XIAO M0, Wio GPS Board, etc.)
2. Add and restructure examples.

Go Up