Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- I N C L U D E --- //
- // -------------------------------------------------------------------------------------------------- //
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <MD_Parola.h>
- #include <MD_MAX72xx.h>
- #include <SPI.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- #include <Arduino_JSON.h>
- #include "matryca.h"
- // -------------------------------------------------------------------------------------------------- //
- // --- D E F I N E --- //
- // -------------------------------------------------------------------------------------------------- //
- #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
- #define MAX_DEVICES 8
- #define CLK_PIN D5 // or SCK
- #define DATA_PIN D7 // or MOSI
- #define CS_PIN D8 // or SS
- #define TERMINAL 1 // 0 - wyłącz SERIAL, 1 - włącz SERIAL ---- //
- #define USBSPEED 57600 // 57600 - prędkość Terminala szeregowego - //
- //
- // -------------------------------------------------------------------------------------------------- //
- // --- O B J E C T --- //
- // -------------------------------------------------------------------------------------------------- //
- MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
- Matryca M(P);
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);
- // -------------------------------------------------------------------------------------------------- //
- // --- C O N S T & V A R I A B L E --- //
- // -------------------------------------------------------------------------------------------------- //
- unsigned long czasZERO = 0; // --- czas ZERO dla funkcji MILLIS() ------------------- //
- unsigned long czas01mt = 0; // --- czas01m dla sprawdzania co 1 minutę -------------- //
- unsigned long timeout1 = 0; // --- timeout dla czas01mt------------------------------ //
- unsigned long czas10mt = 0; // --- czas10m dla sprawdzania co 10 minut -------------- //
- unsigned long timeout2 = 0; // --- timeout dla czas10mt ----------------------------- //
- const char *ssid = "nazwa_twojego_routera"; // np. ZIOMEK_HOME
- const char *password = "haslo_twojego_routera";
- String openWeatherMapApiKey = "565a88311e7c48c2d00b170cd0765c91";
- String city = "Legnica";
- String countryCode = "Pl";
- String jsonBuffer;
- // -------------------------------------------------------------------------------------------------- //
- // S T R U K T U R A C Z A S --- //
- // -------------------------------------------------------------------------------------------------- //
- struct datatime
- {
- String SformatowanaData;
- int DzienTygodnia;
- int DzienMiesiaca;
- int Miesiac;
- int Rok;
- String SformatowanyCzas;
- int Godzina;
- int Minuta;
- int Sekunda;
- } czas;
- // -------------------------------------------------------------------------------------------------- //
- // S T R U K T U R A P O G O D A --- //
- // -------------------------------------------------------------------------------------------------- //
- struct weather
- {
- double temperatura;
- double temperatura_minimalna;
- double temperatura_maksymalna;
- double temperatura_odczuwalna;
- int cisnienie;
- int wilgotnosc;
- int wiatr;
- } pogoda;
- // -------------------------------------------------------------------------------------------------- //
- // --- S E T U P --- //
- // -------------------------------------------------------------------------------------------------- //
- void setup(void)
- {
- P.begin();
- M.Setup();
- #if TERMINAL
- Serial.begin(57600);
- #endif
- WiFi.begin(ssid, password);
- while ( WiFi.status() != WL_CONNECTED )
- {
- delay ( 200 );
- #if TERMINAL
- Serial.print ( "." );
- #endif
- }
- #if TERMINAL
- Serial.println(" ");
- Serial.println(" POLACZONO z WIFI ");
- #endif
- timeClient.begin();
- DateUpdate();
- TimeUpdate();
- WeatherUpdate();
- M.Date2Char(czas.DzienTygodnia, czas.DzienMiesiaca, czas.Miesiac, czas.Rok);
- M.Time2Char(czas.Godzina, czas.Minuta, czas.Sekunda);
- M.Temp2Char(pogoda.temperatura, pogoda.cisnienie, pogoda.wilgotnosc, pogoda.wiatr);
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- L O O P --- //
- // -------------------------------------------------------------------------------------------------- //
- void loop(void)
- {
- czasZERO = millis();
- czas01mt = czasZERO - timeout1;
- czas10mt = czasZERO - timeout2;
- M.RandomDemo(); // URUCHOM DEMO ze wszystkimi metodami (Generator ESP8266TrueRandom)
- // --- wykonaj co 1 minutę ------------------------------------------------------------------------ //
- if(czas01mt >= 60000UL)
- {
- TimeUpdate();
- M.Time2Char(czas.Godzina, czas.Minuta, czas.Sekunda);
- M.Date2Char(czas.DzienTygodnia, czas.DzienMiesiaca, czas.Miesiac, czas.Rok);
- timeout1 = czasZERO;
- }
- // --- wykonu co 10 minut ------------------------------------------------------------------------- //
- if(czas10mt >= 600000UL)
- {
- WeatherUpdate();
- DateUpdate();
- timeout2 = czasZERO;
- }
- // ------------------------------------------------------------------------------------------------ //
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- D A T A U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj datę z serwera : pool.ntp.org ----------------------------------------------------- //
- // -------------------------------------------------------------------------------------------------- //
- 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;
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- T I M E U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj czas z serwera : pool.ntp.org ----------------------------------------------------- //
- // -------------------------------------------------------------------------------------------------- //
- 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 ----------------- //
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- W E A T H E R U P D A T E --- //
- // -------------------------------------------------------------------------------------------------- //
- // --- aktualizuj pogode z serwera : openwearhermap.org --------------------------------------------- //
- // -------------------------------------------------------------------------------------------------- //
- 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 TERMINAL
- //Serial.println(jsonBuffer);
- #endif
- JSONVar myObject = JSON.parse(jsonBuffer);
- if (JSON.typeof(myObject) == "undefined")
- {
- // ------------------------------------------------------------------------------------------ //
- #if TERMINAL
- Serial.println("Blad parsowania");
- #endif
- // ------------------------------------------------------------------------------------------ //
- return;
- }
- // -------------------------------------------------------------------------------------------- //
- #if TERMINAL
- //Serial.println(myObject);
- #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 --- //
- // -------------------------------------------------------------------------------------------- //
- 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"];
- #if TERMINAL
- Serial.println("WeatherUpdate -> pobrane pogode z serwera: openweathermap.org ");
- #endif
- }
- }
- // -------------------------------------------------------------------------------------------------- //
- // --- W E A T H E R G E T --- //
- // -------------------------------------------------------------------------------------------------- //
- String httpGETRequest(const char* serverName)
- {
- HTTPClient http;
- http.begin(serverName);
- int httpResponseCode = http.GET();
- String bufor = "{}";
- if (httpResponseCode>0)
- {
- #if TERMINAL
- //Serial.print("HTTP Response code: ");
- //Serial.println(httpResponseCode);
- #endif
- bufor = http.getString();
- }
- else
- {
- #if TERMINAL
- //Serial.print("Error code: ");
- //Serial.println(httpResponseCode);
- #endif
- }
- http.end();
- return bufor;
- }
- // -------------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement