-->
Page 1 of 1

tcp/socket communications in AP mode?

PostPosted: Fri May 22, 2015 12:59 pm
by russmathis
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!

Re: tcp/socket communications in AP mode?

PostPosted: Fri May 22, 2015 2:21 pm
by martinayotte
There are several examples in arduino-1.6.1/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/ .

Sorry @Bymartinayotte , I'm must be daft?

PostPosted: Sat May 23, 2015 8:23 am
by russmathis
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:

Re: tcp/socket communications in AP mode?

PostPosted: Sat May 23, 2015 8:37 am
by russmathis
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);
}
}
}
}