Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ESP Weather Station v0.1 Beta (DEMO for YOU) Multilanguage: EN / PL (switch LANGUAGE to 1 if EN //
- // this is free demo, no more update !!!
- //
- // powerTGS440@gmail.com
- //
- // -------------------------------------------------------------------------------------------------- //
- #include <WiFi.h> //
- #include <NTPClient.h> //
- #include <WiFiUdp.h> //
- #include <HTTPClient.h> //
- #include <WiFiClient.h> //
- #include <Arduino_JSON.h> //
- #include <TimeLib.h> //
- // -------------------------------------------------------------------------------------------------- //
- //
- //
- //
- // -------------------------------------------------------------------------------------------------- // <--- D E B U G E R
- // --- D E F I N E - T E R M I N A L - S E R I A L D E B U G --- //
- // -------------------------------------------------------------------------------------------------- //
- //
- #define TERMINAL 1 // 0 - wyłącz SERIAL, 1 - włącz SERIAL ---- //
- #define DEBUG 0 // Full Function Debugger ---- //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- D E F I N E - L A N G U A G E - P O L I S H 0 - E N G L I S H 1 --- //
- // -------------------------------------------------------------------------------------------------- // <--- L A N G U A G E
- // --- //
- #define LANGUAGE 0 // jeśli LANGUAGE jest zdefiniowane jako 0 --- //
- // to kolejne warunki dla makra przestawiają --- //
- #if LANGUAGE // język ma Polski --- //
- //
- #define POLISH 0 // //
- #define ENGLISH 1 // jeśli LANGUAGE jest zdefiniowane jako 1 --- //
- //
- #else // to kolejne warunki dla makra przestawiają --- //
- //
- #define POLISH 1 // język na angielski (jak wyżej) --- //
- #define ENGLISH 0 // //
- //
- #endif // M U L T I L A N G U A G E P R O J E C T //
- // //
- // -------------------------------------------------------------------------------------------------- //
- // --- O B J E C T - W I F I & N T P //
- // -------------------------------------------------------------------------------------------------- // <--- W I F I
- //
- WiFiUDP ntpUDP; // ntp dla Time / Date Serwer --- //
- NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600); //
- //
- // -------------------------------------------------------------------------------------------------- // <--- V A R I A B L E S
- // --- O T H E R V A R I A B L E & C O N S T --- //
- // -------------------------------------------------------------------------------------------------- //
- //
- const char *ssid = "NazwaWIFI"; // network wifi ssid --- //
- const char *password = "TwojeHaslo"; // network wifi password --- //
- String openWeatherMapApiKey = "565a88311e7c48c2d00b170cd0765c91"; // token for openWeather --- //
- String city = "Legnica"; // city for weather --- //
- String countryCode = "Pl"; // country for weather --- //
- String jsonBuffer; // buffer - bufor --- //
- //
- // -------------------------------------------------------------------------------------------------- // <--- S T R U C T D A T E & T I M E
- // S T R U K T U R A C Z A S / T I M E S T R U C T U R E --- //
- // -------------------------------------------------------------------------------------------------- //
- struct datatime //
- { //
- String SformatowanaData; // formated data ------------- //
- int DzienTygodnia; // day of week --------------- //
- int DzienMiesiaca; // day of month -------------- //
- int Miesiac; // month --------------------- //
- int Rok; // year ---------------------- //
- String SformatowanyCzas; // formated time ------------- //
- int Godzina; // hour ---------------------- //
- int Minuta; // minute -------------------- //
- int Sekunda; // seconds ------------------- //
- char wschod_slonca[10]; // sunrise ------------------- //
- char zachod_slonca[10]; // sunset -------------------- //
- } czas; // time object --------------- //
- //
- // -------------------------------------------------------------------------------------------------- // <--- S T R U C T W E A T H E R
- // S T R U K T U R A P O G O D A --- //
- // -------------------------------------------------------------------------------------------------- //
- struct weather //
- { //
- double temperatura; // temperature / temperatura - //
- double temperatura_minimalna; // min. temp ----------------- //
- double temperatura_maksymalna; // max. temp ----------------- //
- double temperatura_odczuwalna; // feels temp ---------------- //
- int cisnienie; // cisnienie powietrza ------- //
- int wilgotnosc; // wilgotnosc powietrza ------ //
- int wiatr; // wiatr / wind speed -------- //
- int zachmurzenie; // zachmurzenie / cloudys ---- //
- long widzialnosc; // widzialnosc / visible ----- //
- } pogoda; // weather object ------------ //
- //
- // -------------------------------------------------------------------------------------------------- // <--- S E T U P
- // --- S E T U P M A I N S E C T I O N --- //
- // -------------------------------------------------------------------------------------------------- // //
- //
- void setup() //
- { //
- #if TERMINAL //
- Serial.begin(57600); // set speed USB ---- //
- #endif //
- //
- //
- WiFi.begin(ssid, password); // WiFi start connection ----- //
- //
- delay(2000); //
- //
- while ( WiFi.status() != WL_CONNECTED ) // czekaj na połączenie ----- // <--- WiFi Connected
- { // wait for connect ----- //
- delay ( 200 ); // safe mode ----- //
- Serial.print ( "." ); // print dot / drukuj . ----- //
- } //
- //
- #if TERMINAL //
- //
- #if POLISH //
- Serial.printf("\nPOLACZONO z WiFi : %s\n", ssid); //
- Serial.printf("-------------------------------------\n"); //
- Serial.printf("ESP Wolna Pamięć RAM : %u [kB]\n", ESP.getFreeHeap()); //
- Serial.printf("ESP Zegar procesora : %u [MHz]\n", ESP.getCpuFreqMHz()); //
- #else //
- Serial.printf("\nCONNECT to WiFi : %s\n", ssid); //
- Serial.printf("-------------------------------------\n"); //
- Serial.printf("ESP Memory Free : %u [kB]\n",ESP.getFreeHeap()); //
- Serial.printf("ESP CPU frequency : %u [MHz]\n",ESP.getCpuFreqMHz()); //
- #endif //
- //
- #endif //
- //
- WiFiUDP ntpUDP; // ntp client --- //
- NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600); // ntp server --- //
- //
- TimeUpdate(); // update time from ntp --- // <--- Update TIME
- DateUpdate(); // update date from ntp --- // <--- Update DATE
- WeatherUpdate(); // update weather from om --- // <--- Update WEATHER
- //
- #if TERMINAL //
- //
- DateUpdateShow(); // show Update Date --- // <--- only if TERMINAL true
- TimeUpdateShow(); // show Update Time --- // <--- jeśli TERMINAL jest 1
- WeatherUpdateShow(); // show Update Weather --- //
- //
- #endif //
- } //
- // --- END SETUP ------------------------------------------------------------------------------------ // <--- S E T U P E N D
- //
- //
- //
- //
- //
- //
- //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- L O O P --- // <--- L O O P S T A R T H E R E
- // -------------------------------------------------------------------------------------------------- //
- void loop()
- {
- delay(1000);
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- D A T E P R I N T T O S E R I A L --- // <--- DATE SHOW 2 SERIAL
- // -------------------------------------------------------------------------------------------------- //
- //
- void DateUpdateShow() //
- { //
- #if TERMINAL //
- //
- #if POLISH // <--- Raport po Polsku
- Serial.printf("\n----------- NTP RAPORT --- DATA --------------------\n"); //
- Serial.printf("Dzień tygodnia : %d \n", czas.DzienTygodnia); //
- Serial.printf("Dzień miesiąca : %d \n", czas.DzienMiesiaca); //
- Serial.printf("Miesiąc : %d \n", czas.Miesiac); //
- Serial.printf("Rok : %d", czas.Rok); //
- #endif //
- //
- #if ENGLISH // <--- English Raport
- Serial.printf("\n----------- NTP RAPORT --- DATE --------------------\n"); //
- Serial.printf("Day of week : %d \n", czas.DzienTygodnia); //
- Serial.printf("Day number : %d \n", czas.DzienMiesiaca); //
- Serial.printf("Month number : %d \n", czas.Miesiac); //
- Serial.printf("Year : %d", czas.Rok); //
- #endif //
- //
- #endif //
- } //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- D A T A U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj datę z serwera : pool.ntp.org ----------------------------------------------------- // <--- aktualizacja daty z serwera NTP
- // -------------------------------------------------------------------------------------------------- //
- void DateUpdate() //
- { //
- timeClient.update(); //
- unsigned long epochTime = timeClient.getEpochTime(); //
- struct tm *ptm = gmtime ((time_t *)&epochTime); //
- //
- czas.SformatowanaData = timeClient.getFormattedTime(); //
- czas.DzienTygodnia = timeClient.getDay(); //
- czas.DzienMiesiaca = ptm->tm_mday; //
- czas.Miesiac = ptm->tm_mon+1; //
- czas.Rok = ptm->tm_year+1900; //
- //
- #if DEBUG //
- #if TERMINAL //
- #if POLISH //
- Serial.printf("\nPROJECT : zaktualizowano datę z serwera pool.ntp.org"); //
- #else //
- Serial.printf("\nPROJECT : update data from server pool.ntp.org"); //
- #endif //
- #endif //
- #endif //
- } //
- // -------------------------------------------------------------------------------------------------- //
- // --- T I M E P R I N T T O S E R I A L --- // <---TIME SHOW 2 SERIAL
- // -------------------------------------------------------------------------------------------------- //
- //
- void TimeUpdateShow() //
- { //
- #if TERMINAL //
- //
- #if POLISH // <--- Raport po Polsku
- //
- Serial.printf("\n----------- NTP RAPORT --- CZAS --------------------\n"); //
- //
- if(czas.Godzina < 10) //
- Serial.printf("Godzina : 0%d \n", czas.Godzina); //
- else //
- Serial.printf("Godzina : %d \n", czas.Godzina); //
- //
- if(czas.Minuta < 10) //
- Serial.printf("Minuta : 0%d \n", czas.Minuta); //
- else //
- Serial.printf("Minuta : %d \n", czas.Minuta); //
- //
- if(czas.Sekunda < 10) //
- Serial.printf("Sekunda : 0%d", czas.Sekunda); //
- else //
- Serial.printf("Sekunda : %d", czas.Sekunda); //
- #endif //
- //
- #if ENGLISH // <--- English Raport
- //
- Serial.printf("\n----------- NTP RAPORT --- TIME --------------------\n"); //
- //
- if(czas.Godzina < 10) //
- Serial.printf("Hour : 0%d \n", czas.Godzina); //
- else //
- Serial.printf("Hour : %d \n", czas.Godzina); //
- //
- if(czas.Minuta < 10) //
- Serial.printf("Minute : 0%d \n", czas.Minuta); //
- else //
- Serial.printf("Minute : %d \n", czas.Minuta); //
- //
- if(czas.Sekunda < 10) //
- Serial.printf("Second : 0%d", czas.Sekunda); //
- else //
- Serial.printf("Second : %d", czas.Sekunda); //
- #endif //
- //
- #endif //
- } //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- T I M E U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj czas z serwera : pool.ntp.org ----------------------------------------------------- // <--- aktualizacja czasu z serwera NTP
- // -------------------------------------------------------------------------------------------------- //
- void TimeUpdate() //
- { //
- timeClient.update(); //
- //
- czas.Godzina = timeClient.getHours(); // zapisz godzinę z serwera ----------------- //
- czas.Minuta = timeClient.getMinutes(); // zapisz minutę z serwera ------------------ //
- czas.Sekunda = timeClient.getSeconds(); // zapisz sekundę z serwera ----------------- //
- //
- #if DEBUG //
- #if TERMINAL //
- #if POLISH //
- Serial.printf("\nPROJECT : zaktualizowano czas z serwera pool.ntp.org"); //
- #else //
- Serial.printf("\nPROJECT : update time from server pool.ntp.org"); //
- #endif //
- #endif //
- #endif //
- } //
- // -------------------------------------------------------------------------------------------------- //
- // --- W E A T H E R P R I N T T O S E R I A L --- // <--- WEATHER SHOW 2 SERIAL
- // -------------------------------------------------------------------------------------------------- //
- //
- void WeatherUpdateShow() //
- { //
- #if TERMINAL //
- //
- #if POLISH // <--- Raport po Polsku
- Serial.printf("\n----------- NTP RAPORT --- POGODA ------------------\n"); //
- Serial.printf("Temperatura : %.2f st.C\n", pogoda.temperatura); //
- Serial.printf("Temp. Odczuwalna : %.2f st.C\n", pogoda.temperatura_odczuwalna); //
- Serial.printf("Temp. Minimalna : %.2f st.C\n", pogoda.temperatura_minimalna); //
- Serial.printf("Temp. Maksymalna : %.2f st.C\n", pogoda.temperatura_maksymalna); //
- Serial.printf("Ćisnienie : %d [ hPa ]\n", pogoda.cisnienie); //
- Serial.printf("Wilgotność : %d [ %% ]\n", pogoda.wilgotnosc); //
- Serial.printf("Zachmurzenie : %d [ %% ]\n", pogoda.zachmurzenie); //
- Serial.printf("Prędkość wiatru : %d [ km/h ]\n", pogoda.wiatr); //
- Serial.printf("Widzialność : %d [ m ]\n", pogoda.widzialnosc); //
- Serial.printf("Wschód słońca : %s\n", czas.wschod_slonca); //
- Serial.printf("Zachód słóńca : %s\n", czas.zachod_slonca); //
- Serial.printf("----------------------------------------------------"); //
- #endif //
- //
- #if ENGLISH // <--- English Raport
- Serial.printf("\n----------- NTP RAPORT --- Weather ------------------\n"); //
- Serial.printf("Temperature : %.2f st.C\n", pogoda.temperatura); //
- Serial.printf("Temp. Feels : %.2f st.C\n", pogoda.temperatura_odczuwalna); //
- Serial.printf("Temp. Min. : %.2f st.C\n", pogoda.temperatura_minimalna); //
- Serial.printf("Temp. Max : %.2f st.C\n", pogoda.temperatura_maksymalna); //
- Serial.printf("Pressure : %d [ hPa ]\n", pogoda.cisnienie); //
- Serial.printf("Humidity : %d [ %% ]\n", pogoda.wilgotnosc); //
- Serial.printf("Clouds : %d [ %% ]\n", pogoda.zachmurzenie); //
- Serial.printf("Wind speed : %d [ km/h ]\n", pogoda.wiatr); //
- Serial.printf("Visibility : %d [ m ]\n", pogoda.widzialnosc); //
- Serial.printf("Sunrise Time : %s\n", czas.wschod_slonca); //
- Serial.printf("Sunset Time : %s\n", czas.zachod_slonca); //
- Serial.printf("----------------------------------------------------"); //
- #endif //
- //
- #endif //
- } //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- W E A T H E R U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj pogode z serwera : openwearhermap.org --------------------------------------------- // <--- aktualizacja pogody z serwera OPEN
- // -------------------------------------------------------------------------------------------------- //
- void WeatherUpdate() //
- { //
- if(WiFi.status()== WL_CONNECTED) //
- { //
- String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," //
- + countryCode + "&APPID=" + openWeatherMapApiKey; //
- //
- jsonBuffer = httpGETRequest(serverPath.c_str()); //
- //
- #if DEBUGER //
- #if TERMINAL //
- Serial.println(jsonBuffer); // DRUKUJE CAŁY NIESPARSOWY STRING z POGODA // <--- ALL WEATHER not PARSED
- #endif //
- #endif //
- //
- JSONVar myObject = JSON.parse(jsonBuffer); //
- //
- if (JSON.typeof(myObject) == "undefined") //
- { //
- #if TERMINAL //
- #if POLISH //
- Serial.println("Bląd połączenia..."); //
- #else //
- Serial.println("Error connection..."); //
- #endif //
- #endif //
- //
- return; //
- } //
- // -------------------------------------------------------------------------------------------- //
- // SHOW myObject //
- //
- #if DEBUGER //
- #if TERMINAL //
- Serial.println(myObject); //
- #endif //
- #endif //
- //
- // -------------------------------------------------------------------------------------------- //
- // --- P R Z E T W A R Z A N I E D A N Y C H Z S E R W E R A --- // <--- PARSOWANIE danych z pogody
- // -------------------------------------------------------------------------------------------- // <--- PARSING WEATHER ONLY
- //
- double Kelvin = 273.15; //
- //
- pogoda.temperatura = myObject["main"]["temp"]; // pobieramy wartość do skruktury//
- pogoda.temperatura -= Kelvin; // odejmujemy wartość Kelvin --- //
- //
- pogoda.temperatura_odczuwalna = myObject["main"]["feels_like"]; //
- pogoda.temperatura_odczuwalna -= Kelvin; //
- //
- pogoda.temperatura_minimalna = myObject["main"]["temp_min"]; //
- pogoda.temperatura_minimalna -= Kelvin; //
- //
- pogoda.temperatura_maksymalna = myObject["main"]["temp_max"]; //
- pogoda.temperatura_maksymalna -= Kelvin; //
- //
- pogoda.cisnienie = myObject["main"]["pressure"]; //
- //
- pogoda.wilgotnosc = myObject["main"]["humidity"]; //
- //
- pogoda.wiatr = myObject["wind"]["speed"]; //
- //
- pogoda.zachmurzenie = myObject["clouds"]["all"]; //
- //
- pogoda.widzialnosc = myObject["visibility"]; //
- // ------------- //
- // --- wykorzystana biblioteka TimeLib.h ----------- //
- sprintf(czas.wschod_slonca, "%02d:%02d",hour(myObject["sys"]["sunrise"]),minute(myObject["sys"]["sunrise"])); //
- //
- // --- wykorzystana biblioteka TimeLib.h ----------- //
- sprintf(czas.zachod_slonca, "%02d:%02d",hour(myObject["sys"]["sunset"]),minute(myObject["sys"]["sunset"])); //
- //
- // ------------- //
- #if DEBUG //
- #if TERMINAL //
- #if POLISH //
- Serial.printf("\nPROJECT : zaktualizowano pogodę z serwera openweathermap"); //
- #else //
- Serial.printf("\nPROJECT : update weather from server openweathermap"); //
- #endif //
- #endif //
- #endif //
- } //
- } //
- // -------------------------------------------------------------------------------------------------- //
- // --- httpGET --- // <--- http GET request
- // -------------------------------------------------------------------------------------------------- //
- String httpGETRequest(const char* serverName) //
- { //
- HTTPClient http; //
- http.begin(serverName); //
- int httpResponseCode = http.GET(); //
- String bufor = "{}"; //
- //
- if ( httpResponseCode > 0 ) //
- { //
- #if DEBUG //
- #if TERMINAL //
- #if POLISH //
- Serial.printf("\nHTTP - Kod odpowiedzi: %d\n", httpResponseCode); //
- #else //
- Serial.printf("\nHTTP - Response code: %d\n", httpResponseCode); //
- #endif //
- #endif //
- #endif //
- //
- bufor = http.getString(); //
- } //
- else //
- { //
- #if DEBUG //
- #if TERMINAL //
- #if POLISH //
- Serial.printf("\nHTTP - Kod Błędu: %d\n", httpResponseCode); //
- #else //
- Serial.printf("\nHTTP - Error code: %d\n", httpResponseCode); //
- #endif //
- #endif //
- #endif //
- } //
- //
- http.end(); //
- return bufor; //
- } //
- // -------------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement