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
udp.ino
Go to the documentation of this file.
1
5#include <ELClient.h>
6#include <ELClientSocket.h>
7
8// IP address for this demo is a local IP.
9// Replace it with the IP address where you have a UDP socket server running
10char * const udpServer PROGMEM = "192.168.0.102"; // Send to single ip address
11char * const udpServer2 PROGMEM = "192.168.0.255"; // Broadcast to given network ip mask
12// Port for this demo is the port used by the UDP socket server.
13// Replace it with the port that your UDP socket server is listening to
14uint16_t const udpPort PROGMEM = 5000;
15uint16_t const udpPort2 PROGMEM = 7000;
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 UDP client on the connection to esp-link
37// Initialize a UDP client on the connection to esp-link
38ELClientSocket udp2(&esp);
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 UDP socket, called if data was sent or received
78// Receives socket client number, can be reused for all initialized UDP socket connections
79// !!! UDP doesn't check if the data was received or if the receiver IP/socket is available !!! You need to implement your own
80// error control!
81void udpCb(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data) {
82 Serial.println("udpCb is called");
83 if (len > 0) { // sending complete (no confirmation that it was received!) or we received something
84 if (resp_type == USERCB_SENT) {
85 Serial.println("\tSent " + String(len) + " bytes over connection #" + String(client_num));
86 } else if (resp_type == USERCB_RECV) {
87 char recvData[len+1]; // Prepare buffer for the received data
88 memcpy(recvData, data, len); // Copy received data into the buffer
89 recvData[len] = '\0'; // Terminate the buffer with 0 for proper printout!
90
91 Serial.println("\tReceived " + String(len) + " bytes over client#" + String(client_num));
92 Serial.println("\tReceived: " + String(recvData));
93 } else {
94 Serial.println("Received invalid response type");
95 }
96 } else if (len < 0) { // negative result means there was a problem
97 Serial.print(F("Send error: "));
98 Serial.println(getErrTxt(len));
99 }
100}
101
102// Callback made from esp-link to notify of wifi status changes
103// Here we print something out and set a global flag
104void wifiCb(void *response) {
105 ELClientResponse *res = (ELClientResponse*)response;
106 if (res->argc() == 1) {
107 uint8_t status;
108 res->popArg(&status, 1);
109
110 if(status == STATION_GOT_IP) {
111 Serial.println(F("WIFI CONNECTED"));
112 wifiConnected = true;
113 } else {
114 Serial.print(F("WIFI NOT READY: "));
115 Serial.println(status);
116 wifiConnected = false;
117 }
118 }
119}
120
121void setup() {
122 Serial.begin(115200);
123 i2cuart.begin(9600);
124 Serial.println(F("EL-Client starting!"));
125
126 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
127 // callbacks to the wifi status change callback. The callback gets called with the initial
128 // status right after Sync() below completes.
129 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
130 bool ok;
131 do {
132 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
133 if (!ok) Serial.println(F("EL-Client sync failed!"));
134 } while(!ok);
135 Serial.println(F("EL-Client synced!"));
136
137 // Get immediate wifi status info for demo purposes. This is not normally used because the
138 // wifi status callback registered above gets called immediately.
139 esp.GetWifiStatus();
140 ELClientPacket *packet;
141 if ((packet=esp.WaitReturn()) != NULL) {
142 Serial.print(F("Wifi status: "));
143 Serial.println(packet->value);
144 }
145
146 // Set up the UDP socket client to send a short message to <udpServer> on port <>, this doesn't connect to that server,
147 // it just sets-up stuff on the esp-link side
148 int err = udp.begin(udpServer, udpPort, SOCKET_UDP, udpCb);
149 if (err < 0) {
150 Serial.print(F("UDP begin failed: "));
151 Serial.println(err);
152 delay(10000);
153 asm volatile (" jmp 0");
154 }
155
156 err = udp2.begin(udpServer2, udpPort2, SOCKET_UDP, udpCb);
157 if (err < 0) {
158 Serial.print(F("UDP2 begin failed: "));
159 Serial.println(err);
160 delay(10000);
161 asm volatile (" jmp 0");
162 }
163
164 Serial.println(F("EL-Client ready!"));
165 wait = millis()+29000; // Start first sending in 1 second
166}
167
168void loop() {
169 // process any callbacks coming from esp_link
170 esp.Process();
171
172 // if we're connected send data over UDP socket
173 if(wifiConnected) {
174 if (millis() - wait > 30000) { // Send some data every 30 seconds
175 wait = millis();
176 // Send message to the previously set-up server #1
177 Serial.print(F("Sending message to "));
178 Serial.println(udpServer);
179 udp.send("Message from your Arduino Uno WiFi over UDP socket");
180
181 // Send message to the previously set-up server #2
182 Serial.print(F("Sending broadcast to "));
183 Serial.println(udpServer2);
184 udp2.send("Broadcast from your Arduino Uno WiFi over UDP 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 connection, try to reboot"));
190 delay(10000);
191 asm volatile (" jmp 0");
192 }
193}
Definitions for ELClientSocket.
#define USERCB_RECV
#define SOCKET_UDP
#define USERCB_SENT
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 wifiCb(void *response)
Definition udp.ino:104
void setup()
Definition udp.ino:121
ELClientSocket udp & esp
Definition udp.ino:36
char * getErrTxt(int16_t commError)
Definition udp.ino:68
uint32_t waitTime
Definition udp.ino:43
SC16IS750 i2cuart
Definition udp.ino:22
boolean wifiConnected
Definition udp.ino:45
char *const udpServer PROGMEM
Definition udp.ino:10
void udpCb(uint8_t resp_type, uint8_t client_num, uint16_t len, char *data)
Definition udp.ino:81
uint32_t wait
Definition udp.ino:41
void loop()
Definition udp.ino:168