You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.7 KiB
94 lines
2.7 KiB
/** |
|
* Simple example to demo the El-Client REST calls |
|
*/ |
|
|
|
#include <ELClient.h> |
|
#include <ELClientRest.h> |
|
|
|
// Initialize a connection to esp-link using the normal hardware serial port both for |
|
// SLIP and for debug messages. |
|
ELClient esp(&Serial, &Serial); |
|
|
|
// Initialize a REST client on the connection to esp-link |
|
ELClientRest rest(&esp); |
|
|
|
boolean wifiConnected = false; |
|
|
|
// Callback made from esp-link to notify of wifi status changes |
|
// Here we print something out and set a global flag |
|
void wifiCb(void *response) { |
|
ELClientResponse *res = (ELClientResponse*)response; |
|
if (res->argc() == 1) { |
|
uint8_t status; |
|
res->popArg(&status, 1); |
|
|
|
if(status == STATION_GOT_IP) { |
|
Serial.println("WIFI CONNECTED"); |
|
wifiConnected = true; |
|
} else { |
|
Serial.print("WIFI NOT READY: "); |
|
Serial.println(status); |
|
wifiConnected = false; |
|
} |
|
} |
|
} |
|
|
|
void setup() { |
|
Serial.begin(115200); // the baud rate here needs to match the esp-link config |
|
Serial.println("EL-Client starting!"); |
|
|
|
// Sync-up with esp-link, this is required at the start of any sketch and initializes the |
|
// callbacks to the wifi status change callback. The callback gets called with the initial |
|
// status right after Sync() below completes. |
|
esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) |
|
bool ok; |
|
do { |
|
ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds |
|
if (!ok) Serial.println("EL-Client sync failed!"); |
|
} while(!ok); |
|
Serial.println("EL-Client synced!"); |
|
|
|
// Get immediate wifi status info for demo purposes. This is not normally used because the |
|
// wifi status callback registered above gets called immediately. |
|
esp.GetWifiStatus(); |
|
ELClientPacket *packet; |
|
if ((packet=esp.WaitReturn()) != NULL) { |
|
Serial.print("Wifi status: "); |
|
Serial.println(packet->value); |
|
} |
|
|
|
// Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server, |
|
// it just sets-up stuff on the esp-link side |
|
int err = rest.begin("www.timeapi.org"); |
|
if (err != 0) { |
|
Serial.print("REST begin failed: "); |
|
Serial.println(err); |
|
while(1) ; |
|
} |
|
Serial.println("EL-REST ready"); |
|
} |
|
|
|
#define BUFLEN 266 |
|
|
|
void loop() { |
|
// process any callbacks coming from esp_link |
|
esp.Process(); |
|
|
|
// if we're connected make an HTTP request |
|
if(wifiConnected) { |
|
// Request /utc/now from the previously set-up server |
|
rest.get("/utc/now"); |
|
|
|
char response[BUFLEN]; |
|
memset(response, 0, BUFLEN); |
|
uint16_t code = rest.waitResponse(response, BUFLEN); |
|
if(code == HTTP_STATUS_OK){ |
|
Serial.println("ARDUINO: GET successful:"); |
|
Serial.println(response); |
|
} else { |
|
Serial.print("ARDUINO: GET failed: "); |
|
Serial.println(code); |
|
} |
|
delay(1000); |
|
} |
|
}
|
|
|