PowerTGS440

ESP Weather Station

Feb 19th, 2021 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 47.53 KB | None | 0 0
  1. // ESP Weather Station v0.1 Beta (DEMO for YOU) Multilanguage: EN / PL (switch LANGUAGE to 1 if EN    //
  2. // this is free demo, no more update !!!
  3. //
  4. //
  5. // -------------------------------------------------------------------------------------------------- //
  6. #include <WiFi.h>                                                                                     //
  7. #include <NTPClient.h>                                                                                //
  8. #include <WiFiUdp.h>                                                                                  //
  9. #include <HTTPClient.h>                                                                               //
  10. #include <WiFiClient.h>                                                                               //
  11. #include <Arduino_JSON.h>                                                                             //
  12. #include <TimeLib.h>                                                                                  //
  13. // -------------------------------------------------------------------------------------------------- //
  14. //
  15. //
  16. //
  17. // -------------------------------------------------------------------------------------------------- //    <--- D E B U G E R
  18. // ---    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                         --- //
  19. // -------------------------------------------------------------------------------------------------- //
  20.                                                                                                       //
  21. #define TERMINAL 1                                        // 0 - wyłącz SERIAL, 1 - włącz SERIAL ---- //
  22. #define DEBUG 0                                           // Full Function Debugger              ---- //
  23.                                                                                                       //
  24. // -------------------------------------------------------------------------------------------------- //
  25. // --- 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                  --- //
  26. // -------------------------------------------------------------------------------------------------- //   <--- L A N G U A G E
  27.                                                    //                                             --- //
  28. #define LANGUAGE 0                                 // jeśli LANGUAGE jest zdefiniowane jako 0     --- //
  29.                                                    // to kolejne warunki dla makra przestawiają   --- //
  30. #if LANGUAGE                                       // język ma Polski                             --- //
  31.                                                                                                       //
  32.   #define POLISH 0                                 //                                                 //
  33.   #define ENGLISH 1                                // jeśli LANGUAGE jest zdefiniowane jako 1     --- //
  34.                                                                                                       //
  35. #else                                              // to kolejne warunki dla makra przestawiają   --- //
  36.                                                                                                       //
  37.   #define POLISH 1                                 // język na angielski (jak wyżej)              --- //
  38.   #define ENGLISH 0                                //                                                 //
  39.                                                                                                       //
  40. #endif                                             //   M U L T I   L A N G U A G E   P R O J E C T   //
  41.                                                    //                                                 //
  42. // -------------------------------------------------------------------------------------------------- //
  43. // --- O B J E C T   -   W I F I   &   N T P                                                          //
  44. // -------------------------------------------------------------------------------------------------- //    <--- W I F I
  45.                                                                                                       //  
  46. WiFiUDP ntpUDP;                                                 // ntp dla Time / Date Serwer     --- //
  47. NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);                                                   //
  48.                                                                                                       //
  49. // -------------------------------------------------------------------------------------------------- //    <--- V A R I A B L E S
  50. // --- O T H E R   V A R I A B L E   &   C O N S T                                                --- //
  51. // -------------------------------------------------------------------------------------------------- //
  52.                                                                                                       //                                                                                                      
  53. const char *ssid     = "NazwaWIFI";                                    // network wifi ssid       --- //
  54. const char *password = "TwojeHaslo";                                   // network wifi password   --- //
  55. String openWeatherMapApiKey = "565a88311e7c48c2d00b170cd0765c91";      // token for openWeather   --- //
  56. String city = "Legnica";                                               // city for weather        --- //
  57. String countryCode = "Pl";                                             // country for weather     --- //
  58. String jsonBuffer;                                                     // buffer - bufor          --- //
  59.                                                                                                       //
  60. // -------------------------------------------------------------------------------------------------- //    <--- S T R U C T   D A T E & T I M E
  61. // 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                                  --- //
  62. // -------------------------------------------------------------------------------------------------- //
  63. struct datatime                                                                                       //
  64. {                                                                                                     //
  65.     String SformatowanaData;                                           // formated data ------------- //
  66.     int    DzienTygodnia;                                              // day of week --------------- //
  67.     int    DzienMiesiaca;                                              // day of month -------------- //
  68.     int    Miesiac;                                                    // month --------------------- //
  69.     int    Rok;                                                        // year ---------------------- //
  70.     String SformatowanyCzas;                                           // formated time ------------- //
  71.     int    Godzina;                                                    // hour ---------------------- //
  72.     int    Minuta;                                                     // minute -------------------- //
  73.     int    Sekunda;                                                    // seconds ------------------- //
  74.     char   wschod_slonca[10];                                          // sunrise ------------------- //
  75.     char   zachod_slonca[10];                                          // sunset -------------------- //
  76. } czas;                                                                // time object --------------- //
  77.                                                                                                       //
  78. // -------------------------------------------------------------------------------------------------- //    <--- S T R U C T   W E A T H E R
  79. // S T R U K T U R A   P O G O D A                                                                --- //
  80. // -------------------------------------------------------------------------------------------------- //
  81. struct weather                                                                                        //
  82. {                                                                                                     //
  83.     double temperatura;                                                // temperature / temperatura - //
  84.     double temperatura_minimalna;                                      // min. temp ----------------- //
  85.     double temperatura_maksymalna;                                     // max. temp ----------------- //  
  86.     double temperatura_odczuwalna;                                     // feels temp ---------------- //
  87.     int cisnienie;                                                     // cisnienie powietrza ------- //
  88.     int wilgotnosc;                                                    // wilgotnosc powietrza ------ //
  89.     int wiatr;                                                         // wiatr / wind speed -------- //
  90.     int zachmurzenie;                                                  // zachmurzenie / cloudys ---- //
  91.     long widzialnosc;                                                  // widzialnosc / visible ----- //
  92. } pogoda;                                                              // weather object ------------ //
  93.                                                                                                       //
  94. // -------------------------------------------------------------------------------------------------- //   <--- S E T U P
  95. // --- S E T U P     M A I N   S E C T I O N                                                      --- //
  96. // -------------------------------------------------------------------------------------------------- //                                                                                                      //
  97.                                                                                                       //
  98. void setup()                                                                                          //
  99. {                                                                                                     //
  100.     #if TERMINAL                                                                                      //
  101.         Serial.begin(57600);                                           // set speed USB          ---- //
  102.     #endif                                                                                            //
  103.                                                                                                       //
  104.                                                                                                       //
  105.     WiFi.begin(ssid, password);                                        // WiFi start connection ----- //
  106.                                                                                                       //
  107.     delay(2000);                                                                                      //
  108.                                                                                                       //
  109.     while ( WiFi.status() != WL_CONNECTED )                            // czekaj na połączenie  ----- //   <--- WiFi Connected
  110.     {                                                                  // wait for connect      ----- //
  111.         delay ( 200 );                                                 // safe mode             ----- //    
  112.         Serial.print ( "." );                                          // print dot / drukuj .  ----- //            
  113.     }                                                                                                 //
  114.                                                                                                       //
  115.     #if TERMINAL                                                                                      //
  116.                                                                                                       //
  117.         #if POLISH                                                                                    //
  118.             Serial.printf("\nPOLACZONO z WiFi : %s\n", ssid);                                         //
  119.             Serial.printf("-------------------------------------\n");                                 //
  120.             Serial.printf("ESP Wolna Pamięć RAM : %u [kB]\n", ESP.getFreeHeap());                     //
  121.             Serial.printf("ESP Zegar procesora  : %u [MHz]\n", ESP.getCpuFreqMHz());                  //
  122.         #else                                                                                         //      
  123.             Serial.printf("\nCONNECT to WiFi : %s\n", ssid);                                          //
  124.             Serial.printf("-------------------------------------\n");                                 //
  125.             Serial.printf("ESP Memory Free   : %u [kB]\n",ESP.getFreeHeap());                         //
  126.             Serial.printf("ESP CPU frequency : %u [MHz]\n",ESP.getCpuFreqMHz());                      //
  127.         #endif                                                                                        //                                                                                                      
  128.                                                                                                       //
  129.     #endif                                                                                            //
  130.                                                                                                       //
  131.     WiFiUDP ntpUDP;                                                     // ntp client             --- //
  132.     NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);                 // ntp server             --- //
  133.                                                                                                       //
  134.     TimeUpdate();                                                       // update time from ntp   --- //   <--- Update TIME  
  135.     DateUpdate();                                                       // update date from ntp   --- //   <--- Update DATE
  136.     WeatherUpdate();                                                    // update weather from om --- //   <--- Update WEATHER
  137.                                                                                                       //
  138.     #if TERMINAL                                                                                      //
  139.                                                                                                       //
  140.         DateUpdateShow();                                               // show Update Date       --- //   <--- only if TERMINAL true
  141.         TimeUpdateShow();                                               // show Update Time       --- //   <--- jeśli TERMINAL jest 1
  142.         WeatherUpdateShow();                                            // show Update Weather    --- //
  143.                                                                                                       //
  144.     #endif                                                                                            //
  145. }                                                                                                     //
  146. // --- END SETUP ------------------------------------------------------------------------------------ //   <--- S E T U P   E N D
  147. //
  148. //
  149. //
  150. //
  151. //
  152. //
  153. //
  154. //
  155. // -------------------------------------------------------------------------------------------------- //
  156. // --- L O O P                                                                                    --- //    <--- L O O P   S T A R T   H E R E
  157. // -------------------------------------------------------------------------------------------------- //
  158.  
  159. void loop()
  160. {
  161.    
  162.     delay(1000);
  163.  
  164. }
  165.  
  166. // -------------------------------------------------------------------------------------------------- //
  167. // --- D A T E   P R I N T   T O   S E R I A L                                                    --- //   <--- DATE SHOW 2 SERIAL
  168. // -------------------------------------------------------------------------------------------------- //
  169.                                                                                                       //
  170. void DateUpdateShow()                                                                                 //
  171. {                                                                                                     //
  172.     #if TERMINAL                                                                                      //
  173.                                                                                                       //
  174.         #if POLISH                                                                                    //   <--- Raport po Polsku
  175.             Serial.printf("\n----------- NTP RAPORT --- DATA --------------------\n");                //
  176.             Serial.printf("Dzień tygodnia   : %d \n", czas.DzienTygodnia);                            //
  177.             Serial.printf("Dzień miesiąca   : %d \n", czas.DzienMiesiaca);                            //
  178.             Serial.printf("Miesiąc          : %d \n", czas.Miesiac);                                  //
  179.             Serial.printf("Rok              : %d", czas.Rok);                                         //
  180.         #endif                                                                                        //
  181.                                                                                                       //
  182.         #if ENGLISH                                                                                   //   <--- English Raport
  183.             Serial.printf("\n----------- NTP RAPORT --- DATE --------------------\n");                //
  184.             Serial.printf("Day of week      : %d \n", czas.DzienTygodnia);                            //
  185.             Serial.printf("Day number       : %d \n", czas.DzienMiesiaca);                            //
  186.             Serial.printf("Month number     : %d \n", czas.Miesiac);                                  //
  187.             Serial.printf("Year             : %d", czas.Rok);                                         //
  188.         #endif                                                                                        //
  189.                                                                                                       //
  190.     #endif                                                                                            //
  191. }                                                                                                     //
  192.                                                                                                       //
  193. // -------------------------------------------------------------------------------------------------- //
  194. // --- D A T A   U P D A T E                                                                      --- //
  195. // -------------------------------------------------------------------------------------------------- //
  196. // --- aktualizuj datę z serwera : pool.ntp.org ----------------------------------------------------- //   <--- aktualizacja daty z serwera NTP
  197. // -------------------------------------------------------------------------------------------------- //
  198. void DateUpdate()                                                                                     //
  199. {                                                                                                     //
  200.   timeClient.update();                                                                                //
  201.   unsigned long epochTime = timeClient.getEpochTime();                                                //
  202.   struct tm *ptm = gmtime ((time_t *)&epochTime);                                                     //
  203.                                                                                                       //
  204.   czas.SformatowanaData  =   timeClient.getFormattedTime();                                           //
  205.   czas.DzienTygodnia     =   timeClient.getDay();                                                     //
  206.   czas.DzienMiesiaca     =   ptm->tm_mday;                                                            //
  207.   czas.Miesiac           =   ptm->tm_mon+1;                                                           //
  208.   czas.Rok               =   ptm->tm_year+1900;                                                       //
  209.                                                                                                       //
  210.   #if DEBUG                                                                                           //
  211.       #if TERMINAL                                                                                    //
  212.           #if POLISH                                                                                  //
  213.               Serial.printf("\nPROJECT : zaktualizowano datę z serwera pool.ntp.org");                //
  214.           #else                                                                                       //
  215.               Serial.printf("\nPROJECT : update data from server pool.ntp.org");                      //
  216.           #endif                                                                                      //
  217.       #endif                                                                                          //
  218.   #endif                                                                                              //
  219. }                                                                                                     //
  220. // -------------------------------------------------------------------------------------------------- //
  221. // --- T I M E   P R I N T   T O   S E R I A L                                                    --- //   <---TIME SHOW 2 SERIAL
  222. // -------------------------------------------------------------------------------------------------- //
  223.                                                                                                       //
  224. void TimeUpdateShow()                                                                                 //
  225. {                                                                                                     //
  226.     #if TERMINAL                                                                                      //
  227.                                                                                                       //
  228.         #if POLISH                                                                                    //   <--- Raport po Polsku
  229.                                                                                                       //
  230.             Serial.printf("\n----------- NTP RAPORT --- CZAS --------------------\n");                //
  231.                                                                                                       //
  232.             if(czas.Godzina < 10)                                                                     //
  233.                 Serial.printf("Godzina          : 0%d \n", czas.Godzina);                             //
  234.             else                                                                                      //
  235.                 Serial.printf("Godzina          : %d \n", czas.Godzina);                              //
  236.                                                                                                       //
  237.             if(czas.Minuta < 10)                                                                      //
  238.                 Serial.printf("Minuta           : 0%d \n", czas.Minuta);                              //
  239.             else                                                                                      //
  240.                 Serial.printf("Minuta           : %d \n", czas.Minuta);                               //
  241.                                                                                                       //
  242.             if(czas.Sekunda < 10)                                                                     //
  243.                 Serial.printf("Sekunda          : 0%d", czas.Sekunda);                                //
  244.             else                                                                                      //
  245.                 Serial.printf("Sekunda          : %d", czas.Sekunda);                                 //
  246.         #endif                                                                                        //
  247.                                                                                                       //
  248.         #if ENGLISH                                                                                   //   <--- English Raport
  249.                                                                                                       //
  250.             Serial.printf("\n----------- NTP RAPORT --- TIME --------------------\n");                //
  251.                                                                                                       //
  252.             if(czas.Godzina < 10)                                                                     //
  253.                 Serial.printf("Hour             : 0%d \n", czas.Godzina);                             //
  254.             else                                                                                      //
  255.                 Serial.printf("Hour             : %d \n", czas.Godzina);                              //
  256.                                                                                                       //
  257.             if(czas.Minuta < 10)                                                                      //
  258.                 Serial.printf("Minute           : 0%d \n", czas.Minuta);                              //
  259.             else                                                                                      //
  260.                 Serial.printf("Minute           : %d \n", czas.Minuta);                               //
  261.                                                                                                       //
  262.             if(czas.Sekunda < 10)                                                                     //
  263.                 Serial.printf("Second           : 0%d", czas.Sekunda);                                //
  264.             else                                                                                      //
  265.                 Serial.printf("Second           : %d", czas.Sekunda);                                 //
  266.         #endif                                                                                        //
  267.                                                                                                       //
  268.     #endif                                                                                            //
  269. }                                                                                                     //
  270.                                                                                                       //
  271. // -------------------------------------------------------------------------------------------------- //
  272. // --- T I M E   U P D A T E                                                                      --- //
  273. // -------------------------------------------------------------------------------------------------- //
  274. // --- aktualizuj czas z serwera : pool.ntp.org ----------------------------------------------------- //   <--- aktualizacja czasu z serwera NTP
  275. // -------------------------------------------------------------------------------------------------- //
  276. void TimeUpdate()                                                                                     //
  277. {                                                                                                     //
  278.   timeClient.update();                                                                                //
  279.                                                                                                       //
  280.   czas.Godzina  =  timeClient.getHours();               // zapisz godzinę z serwera ----------------- //
  281.   czas.Minuta   =  timeClient.getMinutes();             // zapisz minutę z serwera ------------------ //
  282.   czas.Sekunda  =  timeClient.getSeconds();             // zapisz sekundę z serwera ----------------- //
  283.                                                                                                       //
  284.   #if DEBUG                                                                                           //
  285.       #if TERMINAL                                                                                    //
  286.           #if POLISH                                                                                  //
  287.               Serial.printf("\nPROJECT : zaktualizowano czas z serwera pool.ntp.org");                //
  288.           #else                                                                                       //
  289.               Serial.printf("\nPROJECT : update time from server pool.ntp.org");                      //
  290.           #endif                                                                                      //
  291.       #endif                                                                                          //
  292.   #endif                                                                                              //
  293. }                                                                                                     //
  294. // -------------------------------------------------------------------------------------------------- //
  295. // --- W E A T H E R   P R I N T   T O   S E R I A L                                              --- //   <--- WEATHER SHOW 2 SERIAL
  296. // -------------------------------------------------------------------------------------------------- //
  297.                                                                                                       //
  298. void WeatherUpdateShow()                                                                              //
  299. {                                                                                                     //
  300.     #if TERMINAL                                                                                      //
  301.                                                                                                       //
  302.         #if POLISH                                                                                    //   <--- Raport po Polsku
  303.             Serial.printf("\n----------- NTP RAPORT --- POGODA ------------------\n");                //
  304.             Serial.printf("Temperatura      : %.2f st.C\n", pogoda.temperatura);                      //
  305.             Serial.printf("Temp. Odczuwalna : %.2f st.C\n", pogoda.temperatura_odczuwalna);           //
  306.             Serial.printf("Temp. Minimalna  : %.2f st.C\n", pogoda.temperatura_minimalna);            //
  307.             Serial.printf("Temp. Maksymalna : %.2f st.C\n", pogoda.temperatura_maksymalna);           //
  308.             Serial.printf("Ćisnienie        : %d [ hPa ]\n", pogoda.cisnienie);                       //
  309.             Serial.printf("Wilgotność       : %d [ %% ]\n", pogoda.wilgotnosc);                       //
  310.             Serial.printf("Zachmurzenie     : %d [ %% ]\n", pogoda.zachmurzenie);                     //
  311.             Serial.printf("Prędkość wiatru  : %d [ km/h ]\n", pogoda.wiatr);                          //
  312.             Serial.printf("Widzialność      : %d [ m ]\n", pogoda.widzialnosc);                       //
  313.             Serial.printf("Wschód słońca    : %s\n", czas.wschod_slonca);                             //
  314.             Serial.printf("Zachód słóńca    : %s\n", czas.zachod_slonca);                             //
  315.             Serial.printf("----------------------------------------------------");                    //
  316.         #endif                                                                                        //
  317.                                                                                                       //
  318.         #if ENGLISH                                                                                   //   <--- English Raport
  319.             Serial.printf("\n----------- NTP RAPORT --- Weather ------------------\n");               //          
  320.             Serial.printf("Temperature      : %.2f st.C\n", pogoda.temperatura);                      //
  321.             Serial.printf("Temp. Feels      : %.2f st.C\n", pogoda.temperatura_odczuwalna);           //
  322.             Serial.printf("Temp. Min.       : %.2f st.C\n", pogoda.temperatura_minimalna);            //
  323.             Serial.printf("Temp. Max        : %.2f st.C\n", pogoda.temperatura_maksymalna);           //
  324.             Serial.printf("Pressure         : %d [ hPa ]\n", pogoda.cisnienie);                       //
  325.             Serial.printf("Humidity         : %d [ %% ]\n", pogoda.wilgotnosc);                       //
  326.             Serial.printf("Clouds           : %d [ %% ]\n", pogoda.zachmurzenie);                     //
  327.             Serial.printf("Wind speed       : %d [ km/h ]\n", pogoda.wiatr);                          //
  328.             Serial.printf("Visibility       : %d [ m ]\n", pogoda.widzialnosc);                       //
  329.             Serial.printf("Sunrise Time     : %s\n", czas.wschod_slonca);                             //
  330.             Serial.printf("Sunset Time      : %s\n", czas.zachod_slonca);                             //
  331.             Serial.printf("----------------------------------------------------");                    //
  332.         #endif                                                                                        //
  333.                                                                                                       //
  334.     #endif                                                                                            //
  335. }                                                                                                     //
  336.                                                                                                       //
  337. // -------------------------------------------------------------------------------------------------- //
  338. // --- W E A T H E R   U P D A T E                                                                --- //
  339. // -------------------------------------------------------------------------------------------------- //
  340. // --- aktualizuj pogode z serwera : openwearhermap.org --------------------------------------------- //   <--- aktualizacja pogody z serwera OPEN
  341. // -------------------------------------------------------------------------------------------------- //
  342. void WeatherUpdate()                                                                                  //
  343. {                                                                                                     //
  344.   if(WiFi.status()== WL_CONNECTED)                                                                    //
  345.   {                                                                                                   //
  346.       String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + ","            //
  347.                            + countryCode + "&APPID=" + openWeatherMapApiKey;                          //
  348.                                                                                                       //
  349.       jsonBuffer = httpGETRequest(serverPath.c_str());                                                //
  350.                                                                                                       //
  351.       #if DEBUGER                                                                                     //
  352.           #if TERMINAL                                                                                //
  353.               Serial.println(jsonBuffer);             // DRUKUJE CAŁY NIESPARSOWY STRING z POGODA     //   <--- ALL WEATHER not PARSED
  354.           #endif                                                                                      //
  355.       #endif                                                                                          //
  356.                                                                                                       //
  357.       JSONVar myObject = JSON.parse(jsonBuffer);                                                      //
  358.                                                                                                       //
  359.       if (JSON.typeof(myObject) == "undefined")                                                       //
  360.       {                                                                                               //        
  361.         #if TERMINAL                                                                                  //
  362.             #if POLISH                                                                                //
  363.                 Serial.println("Bląd połączenia...");                                                 //
  364.             #else                                                                                     //
  365.                 Serial.println("Error connection...");                                                //
  366.             #endif                                                                                    //
  367.         #endif                                                                                        //
  368.                                                                                                       //
  369.         return;                                                                                       //
  370.       }                                                                                               //
  371.       // -------------------------------------------------------------------------------------------- //
  372.       // SHOW myObject                                                                                //
  373.                                                                                                       //
  374.       #if DEBUGER                                                                                     //
  375.           #if TERMINAL                                                                                //
  376.               Serial.println(myObject);                                                               //
  377.           #endif                                                                                      //
  378.       #endif                                                                                          //
  379.                                                                                                       //
  380.       // -------------------------------------------------------------------------------------------- //
  381.       // --- 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
  382.       // -------------------------------------------------------------------------------------------- //   <--- PARSING WEATHER ONLY
  383.                                                                                                       //
  384.       double Kelvin = 273.15;                                                                         //
  385.                                                                                                       //
  386.       pogoda.temperatura = myObject["main"]["temp"];                 // pobieramy wartość do skruktury//
  387.       pogoda.temperatura -= Kelvin;                                  // odejmujemy wartość Kelvin --- //
  388.                                                                                                       //
  389.       pogoda.temperatura_odczuwalna = myObject["main"]["feels_like"];                                 //
  390.       pogoda.temperatura_odczuwalna -= Kelvin;                                                        //
  391.                                                                                                       //
  392.       pogoda.temperatura_minimalna = myObject["main"]["temp_min"];                                    //
  393.       pogoda.temperatura_minimalna -= Kelvin;                                                         //
  394.                                                                                                       //
  395.       pogoda.temperatura_maksymalna = myObject["main"]["temp_max"];                                   //
  396.       pogoda.temperatura_maksymalna -= Kelvin;                                                        //
  397.                                                                                                       //
  398.       pogoda.cisnienie = myObject["main"]["pressure"];                                                //
  399.                                                                                                       //
  400.       pogoda.wilgotnosc = myObject["main"]["humidity"];                                               //
  401.                                                                                                       //
  402.       pogoda.wiatr = myObject["wind"]["speed"];                                                       //
  403.                                                                                                       //
  404.       pogoda.zachmurzenie = myObject["clouds"]["all"];                                                //
  405.                                                                                                       //
  406.       pogoda.widzialnosc = myObject["visibility"];                                                    //
  407.                                                                                                       // ------------- //
  408.       // --- wykorzystana biblioteka TimeLib.h -----------                                                             //
  409.       sprintf(czas.wschod_slonca, "%02d:%02d",hour(myObject["sys"]["sunrise"]),minute(myObject["sys"]["sunrise"]));    //                                    
  410.                                                                                                                        //
  411.       // --- wykorzystana biblioteka TimeLib.h -----------                                                             //
  412.       sprintf(czas.zachod_slonca, "%02d:%02d",hour(myObject["sys"]["sunset"]),minute(myObject["sys"]["sunset"]));      //
  413.                                                                                                                        //
  414.                                                                                                       // ------------- //
  415.       #if DEBUG                                                                                       //
  416.           #if TERMINAL                                                                                //
  417.               #if POLISH                                                                              //
  418.                   Serial.printf("\nPROJECT : zaktualizowano pogodę z serwera openweathermap");        //
  419.               #else                                                                                   //
  420.                   Serial.printf("\nPROJECT : update weather from server openweathermap");             //
  421.               #endif                                                                                  //
  422.           #endif                                                                                      //
  423.       #endif                                                                                          //
  424.   }                                                                                                   //
  425. }                                                                                                     //
  426. // -------------------------------------------------------------------------------------------------- //
  427. // --- httpGET                                                                                    --- //   <--- http GET request
  428. // -------------------------------------------------------------------------------------------------- //
  429. String httpGETRequest(const char* serverName)                                                         //
  430. {                                                                                                     //
  431.     HTTPClient http;                                                                                  //
  432.     http.begin(serverName);                                                                           //
  433.     int httpResponseCode = http.GET();                                                                //
  434.     String bufor = "{}";                                                                              //
  435.                                                                                                       //
  436.     if ( httpResponseCode > 0 )                                                                       //
  437.     {                                                                                                 //      
  438.         #if DEBUG                                                                                     //
  439.             #if TERMINAL                                                                              //
  440.                 #if POLISH                                                                            //
  441.                     Serial.printf("\nHTTP - Kod odpowiedzi: %d\n", httpResponseCode);                 //
  442.                 #else                                                                                 //
  443.                     Serial.printf("\nHTTP - Response code: %d\n", httpResponseCode);                  //
  444.                 #endif                                                                                //
  445.             #endif                                                                                    //
  446.         #endif                                                                                        //
  447.                                                                                                       //
  448.         bufor = http.getString();                                                                     //
  449.     }                                                                                                 //
  450.     else                                                                                              //
  451.     {                                                                                                 //        
  452.         #if DEBUG                                                                                     //
  453.             #if TERMINAL                                                                              //
  454.                 #if POLISH                                                                            //
  455.                     Serial.printf("\nHTTP - Kod Błędu: %d\n", httpResponseCode);                      //
  456.                 #else                                                                                 //
  457.                     Serial.printf("\nHTTP - Error code: %d\n", httpResponseCode);                     //
  458.                 #endif                                                                                //
  459.             #endif                                                                                    //
  460.         #endif                                                                                        //
  461.     }                                                                                                 //
  462.                                                                                                       //
  463.     http.end();                                                                                       //
  464.     return bufor;                                                                                     //
  465. }                                                                                                     //
  466. // -------------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment