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
tcp_client_resp.ino
Go to the documentation of this file.
1
7#include <ELClient.h>
8#include <ELClientSocket.h>
9
10// IP address for this demo is a local IP.
11// Replace it with the IP address where you have a TCP socket server running
12char * const tcpServer PROGMEM = "192.168.0.102";
13// Port for this demo is the port used by the TCP socket server.
14// Replace it with the port that your TCP socket server is listening to
15uint16_t const tcpPort PROGMEM = 7001;
16
17//###########################################################
18// For ARDUINO UNO WIFI with I2C to serial chip!
19//###########################################################
20// Serial port to ESP8266
21#include <SC16IS750.h>
22SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_AA);
23
24// Initialize a connection to esp-link using the I2Cuart chip of the Arduino Uno WiFi board for
25// SLIP messages.
27
28//###########################################################
29// For boards using the hardware serial port!
30//###########################################################
31// Initialize a connection to esp-link using the normal hardware serial port both for
32// SLIP and for debug messages.
33//ELClient esp(&Serial, &Serial);
34
35// Initialize a TCP socket client on the connection to esp-link
37// Connection number for tcp
39
40// Timer value to send out data
41uint32_t wait;
42// Time to wait between sending out data
43uint32_t waitTime;
44// Flag for wifi connection
45boolean wifiConnected = false;
46
47// Parse error codes and returns error message as char *
48// Definitions from error values from espconn.h (Espressif SDK)
49// #define ESPCONN_OK 0 /**< No error, everything OK. */
50// #define ESPCONN_MEM -1 /**< Out of memory. */
51// #define ESPCONN_TIMEOUT -3 /**< Timeout. */
52// #define ESPCONN_RTE -4 /**< Routing problem. */
53// #define ESPCONN_INPROGRESS -5 /**< Operation in progress. */
54// #define ESPCONN_MAXNUM -7 /**< Total number exceeds the maximum limitation. */
55
56// #define ESPCONN_ABRT -8 /**< Connection aborted. */
57// #define ESPCONN_RST -9 /**< Connection reset. */
58// #define ESPCONN_CLSD -10 /**< Connection closed. */
59// #define ESPCONN_CONN -11 /**< Not connected. */
60
61// #define ESPCONN_ARG -12 /**< Illegal argument. */
62// #define ESPCONN_IF -14 /**< UDP send error. */
63// #define ESPCONN_ISCONN -15 /**< Already connected. */
64
65char* const errTxt[] PROGMEM = {"No error, everything OK.","Out of memory.","Unknown code.","Timeout.","Routing problem.","Operation in progress.",
66 "Unknown code.","Total number exceeds the maximum limitation.","Connection aborted.","Connection reset.","Connection closed.",
67 "Not connected.","Illegal argument.","Unknown code.","UDP send error.","Already connected."};
68char * getErrTxt(int16_t commError) {
69 commError = commError*-1;
70 if (commError <= 15) {
71 return (char *) pgm_read_word (&errTxt[commError]);
72 } else {
73 return (char *) pgm_read_word (&errTxt[2]); // Unknown code
74 }
75}
76
77// Callback for TCP socket, called if data was sent or received
78// Receives socket client number, can be reused for all initialized TCP socket connections
79void tcpCb(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data) {
80 Serial.println("tcpCb connection #"+String(client_num));
81 if (resp_type == USERCB_SENT) {
82 Serial.println("\tSent " + String(len) + " bytes over client#" + String(client_num));
83 } else if (resp_type == USERCB_RECV) {
84 char recvData[len+1]; // Prepare buffer for the received data
85 memcpy(recvData, data, len); // Copy received data into the buffer
86 recvData[len] = '\0'; // Terminate the buffer with 0 for proper printout!
87
88 Serial.println("\tReceived " + String(len) + " bytes over the client on connection #" + String(client_num));
89 Serial.println("\tReceived: " + String(recvData));
90 } else if (resp_type == USERCB_RECO) {
91 if (len != -11) { // ignore "not connected" error, handled in USERCB_CONN
92 Serial.print("\tConnection problem: ");
93 Serial.println(getErrTxt(len));
94 }
95 } else if (resp_type == USERCB_CONN) {
96 if (len == 0) {
97 Serial.println("\tDisconnected");
98 } else {
99 Serial.println("\tConnected");
100 }
101 } else {
102 Serial.println("Received invalid response type");
103 }
104}
105
106// Callback made from esp-link to notify of wifi status changes
107// Here we print something out and set a global flag
108void wifiCb(void *response) {
109 ELClientResponse *res = (ELClientResponse*)response;
110 if (res->argc() == 1) {
111 uint8_t status;
112 res->popArg(&status, 1);
113
114 if(status == STATION_GOT_IP) {
115 Serial.println(F("WIFI CONNECTED"));
116 wifiConnected = true;
117 } else {
118 Serial.print(F("WIFI NOT READY: "));
119 Serial.println(status);
120 wifiConnected = false;
121 }
122 }
123}
124
125void setup() {
126 Serial.begin(115200);
127//###########################################################
128// For ARDUINO UNO WIFI with I2C to serial chip!
129//###########################################################
130 i2cuart.begin(9600);
131
132 Serial.println(F("EL-Client starting!"));
133
134 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
135 // callbacks to the wifi status change callback. The callback gets called with the initial
136 // status right after Sync() below completes.
137 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
138 bool ok;
139 do {
140 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
141 if (!ok) Serial.println(F("EL-Client sync failed!"));
142 } while(!ok);
143 Serial.println(F("EL-Client synced!"));
144
145 // Wit for WiFi to be connected.
146 esp.GetWifiStatus();
147 ELClientPacket *packet;
148 Serial.print(F("Waiting for WiFi "));
149 if ((packet=esp.WaitReturn()) != NULL) {
150 Serial.print(F("."));
151 Serial.println(packet->value);
152 }
153 Serial.println("");
154
155 // Set up the TCP socket client for a connection to <tcpServer> on port <>, this doesn't connect to that server,
156 // it just sets-up stuff on the esp-link side and waits until we send some data
157 tcpConnNum = tcp.begin(tcpServer, tcpPort, SOCKET_TCP_CLIENT_LISTEN, tcpCb); // SOCKET_CLIENT ==> we expect a response
158 if (tcpConnNum < 0) {
159 Serial.println(F("TCP socket setup failed, try again in 10 seconds after reboot"));
160 delay(10000);
161 asm volatile (" jmp 0");
162 } else {
163 Serial.println(String(tcpServer)+":"+String(tcpPort)+" is served over connection number # = "+String(tcpConnNum));
164 }
165
166 Serial.println(F("EL-TCP ready"));
167 wait = millis()+29000; // Start first sending in 1 second
168}
169
170void loop() {
171 // process any callbacks coming from esp_link
172 esp.Process();
173
174 // if we're connected send data over TCP socket
175 if(wifiConnected) {
176 if (millis() - wait > 30000) { // Send some data every 30 seconds
177 wait = millis();
178
179 // Send message to the previously set-up server #1
180 Serial.print(F("Sending message to "));
181 Serial.print(tcpServer);
182 Serial.print(":");
183 Serial.println(tcpPort);
184 tcp.send("Message from Uno WiFi over TCP socket");
185 }
186 } else {
187 // This is just for demo, you can as well just try to reconnect
188 // and setup the connection to esp-link again
189 Serial.println(F("Lost WiFi connection, try to reboot"));
190 delay(10000);
191 asm volatile (" jmp 0");
192 }
193}
Definitions for ELClientSocket.
#define USERCB_RECV
#define USERCB_SENT
#define SOCKET_TCP_CLIENT_LISTEN
#define USERCB_RECO
#define USERCB_CONN
Definitions for ELClient.
@ STATION_GOT_IP
Definition ELClient.h:51
int16_t popArg(void *data, uint16_t maxLen)
Extract an argument from the response packet.
ESP esp & Serial
Definition demo.ino:9
uint32_t value
void tcpCb(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data)
int tcpConnNum
void wifiCb(void *response)
void setup()
char * getErrTxt(int16_t commError)
ELClientSocket tcp & esp
uint32_t waitTime
SC16IS750 i2cuart
boolean wifiConnected
char *const tcpServer PROGMEM
uint32_t wait
void loop()