Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #ifndef min
- #define min(x,y) (((x)<(y))?(x):(y))
- #endif
- #ifndef max
- #define max(x,y) (((x)>(y))?(x):(y))
- #endif
- #include <ArduinoJson.h>
- #include <WiFiClientSecure.h>
- const char SSID[] = "***";
- const char PASSWORD[] = "***";
- // Specify your resort
- #define LIFTIE_LOCATION "gunstock"
- // 5 minutes between update checks. The free developer account has a limit
- // on the number of calls so don't go wild.
- #define DELAY_NORMAL (0.5*60*1000)
- // 20 minute delay between updates after an error
- #define DELAY_ERROR (5*60*1000)
- #define LIFTIE "liftie.info"
- // HTTP request
- const char LIFTIE_REQ[] =
- "GET /api/resort/" LIFTIE_LOCATION " HTTP/1.1\r\n"
- "User-Agent: ESP8266/0.1\r\n"
- "Accept: */*\r\n"
- "Host: " LIFTIE "\r\n"
- "Connection: close\r\n"
- "\r\n";
- void setup()
- {
- Serial.begin(115200);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print(F("Connecting to "));
- Serial.println(SSID);
- WiFi.begin(SSID, PASSWORD);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(F("."));
- }
- Serial.println();
- Serial.println(F("WiFi connected"));
- Serial.println(F("IP address: "));
- Serial.println(WiFi.localIP());
- }
- static char respBuf[4096];
- void loop()
- {
- // TODO check for disconnect from AP
- // Open socket to WU server port 80
- Serial.print(F("Connecting to "));
- Serial.println(LIFTIE_REQ);
- // Use WiFiClient class to create TCP connections
- WiFiClient httpclient;
- //WiFiClientSecure httpclient;
- const int httpPort = 80;
- if (!httpclient.connect(LIFTIE_REQ, httpPort)) {
- Serial.println(F("connection failed"));
- delay(DELAY_ERROR);
- return;
- }
- // This will send the http request to the server
- Serial.print(LIFTIE_REQ);
- httpclient.print(LIFTIE_REQ);
- httpclient.flush();
- // Collect http response headers and content from liftie.info
- // HTTP headers are discarded.
- // The content is formatted in JSON and is left in respBuf.
- int respLen = 0;
- bool skip_headers = true;
- while (httpclient.connected() || httpclient.available()) {
- if (skip_headers) {
- String aLine = httpclient.readStringUntil('\n');
- Serial.println(aLine);
- // Blank line denotes end of headers
- if (aLine.length() <= 1) {
- skip_headers = false;
- }
- }
- else {
- int bytesIn;
- bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
- Serial.print(F("bytesIn ")); Serial.println(bytesIn);
- if (bytesIn > 0) {
- respLen += bytesIn;
- if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
- }
- else if (bytesIn < 0) {
- Serial.print(F("read error "));
- Serial.println(bytesIn);
- }
- }
- delay(1);
- }
- httpclient.stop();
- if (respLen >= sizeof(respBuf)) {
- Serial.print(F("respBuf overflow "));
- Serial.println(respLen);
- delay(DELAY_ERROR);
- return;
- }
- // Terminate the C string
- respBuf[respLen++] = '\0';
- Serial.print(F("respLen "));
- Serial.println(respLen);
- //Serial.println(respBuf);
- if (showLifts(respBuf)) {
- delay(DELAY_NORMAL);
- }
- else {
- delay(DELAY_ERROR);
- }
- }
- bool showLifts(char *json)
- {
- StaticJsonBuffer<3*1024> jsonBuffer;
- // Skip characters until first '{' found
- // Ignore chunked length, if present
- char *jsonstart = strchr(json, '{');
- Serial.print(F("jsonstart ")); Serial.println(jsonstart);
- if (jsonstart == NULL) {
- Serial.println(F("JSON data missing"));
- return false;
- }
- json = jsonstart;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement