doktorinjh

HTTPS_Liftie

Mar 11th, 2018
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. //Include the SSL client
  5. #include <WiFiClientSecure.h>
  6.  
  7. char ssid[] = "***"; // your network SSID (name)
  8. char password[] = "***"; // your network key
  9.  
  10. //Add a SSL client
  11. WiFiClientSecure client;
  12.  
  13. String subredditName = "gunstock";
  14.  
  15. long checkRedditDueTime;
  16. int checkRedditDelay = 60000; // 60 x 1000 (60 seconds)
  17.  
  18. void setup() {
  19.  
  20. Serial.begin(115200);
  21.  
  22. // Set WiFi to station mode and disconnect from an AP if it was Previously
  23. // connected
  24. WiFi.mode(WIFI_STA);
  25. WiFi.disconnect();
  26. delay(100);
  27.  
  28. // Attempt to connect to Wifi network:
  29. Serial.print("Connecting Wifi: ");
  30. Serial.println(ssid);
  31. WiFi.begin(ssid, password);
  32. while (WiFi.status() != WL_CONNECTED) {
  33. Serial.print(".");
  34. delay(500);
  35. }
  36. Serial.println("");
  37. Serial.println("WiFi connected");
  38. Serial.println("IP address: ");
  39. IPAddress ip = WiFi.localIP();
  40. Serial.println(ip);
  41.  
  42. }
  43.  
  44. String getTitleOfNewestPost(String sub) {
  45. String title = "";
  46. String headers = "";
  47. String body = "";
  48. bool finishedHeaders = false;
  49. bool currentLineIsBlank = true;
  50. bool gotResponse = false;
  51. long now;
  52.  
  53. char host[] = "liftie.info";
  54.  
  55. if (client.connect(host, 443)) {
  56. Serial.println("connected");
  57.  
  58. String URL = "/api/resort/" + sub + "/";
  59.  
  60. Serial.println(URL);
  61.  
  62. client.println("GET " + URL + " HTTP/1.1");
  63. client.print("Host: "); client.println(host);
  64. client.println("User-Agent: arduino/1.0");
  65. client.println("");
  66.  
  67. now = millis();
  68. // checking the timeout
  69. while (millis() - now < 1500) {
  70. while (client.available()) {
  71. char c = client.read();
  72. //Serial.print(c);
  73.  
  74. if (finishedHeaders) {
  75. body=body+c;
  76. } else {
  77. if (currentLineIsBlank && c == '\n') {
  78. finishedHeaders = true;
  79. }
  80. else {
  81. headers = headers + c;
  82. }
  83. }
  84.  
  85. if (c == '\n') {
  86. currentLineIsBlank = true;
  87. }else if (c != '\r') {
  88. currentLineIsBlank = false;
  89. }
  90.  
  91. //marking we got a response
  92. gotResponse = true;
  93.  
  94. }
  95. if (gotResponse) {
  96.  
  97. DynamicJsonBuffer jsonBuffer;
  98. JsonObject& root = jsonBuffer.parseObject(body);
  99. if (root.success()) {
  100. if (root.containsKey("lifts")) {
  101. JsonObject& post = root["lifts"]["status"];
  102. if (post.containsKey("lifts")) {
  103. title = post["lifts"]["status"].as<String>();
  104. }
  105. }
  106. } else {
  107. Serial.println("failed to parse JSON");
  108. }
  109.  
  110. break;
  111. }
  112. }
  113. }
  114.  
  115. Serial.println(body);
  116.  
  117. return title;
  118. }
  119.  
  120. void loop() {
  121. long now = millis();
  122. if(now >= checkRedditDueTime) {
  123. Serial.println("---------");
  124. String title = getTitleOfNewestPost(subredditName);
  125. if(title != "") {
  126. Serial.println("Most recent post on /r/" + subredditName);
  127. Serial.println(title);
  128. } else {
  129. Serial.println("Error getting title.");
  130. }
  131. Serial.println("---------");
  132. checkRedditDueTime = now + checkRedditDelay;
  133. }
  134. }
RAW Paste Data