Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------------------------------------------------------------------------------------------- //
- // //
- // Simple ESP8266 NodeMCU 2.3 SimpleTFT Clock & DHT11 & AM2320 by TOMASZ MAJCHER (TRANSLINE GROUP POAND) //
- // //
- // e-mail : [email protected] //
- // licence : DonationWare //
- // paypal : @TomaszMajcher //
- // www : https://esp32dev.blogspot.com/ //
- // //
- // ------------------------------------------------------------------------------------------------------------------- //
- //
- // v1.0 Base (13.06.2021) by Tomasz Majcher (tools & base)
- // v2.3 Beta (23.06.2021) by Tomasz Majcher (clock, date, DHT11, AM2320) Free Full-Beta Version
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- //
- // ILI9341 Driver (Pinout) Hardware SPI !!!
- //
- // VCC to : 3.3V
- // GND to : GND
- // CS pin to : D3
- // Reset pin to : Reset
- // DC pin to : D4
- // SDI pin to : D7
- // SCK pin to : D5
- //
- //
- // AM2320 Driver (Pinout) SCA/SDL
- //
- // VCC to : 3.3V
- // GND to : GND
- // SCL to : D1 (ESP8266)
- // SDA to : D2 (ESP8266)
- //
- //
- // DHT11 Driver (Pinout)
- //
- // VCC to : 3.3V
- // GND to : GND
- // DATA to : D0
- //
- //
- //
- //
- // (23.06.2021) v2.4 beta - update : add multilanguage (on TFT)
- // (23.06.2021) v2.3 beta - update : add switch (dat/evening/night) mode
- // (23.06.2021) v2.2 beta - update : add Night Mode (stars)
- // (23.06.2021) v2.1 beta - update : add Evening Mode Color
- // (22.06.2021) v2.0 final - official free final version !!!
- // (22.06.2021) v1.9 beta - update : add TFT_ShowTemperature & TFT_ShowHumidity (DHT11) outside ROOM
- // (22.06.2021) v1.8 beta - update : add DHT11 sensor (temperature & humidity) outside ROOM
- // (21.06.2021) v1.7 beta - update : add TFT_ShowTemperature & TFT_ShowHumidity (AM2320) inside ROOM
- // (21.06.2021) v1.6 beta - update : add AM2320 sensor (temperature & humudity) inside ROOM
- // (20.06.2021) v1.5 beta - update : add TFT_ShowProgres (show progres bar for second)
- // (20.06.2021) v1.4 beta - update : add TFT_ShowBug (show flying bug)
- // (20.06.2021) v1.3 beta - update : add TFT_ShowDate (show date on TFT display) from Task3 (one per min)
- // (20.06.2021) v1.2 beta - update : add TFT_ShowTime (show time on TFT display) from Task0 (one per sec)
- // (20.06.2021) v1.1 beta - update : add TFT (test, setup library -> ILI9341, pinout)
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- I N C L U D E L I B R A R Y --------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- extern "C" char* sbrk(int incr);
- #include <ESP8266WiFi.h> // biblioteka Wi-Fi
- #include <NTPClient.h> // NTP klient (Time)
- #include <WiFiUdp.h> // UDP dla NTP (Time)
- #include <TaskScheduler.h> // scheluder
- #include <TimeLib.h>
- #include <Arduino.h>
- // --- AM2320 Temperature & Humidity Sensor -------------------------------------------------------------------------- //
- #include <Adafruit_Sensor.h> //AM2320 sensor library
- #include <Adafruit_AM2320.h> //AM2320 sensor library
- Adafruit_AM2320 AM2320 = Adafruit_AM2320(); // create object
- // --- DHT11 Temperature & Humidity Sensor --------------------------------------------------------------------------- //
- #include "DHTesp.h" // library
- #define DHTpin D0 // define PIN
- DHTesp dht; // create object
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- L A N G U A G E S E T U P ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define LANGUAGE 0 // 0 (Polish), 1 (English), 2 (Germany)
- #if LANGUAGE == 0
- #define POLISH 1
- #define ENGLISH 0
- #define GERMANY 0
- #endif
- #if LANGUAGE == 1
- #define POLISH 0
- #define ENGLISH 1
- #define GERMANY 0
- #endif
- #if LANGUAGE == 2
- #define POLISH 0
- #define ENGLISH 0
- #define GERMANY 1
- #endif
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M A C R O D E F I N E --------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- N E T W O R K S E T T I N G S ------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- const char *ssid = "your_ssid"; // network wifi ssid
- const char *password = "your_pass"; // network wifi password
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T --------------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- //#include "Free_Fonts.h"
- #include "SPI.h"
- #include "TFT_eSPI.h"
- TFT_eSPI tft = TFT_eSPI();
- // define color
- #define TFT_LIGHTGRAY tft.color565(163, 159, 158)
- #define TFT_SWEETBLUE tft.color565(0, 128, 255)
- // define day mode color
- #define TFT_DAY_TEXT TFT_BLACK
- #define TFT_DAY_BACKGROUND TFT_LIGHTGRAY
- // define night mode color
- #define TFT_NIGHT_TEXT TFT_LIGHTGRAY
- #define TFT_NIGHT_BACKGROUND TFT_BLACK
- // tft mode
- bool day_mode = true; // tryb dzienny
- bool evening_mode = false; // tryb wieczorny
- bool night_mode = false; // tryb nocny
- bool change_mode = false; // flag mode
- int TFT_MODE_COLOR = TFT_BLACK; // start default color
- int TFT_MODE_BACKGROUND = TFT_LIGHTGRAY; // start default color
- //int TFT_MODE_COLOR = TFT_LIGHTGRAY; // start default color
- //int TFT_MODE_BACKGROUND = TFT_BLACK; // start default color
- // define cords
- #define x_min 0
- #define y_min 0
- #define x_max 320
- #define y_max 240
- #define frame 5
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E R I A L S E T T I N G S --------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define SERIAL true
- #define SerialSpeed 57600
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- P R O T O T Y P E --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void scheluderInit(); // run from setup() - init scheluder
- void statWiFi(); // show Wi-Fi Status
- void initWiFi(); // init Wi-Fi connection
- void TFT_Init(); // init TFT (from setup)
- void TFT_ShowDate(); // show Date on TFT
- void TFT_ShowTime(); // show Time on TFT
- void TFT_ShowBug(); // show BUG on TFT
- void TFT_ShowHumidity(); // show Humidity (inside / outside) from sensor AM2320 / DHT11
- void TFR_ShowTemperature(); // show Temperature (inside / outside) from sensor AM2320 / DHT11
- void TFT_ShowStars(); // show stars and time on NIGHT (night_mode)
- void TFT_ShowProgres(); // show progres bar for second
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- P R O T O T Y P E --- C O R E T A S K ------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task0Callback(); // function Task0 for ESP8266 Core
- void Task1Callback(); // function Task1 for ESP8266 Core
- void Task2Callback(); // function Task2 for ESP8266 Core
- void Task3Callback(); // function Task3 for ESP8266 Core
- void Task4Callback(); // function Task4 for ESP8266 Core
- void Task5Callback(); // function Task4 for ESP8266 Core
- void Task6Callback(); // function Task4 for ESP8266 Core
- #define Micros_01 50 // 1/100 sekundy
- #define Second_01 1000 // 01 second timeout makro definition
- #define Second_05 5000 // 05 second timeout makro definition
- #define Second_10 10000 // 10 second timeout makro definition
- #define Second_30 30000 // 30 second timeout makro definition
- #define Second_60 60000 // 60 second timeout makro definition
- #define Minute_01 60000 // 01 minute timeout makro definition
- #define Minute_05 300000 // 05 minute timeout makro definition
- #define Minute_10 600000 // 10 minute timeout makro definition
- #define Minute_30 1800000 // 30 minute timeout makro definiction
- #define Minute_60 3600000 // 30 minute timeout makro definiction
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Z A D A N I A S C H E L U D E R ' A ------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- Task task_0( Second_01, TASK_FOREVER, &Task0Callback); // task_0 run at interval Second_01
- Task task_1( Second_60, TASK_FOREVER, &Task1Callback); // task_1 run at interval Second_60
- Task task_2( Second_01, TASK_FOREVER, &Task2Callback); // task_2 run at interval Second_10
- Task task_3( Minute_01, TASK_FOREVER, &Task3Callback); // task_3 run at interval Minute_01
- Task task_4( Minute_10, TASK_FOREVER, &Task4Callback); // task_4 run at interval Minute_10
- Task task_5( Micros_01, TASK_FOREVER, &Task5Callback); // task_4 run at interval 1/10 sec
- Task task_6( Minute_60, TASK_FOREVER, &Task6Callback); // task_4 run at interval Minute_60
- Scheduler runner;
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** WiFi Reconect *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- unsigned long previousMillis = 0;
- unsigned long interval = 30000;
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA Time *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- struct TIME_DATA
- {
- int Hour = 0;
- int Minute = 0;
- int Second = 0;
- String FormatedData = "NULL";
- String ShortData = "NULL";
- String LongData = "NULL";
- String TFT_Data = "NULL";
- String TFT_Time = "NULL";
- int DayOfTheWeek = 0;
- int DayOfTheMonth = 0;
- int Month = 0;
- int Year = 0;
- }
- Time;
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA AM2320 *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- struct AM2320Sensor
- {
- float temperature = 0;
- float humidity = 0;
- }
- sensor;
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA DHT11 *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- struct MyDHT
- {
- float temperature = 22.12; // default - DHT11/DHT22 sample start value
- float humidity = 84.05; // default - DHT11/DHT22 sample start value
- }
- myDHT;
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** S T A R S *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define NSTARS 1024
- uint8_t sx[NSTARS] = {};
- uint8_t sy[NSTARS] = {};
- uint8_t sz[NSTARS] = {};
- uint8_t za, zb, zc, zx;
- uint8_t __attribute__((always_inline)) rng()
- {
- zx++;
- za = (za^zc^zx);
- zb = (zb+za);
- zc = ((zc+(zb>>1))^za);
- return zc;
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA DZIEN *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- #if POLISH
- const char Day_0 [] = "Niedziela";
- const char Day_1 [] = "Poniedzialek";
- const char Day_2 [] = "Wtorek";
- const char Day_3 [] = "Sroda";
- const char Day_4 [] = "Czwartek";
- const char Day_5 [] = "Piatek";
- const char Day_6 [] = "Sobota";
- #endif
- #if ENGLISH
- const char Day_0 [] = "Sunday";
- const char Day_1 [] = "Monday";
- const char Day_2 [] = "Tuesday";
- const char Day_3 [] = "Wednesday";
- const char Day_4 [] = "Thursday";
- const char Day_5 [] = "Friday";
- const char Day_6 [] = "Saturday";
- #endif
- #if GERMANY
- const char Day_0 [] = "Sonntag";
- const char Day_1 [] = "Montag";
- const char Day_2 [] = "Dienstag";
- const char Day_3 [] = "Mittwoch";
- const char Day_4 [] = "Donnerstag";
- const char Day_5 [] = "Freitag";
- const char Day_6 [] = "Samstag";
- #endif
- const char * const DAY[] = { Day_0, Day_1, Day_2, Day_3, Day_4, Day_5, Day_6 }; // dni tygodnia [0..6]
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA MONTH *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- #if POLISH
- const char MONTH_0 [] = "NULL";
- const char MONTH_1 [] = "Styczen";
- const char MONTH_2 [] = "Luty";
- const char MONTH_3 [] = "Marzec";
- const char MONTH_4 [] = "Kwiecien";
- const char MONTH_5 [] = "Maj";
- const char MONTH_6 [] = "Czerwiec";
- const char MONTH_7 [] = "Lipiec";
- const char MONTH_8 [] = "Sierpien";
- const char MONTH_9 [] = "Wrzesien";
- const char MONTH_10 [] = "Pazdziernik";
- const char MONTH_11 [] = "Listopad";
- const char MONTH_12 [] = "Grudzien";
- #endif
- #if ENGLISH
- const char MONTH_0 [] = "NULL";
- const char MONTH_1 [] = "January";
- const char MONTH_2 [] = "February";
- const char MONTH_3 [] = "March";
- const char MONTH_4 [] = "April";
- const char MONTH_5 [] = "May";
- const char MONTH_6 [] = "June";
- const char MONTH_7 [] = "July";
- const char MONTH_8 [] = "August";
- const char MONTH_9 [] = "September";
- const char MONTH_10 [] = "October";
- const char MONTH_11 [] = "November";
- const char MONTH_12 [] = "December";
- #endif
- #if GERMANY
- const char MONTH_0 [] = "NULL";
- const char MONTH_1 [] = "Januar";
- const char MONTH_2 [] = "Februar";
- const char MONTH_3 [] = "März";
- const char MONTH_4 [] = "April";
- const char MONTH_5 [] = "Mai";
- const char MONTH_6 [] = "Juni";
- const char MONTH_7 [] = "Juli";
- const char MONTH_8 [] = "August";
- const char MONTH_9 [] = "September";
- const char MONTH_10 [] = "Oktober";
- const char MONTH_11 [] = "November";
- const char MONTH_12 [] = "Dezember";
- #endif
- const char * const MONTH[] = { MONTH_0, MONTH_1, MONTH_2, MONTH_3, MONTH_4, MONTH_5, MONTH_6,
- MONTH_7, MONTH_8, MONTH_9, MONTH_10, MONTH_11, MONTH_12 };
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T O O L S C L A S S ----------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- class TOOLS
- {
- private:
- public:
- void TypeWriter(const String & message, unsigned int wait, bool lf = true);
- void FreeRam(bool lf = true);
- void CpuInfo(bool lf = true);
- void WiFiInfo(bool lf = true);
- inline void BreakLine();
- inline void PrintLine(byte lenght = 60);
- };
- // ------------------------------------------------------------------------------------------------------------------- //
- void TOOLS :: TypeWriter(const String & message, unsigned int wait, bool lf)
- {
- if(lf) Serial.printf("\n"); // if lf is true print next line
- for(const char c : message)
- {
- Serial.print(c);
- delay(wait);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- void TOOLS :: FreeRam(bool lf)
- {
- if(lf) Serial.printf("\n"); // if lf is true print next line
- Serial.printf("[TOOLS] [Memory Free] \t\t: %u [kB]", ESP.getFreeHeap());
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- void TOOLS :: CpuInfo(bool lf)
- {
- if(lf) Serial.printf("\n"); // if lf is true print next line
- Serial.printf("[TOOLS] [CPU frequency] \t: %u [MHz]",ESP.getCpuFreqMHz());
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- void TOOLS :: WiFiInfo(bool lf)
- {
- if(lf) Serial.printf("\n"); // if lf is true print next line
- Serial.printf("[TOOLS] [IP adress] \t\t: %s", WiFi.localIP().toString().c_str());
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- inline void TOOLS :: BreakLine()
- {
- Serial.printf("\n"); // if lf is true print next line
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- inline void TOOLS :: PrintLine(byte lenght)
- {
- for(byte i = 0; i <= lenght; i++) Serial.print("-");
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- O B J E C T I N I T ----------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);
- TOOLS tools;
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task0Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task0Callback()
- {
- String bufor = "NULL";
- static bool STEP = false;
- if (Time.Hour < 10)
- {
- bufor = "0";
- bufor += String(Time.Hour);
- }
- else
- {
- bufor = String(Time.Hour);
- }
- if (STEP) // step 0 sec show :
- {
- bufor += ":";
- STEP = false;
- }
- else // step 1 sec show _
- {
- bufor += " ";
- STEP = true;
- }
- if (Time.Minute < 10)
- {
- bufor += "0";
- bufor += String(Time.Minute);
- }
- else
- {
- bufor += String(Time.Minute);
- }
- Time.TFT_Time = bufor; // copy bufor to TFT_Time
- if (!night_mode) TFT_ShowTime(); // show time on TFT
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task1Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task1Callback()
- {
- // -------------------------------------------------------------------------------------------- //
- // --- temperature & humidity (INSIDE) from AM2320 sensor ------------------------------------- //
- // -------------------------------------------------------------------------------------------- //
- sensor.temperature = AM2320.readTemperature(); // get temperature from AM2320 sensor
- sensor.humidity = AM2320.readHumidity(); // get humidity from AM2320 sensor
- if (!night_mode) TFT_ShowTemperature(); // show temperature IN/OUT on TFT
- if (!night_mode) TFT_ShowHumidity(); // show humidity IN/OUT on TFT
- // -------------------------------------------------------------------------------------------- //
- // --- temperature & humidiry (OUTSIDE) from DHT11 sensor ------------------------------------- //
- // -------------------------------------------------------------------------------------------- //
- TempAndHumidity sensor = dht.getTempAndHumidity();
- myDHT.temperature = sensor.temperature;
- myDHT.humidity = sensor.humidity;
- // -------------------------------------------------------------------------------------------- //
- #if SERIAL
- String bufor;
- if (Time.Hour < 10)
- {
- bufor = "0";
- bufor += String(Time.Hour);
- }
- else
- {
- bufor = String(Time.Hour);
- }
- bufor += ":";
- if (Time.Minute < 10)
- {
- bufor += "0";
- bufor += String(Time.Minute);
- }
- else
- {
- bufor += String(Time.Minute);
- }
- if (Time.Hour < 12) bufor += " AM";
- else bufor += " PM";
- Serial.printf("\n\n[ESP] [TASK1] [Time NTP] \t: %s", bufor.c_str());
- Serial.printf("\n[ESP] [TASK1] [LongData] \t: %s ", Time.LongData.c_str());
- Serial.printf("\n[ESP] [TASK1] [ShortData] \t: %s ", Time.ShortData.c_str());
- #if POLISH
- Serial.printf("\n\n[ESP] [TASK1] [Temperatura] \t: %2.2f stopni C",sensor.temperature);
- Serial.printf("\n[ESP] [TASK1] [Wilgotnosc] \t: %2.2f %%RH",sensor.humidity);
- #endif
- #if GERMANY
- Serial.printf("\n\n[ESP] [TASK1] [Temperature] \t: %2.2f Grad C",sensor.temperature);
- Serial.printf("\n[ESP] [TASK1] [Feuchtigkeit] \t: %2.2f %%RH",sensor.humidity);
- #endif
- #if ENGLISH
- Serial.printf("\n\n[ESP] [TASK1] [Temperature] \t: %2.2f degrees C",sensor.temperature);
- Serial.printf("\n[ESP] [TASK1] [Humidity] \t: %2.2f %%RH",sensor.humidity);
- #endif
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task2Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task2Callback()
- {
- timeClient.update(); // time update
- Time.Hour = timeClient.getHours()+1; // zapisz godzinę z serwera
- Time.Minute = timeClient.getMinutes(); // zapisz minutę z serwera
- Time.Second = timeClient.getSeconds(); // zapisz sekundę z serwera
- if (!night_mode) TFT_ShowProgres();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task3Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task3Callback()
- {
- tools.BreakLine(); // breaking line
- tools.FreeRam(); // show Free RAM
- tools.CpuInfo(); // Show CPU
- tools.WiFiInfo(); // Show WiFi Adress
- if (!night_mode) TFT_ShowDate(); // Show Date on TFT
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task4Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task4Callback()
- {
- // ------------------------------------------------------------------------------------- //
- if(day_mode && Time.Hour >= 16) // change color to evening mode
- {
- day_mode = false;
- evening_mode = true;
- change_mode = true;
- }
- else
- if(evening_mode && Time.Hour >= 19) // change color to night mode
- {
- evening_mode = false;
- night_mode = true;
- change_mode = true;
- }
- else
- if(night_mode && Time.Hour >= 7 && Time.Hour < 20) // change color to day mode
- {
- night_mode = false;
- day_mode = true;
- change_mode = true;
- }
- // ------------------------------------------------------------------------------------ //
- if(day_mode)
- {
- TFT_MODE_COLOR = TFT_DAY_TEXT;
- TFT_MODE_BACKGROUND = TFT_DAY_BACKGROUND;
- }
- else
- if(evening_mode)
- {
- TFT_MODE_COLOR = TFT_NIGHT_TEXT;
- TFT_MODE_BACKGROUND = TFT_NIGHT_BACKGROUND;
- }
- else
- if(night_mode)
- {
- TFT_MODE_COLOR = TFT_NIGHT_TEXT;
- TFT_MODE_BACKGROUND = TFT_NIGHT_BACKGROUND;
- }
- // ------------------------------------------------------------------------------------ //
- if(change_mode && !night_mode)
- {
- TFT_DrawPanel();
- TFT_ShowBug();
- TFT_ShowDate(); // show Date on TFT
- TFT_ShowTime(); // show Time on TFT
- TFT_ShowHumidity(); // show Humidity (inside / outside) from sensor AM2320 / DHT11
- TFT_ShowTemperature(); // show Temperature (inside / outside) from sensor AM2320 / DHT11
- TFT_ShowProgres();
- change_mode = false;
- }
- else
- if(change_mode && night_mode)
- {
- change_mode = false;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task3Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task5Callback()
- {
- if (!night_mode) TFT_ShowBug(); // pokaż robala
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Task6Callback() ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task6Callback()
- {
- timeClient.update();
- unsigned long epochTime = timeClient.getEpochTime();
- struct tm *ptm = gmtime ( (time_t *) &epochTime );
- Time.DayOfTheWeek = timeClient.getDay();
- Time.DayOfTheMonth = ptm->tm_mday;
- Time.Month = ptm->tm_mon+1;
- Time.Year = ptm->tm_year+1900;
- Time.FormatedData = String(DAY[Time.DayOfTheWeek])
- + ", "
- + String(Time.DayOfTheMonth)
- + " "
- + String(MONTH[Time.Month])
- + " "
- + String(Time.Year);
- Time.ShortData = String(Time.DayOfTheMonth)
- + " "
- + String(MONTH[Time.Month])
- + " "
- + String(Time.Year);
- Time.LongData = String(DAY[Time.DayOfTheWeek])
- + " "
- + String(Time.DayOfTheMonth)
- + "/"
- + String(Time.Month)
- + "/"
- + String(Time.Year);
- String Buffor = "NULL";
- if(Time.DayOfTheMonth < 10)
- {
- Buffor = "0";
- Buffor += Time.DayOfTheMonth;
- }
- else Buffor = Time.DayOfTheMonth;
- Buffor += "/";
- if(Time.Month < 10)
- {
- Buffor += "0";
- Buffor += Time.Month;
- }
- else Buffor += Time.Month;
- Buffor += "/";
- Buffor += Time.Year;
- Time.TFT_Data = Buffor;
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- stat Wi-Fi ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void statWiFi()
- {
- switch (WiFi.status())
- {
- case WL_NO_SSID_AVAIL:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\nConfigured SSID cannot be reached");
- #endif
- #if POLISH
- Serial.printf("\nNie można osiągnąć skonfigurowanego identyfikatora SSID");
- #endif
- #if GERMANY
- Serial.printf("\nKonfigurierte SSID ist nicht erreichbar");
- #endif
- #endif
- break;
- case WL_CONNECTED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\nConnection successfully established");
- Serial.printf("\nYour IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\nReceived Signal Strength Indication : %d", WiFi.RSSI());
- #endif
- #if POLISH
- Serial.printf("\nPomyślnie nawiązano połączenie");
- Serial.printf("\nTwój adres IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\nWskazanie siły odbieranego sygnału : %d", WiFi.RSSI());
- #endif
- #if GERMANY
- Serial.printf("\nVerbindung erfolgreich hergestellt");
- Serial.printf("\nDeine IP-Adresse : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\nAnzeige der empfangenen Signalstärke : %d", WiFi.RSSI());
- #endif
- #endif
- break;
- case WL_CONNECT_FAILED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\nConnection failed");
- #endif
- #if POLISH
- Serial.printf("\nPołączenie nieudane");
- #endif
- #if GERMANY
- Serial.printf("\nVerbindung fehlgeschlagen");
- #endif
- #endif
- break;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- init Wi-Fi ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void initWiFi()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- #if SERIAL
- #if ENGLISH
- Serial.printf("\nConnecting to WiFi ..");
- #endif
- #if POLISH
- Serial.printf("\nŁącze z siecią WiFi ..");
- #endif
- #if GERMANY
- Serial.printf("\nNetzwerkverbindung mit WLAN ..");
- #endif
- #endif
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print('.');
- delay(200);
- }
- statWiFi();
- WiFi.setAutoReconnect(true);
- WiFi.persistent(true);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- reconect Wi-Fi ------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void reconectWifi()
- {
- unsigned long currentMillis = millis();
- if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval))
- {
- #if SERIAL
- #if ENGLISH
- Serial.println("Reconnecting to WiFi...");
- #endif
- #if POLISH
- Serial.println("Ponowne łączenie z Wi-Fi...");
- #endif
- #if GERMANY
- Serial.println("Erneut mit WLAN verbinden...");
- #endif
- #endif
- WiFi.disconnect();
- WiFi.begin(ssid, password);
- statWiFi();
- previousMillis = currentMillis;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- schedulerInit ------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void scheluderInit()
- {
- runner.init(); // inicjalizuj scheluder
- runner.addTask(task_0); // dodaj zadanie do scheluder
- task_0.enable(); // włącz zadanie do scheluder
- runner.addTask(task_1); // dodaj zadanie do scheluder
- task_1.enable(); // włącz zadanie do scheluder
- runner.addTask(task_2); // dodaj zadanie do scheluder
- task_2.enable(); // włącz zadanie do scheluder
- runner.addTask(task_3); // dodaj zadanie do scheluder
- task_3.enable(); // włącz zadanie do scheluder
- runner.addTask(task_4); // dodaj zadanie do scheluder
- task_4.enable(); // włącz zadanie do scheluder
- runner.addTask(task_5); // dodaj zadanie do scheluder
- task_5.enable(); // włącz zadanie do scheluder
- runner.addTask(task_6); // dodaj zadanie do scheluder
- task_6.enable(); // włącz zadanie do scheluder
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E T U P --- T F T --- I N I T ------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_Init()
- {
- tft.begin();
- tft.setRotation(1);
- if(!night_mode) TFT_DrawPanel();
- else
- if(night_mode) TFT_ShowStars();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E T U P ----------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void setup()
- {
- #if SERIAL
- Serial.begin(SerialSpeed);
- #endif
- delay(200);
- tools.TypeWriter("Project by Tomasz Majcher", 100, true);
- tools.TypeWriter("Clock version 2.30", 100);
- tools.TypeWriter("Tools version 0.01", 100);
- tools.BreakLine();
- initWiFi();
- TFT_Init();
- scheluderInit();
- AM2320.begin(); // AM2320 sensor start
- dht.setup(DHTpin, DHTesp::DHT11); // DHT11 sensor start
- // stars generator ---------------------------------------------------- //
- za = random(256);
- zb = random(256);
- zc = random(256);
- zx = random(256);
- Task6Callback();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- L O O P ------------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void loop()
- {
- runner.execute(); // scheluder init
- reconectWifi(); // check Wi-Fi status
- if(night_mode) TFT_ShowStars();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- D R A W P A N E L T F T ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_DrawPanel()
- {
- tft.fillScreen(TFT_MODE_BACKGROUND); // BACKGROUND COLOR
- tft.drawFastVLine(x_min+frame, y_min+frame, y_max-2*(frame), TFT_MODE_COLOR); // LEFT LINE
- tft.drawFastVLine(x_min+frame+1, y_min+frame+1, y_max-2*(frame), TFT_MODE_COLOR); // LEFT LINE
- tft.drawFastVLine(x_max-frame, y_min+frame, y_max-2*(frame), TFT_MODE_COLOR); // RIGHT LINE
- tft.drawFastVLine(x_max-frame-1, y_min+frame+1, y_max-2*(frame), TFT_MODE_COLOR); // RIGHT LINE
- tft.drawFastHLine(x_min+frame, y_min+frame, x_max-2*(frame), TFT_MODE_COLOR); // UP LINE
- tft.drawFastHLine(x_min+frame, y_min+frame+1, x_max-2*(frame), TFT_MODE_COLOR); // UP LINE
- tft.drawFastHLine(x_min+frame, y_max-frame, x_max-2*(frame), TFT_MODE_COLOR); // DOWN LINE
- tft.drawFastHLine(x_min+frame+1, y_max-frame-1, x_max-2*(frame), TFT_MODE_COLOR); // DOWN LINE
- tft.drawFastHLine(x_min+frame, y_min+frame+60, x_max-2*(frame), TFT_MODE_COLOR); // CLOCK LINE DOWN
- tft.drawFastHLine(x_min+frame, y_min+frame+61, x_max-2*(frame), TFT_MODE_COLOR); // CLOCK LINE DOWN
- //tft.drawFastVLine(x_min+157, y_min+frame+1, 60, TFT_MODE_COLOR); // CLOCK LINE RIGHT
- //tft.drawFastVLine(x_min+156, y_min+frame+1, 60, TFT_MODE_COLOR); // CLOCK LINE RIGHT
- tft.drawFastHLine(x_min+frame, y_min+frame+80, x_max-2*(frame), TFT_MODE_COLOR); // PROGRES LINE DOWN
- tft.drawFastHLine(x_min+frame, y_min+frame+81, x_max-2*(frame), TFT_MODE_COLOR); // PROGRES LINE DOWN
- tft.drawFastHLine(x_min+frame, y_min+frame+150, x_max-2*(frame), TFT_MODE_COLOR); // SENSOR LINE
- tft.drawFastHLine(x_min+frame, y_min+frame+151, x_max-2*(frame), TFT_MODE_COLOR); // SENSOR LINE
- tft.drawFastVLine(x_min+165, 85, 150, TFT_MODE_COLOR); // SENSOR LINE
- tft.drawFastVLine(x_min+166, 85, 150, TFT_MODE_COLOR); // SENSOR LINE
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W TEMPERATURE -------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowTemperature()
- {
- // ----------------------------------------------------------------- //
- // --- show temperature INSIDE from AM2320 sensor ------------------ //
- // ----------------------------------------------------------------- //
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- #if POLISH
- tft.drawCentreString("Temperatura IN",80,95,2);
- #endif
- #if GERMANY
- tft.drawCentreString("Temperatur IN",80,95,2);
- #endif
- #if ENGLISH
- tft.drawCentreString("Temperature IN",80,95,2);
- #endif
- String Buffor = String(sensor.temperature) + "`C";
- tft.drawCentreString(Buffor,80,120,4);
- // ----------------------------------------------------------------- //
- // --- show temperature OUTSIDE from DHT11 sensor ------------------ //
- // ----------------------------------------------------------------- //
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- #if POLISH
- tft.drawCentreString("Temperatura OUT",240,95,2);
- #endif
- #if GERMANY
- tft.drawCentreString("Temperatur OUT",240,95,2);
- #endif
- #if ENGLISH
- tft.drawCentreString("Temperature OUT",240,95,2);
- #endif
- Buffor = String(myDHT.temperature) + "`C";
- tft.drawCentreString(Buffor,240,120,4);
- // ----------------------------------------------------------------- //
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W HUMIDITY ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowHumidity()
- {
- // ----------------------------------------------------------------- //
- // --- show humidity INSIDE from AM2320 sensor --------------------- //
- // ----------------------------------------------------------------- //
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- #if POLISH
- tft.drawCentreString("Wilgotnosc IN",80,165,2);
- #endif
- #if GERMANY
- tft.drawCentreString("Feuchtigkeit IN",80,165,2);
- #endif
- #if ENGLISH
- tft.drawCentreString("Humidity IN",80,165,2);
- #endif
- String Buffor = String(sensor.humidity) + "%";
- tft.drawCentreString(Buffor,80,190,4);
- // ----------------------------------------------------------------- //
- // --- show humidity OUTSIDE from DHT11 sensor --------------------- //
- // ----------------------------------------------------------------- //
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- #if POLISH
- tft.drawCentreString("Wilgotnosc OUT",240,165,2);
- #endif
- #if GERMANY
- tft.drawCentreString("Feuchtigkeit OUT",240,165,2);
- #endif
- #if ENGLISH
- tft.drawCentreString("Humidity OUT",240,165,2);
- #endif
- Buffor = String(myDHT.humidity) + "%";
- tft.drawCentreString(Buffor,240,190,4);
- // ----------------------------------------------------------------- //
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W D A T E -=---------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowDate()
- {
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- //tft.drawCentreString(DAY[Time.DayOfTheWeek], 240, 10, 4);
- tft.drawCentreString(Time.TFT_Data, 240, 25, 4);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W T I M E ------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowTime()
- {
- if (!night_mode)
- {
- tft.setTextColor(TFT_MODE_COLOR, TFT_MODE_BACKGROUND);
- tft.drawString(Time.TFT_Time, 12, 12, 7);
- }
- else
- if (night_mode)
- {
- tft.setTextColor(TFT_WHITE, TFT_BLACK);
- tft.drawCentreString(Time.TFT_Time, 160, 90, 7);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T P R O G R E S T I M E ------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowProgres()
- {
- int progres = Time.Second;
- static bool F_Clear = false;
- if (progres>50) F_Clear = true;
- if ( !progres && F_Clear )
- {
- tft.fillRect(13, 70, 300, 12, TFT_MODE_BACKGROUND); // fast clear progres bar
- F_Clear = false;
- }
- else
- if ( progres < 5 && F_Clear )
- {
- tft.fillRect(13, 70, 300, 12, TFT_MODE_BACKGROUND); // fast clear progres bar
- F_Clear = false;
- }
- for(int i = 0; i < progres; i++)
- {
- tft.fillRect(13+(i*5), 70, 3, 12, TFT_MODE_COLOR); // fast draw progres bar
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W B U G - ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowBug()
- {
- static int position_x = 0;
- static int position_y = 0;
- static bool step_1 = true;
- static bool step_2 = false;
- static bool step_3 = false;
- static bool step_4 = false;
- if (step_1)
- {
- if (position_x<140)
- {
- tft.fillRect(165+position_x, 15, 1, 5, TFT_MODE_BACKGROUND);
- position_x ++;
- tft.fillRect(165+position_x, 15, 5, 5, TFT_MODE_COLOR);
- }
- else
- {
- step_1 = false; // stop move bug to right
- step_2 = true; // start move bug to down
- }
- }
- else
- if (step_2)
- {
- if (position_y<35)
- {
- tft.fillRect(165+position_x, 15+position_y, 5, 1, TFT_MODE_BACKGROUND);
- position_y ++;
- tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_MODE_COLOR);
- }
- else
- {
- step_2 = false; // stop move bug to down
- step_3 = true; // start move bug to left
- }
- }
- else
- if (step_3)
- {
- if (position_x>0)
- {
- tft.fillRect(165+position_x+5, 15+position_y, 1, 5, TFT_MODE_BACKGROUND);
- position_x --;
- tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_MODE_COLOR);
- }
- else
- {
- tft.fillRect(165+position_x+5, 15+position_y, 1, 5, TFT_MODE_BACKGROUND);
- step_3 = false;
- step_4 = true;
- }
- }
- else
- if (step_4)
- {
- if (position_y>0)
- {
- tft.fillRect(165+position_x, 15+position_y+5, 5, 1, TFT_MODE_BACKGROUND);
- position_y --;
- tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_MODE_COLOR);
- }
- else
- {
- tft.fillRect(165+position_x, 15+position_y+5, 5, 1, TFT_MODE_BACKGROUND);
- step_4 = false;
- step_1 = true;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T S H O W S T A R S ---------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFT_ShowStars()
- {
- uint8_t spawnDepthVariation = 255;
- if (night_mode) TFT_ShowTime();
- for(int i = 0; i < NSTARS; ++i)
- {
- if (sz[i] <= 1)
- {
- sx[i] = 160 - 120 + rng();
- sy[i] = rng();
- sz[i] = spawnDepthVariation--;
- }
- else
- {
- int old_screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
- int old_screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
- tft.drawPixel(old_screen_x, old_screen_y,TFT_BLACK);
- sz[i] -= 2;
- if (sz[i] > 1)
- {
- int screen_x = ((int)sx[i] - 160) * 256 / sz[i] + 160;
- int screen_y = ((int)sy[i] - 120) * 256 / sz[i] + 120;
- if (screen_x >= 0 && screen_y >= 0 && screen_x < 320 && screen_y < 240)
- {
- uint8_t r, g, b;
- r = g = b = 255 - sz[i];
- tft.drawPixel(screen_x, screen_y, tft.color565(r,g,b));
- }
- else sz[i] = 0; // Out of screen, die.
- }
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- E N D -------------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
Advertisement
Add Comment
Please, Sign In to add comment