Go Down

Topic: WebServer library for AVR, Teensy, DUE, SAMD, STM32 & ESP8266 AT-command shields (Read 1 time) previous topic - next topic

khoih-prog

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

How To Install Using Arduino Library Manager


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.

Library is based on and modified from:

1. Ivan Grokhotkov's ESP8266WebServer
2. bportaluri's WiFiEsp library

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.

Sample Code

Code: [Select]

#define DEBUG_ESP8266_AT_WEBSERVER_PORT Serial

#include <ESP8266_AT_WebServer.h>

#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

#else
// For Mega
#define EspSerial Serial3
#define BOARD_TYPE      "AVR Mega"
#endif

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






Juraj

why did you decide to copy the WiFiEsp library code instead of making the WebServer working over any WiFi library?
why not SAMD boards compatibility?

I made a new WiFiEspAT library last year
https://github.com/jandrassy/WiFiEspAT
and I maintain the WiFiLink library
https://github.com/jandrassy/arduino-library-wifilink

khoih-prog

No reason at all.

I just found out WiFiEsp lib at the time I needed a WebServer library having similar functions as ESP8266WiFi WebServer lib in order to write some AT-based WiFiManager for Arduino, Teensy boards.

I think the code can use any WiFi (AT) library with the same interfacing functions.

Just come across your WiFiEspAT lib today in this forum (which I'm very new to).

Have a quick look and it's very good. Hope we can work together.

I don't think there'll be any issue with SAMD (or any) boards, just because I have only Mega, Teensy 4.0 boards to test.

Juraj

the WiFiEsp library has issues with SAMD, because of the RingBuffer class.

you could write one WebServer library for all Arduino networking libraries.

here I have a server wrapper class which uses many different networking libraries.
https://github.com/jandrassy/ArduinoOTA/blob/master/src/ArduinoOTA.h

khoih-prog

Can you clarify how and why the RingBuffer class creates issues with SAMD as I don't have SAMD to test and know now?

Just have a quick look at your server wrapper class, it's interesting and promising. Will try to wrap all networking libs into a super one. If you can proceed with this, please go ahead as you already had plenty experience on the issue. I'll just provide support when necessary.

Juraj

Can you clarify how and why the RingBuffer class creates issues with SAMD as I don't have SAMD to test and know now?

Just have a quick look at your server wrapper class, it's interesting and promising. Will try to wrap all networking libs into a super one. If you can proceed with this, please go ahead as you already had plenty experience on the issue. I'll just provide support when necessary.
install the SAMD package and try to compile.
https://github.com/bportaluri/WiFiEsp/issues/161

khoih-prog

IMHO, this is just a small issue, and can be fixed quickly by modifying the lib.
How about your taking over the super Arduino WebServer lib?

Juraj

IMHO, this is just a small issue, and can be fixed quickly by modifying the lib.
How about your taking over the super Arduino WebServer lib?
I have already too many Arduino plans. this one was behind the horizon.
for example today I added to my todo list "improve ArduinoHttpClient library". it uses only int for contentLength and doesn't support F().
yesterday I finished OTA download examples.
It would be good to finish/make OTA for Nano 33 BLE, Nano every, STM32
Two projects are waiting...

khoih-prog

That's OK. I'll put this as well as the Super EthernetWebServer in my bucket list.

khoih-prog

New Version v1.0.2

1. Add support to SAMD (DUE, ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.) boards

ESP8266_AT_WebServer v1.0.2

khoih-prog

New Version v1.0.3

1. Add support to STM32 (STM32,F0,F1, F2, F3, F4, F7, etc) boards

khoih-prog

New in v1.0.10

1. Fix bug not closing client and releasing socket.
2. Enhance examples.
3. Add function to be enable to work with WebSockets2_Generic Library

New Version v1.0.9

1. Fix bug.
2. Add functions (ESP8266_AT_Drv::wifiDriverReInit and ESP8266_AT_Class::reInit).
3. Restructure codes. Increase RingBuffer Size.
4. Add support to WIS600-01S and W600 WiFi shields

New Version v1.0.8

1. Fix bug.
2. Add features to ESP32-AT.

New Version v1.0.7

1. Add support to ESP32-AT-command shield.
2. Update deprecated ESP8266-AT commands.
3. Restructure examples to separate defines header files.

Go Up