Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By freedom2000
#31009 Hi,

Here is small sketch allowing to connect two androids clients on a single ESP8266.
The server is on ESP

Code: Select all/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

/* Set these to your desired credentials. */
const char *ssid = "JP_TestAP";

int status = WL_IDLE_STATUS;
boolean alreadyConnected[2] ; // whether or not the client was connected previously

WiFiServer server(23);
WiFiClient client[2];


void setup()
{
   delay(1000);
   Serial.begin(115200);
   Serial.println();
   Serial.print("Configuring access point...");
   /* You can remove the password parameter if you want the AP to be open. */
   WiFi.softAP(ssid);
   delay(10000);
   IPAddress myIP = WiFi.softAPIP();
   Serial.print("AP IP address: ");
   Serial.println(myIP);

   Serial.println("\nStarting server...");
  // start the server:
  server.begin();
}

void loop()
{
  unsigned int i;
  for (i= 0; i<2; i++)
  {
    // wait for a new client:
    if (!client[i])
    {
        client[i] = server.available();   
    } 
    else
    {
       if (client[i].status() == CLOSED)
       {
        client[i].stop();
        Serial.println("connection closed on client "  );
        Serial.print(i);
      }   
      if (!alreadyConnected[i]) // when the client sends the first byte, say hello:
      {
        // clead out the input buffer:
        client[i].flush();
        Serial.println("new client ");
        client[i].println("Hello, client !");
        Serial.print(i);
        alreadyConnected[i] = true;
      }
 
      if (client[i].available() > 0)
      {
        // read the bytes incoming from the client:
        char thisChar = client[i].read();
        // echo the bytes back to the client:
        client[i].println(thisChar);
        // echo the bytes to the server as well:
        Serial.write(thisChar);
      }
    }
  }
}


You can test it with Socket Protocol App free on GooglePlay

JP