Soft AP ESP32 & multiple clients

alexpe28
Posts: 3
Joined: Tue Nov 26, 2019 5:48 am

Soft AP ESP32 & multiple clients

Postby alexpe28 » Wed Dec 04, 2019 6:11 am

Hello!
Please help with the following question:
The first ESP32 module is the AP server, and the other two are its clients (clients have the same source code).
One client receives the IP address 192.168.4.2, and the second - 192.168.4.3.
The client who first connected to the server and received the ip address 192.168.4.2 receives the message from the "OnLED" server, and the client who received the address 192.168.4.3 does not receive the message.

How can I write code on the server so that I can send messages to all clients at the same time and how can I write code so that I can send messages to specific IP addresses?

Thank you so much!
I enclose the code:
________________________________________
SERVER:

Code: Select all

// AP Server
#include <WiFi.h>

const int LED_PIN = 16;

// Replace with your network credentials
const char* ssid     = "ESP32-Access-Point";
const char* password = "ESP32accesspoint";

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(1000); // даем время на запуск последовательной коммуникации
  Serial.println("Setting AP (Access Point)…");

  pinMode(LED_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);
  delay(100);
  digitalWrite(LED_PIN, HIGH);

  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address ");
  Serial.println(IP);
  Serial.print("MAC address ");
  Serial.println(WiFi.softAPmacAddress());

  server.begin();

}

void loop() {

  WiFiClient client = server.available();    //Listen for incoming clients

  if (client)
  { //If a new client connects,
    Serial.println("New Client.");           //print a message out in the serial port
    while (client.connected())
    {
      Serial.println("Client connected.");
      Serial.println(client.available());

      if (client.available() > 0)
      {
        // read the bytes incoming from the client:
        char answer = client.read();

        // echo the bytes back to the client:
        server.write(answer);

        client.println("OnLED\r");
      }
    }
    client.stop();
    Serial.println("Client disconnected.");

    digitalWrite(LED_PIN, LOW);
    delay(5000);
    digitalWrite(LED_PIN, HIGH);
  }
}
____________________________________

Client:

Code: Select all

//Client
#include <ESP8266WiFi.h>
const int ledPin =  16;

const char* ssid = "ESP32-Access-Point";
const char* password =  "ESP32accesspoint";
String fromServer;

WiFiClient client;

IPAddress server(192, 168, 4, 1);

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println();

  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
}

void loop()
{
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("connecting...");
  if (client.connect(server, 80))
  {
    Serial.println("connected to server");
    Serial.println(WiFi.localIP());
    client.write("data");
    fromServer = client.readStringUntil('\r');
    Serial.println(fromServer);
    if (fromServer == "OnLED") {

      digitalWrite(ledPin, HIGH);
      delay(5000);
      digitalWrite(ledPin, LOW);
    }
    else {
      digitalWrite(ledPin, LOW);
    }
  }
  else
  {
    Serial.println("failed to connect to server");
  }
  delay(1000);
}
Attachments
esp clients.png
esp clients.png (18.53 KiB) Viewed 14505 times

martinius96
Posts: 33
Joined: Thu Dec 13, 2018 1:39 am

Re: Soft AP ESP32 & multiple clients

Postby martinius96 » Thu Dec 26, 2019 10:06 pm

E-mail me: martinius96@gmail.com
I can help you.

ESP_Sprite
Posts: 9014
Joined: Thu Nov 26, 2015 4:08 am

Re: Soft AP ESP32 & multiple clients

Postby ESP_Sprite » Fri Dec 27, 2019 4:09 am

If you can, please keep the discussions in the forum so other people can also enjoy getting the answers.

User avatar
rudi ;-)
Posts: 1698
Joined: Fri Nov 13, 2015 3:25 pm

Re: Soft AP ESP32 & multiple clients

Postby rudi ;-) » Fri Dec 27, 2019 1:46 pm

alexpe28 wrote:
Wed Dec 04, 2019 6:11 am
Hello!
Please help with the following question:
The first ESP32 module is the AP server, and the other two are its clients (clients have the same source code).
One client receives the IP address 192.168.4.2, and the second - 192.168.4.3.
The client who first connected to the server and received the ip address 192.168.4.2 receives the message from the "OnLED" server, and the client who received the address 192.168.4.3 does not receive the message.

How can I write code on the server so that I can send messages to all clients at the same time and how can I write code so that I can send messages to specific IP addresses?

Thank you so much!
I enclose the code:

Hi

i did not read the code in detail, but
you can change the server code example to accept multiple connection.
the client_1 who does connect at first get the connection at first, but does not "disconnect" from sever
so client_2 does try connect to server but the line is busy so the "OnLED" message never fire for client_2.

if you do not want mutliple connection you can change the server code so, that server disconnect the client after a connect and data exchange, or the client disconnect from server then and the port is free again.

you can also use UDP instead TCP and give the client's ID's
then you can send braodcast messages ( all clients get the message ) and compare the message for the ID on the specific client
if the client data is for the matched ID client then the client can process the data or else what you want to do.

there are few more ways and things to do this.
if is depended what you want to do and how much clients and so on.
time critical or not and so on.

hope this helps

best wishes
rudi ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

martinius96
Posts: 33
Joined: Thu Dec 13, 2018 1:39 am

Re: Soft AP ESP32 & multiple clients

Postby martinius96 » Fri Dec 27, 2019 7:39 pm

Without Async library for webserver you cannot get IP address of client.
So in that case you can use 2 webpages on Webserver and one client will use first page, other will use second page.

Or you will connect to same page with both clients, also... One client must end its connection... (You havent end HTTP connection yet).

I have spent a lot of time last months with some Webserver projects based on ESP32, for instance OPC UA architecture implementation too. So there is implementation for that, what you want, also i have used comfortable methods --> server on(), you can make more subpages with that function.
Webserver code - ESP32

Code: Select all

//Webserver - ESP32
//Author: Martin Chlebovec (martinius96)
//Web: https://arduino.php5.sk?lang=en
//Donate: https://paypal.me/chlebovec

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
const char* ssid = "ESP_WiFi";
const char* password = "thereisnospoon";
WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", "OnLED");
}
void setup(void) {
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server running");
  Serial.print("IP address: ");
  Serial.println(IP);
}

void loop() {
  server.handleClient();
}
Client code - ESP32

Code: Select all

//Webclient - ESP32
//Author: Martin Chlebovec (martinius96)
//Web: https://arduino.php5.sk?lang=en
//Donate: https://paypal.me/chlebovec

#include <WiFi.h>
const char *ssid = "ESP_WiFi";
const char *password = "thereisnospoon";
const char * host = "192.168.4.1";
const int httpPort = 80;

void setup() {
  Serial.begin(115200);
  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println(".");
    delay(500);
  }
  Serial.println("WiFi connected sucessfully");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Ready");
}

void loop() {
  WiFiClient client;
  if (client.connect(host, httpPort)) {
    String url = "/";
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    while (client.connected()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
      if (line == "\r") {
        break;
      }
    }
    String line = client.readStringUntil('\n');
    Serial.println("Webserver reply:");
    Serial.println(line);
    Serial.println();
    if (line == "OnLED") {
      Serial.println("LED IS ON!");
    }
  }
  client.stop();
  delay(5000);
}
Client output:
Image

sathish
Posts: 7
Joined: Sat Jan 11, 2020 7:23 am

Re: Soft AP ESP32 & multiple clients

Postby sathish » Tue Jan 14, 2020 7:21 am

Hi,
I am looking for Esp32 as Router. For this if there any code available

alexpe28
Posts: 3
Joined: Tue Nov 26, 2019 5:48 am

Re: Soft AP ESP32 & multiple clients

Postby alexpe28 » Thu May 14, 2020 9:58 am

Thanks a lot for the tips!
Really it was necessary to register "client.stop ();" not only on the server but also on the client. Thus, the client releases IP address 192.168.4.1 and the next client connects normally and receives messages from the server.
I don't care about getting teams quickly, so it worked!
Thank!

Who is online

Users browsing this forum: No registered users and 51 guests