Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By freedom2000
#30753 Thank you again :)

You are right I am a bit lost with the syntax of arduino sketches...

My Android app is working well and I have even tested another one "Socket Protocol" (free on google Play)
Both are using classical sockets to communicate with tcp connection.

My code is blocking here on the client.read() instruction
and even worse, it never enters the client.available condition...

Code: Select all if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes to the serial
      Serial.write(thisChar);


Finally my question is :
- once connected to client, how can I detect and read incoming data ?
- client is Android
- server is ESP8266
- ESP8266 being in AP mode

JP
User avatar
By martinayotte
#30804 I've took your code as is, and I figured out few things that I didn't saw earlier :

- First, the client was a local variable, so it was destroyed on every loop.
- Second "client = server.available()" should only be called when client is null, otherwise the previous client is again destroyed.
- Third, we need to handle the client disconnect with a "client.stop()"

So here is the new version :

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 = false; // whether or not the client was connected previously

WiFiServer server(23);
WiFiClient client;

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()
{
  // wait for a new client:
  if (!client) {
      client = server.available();
  } 
  else
  {
     if (client.status() == CLOSED) {
      client.stop();
      Serial.println("connection closed !");
    }   
    if (!alreadyConnected) // when the client sends the first byte, say hello:
    {
      // clead out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}
User avatar
By freedom2000
#30892 Hi again,

Here is what I get in the log :

Code: Select allConfiguring access point...AP IP address: 192.168.4.1

Starting server...
We have a new client
Martinayotte is a boss !


Clearly means that it works :D

There was still a little bug on my side...
Here is the final code :

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 = false; // whether or not the client was connected previously

WiFiServer server(23);
WiFiClient client;

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()
{
  // wait for a new client:
  if (!client)
  {
      client = server.available();
  } 
  else
  {
     if (client.status() == CLOSED)
     {
      client.stop();
      Serial.println("connection closed !");
    }   
    if (!alreadyConnected) // when the client sends the first byte, say hello:
    {
      // clead out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

    if (client.available() > 0)
    {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      client.println(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
    }
  }
}


Next step for me will be to open another server to handle point to point communication between two android phones.

Thank you again for your nice explainations
JP