Failure to send data from client to server in ESP-01 WiFi

362 Views Asked by At

Using the ESP8266WiFi library, I have two ESP-01's/ESP8266's connected over WiFi. It works perfectly when the client sends a request (all non HTML!) to the server (using port 5000 - to prevent any confusion with HTTP, FTP etc.). But I cannot get the client to receive an answer back from the server. Now, in the ESP8266WiFi library (3.0.2) there is a note that server.write() is not implemented, and that I should use server.accept() instead of server.available(); though I did not see any applicable examples using server.accept(), but I see many examples using client.print() so I try to follow those - to no avail, yet. What I am doing is the following: 1. establish connectivity to the WiFi; 2. have the client connect to the server and send two bytes to the server. 3. Do a digital write to a pin of the server-ESP8266.(this toggles a relay, which works fine) 4. write back from server to client that the digital write has been done. On the client side, after writing to the server, I run in a loop for some 10 seconds trying to receive something from the server, which never comes. Then I cycle back to the beginning, and the client asks to toggle the relay again - this runs nicely for hours.

Any insights here on what I should do differently are highly appreciated. I really want to be able to get some acknowledgement back to the client once the server has toggled the relay. Or if someone has a working example with server.accept() - I would try that too.

Client side code:

int pin_value;
uint8_t ip[4];
void setup()
{
    Serial.begin(115200);
    ip[0]=10;
    ip[1]=0;
    ip[2]=0;
    ip[3]=6;
    
    //We connect to the WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    //Wait until connected
    while (WiFi.status() != WL_CONNECTED){
      delay(500);
      Serial.print(".");
      }
  Serial.print("Client - ");
  Serial.println("WiFi connected");
}

void loop(){ 
    //Variable that we will use to connect to the server
    WiFiClient client;
    //if not able to connect, return.
    if (!client.connect(ip, SERVER_PORT)){  return; }

    // We create a buffer to put the send data
    uint8_t buffer[Protocol::BUFFER_SIZE];
    
    //We put the pin number in the buffer
    // whose state we want to send
    buffer[Protocol::PIN] = RELAY;
    //put the current state of the pin in the send buffer
    buffer[Protocol::VALUE] = pin_value;
    //We send the data to the server
    client.write(buffer, Protocol::BUFFER_SIZE);
      
    // try to read the answer from the server for about 10 seconds
    int nr_of_tries = 10000;
     
    while (client.connected()  && nr_of_tries > 0)
      {if (client.available())
        { String line = client.readStringUntil('\n');
          nr_of_tries = 0;
          Serial.print("line= ");
          Serial.println(line);
          }
        else
          {delay(1);
          nr_of_tries=nr_of_tries-1;
          }
      }
   Serial.print("nr of tries= ");
   Serial.println(nr_of_tries);
   Serial.print("connected: ");
   Serial.println(client.connected());
       
   client.flush();
   client.stop();
   Serial.println(" change sent");
   if (pin_value == 0) 
     {pin_value =1;
      Serial.println("Pin_value set to 1");
     }
   else
     {pin_value=0;
     Serial.println("Pin_value set to 0");}
   delay(10000);
}

Server side code:

WiFiServer server(SERVER_PORT);

void setup()
{
  Serial.begin(115200); // must have the same baud rate as the serial monitor
  pinMode(RELAY,OUTPUT);
  digitalWrite(RELAY, LOW);
  
  // Connect to the 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("Server - ");
  Serial.println("WiFi connected");
  // Set this ESP to behave as a WiFi Access Point
  // WiFi.mode(WIFI_AP);
  // set SSID and Password to connect to this ESP
  // WiFi.softAP(SSID, PASSWORD);
  
  // Start the server
  server.begin();
  Serial.println("Server started");
  
  // Output of the IP address
  Serial.print("Use this IP to connect: ");
  Serial.println(WiFi.localIP());
}

void loop()
{
    // Check if there is any client connecting
    WiFiClient client = server.available();
    if (client) 
    {      
        //Serial.println("Client detected");
        //If the client has data he wants to send us
        //check for a second or so as transmission can take time
        int nr_of_tries = 1000;
        while(!client.available() && nr_of_tries > 0)
          { nr_of_tries=nr_of_tries-1;
            delay(1);
          }
        if (client.available())
          {
            // Serial.println(" Client data");
            // create a buffer to put the data to be received
            uint8_t buffer[Protocol::BUFFER_SIZE];
            
            // We put the data sent by the client in the buffer
            // but do not read more than the buffer length.
            int len = client.read(buffer, Protocol::BUFFER_SIZE);
            
            // retrieve which pin number the client sent
            int pinNumber = buffer[Protocol::PIN];
            
            Serial.print("Pin Number: ");
            Serial.println(pinNumber);
            // retrieve the value of this pin
            int value = buffer[Protocol::VALUE];
            Serial.print("Value: ");
            Serial.println(value);
            // Set the pin indicated by the received pin number in output mode
            // but only if the pin is the GPIO0 pin!
            if (pinNumber == RELAY)
              { pinMode(pinNumber, OUTPUT);
                // Set the pin indicated by the received pin number to the passed value
                digitalWrite(pinNumber, value);
              } 
             // tell the client that the relay has been set or reset.
             size_t i;
             if (value == 0) {
               i=server.println("Set");
               Serial.print("i= ");
               Serial.println(i); 
               }
               else {
               i=server.println("Reset");
               Serial.print("i= ");
               Serial.println(i); 
               }
              
             }
        }

        //Close the connection with the client
        //client.stop();
    }

Common definitions:

#include <ESP8266WiFi.h>

const char* ssid = "blablabla";
const char* password = "blublublu";
#define SERVER_PORT 5000
#define RELAY 0

//Protocol that the Server and Client will use to communicate
enum Protocol{
    PIN, // Pin whose state you want to change
    VALUE, // State to which the pin should go (HIGH = 1 or LOW = 0)
    BUFFER_SIZE // The size of our protocol. IMPORTANT: always leave it as the last item of the enum
};
1

There are 1 best solutions below

0
ToniE On

Solved! By changing server.println("Set"); into client.println("Set") and doing the same for the transmission of "Reset" a few lines lower in the server side code it works!