Advertisement
PowerTGS440

ESP8266 Weather & NTP

Mar 27th, 2021
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.37 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <NTPClient.h>
  4. #include <WiFiUdp.h>
  5. #include <ESP8266HTTPClient.h>
  6. #include <WiFiClient.h>
  7. #include <Arduino_JSON.h>
  8.  
  9. // -------------------------------------------------------------------------------------------------- //
  10. // --- D E F I N E                                                                                --- //
  11. // -------------------------------------------------------------------------------------------------- //
  12.  
  13. #define TERMINAL 1                                        // 0 - wyłącz SERIAL, 1 - włącz SERIAL ---- //
  14. #define USBSPEED 57600                                    // 57600 - prędkość Terminala szeregowego - //
  15.                                                                                                       //
  16. // -------------------------------------------------------------------------------------------------- //
  17. // --- O B J E C T                                                                                --- //
  18. // -------------------------------------------------------------------------------------------------- //
  19.  
  20. WiFiUDP ntpUDP;
  21. NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);
  22.  
  23.  
  24. // -------------------------------------------------------------------------------------------------- //
  25. // --- C O N S T   &   V A R I A B L E                                                            --- //
  26. // -------------------------------------------------------------------------------------------------- //
  27. unsigned long czasZERO = 0;                 // --- czas ZERO dla funkcji MILLIS() ------------------- //
  28. unsigned long czas01mt = 0;                 // --- czas01m dla sprawdzania co 1 minutę -------------- //
  29. unsigned long timeout1 = 0;                 // --- timeout dla czas01mt------------------------------ //
  30. unsigned long czas10mt = 0;                 // --- czas10m dla sprawdzania co 10 minut -------------- //
  31. unsigned long timeout2 = 0;                 // --- timeout dla czas10mt ----------------------------- //
  32.  
  33. const char *ssid     = "nazwa_twojego_routera";
  34. const char *password = "haslo_twojego_routera";
  35.  
  36. String openWeatherMapApiKey = "565a88311e7c48c2d00b170cd0765c91";
  37.  
  38. String city = "Legnica";
  39. String countryCode = "Pl";
  40. String jsonBuffer;
  41.  
  42. // -------------------------------------------------------------------------------------------------- //
  43. // S T R U K T U R A   C Z A S                                                                    --- //
  44. // -------------------------------------------------------------------------------------------------- //
  45. struct datatime
  46. {    
  47.     String SformatowanaData;    
  48.     int    DzienTygodnia;
  49.     int    DzienMiesiaca;
  50.     int    Miesiac;
  51.     int    Rok;
  52.     String SformatowanyCzas;
  53.     int    Godzina;
  54.     int    Minuta;
  55.     int    Sekunda;
  56. } czas;
  57.  
  58. // -------------------------------------------------------------------------------------------------- //
  59. // S T R U K T U R A   P O G O D A                                                                --- //
  60. // -------------------------------------------------------------------------------------------------- //
  61. struct weather
  62. {
  63.     double temperatura;
  64.     double temperatura_minimalna;
  65.     double temperatura_maksymalna;
  66.     double temperatura_odczuwalna;
  67.     int cisnienie;
  68.     int wilgotnosc;
  69.     int wiatr;
  70.  
  71. } pogoda;
  72.  
  73. // -------------------------------------------------------------------------------------------------- //
  74. // --- S E T U P                                                                                  --- //
  75. // -------------------------------------------------------------------------------------------------- //
  76. void setup(void)
  77. {
  78.  
  79.   #if TERMINAL
  80.       Serial.begin(57600);
  81.   #endif
  82.  
  83.   WiFi.begin(ssid, password);
  84.  
  85.   while ( WiFi.status() != WL_CONNECTED )
  86.   {
  87.     delay ( 200 );
  88.     #if TERMINAL        
  89.         Serial.print ( "." );
  90.     #endif
  91.   }
  92.  
  93.   #if TERMINAL
  94.     Serial.println(" ");
  95.     Serial.println(" POLACZONO z WIFI ");
  96.   #endif
  97.  
  98.   timeClient.begin();
  99.  
  100.   DateUpdate();
  101.   TimeUpdate();
  102.   WeatherUpdate();
  103.  
  104. }
  105. // -------------------------------------------------------------------------------------------------- //
  106. // --- L O O P                                                                                    --- //
  107. // -------------------------------------------------------------------------------------------------- //
  108. void loop(void)                                
  109. {                                              
  110.   czasZERO = millis();                    
  111.   czas01mt = czasZERO - timeout1;          
  112.   czas10mt = czasZERO - timeout2;                  
  113.                                            
  114.   // --- wykonaj co 1 minutę ------------------------------------------------------------------------ //
  115.   if(czas01mt >= 60000UL)                  
  116.   {                                        
  117.       TimeUpdate();                        
  118.       timeout1 = czasZERO;                                                                        
  119.   }                                                                                                
  120.   // --- wykonu co 10 minut ------------------------------------------------------------------------- //
  121.   if(czas10mt >= 600000UL)
  122.   {
  123.       WeatherUpdate();
  124.       DateUpdate();
  125.       timeout2 = czasZERO;
  126.   }
  127.   // ------------------------------------------------------------------------------------------------ //
  128. }
  129.                                                          
  130. // -------------------------------------------------------------------------------------------------- //
  131. // --- D A T A   U P D A T E                                                                      --- //
  132. // -------------------------------------------------------------------------------------------------- //
  133. // --- aktualizuj datę z serwera : pool.ntp.org ----------------------------------------------------- //
  134. // -------------------------------------------------------------------------------------------------- //
  135. void DateUpdate()                                                        
  136. {                                                                        
  137.   timeClient.update();                                                  
  138.   unsigned long epochTime = timeClient.getEpochTime();                  
  139.   struct tm *ptm = gmtime ((time_t *)&epochTime);                        
  140.                                                                          
  141.   czas.SformatowanaData  =   timeClient.getFormattedTime();              
  142.   czas.DzienTygodnia     =   timeClient.getDay();                        
  143.   czas.DzienMiesiaca     =   ptm->tm_mday;                              
  144.   czas.Miesiac           =   ptm->tm_mon+1;                              
  145.   czas.Rok               =   ptm->tm_year+1900;                          
  146. }                                                                        
  147. // -------------------------------------------------------------------------------------------------- //
  148. // --- T I M E   U P D A T E                                                                      --- //
  149. // -------------------------------------------------------------------------------------------------- //
  150. // --- aktualizuj czas z serwera : pool.ntp.org ----------------------------------------------------- //
  151. // -------------------------------------------------------------------------------------------------- //
  152. void TimeUpdate()                                                
  153. {                                                                
  154.   timeClient.update();                                          
  155.                                                                  
  156.   czas.Godzina  =  timeClient.getHours();               // zapisz godzinę z serwera ----------------- //
  157.   czas.Minuta   =  timeClient.getMinutes();             // zapisz minutę z serwera ------------------ //
  158.   czas.Sekunda  =  timeClient.getSeconds();             // zapisz sekundę z serwera ----------------- //
  159. }                                                                
  160. // -------------------------------------------------------------------------------------------------- //
  161. // --- W E A T H E R   U P D A T E                                                                --- //
  162. // -------------------------------------------------------------------------------------------------- //
  163. // --- aktualizuj pogode z serwera : openwearhermap.org --------------------------------------------- //
  164. // -------------------------------------------------------------------------------------------------- //
  165. void WeatherUpdate()                                                                                
  166. {                                                                                                    
  167.   if(WiFi.status()== WL_CONNECTED)                                                                  
  168.   {                                                                                                  
  169.       String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + ","          
  170.                            + countryCode + "&APPID=" + openWeatherMapApiKey;                        
  171.                                                                                                      
  172.       jsonBuffer = httpGETRequest(serverPath.c_str());                                              
  173.                                                                                                      
  174.       #if TERMINAL                                                                                  
  175.           //Serial.println(jsonBuffer);                                                              
  176.       #endif                                                                                        
  177.                                                                                                      
  178.       JSONVar myObject = JSON.parse(jsonBuffer);                                                    
  179.                                                                                                      
  180.       if (JSON.typeof(myObject) == "undefined")                                                      
  181.       {                                                                                              
  182.         // ------------------------------------------------------------------------------------------ //                                        
  183.         #if TERMINAL                                                                                  
  184.             Serial.println("Blad parsowania");                                                    
  185.         #endif                                                                                        
  186.         // ------------------------------------------------------------------------------------------ //                                        
  187.         return;                                                                                
  188.       }                                                                                        
  189.       // -------------------------------------------------------------------------------------------- //
  190.       #if TERMINAL                                                                                    
  191.           //Serial.println(myObject);                                                                
  192.       #endif                                                                                          
  193.       // -------------------------------------------------------------------------------------------- //
  194.       // --- 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                          --- //
  195.       // -------------------------------------------------------------------------------------------- //
  196.                                                                                                    
  197.       double Kelvin = 273.15;                                                                      
  198.                                                                                                    
  199.       pogoda.temperatura = myObject["main"]["temp"];                 // pobieramy wartość do skruktury
  200.       pogoda.temperatura -= Kelvin;                                  // odejmujemy wartość Kelvin ----
  201.                                                                                                    
  202.       pogoda.temperatura_odczuwalna = myObject["main"]["feels_like"];                                
  203.       pogoda.temperatura_odczuwalna -= Kelvin;                                                      
  204.                                                                                                      
  205.       pogoda.temperatura_minimalna = myObject["main"]["temp_min"];                                  
  206.       pogoda.temperatura_minimalna -= Kelvin;                                                        
  207.                                                                                                      
  208.       pogoda.temperatura_maksymalna = myObject["main"]["temp_max"];                                  
  209.       pogoda.temperatura_maksymalna -= Kelvin;                                                      
  210.                                                                                                      
  211.       pogoda.cisnienie = myObject["main"]["pressure"];                                              
  212.                                                                                                      
  213.       pogoda.wilgotnosc = myObject["main"]["humidity"];                                              
  214.                                                                                                      
  215.       pogoda.wiatr = myObject["wind"]["speed"];                                                      
  216.                                                                                                      
  217.       #if TERMINAL                                                                                    
  218.         Serial.println("WeatherUpdate -> pobrane pogode z serwera: openweathermap.org ");            
  219.       #endif                                                                                          
  220.   }                                                                                                  
  221. }                                                                                                    
  222. // -------------------------------------------------------------------------------------------------- //
  223. // --- W E A T H E R   G E T                                                                      --- //
  224. // -------------------------------------------------------------------------------------------------- //
  225. String httpGETRequest(const char* serverName)
  226. {
  227.     HTTPClient http;
  228.     http.begin(serverName);
  229.     int httpResponseCode = http.GET();
  230.     String bufor = "{}";
  231.     if (httpResponseCode>0)
  232.     {
  233.         #if TERMINAL
  234.             //Serial.print("HTTP Response code: ");
  235.             //Serial.println(httpResponseCode);
  236.         #endif
  237.         bufor = http.getString();
  238.     }
  239.     else
  240.     {
  241.         #if TERMINAL
  242.             //Serial.print("Error code: ");
  243.             //Serial.println(httpResponseCode);
  244.         #endif
  245.      }
  246.     http.end();
  247.     return bufor;
  248. }
  249. // -------------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement