ESP8266 with WiFiclient library to send data through WiFiclient.write()

I'm using a custom handshake protocol on my Socketserver and I want to connect a esp8266 as a client to my socketserver. I'm not familiar with WiFiclient library. My requirement is, I want to send a struct(which having bytes, int, and char values) data through WiFiclient.write(buff, length);(or using some other function) as byte array and then I have to receive a byte array by WiFiclient.read(); (or using some other function) and have to compare with the previous sent byte array (struct value) to go to the next step. Please give me some babysteps to continue. Thank you .

TNIRO1: I'm using a custom handshake protocol on my Socketserver and I want to connect a esp8266 as a client to my socketserver.

[....]

Please give me some babysteps to continue. Thank you .

It would be a big help if you would post your existing program and give details of what it actually does and what you want it to do that is different.

...R

Sorry for that:

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

WiFiClient client;

char Host[]    = "10.20.4.5";  // server ip
uint16_t Port  = 7000;         // server port



//# HANDSHAKE PROTOCOL  Defining variables#//

#define VERSION 4         // 1byte
#define Length  6         // 2bytes
#define Length_SET  4     // 2bytes

//Protocols
#define ModeBus   0       // 1byte
#define MyCloud    1       // 1byte

//Type Message
#define Client_Start  0   // 1byte
#define Server_Start  1   // 1byte

//Options
#define Options  0       // 1byte

#define DEVICE_ACK                  0x0062      // 2bytes
#define SET_ACT                     0x0063      // 2bytes
#define DEVICE_HELLO                0x0064      // 2bytes
#define SET_PROTOCOL                0x0065      // 2bytes
#define DEVICE_REG                  0x0066      // 2bytes
#define SET_DEVICE                  0x0067      // 2bytes
#define DEVICE_LAN_STATUS           0x0068      // 2bytes
#define SET_STATUS_LAN              0x0069      // 2bytes
#define Protocol1                       10          // 1byte  (DEC)
#define Protocol2                       11          // 1byte  (DEC)
#define Protocol3                       12          // 1byte  (DEC)
#define Protocol4                       13          // 1byte  (DEC)
#define DEVICE_STATUS_OK              0x00        // 1bytes
#define DEVICE_STATUS_FAIL            0x01        // 1bytes
#define DEVICE_ERROR_1                0x00        // 1bytes
#define DEVICE_ERROR_2                0x01        // 1bytes

// Message variables
const char *Alias = "18e508cbed9fe45cb9d0a5fe81112da5";         // 32bytes
const char *Pass  = "12345678";                                 // 16bytes
String  MAC   = WiFi.macAddress();                              // 6bytes
String  CODE   = "000806";                                      // 6bytes
String  Type  = "000099";                                       // 6bytes
String  Rev   = "001";                                          // 6bytes

void Handshake();


//Server connect to WiFi Network
const char *ssid = "MY_WIFI_SSID"; 
const char *password = "MY_PASSWORD";


//Handshake Header
#define Header_SIZE 6
typedef struct Header_t
{
  byte  Version_t;                      // 1 byte
  short Length_t;                       // 2 bytes
  byte  ProtocolType_t;                 // 1 byte
  byte  MessageType_t;                  // 1 byte
  byte  Options_t;                      // 1 byte
};

Header_t Header = {VERSION, Length, MyCloud, Client_Start, Options};
union Dev_Header
{
  Header_t Header;
  byte lengthBufferHeader[sizeof(Header_t)];
};


union Dev_Header Headerunion;


//Handshake Hellow
#define Hello_SIZE 12
typedef struct Hello_t
{
  Header_t HeaderHello;
  byte DEVICE_HELLO_t;                // 2 byte
  byte Protocol1_t;                   // 1 bytes
  byte Protocol2_t;                   // 1 byte
  byte Protocol3_t;                   // 1 byte
  byte Extra_t;                       // 1 byte
};
Hello_t Hello = {VERSION, Length, MyCloud, Client_Start, Options, DEVICE_HELLO, Protocol1, Protocol2, Protocol3, Protocol4};
union DEVICE_Hello
{
  Hello_t Hello;
  byte* lengthBuffer[sizeof(Hello_t)];
};
union DEVICE_Hello Hellounion;

// expected{ 04 06 00 01 00 00 64 00 0a 0b 0c 0d}


//Handshake SET_PROTOCOL_ok
#define SET_PROTOCOL_ok_SIZE 10
typedef struct SET_PROTOCOL_ok
{
  Header_t HeaderSetProtocolOk;
  byte SET_PROTOCOL_t;                  // 2 byte
  byte DEVICE_STATUS_OK_t;              // 1 bytes
  byte DEVICE_ERROR_1_t;                // 1 byte
};
SET_PROTOCOL_ok Protocolok = {VERSION, Length_SET, MyCloud, 0x00, 0x00, SET_PROTOCOL, DEVICE_STATUS_OK, DEVICE_ERROR_1};
union PROTOCOL_ok
{
  SET_PROTOCOL_ok Protocolok;
  byte lengthBufferProtocolOk[sizeof(SET_PROTOCOL_ok)];
};
union PROTOCOL_ok ProtocolOkunion;

// expected values are { 04 04 00 01 00 00 65 00 0 0}


//Handshake SET_PROTOCOL_ERROR1
#define SET_PROTOCOL_ERROR1_SIZE 10
typedef struct SET_PROTOCOL_ERROR1
{
  Header_t HeaderSetProtocolError1;
  byte SET_PROTOCOL_t;                     // 2 byte
  byte DEVICE_STATUS_FAIL_t;               // 1 bytes
  byte DEVICE_ERROR_1_t;                   // 1 byte
};
SET_PROTOCOL_ERROR1 ProtocolERROR1 = {VERSION, Length_SET, MyCloud, 0x00, 0x00, SET_PROTOCOL, DEVICE_STATUS_FAIL, DEVICE_ERROR_1};
union PROTOCOL_ERROR1
{
  SET_PROTOCOL_ERROR1 ProtocolERROR1;
  byte lengthBufferProtocolERROR1[sizeof(SET_PROTOCOL_ERROR1)];
};
union PROTOCOL_ERROR1 ProtocolERROR1union;

// expected values are { 04 04 00 01 00 00 65 00 1 0}

//Handshake SET_PROTOCOL_ERROR2
#define SET_PROTOCOL_ERROR2_SIZE 10
typedef struct SET_PROTOCOL_ERROR2
{
  Header_t HeaderSetProtocolError2;
  byte SET_PROTOCOL_t;                     // 2 byte
  byte DEVICE_STATUS_FAIL_t;               // 1 bytes
  byte DEVICE_ERROR_2_t;                   // 1 byte
};
SET_PROTOCOL_ERROR2 ProtocolERROR2 = {VERSION, Length_SET, MyCloud, 0x00, 0x00, SET_PROTOCOL, DEVICE_STATUS_FAIL, DEVICE_ERROR_2};
union PROTOCOL_ERROR2
{
  SET_PROTOCOL_ERROR2 ProtocolERROR2;
  byte lengthBufferProtocolERROR2[sizeof(SET_PROTOCOL_ERROR2)];
};
union PROTOCOL_ERROR2 ProtocolERROR2union;

// expected values are { 04 04 00 01 00 00 65 00 1 1}

//Handshake Register
#define Register_SIZE 77
typedef struct Register_t
{
  Header_t Header2;
  byte        DEVICE_REG_t;               // 2 byte
  const char *Alias_t;                    // 32 bytes
  const char *Pass_t;                     // 16 byte
  String      MAC_t;                      // 6 byte
  String      CODE_t;                     // 6 byte
  String      TYPE_t;                     // 6 byte
  String      Revision_t;                 // 3 byte

};
Register_t Register = {VERSION, Length, MyCloud, Client_Start, Options, DEVICE_REG, Alias, Pass, MAC, CODE, Type, Rev};
typedef union DEVICE_REG_t {
  Register_t Register;
  byte lengthBufferRegister[sizeof(Register_t)];
};
typedef union DEVICE_REG_t Regunion;


//Handshake SET_DEVICE
#define SET_DEVICE_SIZE 10
typedef struct SET_DEVICE_t
{
  Header_t HeaderSET_DEVICE;
  byte SET_DEVICE_t;                 // 2 byte
  byte Extra1_t;                     // 1 byte
  byte Extra2_t;                     // 1 byte
};
SET_DEVICE_t SetDevice = {VERSION, Length_SET, MyCloud, 0x00, 0x00, SET_DEVICE, 0x00, 0x00};
typedef union SET_Device_t {
  SET_DEVICE_t SetDevice;
  byte lengthBufferSetDevice[sizeof(SET_DEVICE_t)];
};
typedef union SET_Device_t SetDeviceunion;


void setup()
{

  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password); //Connect to wifi

 
  Serial.println("Connecting to Wifi");

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

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


  Handshake();

}


void loop()
{

}


//Handshake Protocol
void Handshake()
{
  if (client.connect(Host, Port)) //Try to connect to TCP Server
  {
    if (client.connected())
    {
      Serial.print("Connected to:");
      Serial.print(Host);
      Serial.print(":");
      Serial.println(Port);
      Serial.print("MAC Address:");
      Serial.println(MAC);
      byte getValue1[0];
      byte getValue2[0];

      client.write((byte*)&Hellounion.lengthBuffer, sizeof(Hello_t));
      // expected value to be sent is
      //{ 04 04 00 01 00 00 65 00 0 0}
      
      client.read(getValue1, SET_PROTOCOL_ok_SIZE);
      // expected value to get if the above sequance is correct
      // { 04 04 00 01 00 00 65 00 0 0}
    
      while (client.available() > 0) {
        Serial.println("Client Available");

  
    //below this aria is not working but I want something like this.
      // to compare values that get from client.read to SET_ProtocolOk struct
      
        if (getValue1 == SET_PROTOCOL_ok )
        {
          Serial.println("Accepted");
          client.write((byte*)&Regunion.lengthBufferRegister, sizeof(Register_SIZE));
          client.read(getValue2, SET_DEVICE_SIZE);
        }
        else if (getValue1 == SET_PROTOCOL_ERROR1)
        {
          Serial.println("Not Accepted and Error: ");
          Serial.println("BAD NUM PROTOCOL");
        }
        else
        {
          Serial.println("Not Accepted and Error: ");
          Serial.println("BAD PROTOCOL");
        }

        if (getValue2 == SET_DEVICE_t)
        {
          Serial.println("Device is Connected");
        }
        else
        {
          Serial.println("Device is not Connected");
        }

      }

    }

  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting ... ");
    client.stop();
    Serial.println("client stop");
  }

}

Testing_HandshakeProtocol_ESP8266.ino (12.1 KB)

TNIRO1:
Sorry for that:

There seem to be two separate programs in your Reply #2 and you have not explained what the are, what they actually do or what you want them to do that is different.

Trying to figure out what a program does by reading the code is very time-consuming.

…R

some example https://github.com/jandrassy/Regulator/blob/c7fc685e29132b8022908b6da5249f600d1ee54f/Regulator/Modbus.ino#L182

some comments: - after connect() returns true it is connected. no need to test it with connected() - read() only reads out the RX buffer, it doesn't wait for data. readBytes does