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

Moderator: igrr

User avatar
By mostafa khedrawy
#52110 I am using arduino ide to program esp8266 (wifi module) and I created a tcp server on the module then i used tcp client tester program to send and receive data .this is the code i am using
I managed to send data from the module but can't receive any data from my phone , Can anyone help me?
this is the code i am using
Code: Select all#include <ESP8266WiFi.h>
int i =0;
char ssid[] = "moataz";          //  your network SSID (name)
char pass[] = "58295829";   // your network password
int status = WL_IDLE_STATUS;

WiFiServer server(1050);

void setup()
{
  // initialize serial:
  Serial.begin(115200);
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin("moataz", "58295829");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("connectedd");
    server.begin();
    IPAddress myAddress = WiFi.localIP();
    Serial.println(myAddress);
delay(200);
}

bool alreadyConnected = 0;
void loop() {
WiFiClient client = server.available();
 if (client) {
    if (!alreadyConnected) {
      // clear out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }
 }
 if (client.available() > 0) {
      char thisChar = client.read();
      Serial.println("We got data");
      Serial.println(thisChar);
      delay(200);
   }
}
User avatar
By Orcanbull
#52278 Hi Mostafa,

Probably you would like to try some code similar to this :

Code: Select allextern "C" {
#include "user_interface.h"
}

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

int i =0;
char ssid[] = "moataz";          //  your network SSID (name)
char pass[] = "58295829";   // your network password
int status = WL_IDLE_STATUS;

WiFiServer server(1050);
WiFiClient serverClients[MAX_SRV_CLIENTS];
#define MAX_SRV_CLIENTS 6

void setup()
{
   Serial.begin(9600);
   server.begin()
   server.setNoDelay(true);

}
void loop()
{

 if (server.hasClient()) {    // connecting the client to the server
    connected = 0;
    for (i = 0; i < MAX_SRV_CLIENTS; i++) {
      //find free/disconnected spot
      if (!serverClients[i] || !serverClients[i].connected()) {
        if (serverClients[i]) serverClients[i].stop();
        serverClients[i] = server.available();
        Serial.print("New client: "); Serial.println(i);
        continue;
      }
      connected ++;
    }
    Serial.print("Connected clients = ");
    Serial.println(connected);
    //no free/disconnected spot so reject
    WiFiClient serverClient = server.available();
    serverClient.stop();
  }
  //check clients for data
  for (int i = 0; i < MAX_SRV_CLIENTS; i++)
  {
    if (serverClients[i] && serverClients[i].connected())
    {
      if (serverClients[i].available())
      {
        Serial.print("Recieved Message from: ");
        Serial.print(serverClients[i].remoteIP());
        Serial.print("\t");
        Message = "";
        while (serverClients[i].available())
        {
          char tempchar = serverClients[i].read();
          Message += tempchar;
        }
        Serial.println(Message);
serverClients[i].write("MY MESSAGE BACK");
    }
  }
}


I didn't test this code, i took snippets from my working code concerning the data connection, hope this helps.

Kind regards,
Orcanbull