Advertisement
PowerTGS440

ESP2886_MatrixWeather.ino

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