Compiler error trying to use WiFiEvents

I'm having problems using the "WiFiEvent" as described in guides online. As far as I can tell, a listener on these events is how an ESP8266 server may respond to events like clients connecting, disconnecting, etc. So I add a small listener in setup(), but I get compile errors on all of the event enums.

Please take a look at this excerpt from my code, which I hope contains the relevant code:

#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>

AsyncWebServer server(80);

void WiFiEvent(WiFiEvent_t event)
{
  if (event == ARDUINO_EVENT_WIFI_AP_STACONNECTED) {
    // handle your specific event here
    Serial.println("Client connected!");
  }
}

void setup() {
  WiFi.begin(ssid, password);
  ... setup etc

  WiFi.onEvent(WiFiEvent);  // register listener
  server.begin();
}

void loop(){
  digitalWrite(output5, HIGH);
}

The compiler error I get is:

In function 'void WiFiEvent(WiFiEvent_t)':
lockServer:87:16: error: 'ARDUINO_EVENT_WIFI_AP_STACONNECTED' was not declared in this scope
87 | if (event == ARDUINO_EVENT_WIFI_AP_STACONNECTED) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit status 1
'ARDUINO_EVENT_WIFI_AP_STACONNECTED' was not declared in this scope

Hello

These are for ESP32

For ESP8266 :

And onEvent is deprecated. See this example : Arduino/WiFiEvents.ino at master · esp8266/Arduino · GitHub

Thanks!

My server ESP is connecting to my home wifi, so it's not setting up a soft AP. The other ESPs (clients) are not connecting to the server, but simply sending POST requests to the server on its IP address. So, I guess I'm actually on a different approach; my clients aren't connecting to the server at all.

I've set it up like this because I want to be able to easily access a web dashboard on the server from my cellphone/computer, without having to change network (or do anything advanced in the router).

I probably could have the clients actually establish a TCP connection to the server (right?), but are there WiFiEvents which correspond to this? What's a good network design for something like this? Am I on the totally wrong track?

And sorry if this is veering off topic :wink:

After reading your post again, I think you are confusing WiFi with web server. Those are WiFi events, it has nothing to do with the web server.

Maybe this is what you are looking for : GitHub - me-no-dev/ESPAsyncWebServer: Async Web Server for ESP8266 and ESP32

Yes, but then it's a matter of making/opening a websocket, which is establishing a persistent connection to the server. I think this is indeed what I'm looking for. For now, however, I'm experimenting with a simple "pulse" endpoint, where each client periodically (every 10 secs) sends a POST request. This way, the server may keep track of which clients are still out there.

I am indeed confusing these network things, so thanks for the pointers :slight_smile:

To be honest, I don't know much about web servers etc, maybe someone more knowledgeable will answer

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.