Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
ELClientSocket Class Reference

#include <ELClientSocket.h>

Public Member Functions

 ELClientSocket (ELClient *e)
 Class to send/receive data.
 
int begin (const char *host, uint16_t port, uint8_t sock_mode, void(*userCb)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data)=0)
 Initialize communication to a remote server.
 
void send (const char *data)
 Send null-terminated data to the remote server.
 
void send (const char *data, int len)
 Send data to the remote server.
 
uint16_t getResponse (uint8_t *resp_type, uint8_t *client_num, char *data, uint16_t maxLen)
 Retrieve response.
 
uint16_t waitResponse (uint8_t *resp_type, uint8_t *client_num, char *data, uint16_t maxLen, uint32_t timeout=DEFAULT_SOCKET_TIMEOUT)
 Wait for the response.
 

Data Fields

int32_t remote_instance
 

Detailed Description

Definition at line 40 of file ELClientSocket.h.

Constructor & Destructor Documentation

◆ ELClientSocket()

ELClientSocket::ELClientSocket ( ELClient e)

Class to send/receive data.

ELClientSocket(ELClient *e)

The ELClientSocket class sends data over a Socket to a remote server or acts as a TCP socket server. Each instance is used to communicate with one server and multiple instances can be created to send to multiple servers. The ELClientSocket class does not support concurrent requests to the same server because only a single response can be recevied at a time and the responses of the two requests may arrive out of order.

Parameters
ePointer to ELClient. Check ELClient API documentation.
Example
REST rest & esp
Definition demo.ino:11

Definition at line 24 of file ELClientSocket.cpp.

Member Function Documentation

◆ begin()

int ELClientSocket::begin ( const char *  host,
uint16_t  port,
uint8_t  sock_mode,
void(*)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data)  userCb = 0 
)

Initialize communication to a remote server.

begin(const char* host, uint16_t port, uint8_t sock_mode, void (*userCb)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data))

Initialize communication to a remote server, this communicates with esp-link but does not open a connection to the remote server.

Parameters
hostHost to be connected. Can be a URL or an IP address in the format of xxx.xxx.xxx.xxx .
portPort to be used to send/receive packets. Port MUST NOT be 80, 23 or 2323, as these ports are already used by EL-CLIENT on the ESP8266
sock_modeSet socket mode to SOCKET_TCP_CLIENT, SOCKET_TCP_CLIENT_LISTEN, SOCKET_TCP_SERVER or SOCKET_UDP
(*userCb)(uint8_tresp_type, uint8_t client_num, uint16_t len, char *data) (optional) Pointer to callback function that is called if data after data has been sent, received or if an error occured
Warning
Port MUST NOT be 80, 23 or 2323, as these ports are already used by EL-CLIENT on the ESP8266. Max 4 connections are supported!
Example1
// Setup a simple client to send data and disconnect after data was sent
socketConnNum = socket.begin(socketServer, socketPort, SOCKET_TCP_CLIENT, socketCb);
#define SOCKET_TCP_CLIENT
Example2
// Setup a client to send data and wait for response from remote server
socketConnNum = socket.begin(socketServer, socketPort, SOCKET_TCP_CLIENT_LISTEN, socketCb);
#define SOCKET_TCP_CLIENT_LISTEN
Example3
// Setup a TCP server and wait for a client to connect
socketConnNum = socket.begin(socketServer, socketPort, SOCKET_TCP_SERVER, socketCb);
#define SOCKET_TCP_SERVER
Example3
// Setup a TCP server and wait for a client to connect
socketConnNum = socket.begin(socketServer, socketPort, SOCKET_UDP, socketCb);
#define SOCKET_UDP

Definition at line 121 of file ELClientSocket.cpp.

Here is the call graph for this function:

◆ getResponse()

uint16_t ELClientSocket::getResponse ( uint8_t *  resp_type,
uint8_t *  client_num,
char *  data,
uint16_t  maxLen 
)

Retrieve response.

getResponse(uint8_t resp_type, uint8_t *client_num, char data, uint16_t maxLen)

Check if a response from the remote server was received, returns the number of send or received bytes, 0 if no response (may need to wait longer)

Parameters
resp_typePointer to response type. Is USERCB_SENT if packet was sent or USERCB_RECV if a packet was received.
client_numPointer to connection number. Can be used to distinguish between different socket clients.
dataPointer to buffer for received packet
maxLenSize of buffer for received packet. If the received packet is larger than the buffer, the received packet will be truncated.
Returns
uint16_t Size of received packet or number of sent bytes or 0 if no response
Example
#define BUFLEN 266
void loop()
{
// process any callbacks coming from esp_link
esp.Process();
// Check if we received a packet or if the last send request has finished
char response[BUFLEN];
memset(response, 0, BUFLEN);
uint8_t resp_type;
uint8_t client_num;
uint16_t len = socket.getResponse(&resp_type, &client_num, response, BUFLEN);
if (len != 0)
{
if (resp_type == USERCB_SENT)
{
Serial.println("Sent "+String(len)+" bytes");
}
else
{
Serial.print("Received packet: ");
for (int i=0; i<len; i++)
{
Serial.print(response[i]);
}
Serial.println("");
}
}
}
#define USERCB_SENT
ESP esp & Serial
Definition demo.ino:9
void loop()
Definition demo.ino:73
#define BUFLEN
Definition rest.ino:71

Definition at line 236 of file ELClientSocket.cpp.

Here is the caller graph for this function:

◆ send() [1/2]

void ELClientSocket::send ( const char *  data)

Send null-terminated data to the remote server.

send(const char* data)

Parameters
dataPointer to SOCKET packet, must be null-terminated
Example
Serial.println("Sending text message to SOCKET server");
socket.send("Message from your Arduino Uno WiFi over TCP socket");

Definition at line 184 of file ELClientSocket.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ send() [2/2]

void ELClientSocket::send ( const char *  data,
int  len 
)

Send data to the remote server.

send(const char* data, int len)

Parameters
dataPointer to SOCKET packet
lenLength of SOCKET packet
Example
Serial.println("Sending JSON array to SOCKET server");
char socketPacket = "{"device":"spm","s":622.02,"c":-165.86}"
socket.send(socketPacket, 39);

Definition at line 160 of file ELClientSocket.cpp.

Here is the call graph for this function:

◆ waitResponse()

uint16_t ELClientSocket::waitResponse ( uint8_t *  resp_type,
uint8_t *  client_num,
char *  data,
uint16_t  maxLen,
uint32_t  timeout = DEFAULT_SOCKET_TIMEOUT 
)

Wait for the response.

waitResponse(uint8_t resp_type, uint8_t *client_num, char data, uint16_t maxLen, uint32_t timeout)

Checks if a response from the remote server has arrived for timeout seconds, returns the number of send or received bytes, 0 if no response (may need to wait longer)

Warning
Blocks the Arduino code for 5 seconds! not recommended to use. Use callback function instead! Received packet is NOT null-terminated
Parameters
resp_typePointer to response type. Is USERCB_SENT if packet was sent or USERCB_RECV if a packet was received.
client_numPointer to connection number. Can be used to distinguish between different socket clients.
dataPointer to buffer for received packet
maxLenSize of buffer for received packet. If the received packet is larger than the buffer, the received packet will be truncated.
timeout(optional) Timout in milli seconds to wait for a response, defaults to 5000ms
Returns
uint16_t Size of received packet or number of sent bytes or 0 if no response
Example
#define BUFLEN 266
bool haveRemoteResponse = true;
void loop()
{
// process any callbacks coming from esp_link
esp.Process();
if (haveRemoteResponse) // If last packet was sent, send a new one
{
Serial.println("Sending JSON array to TCP server");
char socketPacket = "{"device":"spm","s":622.02,"c":-165.86}"
socket.send(socketPacket, 39);
haveRemoteResponse = false;
}
// Check if we received a packet or if the last send request has finished
char response[BUFLEN];
memset(response, 0, BUFLEN);
uint8_t resp_type;
uint8_t client_num;
uint16_t len = socket.waitResponse(&resp_type, &client_num, response, BUFLEN);
if (len != 0)
{
if (resp_type == USERCB_SENT)
{
Serial.println("Sent "+String(len)+" bytes");
}
else
{
Serial.print("Received packet: ");
for (int i=0; i<len; i++)
{
Serial.print(response[i]);
}
Serial.println("");
haveRemoteResponse = true;
}
}
}

Definition at line 306 of file ELClientSocket.cpp.

Here is the call graph for this function:

Field Documentation

◆ remote_instance

int32_t ELClientSocket::remote_instance

Connection number, value can be 0 to 3

Definition at line 71 of file ELClientSocket.h.


The documentation for this class was generated from the following files: