5

I want to create a simple Wifi TCP server by ESP8266 in Arduino IDE. But I have a big problem: when I send a character or string from client I can't receive it on the server.

In fact I connect esp8266 to my PC and I want to see send character from client in pc terminal. my sending side is Socket protocol app for android!and complete code I write in sever side is:

WiFiServer server(8888);
void setup() 
{
  initHardware();
  setupWiFi();
  server.begin();
}
void loop() 
{
  WiFiClient client = server.available();
  if (client) {
    if (client.available() > 0) {
      char c = client.read();
      Serial.write(c);
    }
  }
}
void setupWiFi()
{
  WiFi.mode(WIFI_AP);
  WiFi.softAP("RControl", WiFiAPPSK);
}

void initHardware()
{
  Serial.begin(115200);
}

Baudrate it set to 115200 on both sides.

6
  • Please provide more code, seeing both sides will help. Commented Oct 16, 2015 at 19:00
  • @Marged: I'd assume that, too, but "doesn't receive" doesn't imply "but the program executes successfully on the sending side". Commented Oct 16, 2015 at 19:05
  • my sending side is SocketProtocol app when I click on send button thats status change to sending message!I think that cant send message! Commented Oct 16, 2015 at 19:54
  • 1
    Did you look to the telnet sample ? Commented Oct 17, 2015 at 22:34
  • wow!thanks very much mpromonet .that's work correctly!Its a useful link. Commented Oct 18, 2015 at 13:33

1 Answer 1

4

In the loop, you are closing the client connection as soon as it is established deleting the WiFiClient object.

In order to keep the connection open you could modify the loop like this :

WiFiClient client;
void loop() 
{
    if (!client.connected()) {
        // try to connect to a new client
        client = server.available();
    } else {
        // read data from the connected client
        if (client.available() > 0) {
            Serial.write(client.read());
        }
    }
}

When client is not connected it tries to connect one and when a client is connected, it reads incoming data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.