Post topics, source code that relate to the Arduino Platform

User avatar
By frank65
#56565 Hello guys

i have problem with connect multiple client to my ESP (in server mode), i'm work whit arduino IDE.

set ESP on server mode then connect 2 client to this, but only can receive one of client message in one time. :(

how can receive 2 or more client message in same time ?

please help me guys
User avatar
By frank65
#56609
rudy wrote:It would help if you posted your code.


Hi rudy, thanks so much for your replay. send my code for your.

i'm enable ESP in softAP+STA mode
so use of FS.H library for save my home-AP and ESP-AP SSID and PAS in ESP flash memory (SPIFFS), i make 2 function for this (GetEspApValue() & GethomeApValue()) user can be connect to ESP in AP mode so ESP can connect to any other AP in my code. but only can receive one user (client) message in same time.

please teach me how can solve my problem.

Code: Select all#include <ESP8266WiFi.h>
#include "FS.h"

//ESP SSDI AND PASS when config in softAP mode
char ESP_AP_SSID[20]= "*******";
char ESP_AP_PASS[20]= "*******";
//Home AP SSID AND PASS when ESP config in Station mode
char home_AP_SSID[20]= "*******";
char home_AP_PASS[20]= "*******";


WiFiServer server(8080);



void GetEspApValue(){
  //espAP.text Save ESP/AP SSID AND PASS IF exsit read from if not exist create and write default SSID AND PASS
  bool exist = SPIFFS.exists("/espAP.text");
  if (exist){
    File f = SPIFFS.open("/espAP.text", "r");
    if (f){
      for (int i=1; i<=2; i++){
        String s=f.readStringUntil('\n');
        if (i==1){
          s.replace((0x0D),(0x00));
          s.toCharArray(ESP_AP_SSID, 20);
        }
        if (i==2){
          s.replace((0x0D),(0x00));
          s.toCharArray(ESP_AP_PASS, 20);
        }
      }
      f.close();
    }
  }
  else{
    File f = SPIFFS.open("/espAP.text", "w");
    if (f){
        f.println(ESP_AP_PASS);
        f.println(ESP_AP_PASS);       
    }
    f.close();
  } 
}

void GethomeApValue(){
  //homeAP.text Save home/AP SSID AND PASS IF exsit read from if not exist create and write default SSID AND PASS
  bool exist1 = SPIFFS.exists("/homeAP.text");
  if (exist1){
    File f = SPIFFS.open("/homeAP.text", "r");
    if (f){
      for (int i=1; i<=2; i++){
        String s=f.readStringUntil('\n');
        if (i==1){
          s.replace((0x0D),(0x00));
          s.toCharArray(home_AP_SSID, 20);
        }
        if (i==2){
          s.replace((0x0D),(0x00));
          s.toCharArray(home_AP_PASS, 20);
        }
      }
      f.close();
    }
  }
  else{
    File f = SPIFFS.open("/homeAP.text", "w");
    if (f){
        f.println(home_AP_SSID);
        f.println(home_AP_PASS);
    }
    f.close();
  }   
}
void setup() {
  Serial.begin(115200);
  SPIFFS.begin();
  SPIFFS.format();
  GetEspApValue();
  GethomeApValue();
  delay(20);
  WiFi.softAP((const char*)ESP_AP_SSID, (const char*)ESP_AP_PASS);

 
  Serial.print("Connecting to ");
  Serial.println(home_AP_SSID);
 
  WiFi.begin((const char*)home_AP_SSID, (const char*)home_AP_PASS);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  server.begin();
  Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
 
  }



void loop() {

WiFiClient client = server.available();
  // wait for a client (web browser) to connect
  if (client)
  {
    Serial.println("\n[Client connected]");
    while (client.connected())
    {
      // read line by line what the client (web browser) is requesting
      if (client.available())
      {
        String line = client.readStringUntil('\r');
        Serial.print(line);
        // wait for end of client's request, that is marked with an empty line
        if (line.length() == 1 && line[0] == '\n')
        {
          client.println(prepareHtmlPage());
          break;
        }
      }
    }
    delay(1); // give the web browser time to receive the data

    // close the connection:
    client.stop();
    Serial.println("[Client disonnected]");
  }
 
}