Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * HTTP Client GET Request
- * Copyright (c) 2018, circuits4you.com
- * All rights reserved.
- * https://circuits4you.com
- * Connects to WiFi HotSpot. */
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266HTTPClient.h>
- #include <Wire.h> // standardowa biblioteka Arduino
- #include <LiquidCrystal_I2C.h> // dolaczenie pobranej biblioteki I2C dla LCD
- #include <IRremoteESP8266.h>
- #include <Vector.h>
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Ustawienie adresu ukladu na 0x27
- String allPolishSymbols[] = {"ą", "ć", "ę", "ł", "ń", "ó", "ś", "ź", "ż"};
- String symbolsToReplace[] = {"a", "c", "e", "l" ,"n", "o", "s", "z", "z"};
- class Bus
- {
- public:
- String estimatedTime;
- String headsign;
- String routeId;
- Bus(const String &src)
- {
- int temp = src.indexOf("estimatedTime");
- estimatedTime = src.substring(temp+16, src.indexOf(",", temp)-1);
- temp = src.indexOf("headsign");
- headsign = src.substring(temp+11, src.indexOf(",", temp)-1);
- if(headsign.length() > 9)
- headsign = headsign.substring(0, 9);
- for(int i = 0; i < sizeof(headsign); i++)
- {
- //headsign.replace(allPolishSymbols[i], symbolsToReplace[i]);
- if(headsign[i] == 0xc584)
- headsign[i] = 'a';
- /*
- for(int ii = 0; ii < sizeof(allPolishSymbols); ii++)
- {
- Serial.print("headsign[");
- Serial.print(i);
- Serial.print("](");
- Serial.print(headsign[i]);
- Serial.print(") == allpolishSymbols[");
- Serial.print(ii);
- Serial.print("](");
- Serial.print(allPolishSymbols[ii]);
- Serial.print(")");
- headsign.replace(allPolishSymbols[ii]);
- }
- */
- }
- temp = src.indexOf("routeId");
- routeId = src.substring(temp+9, src.indexOf(",", temp));
- if(routeId.startsWith("4"))
- routeId.replace("4", "N");
- }
- };
- /* Set these to your desired credentials. */
- const char *ssid = "Harnas to krol gor 2.4"; //ENTER YOUR WIFI SETTINGS
- const char *password = "Harnaskrol1337";
- const char *ZTMOtwarteDaneURL = "http://ckan2.multimediagdansk.pl/delays?stopId=1369";
- #define REFRESH_DELAY 20000
- std::vector<Bus> allBuses;
- //Web/Server address to read/write from
- //const char *host = "http://ckan2.multimediagdansk.pl";
- //=======================================================================
- // Power on setup
- //=======================================================================
- void setup()
- {
- delay(1000);
- Serial.begin(115200);
- WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
- delay(1000);
- WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
- WiFi.begin(ssid, password); //Connect to your WiFi router
- Serial.println("");
- Serial.print("Connecting");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- //If connection successful show IP address in serial monitor
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP()); //IP address assigned to your ESP
- lcd.begin(16,2); // Inicjalizacja LCD 2x16
- lcd.backlight(); // zalaczenie podwietlenia
- lcd.setCursor(0,0);
- lcd.print("127 JasienPKM 5m");
- delay(500);
- lcd.setCursor(0,1);
- lcd.print("162 Orunia Go 8m");
- }
- String GETrequest(String url)
- {
- Serial.println("Entered GETrequest");
- HTTPClient http;
- http.begin(url);
- int httpResponseCode = http.GET();
- String httpReceivedData;
- Serial.print("HTTP response code: ");
- Serial.println(httpResponseCode);
- if(httpResponseCode == 200)
- httpReceivedData = http.getString();
- else
- httpReceivedData = "ERRORCODE: %d\n", httpResponseCode;
- http.end();
- return httpReceivedData;
- }
- std::vector<Bus> ParseArrivingBuses(String data)
- {
- Serial.println("Entered ParseArrivingBuses");
- std::vector<Bus> allParsedBuses;
- int fFront = data.indexOf('[');
- int fBack = data.indexOf(']');
- data.remove(0, fFront);
- data.remove(fBack, data.length());
- while(1)
- {
- int front = data.indexOf('{');
- int back = data.indexOf('}');
- if(front != -1 && back != -1)
- {
- Serial.println("###### START ######");
- Serial.print("bus-start :");
- String bus = data.substring(front, back + 1);
- Serial.print("bus :");
- Serial.println(bus);
- Bus tempBus(bus);
- allParsedBuses.push_back(tempBus);
- //allBuses.push_back(bus);
- data.remove(front, back - front + 1);
- Serial.print("bus-koniec :");
- Serial.println(bus);
- Serial.println("###### KUNIEC ######");
- }
- else
- break;
- }
- return allParsedBuses;
- }
- void PrintBusesOnLCD()
- {
- Serial.println("Entered PrintBusesOnLCD");
- lcd.clear();
- int i = 0;
- for(auto bus : allBuses)
- {
- Serial.println(bus.estimatedTime);
- Serial.println(bus.headsign);
- Serial.println(bus.routeId);
- lcd.setCursor(0,i);
- i++;
- lcd.print(bus.routeId + " " + bus.headsign + " xx");
- if(i == 2)
- break;
- }
- }
- void loop()
- {
- Serial.println("Checkpoint 1");
- String arrivingBusesData = GETrequest(ZTMOtwarteDaneURL);
- Serial.println("Checkpoint 2");
- if(arrivingBusesData.startsWith("ERROR"))
- {
- Serial.println("Checkpoint 3");
- Serial.println("Something went wrong: " + arrivingBusesData);
- delay(REFRESH_DELAY);
- return;
- }
- Serial.println("Checkpoint 4");
- allBuses = ParseArrivingBuses(arrivingBusesData);
- Serial.println("Checkpoint 5");
- PrintBusesOnLCD();
- Serial.println("Checkpoint 6");
- delay(REFRESH_DELAY);
- }
- //=======================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement