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

Moderator: igrr

User avatar
By hakha4
#29713 martinayotte wrote :
If not, and you just wish to received a connection request later from the server, let says an hour later, then you need to implement a server on ESP side too, which is the goal of client.available(). But I don't see any server code here.

You're right, I want to send from MLServer at a later time and now tried to implement a server instead but I don't get any indata so obviously I'm doing something wrong! Can it be that both MLServer and the ESP server can listen on the same port ?!.
I appreciate If You could point me in the right direction. I've read forum back and forth but can't figure out how to get it work. My present code :

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "**********";
const char* password = "***************";
const int Port = 23;
const char* host = "192.168.1.30";
String InputString;
// Use WiFiClient class to create TCP connections
WiFiClient client;
WiFiServer Server(Port);//listen on port 23


void setup() {
  Serial.begin(115200);
  delay(1000);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


}



void loop() {

  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    // return;
  }
  else {

    Serial.println("Sending ...");
    client.print("TEST DATA TO MLServer\r\n");
    delay(100);
    // yield();
  }
  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
    // return;
  }

  else {
   
    //*********************************************************
    //Connected to MLServer waiting for incoming data
    //*********************************************************
//Server
  client = Server.available();
 
 
  if (client) {
     Serial.println("Recieving ...");
    while (client.connected()) {


      while (client.available()) {
        String line = client.readStringUntil('\r');
        delay(10);
        Serial.println(line);
        line = "";

      }

     
    }
   // client.stop();//???????????

   
  }



}
delay(3000);
}


Regards Håkan
User avatar
By martinayotte
#29719 Many things need to be reformatted. First, the delay(3000) prevent your server to run properly, second, you should not use the same client variable for both server-client and the client used for send, use local variable in this case.

Here, I will try to give you a small snippet of code, (hoping I didn't do copy/paste mistake, I didn't test it) :

Code: Select allunsigned long previousMillis = 0;

void SendDataToServer() {
  WiFiClient client;
  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
  }
  else {
    Serial.println("Sending ...");
    client.print("TEST DATA TO MLServer\r\n");
    delay(100);
    /* maybe you can verify an acknowledge here, but facultative */
  }
}

void loop()
{
  if (client) {
    if (client.status() == CLOSED) {
      client.stop();
    }
    if (client.available()) {
      String req = client.readStringUntil('\r');
      req.trim();
      if (req.equals("hello")) {
        client.println("Hello from ESPDuino !!!");       
      }
      else if (req.equals("command2")) {
        client.println("Response for Command2");       
      }
  else {
    client = server.available();
  }
  delay(1);
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > 3000) {
    previousMillis = currentMillis;
    // It is time to send something here ...
    SendDataToServer();
  }
}


Hoping this help !
User avatar
By hakha4
#29727 Thank's alot for code snippet!
Here, I will try to give you a small snippet of code, (hoping I didn't do copy/paste mistake, I didn't test it) :

I had to add two '}' at bottom of void loop to get the sketch to compile so maybe there was a copy/paste error because sketch halts after printing local IP and never runs SendDataToServer() or recieve from MLServer. May I ask you to take a last look at code and then I'll analyze code and try learning from it
Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "DOVADO";
const char* password = "5AEDDAD24D";
const int Port = 23;
const char* host = "192.168.1.30";
String InputString;
unsigned long previousMillis = 0;

// Use WiFiClient class to create TCP connections
WiFiClient client;
WiFiServer server(23);//listen on port 23


void setup() {
  Serial.begin(115200);
  delay(1000);

  // We start by connecting to a WiFi network

  Serial.println();

  WiFi.begin(ssid, password);
  server.begin();
  Serial.println("Server started");
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


}



void SendDataToServer() {
  WiFiClient client;
  if (!client.connect(host, Port)) {
    Serial.println("connection failed");
  }
  else {
    Serial.println("Sending ...");
    client.print("TEST DATA TO MLServer\r\n");
    delay(100);
    /* maybe you can verify an acknowledge here, but facultative */
  }
}

void loop()
{


  if (client) {
    if (client.status() == CLOSED) {
      client.stop();
      Serial.println("connection closed !");
    }
    if (client.available()) {
      String req = client.readStringUntil('\r');
      req.trim();
      if (req.equals("hello")) {
        client.println("Hello from ESPDuino !!!");
      }
      else if (req.equals("command2")) {
        client.println("Response for Command2");
      }
      else {
        client = server.available();
      }
      delay(1);
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis > 3000) {
        previousMillis = currentMillis;
        // It is time to send something here ...
        SendDataToServer();
      }
    }
  }
}

Thank's for helping a 'Newbee'