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
rest.ino
Go to the documentation of this file.
1
5#include <ELClient.h>
6#include <ELClientRest.h>
7
8// Initialize a connection to esp-link using the normal hardware serial port both for
9// SLIP and for debug messages.
11
12// Initialize a REST client on the connection to esp-link
14
15boolean wifiConnected = false;
16
17// Callback made from esp-link to notify of wifi status changes
18// Here we print something out and set a global flag
19void wifiCb(void *response) {
20 ELClientResponse *res = (ELClientResponse*)response;
21 if (res->argc() == 1) {
22 uint8_t status;
23 res->popArg(&status, 1);
24
25 if(status == STATION_GOT_IP) {
26 Serial.println("WIFI CONNECTED");
27 wifiConnected = true;
28 } else {
29 Serial.print("WIFI NOT READY: ");
30 Serial.println(status);
31 wifiConnected = false;
32 }
33 }
34}
35
36void setup() {
37 Serial.begin(115200); // the baud rate here needs to match the esp-link config
38 Serial.println("EL-Client starting!");
39
40 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
41 // callbacks to the wifi status change callback. The callback gets called with the initial
42 // status right after Sync() below completes.
43 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
44 bool ok;
45 do {
46 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
47 if (!ok) Serial.println("EL-Client sync failed!");
48 } while(!ok);
49 Serial.println("EL-Client synced!");
50
51 // Get immediate wifi status info for demo purposes. This is not normally used because the
52 // wifi status callback registered above gets called immediately.
53 esp.GetWifiStatus();
54 ELClientPacket *packet;
55 if ((packet=esp.WaitReturn()) != NULL) {
56 Serial.print("Wifi status: ");
57 Serial.println(packet->value);
58 }
59
60 // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server,
61 // it just sets-up stuff on the esp-link side
62 int err = rest.begin("www.timeapi.org");
63 if (err != 0) {
64 Serial.print("REST begin failed: ");
65 Serial.println(err);
66 while(1) ;
67 }
68 Serial.println("EL-REST ready");
69}
70
71#define BUFLEN 266
72
73void loop() {
74 // process any callbacks coming from esp_link
75 esp.Process();
76
77 // if we're connected make an HTTP request
78 if(wifiConnected) {
79 // Request /utc/now from the previously set-up server
80 rest.get("/utc/now");
81
82 char response[BUFLEN];
83 memset(response, 0, BUFLEN);
84 uint16_t code = rest.waitResponse(response, BUFLEN);
85 if(code == HTTP_STATUS_OK){
86 Serial.println("ARDUINO: GET successful:");
87 Serial.println(response);
88 } else {
89 Serial.print("ARDUINO: GET failed: ");
90 Serial.println(code);
91 }
92 delay(1000);
93 }
94}
Definitions for ELClientRes.
@ HTTP_STATUS_OK
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.
void get(const char *path, const char *data=NULL)
Send GET request to REST server.
ELClientRest rest & esp
Definition rest.ino:13
void wifiCb(void *response)
Definition rest.ino:19
void setup()
Definition rest.ino:36
boolean wifiConnected
Definition rest.ino:15
ELClient esp & Serial
Definition rest.ino:10
#define BUFLEN
Definition rest.ino:71
void loop()
Definition rest.ino:73
uint32_t value