Advertisement
doktorinjh

Liftie_v5

Mar 11th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #ifndef min
  3. #define min(x,y) (((x)<(y))?(x):(y))
  4. #endif
  5. #ifndef max
  6. #define max(x,y) (((x)>(y))?(x):(y))
  7. #endif
  8. #include <ArduinoJson.h>
  9. #include <WiFiClientSecure.h>
  10.  
  11. const char SSID[] = "***";
  12. const char PASSWORD[] = "***";
  13.  
  14. // Specify your resort
  15. #define LIFTIE_LOCATION "gunstock"
  16.  
  17. // 5 minutes between update checks. The free developer account has a limit
  18. // on the number of calls so don't go wild.
  19. #define DELAY_NORMAL (0.5*60*1000)
  20. // 20 minute delay between updates after an error
  21. #define DELAY_ERROR (5*60*1000)
  22.  
  23. #define LIFTIE "liftie.info"
  24.  
  25. // HTTP request
  26. const char LIFTIE_REQ[] =
  27. "GET /api/resort/" LIFTIE_LOCATION " HTTP/1.1\r\n"
  28. "User-Agent: ESP8266/0.1\r\n"
  29. "Accept: */*\r\n"
  30. "Host: " LIFTIE "\r\n"
  31. "Connection: close\r\n"
  32. "\r\n";
  33.  
  34. void setup()
  35. {
  36. Serial.begin(115200);
  37.  
  38. // We start by connecting to a WiFi network
  39. Serial.println();
  40. Serial.println();
  41. Serial.print(F("Connecting to "));
  42. Serial.println(SSID);
  43.  
  44. WiFi.begin(SSID, PASSWORD);
  45.  
  46. while (WiFi.status() != WL_CONNECTED) {
  47. delay(500);
  48. Serial.print(F("."));
  49. }
  50.  
  51. Serial.println();
  52. Serial.println(F("WiFi connected"));
  53. Serial.println(F("IP address: "));
  54. Serial.println(WiFi.localIP());
  55. }
  56.  
  57. static char respBuf[4096];
  58.  
  59. void loop()
  60. {
  61. // TODO check for disconnect from AP
  62.  
  63. // Open socket to WU server port 80
  64. Serial.print(F("Connecting to "));
  65. Serial.println(LIFTIE_REQ);
  66.  
  67. // Use WiFiClient class to create TCP connections
  68. WiFiClient httpclient;
  69. //WiFiClientSecure httpclient;
  70. const int httpPort = 80;
  71. if (!httpclient.connect(LIFTIE_REQ, httpPort)) {
  72. Serial.println(F("connection failed"));
  73. delay(DELAY_ERROR);
  74. return;
  75. }
  76.  
  77. // This will send the http request to the server
  78. Serial.print(LIFTIE_REQ);
  79. httpclient.print(LIFTIE_REQ);
  80. httpclient.flush();
  81.  
  82. // Collect http response headers and content from liftie.info
  83. // HTTP headers are discarded.
  84. // The content is formatted in JSON and is left in respBuf.
  85. int respLen = 0;
  86. bool skip_headers = true;
  87. while (httpclient.connected() || httpclient.available()) {
  88. if (skip_headers) {
  89. String aLine = httpclient.readStringUntil('\n');
  90. Serial.println(aLine);
  91. // Blank line denotes end of headers
  92. if (aLine.length() <= 1) {
  93. skip_headers = false;
  94. }
  95. }
  96. else {
  97. int bytesIn;
  98. bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
  99. Serial.print(F("bytesIn ")); Serial.println(bytesIn);
  100. if (bytesIn > 0) {
  101. respLen += bytesIn;
  102. if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
  103. }
  104. else if (bytesIn < 0) {
  105. Serial.print(F("read error "));
  106. Serial.println(bytesIn);
  107. }
  108. }
  109. delay(1);
  110. }
  111. httpclient.stop();
  112.  
  113. if (respLen >= sizeof(respBuf)) {
  114. Serial.print(F("respBuf overflow "));
  115. Serial.println(respLen);
  116. delay(DELAY_ERROR);
  117. return;
  118. }
  119. // Terminate the C string
  120. respBuf[respLen++] = '\0';
  121. Serial.print(F("respLen "));
  122. Serial.println(respLen);
  123. //Serial.println(respBuf);
  124.  
  125. if (showLifts(respBuf)) {
  126. delay(DELAY_NORMAL);
  127. }
  128. else {
  129. delay(DELAY_ERROR);
  130. }
  131. }
  132.  
  133. bool showLifts(char *json)
  134. {
  135. StaticJsonBuffer<3*1024> jsonBuffer;
  136.  
  137. // Skip characters until first '{' found
  138. // Ignore chunked length, if present
  139. char *jsonstart = strchr(json, '{');
  140. Serial.print(F("jsonstart ")); Serial.println(jsonstart);
  141. if (jsonstart == NULL) {
  142. Serial.println(F("JSON data missing"));
  143. return false;
  144. }
  145. json = jsonstart;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement