-->
Page 1 of 5

tcp client on Android, server on ESP as AccessPoint.

PostPosted: Tue Oct 06, 2015 2:44 pm
by freedom2000
Hi all,

I have a stupid problem ...

I would like to setup a tcp socket connection between an android phone and an ESP8266 running in AP mode.

Here is the code I currently use :
Code: Select all/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.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);

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:
  WiFiClient client = server.available();
  // when the client sends the first byte, say hello:
  if (client)
  {
    if (!alreadyConnected)
    {
      // 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);
    }
  }
}



This code "almost" works...
The AP is dicovered by my Android, I can connect
I can as well open a socket on port 23 with IP 192.168.4.1
the ESP sends back the "Hello Client" message to android.

The problem is that if I try to send a message to ESP from Android nothing happens...

Here is the log
Code: Select allConfiguring access point...AP IP address: 192.168.4.1

Starting server...
We have a new client


There should be an obviou bug , but I can't find where...

Thank you for your help
JP

Re: tcp client on Android, server on ESP as AccessPoint.

PostPosted: Tue Oct 06, 2015 3:31 pm
by martinayotte
Of course, the above code is simply echoing the character received from the client.
You will have to accumulate those characters into a buffer, and when a complete line is buffered by detecting newline or carriage return character, you can then analyse the buffer to figure out which command is been received.
It is in fact becoming of Telnet server with your own shell commands.

Re: tcp client on Android, server on ESP as AccessPoint.

PostPosted: Tue Oct 06, 2015 3:40 pm
by freedom2000
THank you for your answer.

But it is even worse than that... No echo is received on client side.

I am afraid that the second half of code doesn't work at all..

JP

Re: tcp client on Android, server on ESP as AccessPoint.

PostPosted: Tue Oct 06, 2015 4:38 pm
by martinayotte
Is the Android application is a telnet client ?
Did you try a telnet from a PC ?
Also, for the code above, maybe you can try changing server.write() with client.write() ...