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_server.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 = 7002;
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 server on connection #" + String(client_num));
89 Serial.println("\tReceived: " + String(recvData));
90 // TODO add some functions to react to the received data
91 char respData[len+11]; // Prepare buffer for the response data
92 char *respHdr = "Received: ";
93 memcpy (respData, respHdr, 10);
94 memcpy(&respData[10], recvData, len); // Copy received data into the buffer
95 respData[len+10] = '\0';
96 Serial.println("\tSend response: " + String(respData));
97 tcp.send(respData);
98 } else if (resp_type == USERCB_RECO) {
99 if (len != -11) { // ignore "not connected" error, handled in USERCB_CONN
100 Serial.print("\tConnection problem: ");
101 Serial.println(getErrTxt(len));
102 }
103 } else if (resp_type == USERCB_CONN) {
104 if (len == 0) {
105 Serial.println("\tDisconnected");
106 } else {
107 Serial.println("\tConnected");
108 }
109 } else {
110 Serial.println("Received invalid response type");
111 }
112}
113
114// Callback made from esp-link to notify of wifi status changes
115// Here we print something out and set a global flag
116void wifiCb(void *response) {
117 ELClientResponse *res = (ELClientResponse*)response;
118 if (res->argc() == 1) {
119 uint8_t status;
120 res->popArg(&status, 1);
121
122 if(status == STATION_GOT_IP) {
123 Serial.println(F("WIFI CONNECTED"));
124 wifiConnected = true;
125 } else {
126 Serial.print(F("WIFI NOT READY: "));
127 Serial.println(status);
128 wifiConnected = false;
129 }
130 }
131}
132
133void setup() {
134 Serial.begin(115200);
135//###########################################################
136// For ARDUINO UNO WIFI with I2C to serial chip!
137//###########################################################
138 i2cuart.begin(9600);
139
140 Serial.println(F("EL-Client starting!"));
141
142 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
143 // callbacks to the wifi status change callback. The callback gets called with the initial
144 // status right after Sync() below completes.
145 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
146 bool ok;
147 do {
148 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
149 if (!ok) Serial.println(F("EL-Client sync failed!"));
150 } while(!ok);
151 Serial.println(F("EL-Client synced!"));
152
153 // Wit for WiFi to be connected.
154 esp.GetWifiStatus();
155 ELClientPacket *packet;
156 Serial.print(F("Waiting for WiFi "));
157 if ((packet=esp.WaitReturn()) != NULL) {
158 Serial.print(F("."));
159 Serial.println(packet->value);
160 }
161 Serial.println("");
162
163 // Set up the TCP socket server to wait for a client on port <>,
164 // it just sets-up stuff on the esp-link side and waits until a client sends some data
165 tcpConnNum = tcp.begin(tcpServer, tcpPort, SOCKET_TCP_SERVER, tcpCb); // SOCKET_SERVER ==> accept connections
166 if (tcpConnNum < 0) {
167 Serial.println(F("TCP socket setup failed, try again in 10 seconds after reboot"));
168 delay(10000);
169 asm volatile (" jmp 0");
170 } else {
171 Serial.println(String(tcpServer)+":"+String(tcpPort)+" is served over connection number # = "+String(tcpConnNum));
172 }
173 Serial.println(F("EL-TCP ready"));
174}
175
176void loop() {
177 // process any callbacks coming from esp_link
178 esp.Process();
179}
Definitions for ELClientSocket.
#define USERCB_RECV
#define SOCKET_TCP_SERVER
#define USERCB_SENT
#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()