khoih-prog / EthernetWebServer

This is simple yet complete WebServer library for AVR, AVR Dx, Portenta_H7, Teensy, SAM DUE, SAMD21/SAMD51, nRF52, STM32, RP2040-based, 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. Coexisting now with `ESP32 WebServer` and `ESP8266 ESP8266WebServer` libraries. Ethernet_Generic library is used as default for W5x00 with custom SPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Teensy 4.1 : Equivalent for "server.on"

jimmie11 opened this issue · comments

A very nice and complete webserver example has been posted by David Bird at:

https://github.com/G6EJD/ESP-SMART-Thermostat

I am trying to convert that example to use your library with a Teensy 4.1 but getting a compile error for

server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {

Is there an equivalent to that statement?

Thank you.

Those examples are written for ESP32/ESP8266 using ESPAsyncWebServer which is not supported by the EthernetWebServer Library.

You can convert the example to use synchronous (ESP8266)WebServer, then you can use them with this library. The syntax is very similar, such as

server.on("/", handleSomething);

Good Luck,

PS: Currently, the AsyncWebServer is ready only for ESP32/ESP8266 using ESPAsyncWebServer and STM32 using AsyncWebServer_STM32

Thank you but Server.on is what causes the compile error. How will using the same statement and changing the library to synchronous help?

You certainly have to first rewrite the examples to use (ESP8266)WebServer. That will modify the function call from asynch way

server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) 

to synchronous way

server.on("/", handleSomething);

then port the modified examples to EthenetWebServer.

It's better to spend some more time to research the differences between Async and Sync examples.

This is the Async_HelloServer example written for AsyncWebServer_STM32 library

void setup(void)
{
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);

  Serial.begin(115200);
  Serial.println("\nStart Async_HelloServer on " + String(BOARD_NAME));
...

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    handleRoot(request);
  });

  server.on("/inline", [](AsyncWebServerRequest * request)
  {
    request->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)
{
}

and the similar HelloServer written for sync EthernetWebServer library

void setup(void)
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial);

  Serial.print("\nStarting HelloServer on " + String(BOARD_TYPE));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println("EthernetWebServer Version " + String(ETHERNET_WEBSERVER_VERSION));
...

  server.on(F("/"), handleRoot);

  server.on(F("/inline"), []() 
  {
    server.send(200, F("text/plain"), F("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();
}