Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. /*
  2. * HTTP Client GET Request
  3. * Copyright (c) 2018, circuits4you.com
  4. * All rights reserved.
  5. * https://circuits4you.com
  6. * Connects to WiFi HotSpot. */
  7.  
  8. #include <ESP8266WiFi.h>
  9. #include <WiFiClient.h>
  10. #include <ESP8266WebServer.h>
  11. #include <ESP8266HTTPClient.h>
  12.  
  13. #include <Wire.h> // standardowa biblioteka Arduino
  14. #include <LiquidCrystal_I2C.h> // dolaczenie pobranej biblioteki I2C dla LCD
  15. #include <IRremoteESP8266.h>
  16. #include <Vector.h>
  17.  
  18. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Ustawienie adresu ukladu na 0x27
  19.  
  20. String allPolishSymbols[] = {"ą", "ć", "ę", "ł", "ń", "ó", "ś", "ź", "ż"};
  21. String symbolsToReplace[] = {"a", "c", "e", "l" ,"n", "o", "s", "z", "z"};
  22.  
  23. class Bus
  24. {
  25. public:
  26. String estimatedTime;
  27. String headsign;
  28. String routeId;
  29.  
  30. Bus(const String &src)
  31. {
  32. int temp = src.indexOf("estimatedTime");
  33. estimatedTime = src.substring(temp+16, src.indexOf(",", temp)-1);
  34. temp = src.indexOf("headsign");
  35. headsign = src.substring(temp+11, src.indexOf(",", temp)-1);
  36. if(headsign.length() > 9)
  37. headsign = headsign.substring(0, 9);
  38.  
  39. for(int i = 0; i < sizeof(headsign); i++)
  40. {
  41. //headsign.replace(allPolishSymbols[i], symbolsToReplace[i]);
  42. if(headsign[i] == 0xc584)
  43. headsign[i] = 'a';
  44. /*
  45. for(int ii = 0; ii < sizeof(allPolishSymbols); ii++)
  46. {
  47.  
  48. Serial.print("headsign[");
  49. Serial.print(i);
  50. Serial.print("](");
  51. Serial.print(headsign[i]);
  52. Serial.print(") == allpolishSymbols[");
  53. Serial.print(ii);
  54. Serial.print("](");
  55. Serial.print(allPolishSymbols[ii]);
  56. Serial.print(")");
  57.  
  58. headsign.replace(allPolishSymbols[ii]);
  59. }
  60. */
  61. }
  62.  
  63. temp = src.indexOf("routeId");
  64. routeId = src.substring(temp+9, src.indexOf(",", temp));
  65. if(routeId.startsWith("4"))
  66. routeId.replace("4", "N");
  67. }
  68. };
  69.  
  70. /* Set these to your desired credentials. */
  71. const char *ssid = "Harnas to krol gor 2.4"; //ENTER YOUR WIFI SETTINGS
  72. const char *password = "Harnaskrol1337";
  73. const char *ZTMOtwarteDaneURL = "http://ckan2.multimediagdansk.pl/delays?stopId=1369";
  74.  
  75. #define REFRESH_DELAY 20000
  76.  
  77. std::vector<Bus> allBuses;
  78.  
  79. //Web/Server address to read/write from
  80. //const char *host = "http://ckan2.multimediagdansk.pl";
  81.  
  82. //=======================================================================
  83. // Power on setup
  84. //=======================================================================
  85.  
  86.  
  87.  
  88. void setup()
  89. {
  90. delay(1000);
  91. Serial.begin(115200);
  92.  
  93. WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
  94. delay(1000);
  95. WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
  96.  
  97. WiFi.begin(ssid, password); //Connect to your WiFi router
  98. Serial.println("");
  99.  
  100. Serial.print("Connecting");
  101. // Wait for connection
  102. while (WiFi.status() != WL_CONNECTED)
  103. {
  104. delay(500);
  105. Serial.print(".");
  106. }
  107.  
  108. //If connection successful show IP address in serial monitor
  109. Serial.println("");
  110. Serial.print("Connected to ");
  111. Serial.println(ssid);
  112. Serial.print("IP address: ");
  113. Serial.println(WiFi.localIP()); //IP address assigned to your ESP
  114.  
  115. lcd.begin(16,2); // Inicjalizacja LCD 2x16
  116.  
  117. lcd.backlight(); // zalaczenie podwietlenia
  118. lcd.setCursor(0,0);
  119. lcd.print("127 JasienPKM 5m");
  120. delay(500);
  121. lcd.setCursor(0,1);
  122. lcd.print("162 Orunia Go 8m");
  123. }
  124.  
  125. String GETrequest(String url)
  126. {
  127. Serial.println("Entered GETrequest");
  128. HTTPClient http;
  129. http.begin(url);
  130.  
  131. int httpResponseCode = http.GET();
  132. String httpReceivedData;
  133.  
  134. Serial.print("HTTP response code: ");
  135. Serial.println(httpResponseCode);
  136.  
  137. if(httpResponseCode == 200)
  138. httpReceivedData = http.getString();
  139. else
  140. httpReceivedData = "ERRORCODE: %d\n", httpResponseCode;
  141.  
  142. http.end();
  143. return httpReceivedData;
  144. }
  145.  
  146. std::vector<Bus> ParseArrivingBuses(String data)
  147. {
  148. Serial.println("Entered ParseArrivingBuses");
  149. std::vector<Bus> allParsedBuses;
  150.  
  151. int fFront = data.indexOf('[');
  152. int fBack = data.indexOf(']');
  153. data.remove(0, fFront);
  154. data.remove(fBack, data.length());
  155.  
  156. while(1)
  157. {
  158. int front = data.indexOf('{');
  159. int back = data.indexOf('}');
  160.  
  161. if(front != -1 && back != -1)
  162. {
  163. Serial.println("###### START ######");
  164. Serial.print("bus-start :");
  165.  
  166. String bus = data.substring(front, back + 1);
  167. Serial.print("bus :");
  168. Serial.println(bus);
  169. Bus tempBus(bus);
  170. allParsedBuses.push_back(tempBus);
  171.  
  172. //allBuses.push_back(bus);
  173. data.remove(front, back - front + 1);
  174.  
  175. Serial.print("bus-koniec :");
  176. Serial.println(bus);
  177. Serial.println("###### KUNIEC ######");
  178. }
  179. else
  180. break;
  181. }
  182.  
  183. return allParsedBuses;
  184. }
  185.  
  186. void PrintBusesOnLCD()
  187. {
  188. Serial.println("Entered PrintBusesOnLCD");
  189. lcd.clear();
  190. int i = 0;
  191. for(auto bus : allBuses)
  192. {
  193. Serial.println(bus.estimatedTime);
  194. Serial.println(bus.headsign);
  195. Serial.println(bus.routeId);
  196. lcd.setCursor(0,i);
  197. i++;
  198. lcd.print(bus.routeId + " " + bus.headsign + " xx");
  199. if(i == 2)
  200. break;
  201. }
  202. }
  203.  
  204.  
  205. void loop()
  206. {
  207. Serial.println("Checkpoint 1");
  208. String arrivingBusesData = GETrequest(ZTMOtwarteDaneURL);
  209. Serial.println("Checkpoint 2");
  210. if(arrivingBusesData.startsWith("ERROR"))
  211. {
  212. Serial.println("Checkpoint 3");
  213. Serial.println("Something went wrong: " + arrivingBusesData);
  214. delay(REFRESH_DELAY);
  215. return;
  216. }
  217. Serial.println("Checkpoint 4");
  218.  
  219. allBuses = ParseArrivingBuses(arrivingBusesData);
  220. Serial.println("Checkpoint 5");
  221. PrintBusesOnLCD();
  222. Serial.println("Checkpoint 6");
  223.  
  224. delay(REFRESH_DELAY);
  225. }
  226. //=======================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement