esp8266 – To send data perpetually from multiple clients to server using nodeMCU

0
32

I am working on sending data from multiple clients to a server, all of which are nodeMCUs. I have been able to send data for a single iteration. Trouble arises when I want it to send data repeatedly so that if I update my client data, this new data should be reflected at the server as well.

Since I am using multiple clients, I have employed MAC addresses as the means of identifying the clients. This way, the signals received from a particular client are given as output to a particular pin.

To send data perpetually, I tried to use an infinite loop: while(true), but it reads the first connected client even on recognizing the second client.

Here’s my server code:

#include <ESP8266WiFi.h>
#define Max_Clients 5

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

WiFiServer server(80);                                        //Initialize the server on Port 80
WiFiClient *clients[Max_Clients] = {NULL};                    //Initializing client(s)
String inputs [Max_Clients] ={""};                            //Initializing to store the string received from clients

int32_t freq = 10000;

IPAddress remoteIP();

void setup() {
  pinMode(D1,OUTPUT);                               //Intializing pins for showing output and input
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
  pinMode(A0,INPUT);

  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 myIP = WiFi.softAPIP();       //Obtain the IP of the Server
  Serial.println("Server IP is: ");                 //Print the IP to the monitor window 
  Serial.println(myIP);
  server.begin();                                   //Start the HTTP Server
}

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

  unsigned char number_client;
  struct station_info *stat_info;
  struct ip_addr *IPaddress;
  IPAddress address;

  int k=0;

  if (client) {  
    Serial.println("A new Client has connected :)");

   for (int i=0 ; i<Max_Clients ; ++i){              //Acknowledges and sets up new clients
    if (NULL == clients[i]){
      Serial.println(i);
      clients[i] = new WiFiClient(client);          //nothing in clients[i] means it is a new client
      break;
    }
   }

   while (true){ 
    delay(3000);

    number_client = wifi_softap_get_station_num();
    stat_info = wifi_softap_get_station_info();

    Serial.println();
    Serial.print("Total connected clients are = ");
    Serial.print(number_client);
    Serial.println();

    while (stat_info != NULL){
      for(int i=0 ; i<number_client ; ++i){                           //For available clients
        if (NULL != clients[i]){

          IPaddress = &stat_info->ip;
          address = IPaddress->addr;

          Serial.println();
          Serial.print("Client = ");
          Serial.print(i+1);
          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){
            Serial.println("Compared MAC address successfully - 1 :)");
            String request1 = clients[i]->readStringUntil('r');
            Serial.print("Client 1 asks: ");
            Serial.print(request1);
            inputs[i] = request1;
            int duty1 = request1.toInt();
            analogWrite(D1,duty1);
            clients[i]->flush();
            k++;
            clients[i]->print("From Central Server to Client 1 - 150r");
          }

          if (stat_info->bssid[0] == 0xDC && stat_info->bssid[1] == 0x4F && stat_info->bssid[2] == 0x22 && stat_info->bssid[3] == 0x18 && stat_info->bssid[4] == 0x1F && stat_info->bssid[5] == 0x9D){
            Serial.println("Compared MAC address successfully - 2 :)");
            String request2 = clients[i]->readStringUntil('r');
            Serial.print("Client 2 asks: ");
            Serial.print(request2);
            inputs[i] = request2;
            int duty2 = request2.toInt();
            analogWrite(D2,duty2);
            clients[i]->flush();
            clients[i]->print("From Central Server to Client 2 - 200r");
            k++;
            }

          if (stat_info->bssid[0] == 0xDC && stat_info->bssid[1] == 0x4F && stat_info->bssid[2] == 0x22 && stat_info->bssid[3] == 0x18 && stat_info->bssid[4] == 0xB && stat_info->bssid[5] == 0x71){
            Serial.println("Compared MAC address successfully - 3 :)");
            String request3 = clients[i]->readStringUntil('r');
            Serial.print("Client 3 asks: ");
            Serial.print(request3);
            inputs[i] = request3;
            int duty3 = request3.toInt();
            analogWrite(D3,duty3);
            clients[i]->flush();
            k++;
            clients[i]->print("From Central Server to Client 3 - 250r");
          }

        if (k == 0){
            Serial.println();
            Serial.println("Client not recognised.");
          }  

          stat_info = STAILQ_NEXT(stat_info, next);
          Serial.println();
        }
       }
      }
     }
    }
   }     //void loop closure

Here’s my client code:

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

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

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

IPAddress server(192,168,4,1);

void setup() {
  pinMode(A0,INPUT);
  pinMode(D6,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);   // Connection to the server
}

void loop() {
  if(WiFi.status() == WL_CONNECTED){   //Check WiFi connection status
    while(true){
      int duty = analogRead(A0);
      client.print(String(duty)+"r");  //Client's message to the server
      String answer = client.readStringUntil('r');         //Reading response from the server
      Serial.print("Central Server: 200");             //Printing the response
      Serial.print(answer);
     }
   }
   else{
     Serial.println("Error in WiFi connection");   
   }
}

Kindly suggest a solution. Thanks in advance.

LEAVE A REPLY

Please enter your comment!
Please enter your name here