My main ESP in AP mode is creating WiFi network for two others which are in STA mode.
Here is what I want to do:
1. At start I want to send information from server to everyone client
2. Information contains a client number from which server wants to get information
3. Each client compares received information with their own ID number
4. The client, who can talk, sends a packet of information to the server
5. After receiving information server again sends a request to all clients with next number
I know how to send information to the server from the client (at least I think I know, because now it's working), but I have problem with sending a message to all clients from the server. I'm trying to do this with "server.write()", but I don't receive anything in my client ESP with "client.readString()" function.
So here's finally my request - if you could tell me what I have to do to achieve my purpose, how to use "server.write()" correctly. I will be grateful for the anserws or helpful links.
I'm using Arduino Ide (arduino.cc) in 1.6.12 version with ESP addon 2.3.0.
Firmware of ESP v0.9.5.0 AT
Here's client code:
#include <ESP8266WiFi.h>
const char* ssid = "Pomiary";
const char* pass = "pomiar_mocy";
const int Port = 80;
IPAddress host(192,168,4,1);
WiFiClient client;
String req;
void setup() {
delay(1000);
Serial.begin(115200);
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//*********************************************************************
void loop() {
if(WiFi.status() != WL_CONNECTED){
Serial.println("reconnecting");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
while(!client.connect(host, Port)){
Serial.println("\nCoonection failed");
delay(500);
}
Serial.println("\nConnection successed");
// reading from server
if (client.connected()){
req = client.readString(); // doesn't work
Serial.println("reading...");
Serial.println(req);
}
// sending to server
client.print("esp no.1\n");
delay(200);
}
And here's server's code:
#include <ESP8266WiFi.h>
const char *ssid = "Pomiary";
const char *pass = "pomiar_mocy";
const int httpPort = 80;
int token= 1; //number of measuring device to ask
WiFiServer server(httpPort);
String req="";
void setup() {
delay(500);
Serial.begin(115200);
Serial.println("Configuring AP");
WiFi.mode(WIFI_AP);
boolean result = WiFi.softAP(ssid,pass);
if(result == true) Serial.println("Ready");
else Serial.println("Failed!");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
//Serial.printf("Stations connected to soft-AP = %d\n", WiFi.softAPgetStationNum());
WiFiClient client = server.available();
if (client.connected()) {
Serial.println("Send token");
server.write(token);
}
if(client.connected()){
req = client.readString();
Serial.println(req);
server.write("Message\n");
Serial.println("Send");
}
else{
Serial.println("No clients connected");
}
delay(1000);
}
P.s I apologize for my language skills, but English isn't my native