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.h
Go to the documentation of this file.
1
10#ifndef _EL_CLIENT_SOCKET_H_
11#define _EL_CLIENT_SOCKET_H_
12
13#include <Arduino.h>
14#include "FP.h"
15#include "ELClient.h"
16
17#define DEFAULT_SOCKET_TIMEOUT 5000
19#define USERCB_SENT 0
20#define USERCB_RECV 1
21#define USERCB_RECO 2
22#define USERCB_CONN 3
24// Socket mode definitions
25#define SOCKET_TCP_CLIENT 0
26#define SOCKET_TCP_CLIENT_LISTEN 1
27#define SOCKET_TCP_SERVER 2
28#define SOCKET_UDP 3
30// Enable/disable debug output. If defined enables the debug output on Serial port
31//#define DEBUG_EN /**< Enable/disable debug output */
32
33// The ELClientSocket class sends data over a simple Socket connection to a remote server. Each instance
34// is used to communicate with one server and multiple instances can be created to send
35// to multiple servers.
36// The ELClientSocket class does not support concurrent requests to the same server because
37// only a single response can be recevied at a time and the responses of the two requests
38// may arrive out of order.
39// A major limitation of the Socket class is that it does not wait for the response data.
41 public:
43
44 // Initialize communication to a remote server, this communicates with esp-link but does not
45 // open a connection to the remote server. Host may be a hostname or an IP address.
46 // Port needs to be defined different from usual HTTP/HTTPS/FTP/SSH ports
47 // sock_mode defines whether the socket act as a client (with or without receiver) or as a server
48 // Returns 0 if the set-up is
49 // successful, returns a negative error code if it failed.
50 // Optional a pointer to a callback function be added. The callback function will be called after data is sent out,
51 // after data was received or when an error occured. See example code port how to use it.
52 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);
53
54 // Send data to the remote server. The data must be null-terminated
55 void send(const char* data);
56
57 // Send data to the remote server.
58 void send(const char* data, int len);
59
60 // Retrieve the response from the remote server, returns the number of send or received bytes, 0 if no
61 // response (may need to wait longer)
62 // !!! UDP doesn't check if the data was received or if the receiver IP/socket is available !!! You need to implement your own
63 // error control!
64 uint16_t getResponse(uint8_t *resp_type, uint8_t *client_num, char* data, uint16_t maxLen);
65
66 // Wait for the response from the remote server, returns the length of received data or 0 if no
67 // response (timeout occurred)
68 // Blocks the Arduino code for 5 seconds! not recommended to use. See code examples how to use the callback function instead
69 uint16_t waitResponse(uint8_t *resp_type, uint8_t *client_num, char* data, uint16_t maxLen, uint32_t timeout=DEFAULT_SOCKET_TIMEOUT);
70
73 private:
74 ELClient *_elc;
75 void socketCallback(void* resp);
76 FP<void, void*> socketCb;
139 typedef void (* _userCallback)(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data);
140 _userCallback _userCb;
142 bool _hasUserCb = false;
143 int16_t _status;
144 uint16_t _len;
145 char *_data;
146 uint8_t _resp_type;
147 uint8_t _client_num;
148};
149#endif // _EL_CLIENT_SOCKET_H_
#define DEFAULT_SOCKET_TIMEOUT
Definitions for ELClient.
Core Utility - Templated Function Pointer Class.
uint16_t getResponse(uint8_t *resp_type, uint8_t *client_num, char *data, uint16_t maxLen)
Retrieve response.
void send(const char *data)
Send null-terminated data to the remote server.
int32_t remote_instance
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.
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.
API abstraction for a Function Pointers.
Definition FP.h:136