Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- #include <Wire.h>
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <Time.h>
- #include <TimeLib.h>
- #include <Timezone.h>
- #include <WiFiUdp.h>
- #include <String.h>
- #include <WiFiClientSecure.h>
- #include <ArduinoJson.h>
- WiFiClient client;
- LiquidCrystal_I2C lcd(0x20, 16, 2);
- const char* ssid = "NAZWA WIFI";
- const char* password = "HASŁO WIFI";
- String APIKEY = "API Z STRONY OPENWEATHERMAP.ORG";
- String CityID = "ID MIASTA Z STRONY OPENWEATHERMAP.ORG"; //NALEZY ZMIENIĆ JESZCZE LINIJKE String location = "TWOJA MIEJSCOWOŚĆ";
- #define NTP_OFFSET 60 * 60 // In seconds
- #define NTP_INTERVAL 60 * 1000 // In miliseconds
- #define NTP_ADDRESS "uk.pool.ntp.org" // change this to whatever pool is closest (see ntp.org)
- // Set up the NTP UDP client
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
- String date; //create the string for the date which will be printed on the lcd screen below
- String t; // create the string for the time
- const char * days[] = {"Niedziela", "Pon", "Wtorek", "Sroda", "Czwartek", "Piatek", "Sobota"} ;//my screen is 20 across to can't fit in Wednesday
- const char * months[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"} ;
- char servername[]="api.openweathermap.org";
- String result;
- int counter = 10;
- String weatherDescription ="";
- String weatherLocation = "";
- String Country;
- float Temperature;
- float Humidity;
- float Pressure;
- int show = -1;
- void setup()
- {
- int error;
- Serial.begin(115200);
- timeClient.begin();
- int cursorPosition=0;
- Serial.println("LCD...");
- while (!Serial)
- ;
- Serial.println("Dose: check for LCD");
- Wire.begin();
- Wire.beginTransmission(0x20);
- error = Wire.endTransmission();
- Serial.print("Error: ");
- Serial.print(error);
- if (error == 0) {
- Serial.println(": LCD found.");
- show = 0;
- lcd.begin(16, 2);
- Serial.println("POLACZONO");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- lcd.setCursor(cursorPosition,2);
- lcd.print(".");
- cursorPosition++;
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" DZIEN DOBRY");
- lcd.setCursor(0, 1);
- lcd.print("LACZENIE Z BAZA");
- Serial.println("Connected");
- delay(1000);
- } else {
- Serial.println(": LCD not found.");
- }
- }
- void loop()
- {
- if(counter == 10)
- {
- counter = 0;
- displayGettingData();
- delay(1000);
- getWeatherData();
- api.getChannelStatistics(CHANNEL_ID);
- }
- else
- {
- counter++;
- date = "";
- t = "";
- timeClient.update();
- unsigned long epochTime = timeClient.getEpochTime();
- time_t local, utc;
- utc = epochTime;
- TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60};
- TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 60};
- Timezone UK(BST, GMT);
- local = UK.toLocal(utc);
- if (day(local) < 10)
- date += "0";
- date += day(local);
- date += ".";
- if (month(local) - 1 < 10)
- date += "0";
- date += months[month(local) - 1];
- date += ".";
- date += year(local);
- t += days[weekday(local) - 1];
- t += " ";
- if (hour(local) < 10)
- t += "0";
- t += hour(local);
- t += ":";
- if (minute(local) < 10) // add a zero if minute is under 10
- t += "0";
- t += minute(local);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(t);
- lcd.setCursor(0, 1);
- lcd.print(date);
- delay(10000);
- displayWeather(weatherLocation,weatherDescription);
- delay(3000);
- displayConditions(Temperature,Humidity,Pressure);
- delay(10000);
- }
- }
- void getWeatherData()
- {
- if (client.connect(servername, 80)) {
- client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
- client.println("Host: api.openweathermap.org");
- client.println("User-Agent: ArduinoWiFi/1.1");
- client.println("Connection: close");
- client.println();
- }
- else {
- Serial.println("connection failed");
- Serial.println();
- }
- while(client.connected() && !client.available()) delay(1);
- while (client.connected() || client.available()) {
- char c = client.read();
- result = result+c;
- }
- client.stop(); //stop client
- result.replace('[', ' ');
- result.replace(']', ' ');
- Serial.println(result);
- char jsonArray [result.length()+1];
- result.toCharArray(jsonArray,sizeof(jsonArray));
- jsonArray[result.length() + 1] = '\0';
- StaticJsonBuffer<1024> json_buf;
- JsonObject &root = json_buf.parseObject(jsonArray);
- if (!root.success())
- {
- Serial.println("parseObject() failed");
- }
- String location = "TWOJA MIEJSCOWOŚĆ";
- String country = root["sys"]["country"];
- float temperature = root["main"]["temp"];
- float humidity = root["main"]["humidity"];
- String weather = root["weather"]["main"];
- String description = root["weather"]["description"];
- float pressure = root["main"]["pressure"];
- weatherDescription = description;
- weatherLocation = location;
- Country = country;
- Temperature = temperature;
- Humidity = humidity;
- Pressure = pressure;
- }
- void displayWeather(String location,String description)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(location);
- lcd.print(", ");
- lcd.print(Country);
- lcd.setCursor(0,1);
- lcd.print(description);
- }
- void displayConditions(float Temperature,float Humidity, float Pressure)
- {
- lcd.clear();
- lcd.print("T:");
- lcd.print(Temperature,1);
- lcd.print((char)223);
- lcd.print("C ");
- //Printing Humidity
- lcd.print(" W:");
- lcd.print(Humidity,0);
- lcd.print(" %");
- //Printing Pressure
- lcd.setCursor(0,1);
- lcd.print("C: ");
- lcd.print(Pressure,1);
- lcd.print(" hPa");
- }
- void displayGettingData()
- {
- lcd.clear();
- lcd.print(" Pobieram dane");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement