0

I am trying to send multiple messages wirelessly from one ESP8266-12E NodeMCU to another. I have successfully established the connection and am able to send and receive single messages. The MAC addresses are solely for the verification that the data is going to the right client. My trouble is sending multiple messages which I should be able to observe in the serial monitor.

Here's my server code:

#include <ESP8266WiFi.h>

extern "C" {
  #include <user_interface.h>
}

int32_t freq = 10000;

WiFiServer server(80);                            //Initialize the server on Port 80

void setup() {
  pinMode(D1,OUTPUT);
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);

  Serial.begin(9600);                             //Start communication between the ESP8266-12E and the monitor window

  analogWriteFreq(freq);

  WiFi.mode(WIFI_AP);                             //This ESP8266-12E is an AccessPoint 
  WiFi.softAP("NodeMCU_js", "12345678");          //Provide the (SSID, password)  

  IPAddress HTTPS_ServerIP = WiFi.softAPIP();      //Obtain the IP of the Server
  Serial.println("Server IP is: ");                 //Print the IP to the monitor window 
  Serial.println(HTTPS_ServerIP);
  server.begin();                                 //Start the HTTP Server

}

void loop() { 
  WiFiClient client = server.available();         //Check if a client has connected to the server

  struct station_info *stat_info;
  struct ip_addr *IPaddress;
  IPAddress address;

  if (client) {  
    Serial.println("Somebody has connected :)");

    stat_info = wifi_softap_get_station_info();

    while (stat_info != NULL){
      IPaddress = &stat_info->ip;
      address = IPaddress->addr;
      Serial.println();

      Serial.print("Client IP Address = ");
      Serial.print(address);
      Serial.println();

      Serial.print("Client MAC Address = ");
      Serial.print(stat_info->bssid[0],HEX);
      Serial.print(":");
      Serial.print(stat_info->bssid[1],HEX);
      Serial.print(":");
      Serial.print(stat_info->bssid[2],HEX);
      Serial.print(":");
      Serial.print(stat_info->bssid[3],HEX);
      Serial.print(":");
      Serial.print(stat_info->bssid[4],HEX);
      Serial.print(":");
      Serial.print(stat_info->bssid[5],HEX);
      Serial.println();

      if (stat_info->bssid[0] == 0xDC && stat_info->bssid[1] == 0x4F && stat_info->bssid[2] == 0x22 && stat_info->bssid[3] == 0x17 && stat_info->bssid[4] == 0xEE && stat_info->bssid[5] == 0x95){
          int duty1 = 150;
          int duty2 = 200;
          int duty3 = 250;
          Serial.println("Duty - 1 : "+String(duty1));
          Serial.println("Duty - 2 : "+String(duty2));
          Serial.println("Duty - 3 : "+String(duty3));

          analogWrite(D1,duty1);
          analogWrite(D2,duty2);
          analogWrite(D3,duty3);

          client.println(String(duty1)+"\r");                          //Server's response to the client
          client.println(String(duty2)+"\r");
          client.println(String(duty3)+"\r");
       }
      else {
        Serial.println();
        Serial.println("Client not recognized.");
      }
     stat_info = STAILQ_NEXT(stat_info, next); 
    }
  }
}

Here's my client code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

char ssid[] = "NodeMCU_js";
char password[] = "12345678";

IPAddress server(192,168,4,1);       // the fix IP address of the server

WiFiClient client;                // Creates a client that can connect to to a specified internet IP address and port as defined in client.connect()

void setup() {
  pinMode(D4,OUTPUT);

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);

  Serial.begin(9600);                 //Serial connection
  WiFi.begin(ssid,password);          //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {         //Wait for the WiFI connection completion
    delay(500);
    Serial.println("Connecting...");
  }
  Serial.println("Connected.");
  client.connect(server,80);
}

void loop() {
  if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
    String answer1 = client.readStringUntil('\r');
    Serial.println();
    Serial.print("From Server(1): ");
    Serial.print(answer1);
    client.flush();

    String answer2 = client.readStringUntil('\r');
    Serial.println();
    Serial.print("From Server(2): ");
    Serial.print(answer2);
    client.flush();

    String answer3 = client.readStringUntil('\r');
    Serial.println();
    Serial.print("From Server(3): ");
    Serial.print(answer3);
    client.flush();

  }
  else{
    Serial.println("Error in WiFi connection");   
  } 
}    

It would be helpful if I could get the correct way of doing this.

Thanks in advance.

9
  • println sends \r\n. read until \n. flush() waits until data are send. you do not send data from client. why do you use flush?
    – Juraj
    Commented Jun 23, 2018 at 9:50
  • flush() is used for waiting till transmission is completed. It can be used at server or client. Commented Jun 23, 2018 at 9:55
  • you do not transmit from client. only receive.
    – Juraj
    Commented Jun 23, 2018 at 9:57
  • I am receiving at the client, as per my client code. Is it recommended to remove flush() at the client? Commented Jun 23, 2018 at 10:00
  • So I removed the flush() from the client sie and used it at the server. The problem still persists. Commented Jun 23, 2018 at 10:09

1 Answer 1

0

from server you send at the end of line \r\r\n. first \r is in the string and \r\n is added by println. then at the client you read until \r the first line, the you read an empty line until next \r.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.