I've got an interesting problem I'm trying to solve and I keep hitting dead-ends because it looks like the ESP8266WIFI.h library is limited, but I'm sure there is a solution.
So, say I've got 6 ESP8266 + Arduino UNO boards. I want to use 3 of them as "servers" (let's call them S1, S2, S3) and 3 of them as "clients" (C1, C2, C3) (I don't necessarily mean WiFiServer and WiFiClient objects).
Every "client" must be able to send a request to every server, and every server must be able to reply to every client, so the connections (S1C1, S1C2, S1C3, S2C1, S2C2, S2C3, S3C1, S3C2, S3C3) must be active.
At first, I thought about doing this:
Activate SoftAP mode on S1, S2, S3 and create a WiFiServer object on each.
If CX needs to send SX a request: Wifi.begin(SX_ssid, SX_pass) -> create a WifiClient object -> exchange data with server on SX -> close the WifiClient object -> WiFi.disconnect() -> repeat
So the connections are not ALWAYS active. They are opened when they are needed.
This was very similar to what I saw in the source code of the ESP8266WiFiMesh library.
The problem here is the time it takes for the begin and connect functions.
As an experiment, I used 2 ESP8266 boards: one is a server with a softAP, the other is a client. In the loop() function of the client board, I did a WiFi.begin -> WiFiClient.connect -> WiFiClient.close -> WiFi.disconnect sequence.
On average, it took 10 seconds to connect to a server board, but it could also be 20+ seconds sometimes, which is TERRIBLE for my application.
Using the server MAC address and manual IP addresses I managed to get 1-3 second connect times which is much better but I still sometimes got 10 seconds and the WiFiClient.connect() function would sometimes hang, so that my while(WiFiClient.connected() != true) loop would stop everything because the server wouldn't respond.
I have enclosed the source code if anyone would like to try it out and see the results themselves. There are two setup-loop pairs: one for the "server" board, the other for the "client" board. Don't forget to put in your own MAC address!
The main questions are:
- why might the WiFiServer "hang" (WiFiClient.connect() never connects)? Using MAC addresses and manual IP is OK, I can live with that.
- is it possible to actually open ALL connections between the clients and servers so that I don't need to do begin or disconnect ever again until the system is switched off?
- what would be a sensible "next step"? Should I try a different experiment?
If I create a WiFiServer object while my board is in softAP mode (like in my example), the server will be accessible by the softAP IP address. But what if I am in station + softAP mode? Imagine I have a softAP set up but did WiFi.begin with a different AP. Will the WiFiServer I create be accessible by the softAP IP address or the address I got from the other AP via DHCP?