Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By russmathis
#18186 Trying to get several ESP modules to communicate together on their own wifi network.
Need them to independently be able to communicate back and forth at any time.
Guessing some type of socket protocal via tcp/ip.
At any time a module may need to send commands to any other modules in the wifi network.

How do I access or use sockets with the "ESP8266 Arduino IDE" platform?

Any help or ideas, appreciated!
thanks!
User avatar
By russmathis
#18249 Sorry @Bymartinayotte , I must be daft?
I'm might not have the correct git?
But my ESP8266WiFi/examples only contains usage examples for NTPClient, WiFiClient, WiFiScane, WiFiWebServer.
I'm I missing something more.
I'm a newbie so I've missed something I'm certain so I'll dig deeper and perhaps check to see if I have the updated repository or correct git. Thanks! :ugeek:
User avatar
By russmathis
#18253 In case anyone else is interested @ficeto from the github shared the following:

#include <ESP8266WiFi.h>

#define MAX_SRV_CLIENTS 3
const char* ssid = "*************";
const char* password = "************";

WiFiServer server(21);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {
Serial1.begin(115200);
//Serial1.setDebugOutput(true);
WiFi.begin(ssid, password);
Serial1.print("\nConnecting to "); Serial1.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
if(i == 21){
Serial1.print("Could not connect to"); Serial1.println(ssid);
while(1) delay(500);
}
Serial.begin(115200);
server.begin();
server.setNoDelay(true);
Serial1.print("Ready! Use 'telnet ");
Serial1.print(WiFi.localIP());
Serial1.println(" 21' to connect");
}

void loop() {
uint8_t i;
if (server.hasClient()){
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (!serverClients[i] || !serverClients[i].connected()){
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
continue;
}
}
//no free spot
WiFiClient serverClient = server.available();
serverClient.stop();
}
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
if(serverClients[i].available()){
while(serverClients[i].available()) Serial.write(serverClients[i].read());
//you can reply to the client here
}
}
}
if(Serial.available()){
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
//bello is a broadcast to all clients
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){
serverClients[i].write(sbuf, len);
delay(1);
}
}
}
}