Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************************************************************
- Home MainCenter (ESP8266) by Tomasz Majcher
- *****************************************************************************************************************************/
- //
- //
- //
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // H I S T O R Y V E R S I O N L O G
- //
- // v0.01 add function : SerialInit()
- // v0.02 add function : WiFiInit()
- // v0.03 add function : WiFiStatus()
- // v0.04 add function : WiFiReconect()
- // v0.05 add function : NTPInit()
- // v0.06 add function : Show_NTP()
- // v0.07 add structure : MyClock
- // v0.07 add function : MyClock_Update()
- // v0.08 add : day of week (PL/EN) (need rebuild library NTPClient_Generic by Tomasz Majcher) !!!
- // v0.09 add : month (PL/EN)
- // v0.10 update : Show_NTP()
- // v0.11 add : Task Scheluder
- // v0.12 add library : TFT_SPI
- // v0.13 add function : TFT -> Stars
- // v0.14 add library : PCF8564 for relays
- // v0.15 add function : ExpanderInit()
- // v0.16 add : define RELAYS pin on EXPANDER
- // v0.17 add function : RelayOn(byte nr), RelayOff(byte nr) if send 0 turn on all, turn off all
- // v0.18 add : add safe mode for Relay (check wrong Relay)
- // v0.19 add library : blynk library <BlynkSimpleEsp8266.h>
- // v0.20 : conecting button (blynk) to relays
- // v0.21 add RTC : rtc module (minimum ntp update)
- // v0.22 add function : bulb image (on/off)
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- WiFi Setup ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <ESP8266WiFi.h> // Library for WiFi Network (biblioteka do obsługi sieci WiFi)
- #include <WiFiUdp.h> // Library for NTP Server (biblioteka do obsługi serwera NTP)
- char ssid[] = "XXXXXX"; // your network SSID (nazwa twojej sieci WiFi)
- char pass[] = "XXXXXXXXXXX"; // your network password (hasło dostępu do sieci WiFi)
- WiFiUDP ntpUDP;
- unsigned long previousMillis = 0; // for WiFiReconect
- unsigned long interval = 30000; // for WiFiReconect
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- NTP server ---------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define NTP_DBG_PORT Serial // where Debug NTP ---> Serial
- #define _NTP_LOGLEVEL_ 0 // 0 = no NTP debug
- #include <NTPClient_Generic.h> // Library for NTP Server
- char timeServer[] = "pl.pool.ntp.org"; // Time Server (Europe/Poland)
- #define TIME_ZONE_OFFSET_HRS (2) // Time Zone (Europe/Warsaw)
- #define NTP_UPDATE_INTERVAL_MS 60000L // Ntp Update (60 seconds)
- NTPClient timeClient(ntpUDP, timeServer, (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- B L Y N K ----------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <BlynkSimpleEsp8266.h>
- #define VPIN_BUTTON_1 V1
- #define VPIN_BUTTON_2 V2
- #define VPIN_BUTTON_3 V3
- #define VPIN_BUTTON_4 V4
- #define VPIN_BUTTON_5 V5
- #define VPIN_BUTTON_6 V6
- #define VPIN_BUTTON_7 V7
- #define VPIN_BUTTON_8 V8
- #define AUTH "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // You should get Auth Token in the Blynk App.
- BlynkTimer timer;
- int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
- int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
- int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
- int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
- int toggleState_5 = 1; //Define integer to remember the toggle state for relay 5
- int toggleState_6 = 1; //Define integer to remember the toggle state for relay 6
- int toggleState_7 = 1; //Define integer to remember the toggle state for relay 7
- int toggleState_8 = 1; //Define integer to remember the toggle state for relay 8
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- E X P A N D E R ---------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <pcf8574_esp.h>
- #include <Wire.h>
- TwoWire rWire; // wire SCL D2 (GPIO4), SCA D1 (GPIO5)
- PCF857x expander(0x20, &rWire); // expander on 0x20
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- R E L A Y S -------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define RELAY0 0 // reserved (to manage from 1 to 8)
- #define RELAY1 0 // RELAY 1 on pin 0 (EXPANDER)
- #define RELAY2 1 // RELAY 2 on pin 1 (EXPANDER)
- #define RELAY3 2 // RELAY 3 on pin 2 (EXPANDER)
- #define RELAY4 3 // RELAY 4 on pin 3 (EXPANDER)
- #define RELAY5 4 // RELAY 5 on pin 4 (EXPANDER)
- #define RELAY6 5 // RELAY 6 on pin 5 (EXPANDER)
- #define RELAY7 6 // RELAY 7 on pin 6 (EXPANDER)
- #define RELAY8 7 // RELAY 8 on pin 7 (EXPANDER)
- #define RelayMin 1 // define first RELAY (to debug)
- #define RelayMax 8 // define last RELAY (to debug)
- const byte RELAY[9] = { RELAY0, RELAY1, RELAY2, RELAY3, RELAY4, RELAY5, RELAY6, RELAY7, RELAY8 };
- /*
- How to use ?
- RelayOn(0); turn on all relay (set to LOW)
- RelayOff(0); turn off all relay (set to HIGH)
- RelayOn(1); turn on relay nr. 1 (set to LOW)
- RelayOn(3); turn on relay nr. 3 (set to LOW)
- RelayOff(3); turn off relay nr. 1 (set to HIGH)
- RelayOff(1); turn off relay nr. 3 (set to HIGH)
- */
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T ------------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // PINS TFT -> NodeMCU ESP8266
- // SDI pin to : D7
- // SCK pin to : D5
- // CS pin to : D3
- // DC pin to : D4
- // RST pin to : RST
- // ------------------------------------------------------------------------------------------------------------------- //
- #include "SPI.h"
- #include "TFT_eSPI.h"
- TFT_eSPI tft = TFT_eSPI();
- #define x_min 0
- #define y_min 0
- #define x_max 320
- #define y_max 240
- #define frame 5
- #include "bulb.h" // zarowki
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- R T C --- D S 1 3 0 2 ----------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <virtuabotixRTC.h>
- virtuabotixRTC myRTC(D0, D6, D8); // stwórz obiekt myRTC i zdefiniuj PIN
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K ------------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #include <TaskScheduler.h> // scheluder
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- P R O T O T Y P Y --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task0Callback(); // funkcja dla task_0
- void Task1Callback(); // funkcja dla task_1
- void Task2Callback(); // funkcja dla task_2
- void Task3Callback(); // funkcja dla task_3
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Z A D A N I A S C H E L U D E R ' A ------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- Task task_0( 3600000, TASK_FOREVER, &Task0Callback); // task_0 - co 1 godzinę
- Task task_1( 60000, TASK_FOREVER, &Task1Callback); // task_1 - co 1 minutę
- Task task_2( 1000, TASK_FOREVER, &Task2Callback); // task_2 - co 1 sekundę
- Task task_3( 90000, TASK_FOREVER, &Task3Callback); // task_3 - co 90 sekund
- Scheduler runner;
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- L A N G U A G E S E T U P ----------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define LANGUAGE 0 // 0 (Polish), 1 (English)
- #if LANGUAGE == 0 // if POLISH Language
- #define POLISH 1
- #define ENGLISH 0
- #endif
- #if LANGUAGE == 1 // if ENGLISH Language
- #define POLISH 0
- #define ENGLISH 1
- #endif
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E R I A L S E T U P --------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- #define SERIAL true
- #define SerialSpeed 57600
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA DNI TYGODNIA / STRUCT DAY OF WEEK *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- const char PL_Day_0 [] = "Fake";
- const char PL_Day_1 [] = "Niedziela";
- const char PL_Day_2 [] = "Poniedzialek";
- const char PL_Day_3 [] = "Wtorek";
- const char PL_Day_4 [] = "Sroda";
- const char PL_Day_5 [] = "Czwartek";
- const char PL_Day_6 [] = "Piatek";
- const char PL_Day_7 [] = "Sobota";
- const char EN_Day_0 [] = "Fake";
- const char EN_Day_1 [] = "Sunday";
- const char EN_Day_2 [] = "Monday";
- const char EN_Day_3 [] = "Tuesday";
- const char EN_Day_4 [] = "Wednesday";
- const char EN_Day_5 [] = "Thursday";
- const char EN_Day_6 [] = "Friday";
- const char EN_Day_7 [] = "Saturday";
- const char * const PL_DAY[] = { PL_Day_0, PL_Day_1, PL_Day_2, PL_Day_3, PL_Day_4, PL_Day_5, PL_Day_6, PL_Day_7 }; // in POLISH
- const char * const EN_DAY[] = { EN_Day_0, EN_Day_1, EN_Day_2, EN_Day_3, EN_Day_4, EN_Day_5, EN_Day_6, EN_Day_7 }; // in ENGLISH
- // ------------------------------------------------------------------------------------------------------------------- //
- // *** STRUKTURA MIESIĄCE / STRUCT MONTH *** //
- // ------------------------------------------------------------------------------------------------------------------- //
- const char PL_MONTH_00 [] = "Fake";
- const char PL_MONTH_01 [] = "Styczen";
- const char PL_MONTH_02 [] = "Luty";
- const char PL_MONTH_03 [] = "Marzec";
- const char PL_MONTH_04 [] = "Kwiecien";
- const char PL_MONTH_05 [] = "Maj";
- const char PL_MONTH_06 [] = "Czerwiec";
- const char PL_MONTH_07 [] = "Lipiec";
- const char PL_MONTH_08 [] = "Sierpien";
- const char PL_MONTH_09 [] = "Wrzesien";
- const char PL_MONTH_10 [] = "Pazdziernik";
- const char PL_MONTH_11 [] = "Listopad";
- const char PL_MONTH_12 [] = "Grudzien";
- const char EN_MONTH_00 [] = "Fake";
- const char EN_MONTH_01 [] = "January";
- const char EN_MONTH_02 [] = "February";
- const char EN_MONTH_03 [] = "March";
- const char EN_MONTH_04 [] = "April";
- const char EN_MONTH_05 [] = "May";
- const char EN_MONTH_06 [] = "June";
- const char EN_MONTH_07 [] = "July";
- const char EN_MONTH_08 [] = "August";
- const char EN_MONTH_09 [] = "September";
- const char EN_MONTH_10 [] = "October";
- const char EN_MONTH_11 [] = "November";
- const char EN_MONTH_12 [] = "December";
- const char * const PL_MONTH[] = { PL_MONTH_00,
- PL_MONTH_01, PL_MONTH_02, PL_MONTH_03,
- PL_MONTH_04, PL_MONTH_05, PL_MONTH_06,
- PL_MONTH_07, PL_MONTH_08, PL_MONTH_09,
- PL_MONTH_10, PL_MONTH_11, PL_MONTH_12
- };
- const char * const EN_MONTH[] = { EN_MONTH_00,
- EN_MONTH_01, EN_MONTH_02, EN_MONTH_03,
- EN_MONTH_04, EN_MONTH_05, EN_MONTH_06,
- EN_MONTH_07, EN_MONTH_08, EN_MONTH_09,
- EN_MONTH_10, EN_MONTH_11, EN_MONTH_12
- };
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M Y N T P S T R U C T --------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- struct TIMEDATA
- {
- // *** L O C T I M E & D A T A *** ///
- int iLOCYear;
- int iLOCMonth;
- int iLOCWeekDay; // Day of week (need re-build library by Tomasz Majcher)
- int iLOCDay;
- int iLOCHours;
- int iLOCMinutes;
- int iLOCSeconds;
- char cHour[3];
- char cMinutes[3];
- char cSeconds[3];
- // *** E X T R A S T R I N G *** ///
- String sPL_DayOfWeek; // Day of week (need re-build library by Tomasz Majcher)
- String sEN_DayOfWeek; // Day of week (need re-build library by Tomasz Majcher)
- String sPL_Month;
- String sEN_Month;
- }
- MyClock;
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E R I A L I N I T ---------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void SerialInit()
- {
- #if SERIAL
- Serial.begin(SerialSpeed); // init Serial and set speed
- while (!Serial); // wait if not ready
- Serial.printf("\n\n"); // next line please
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- D R A W L I N E --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void DrawLine(byte l, char s)
- {
- #if SERIAL
- Serial.printf("\n");
- for (byte i=0; i<l; i++) Serial.print(s);
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- W i F i I N I T --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiInit()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, pass);
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connecting to WiFi ..");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Łącze z siecią WiFi ..");
- #endif
- #endif
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print('.');
- delay(200);
- }
- //statWiFi();
- WiFi.setAutoReconnect(true);
- WiFi.persistent(true);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- WiFiStat ------------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiStat()
- {
- switch (WiFi.status())
- {
- case WL_NO_SSID_AVAIL:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Configured SSID cannot be reached");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Nie można osiągnąć skonfigurowanego identyfikatora SSID");
- #endif
- #endif
- break;
- case WL_CONNECTED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connection successfully established");
- Serial.printf("\n[ESP] [WiFI] Your IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\n[ESP] [WiFI] Received Signal Strength Indication : %d", WiFi.RSSI());
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Pomyślnie nawiązano połączenie");
- Serial.printf("\n[ESP] [WiFI] Twój adres IP : %s", WiFi.localIP().toString().c_str());
- Serial.printf("\n[ESP] [WiFI] Wskazanie siły odbieranego sygnału : %d", WiFi.RSSI());
- #endif
- #endif
- break;
- case WL_CONNECT_FAILED:
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Connection failed");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Połączenie nieudane");
- #endif
- #endif
- break;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- reconect Wi-Fi ------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void WiFiReconect()
- {
- unsigned long currentMillis = millis();
- if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) // if no WiFi connect now
- {
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [WiFI] Reconnecting to WiFi...");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [WiFI] Ponowne łączenie z Wi-Fi...");
- #endif
- #endif
- WiFi.disconnect();
- WiFi.begin(ssid, pass);
- WiFiStat(); // show WiFi Status
- previousMillis = currentMillis;
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- R T C U P A T E------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- void myRTC_Update()
- {
- if ( WiFi.status() == WL_CONNECTED ) // if WiFi is OK
- {
- timeClient.update();
- myRTC.setDS1302Time(timeClient.getSeconds(), timeClient.getMinutes(), timeClient.getHours(), timeClient.getWeek(), timeClient.getDay(), timeClient.getMonth(), timeClient.getYear());
- myRTC.updateTime();
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n\n[ESP] [RTC] RTC module updated ");
- #endif
- #if POLISH
- Serial.printf("\n\n[ESP] [RTC] czas został zaktualizowany i zsynchronizowany ");
- #endif
- #endif
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // ---N T P I N I T ------------------------------------------------------------------------------------------------ //
- // ------------------------------------------------------------------------------------------------------------------- //
- int NTPInit()
- {
- timeClient.update();
- if (timeClient.updated())
- {
- #if SERIAL
- DrawLine(60,'*');
- #if ENGLISH
- Serial.printf("\n[ESP] [NTP] Time Update :-)");
- Serial.printf("\n[ESP] [NTP] Using NTP Server : %s", timeClient.getPoolServerName().c_str());
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [NTP] Czas został zaktualizowany :-)");
- Serial.printf("\n[ESP] [NTP] Używasz Serwera NTP : %s", timeClient.getPoolServerName().c_str());
- #endif
- #endif
- return 1; // return 1 if time is update
- }
- else
- {
- #if SERIAL
- #if ENGLISH
- Serial.printf("\n[ESP] [NTP] Time in not Update !!!");
- #endif
- #if POLISH
- Serial.printf("\n[ESP] [NTP] Czas nie został zaktualizowany !!!");
- #endif
- #endif
- return 0; // return 0 if time is not update
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S H O W N T P ----------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Show_NTP()
- {
- myRTC.updateTime();
- #if SERIAL
- DrawLine(60,'*');
- #if POLISH
- //Serial.printf("\n[ESP] [TIME] UTC Czas : %s", timeClient.getFormattedUTCTime().c_str());
- //Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
- //Serial.printf("\n[ESP] [TIME] LOC Czas : %s", timeClient.getFormattedTime().c_str());
- //Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
- //Serial.printf("\n[ESP] [TIME] EXT Data : %s %d %s %d", MyClock.sPL_DayOfWeek.c_str(), timeClient.getDay(), MyClock.sPL_Month, timeClient.getYear());
- Serial.printf("\n[ESP] [TIME] RTC Czas : %d:%d:%d", myRTC.hours, myRTC.minutes, myRTC.seconds);
- Serial.printf("\n[ESP] [TIME] RTC Data : %d/%d/%d", myRTC.dayofmonth, myRTC.month, myRTC.year);
- #endif
- #if ENGLISH
- //Serial.printf("\n[ESP] [TIME] UTC Time : %s", timeClient.getFormattedUTCTime().c_str());
- //Serial.printf("\n[ESP] [TIME] UTC Data : %d/%d/%d", timeClient.getUTCDay(),timeClient.getUTCMonth(),timeClient.getUTCYear());
- //Serial.printf("\n[ESP] [TIME] LOC Time : %s", timeClient.getFormattedTime().c_str());
- //Serial.printf("\n[ESP] [TIME] LOC Data : %d/%d/%d", timeClient.getDay(),timeClient.getMonth(),timeClient.getYear());
- //Serial.printf("\n[ESP] [TIME] EXT Data : %s %d %s %d", MyClock.sEN_DayOfWeek, timeClient.getDay(), MyClock.sEN_Month, timeClient.getYear());
- Serial.printf("\n[ESP] [TIME] RTC Time : %d:%d:%d", myRTC.hours, myRTC.minutes, myRTC.seconds);
- Serial.printf("\n[ESP] [TIME] RTC Data : %d/%d/%d", myRTC.dayofmonth, myRTC.month, myRTC.year);
- #endif
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- U P D A T E M Y N T P S T R U C T U R E ------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void MyClock_Update()
- {
- // *** L O C T I M E & D A T A *** ///
- MyClock.iLOCYear = myRTC.year;
- MyClock.iLOCMonth = myRTC.month;
- MyClock.iLOCWeekDay = myRTC.dayofweek;
- MyClock.iLOCDay = myRTC.dayofmonth;
- MyClock.iLOCHours = myRTC.hours;
- MyClock.iLOCMinutes = myRTC.minutes;
- MyClock.iLOCSeconds = myRTC.seconds;
- // *** E X T R A C H A R *** ///
- if( MyClock.iLOCHours < 10 )
- {
- strcpy (MyClock.cHour, "0"); // 0H:MM:SS
- strcat (MyClock.cHour, String(MyClock.iLOCHours).c_str()); // 01:MM:SS
- }
- else
- if( MyClock.iLOCHours > 9 )
- {
- strcpy (MyClock.cHour, String(MyClock.iLOCHours).c_str()); // 01:MM:SS
- }
- if( MyClock.iLOCMinutes < 10 )
- {
- strcpy (MyClock.cMinutes, "0"); // HH:0M:SS
- strcat (MyClock.cMinutes, String(MyClock.iLOCMinutes).c_str()); // 01:M0:SS
- }
- else
- if( MyClock.iLOCMinutes > 9 )
- {
- strcpy (MyClock.cMinutes, String(MyClock.iLOCMinutes).c_str()); // HH:10:SS
- }
- if( MyClock.iLOCSeconds < 10 )
- {
- strcpy (MyClock.cSeconds, "0"); // HH:MM:0S
- strcat (MyClock.cSeconds, String(MyClock.iLOCSeconds).c_str()); // HH:MH:S0
- }
- else
- if( MyClock.iLOCSeconds > 9 )
- {
- strcpy (MyClock.cSeconds, String(MyClock.iLOCSeconds).c_str()); // HH:MM:10
- }
- // *** E X T R A S T R I N G *** ///
- MyClock.sPL_DayOfWeek = PL_DAY[MyClock.iLOCWeekDay]; // day of week (dzień tygodnia) POLISH (need Re-Build Library NTP by Tomasz Majcher)
- MyClock.sEN_DayOfWeek = EN_DAY[MyClock.iLOCWeekDay]; // day of week (dzień tygodnia) ENGLISH(need Re-Build Library NTP by Tomasz Majcher)
- MyClock.sPL_Month = PL_MONTH[MyClock.iLOCMonth]; // month (miesiąc) in POLISH
- MyClock.sEN_Month = EN_MONTH[MyClock.iLOCMonth]; // month (miesiąć) in ENGLISH
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K S E T U P ------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TaskInit()
- {
- 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
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K C A L L B A C K ------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task0Callback() // run with interval 60 minutes
- {
- myRTC_Update();
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K C A L L B A C K ------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task1Callback() // run with interval 60 seconds
- {
- myRTC.updateTime();
- #if SERIAL
- Serial.printf("\n\n[ESP] [TASK1] Inside Task1Callback() ");
- DrawLine(60,'*');
- Serial.printf("\n[ESP] [TASK1] [Memory Free] : %u [kB]", ESP.getFreeHeap());
- Serial.printf("\n[ESP] [TASK1] [CPU frequency] : %u [MHz]",ESP.getCpuFreqMHz());
- Serial.printf("\n[ESP] [TASK1] [WiFi IP] : "); Serial.print(WiFi.localIP() );
- Serial.printf("\n[ESP] [TASK1] RTC Time : %d:%d:%d", myRTC.hours, myRTC.minutes, myRTC.seconds);
- Serial.printf("\n[ESP] [TASK1] RTC Time : %s:%s:%s", MyClock.cHour, MyClock.cMinutes, MyClock.cSeconds);
- Serial.printf("\n[ESP] [TASK1] RTC Data : %s %d/%d/%d", MyClock.sPL_DayOfWeek, myRTC.dayofmonth, myRTC.month, myRTC.year);
- Serial.printf("\n[ESP] [TASK1] RTC Data : %s %d %s %d", MyClock.sPL_DayOfWeek, myRTC.dayofmonth, MyClock.sPL_Month, myRTC.year);
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K C A L L B A C K ------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task2Callback() // run with interval 1 seconds
- {
- MyClock_Update(); // copy Time & Data to structure MyClock from RTC
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T A S K C A L L B A C K ------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Task3Callback() // run with interval 90 seconds
- {
- #if SERIAL
- Serial.printf("\n\n[ESP] [TASK3] Inside Task3Callback() ");
- #endif
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- S E T U P --- T F T --- I N I T ------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void TFTInit()
- {
- tft.begin();
- tft.setRotation(1);
- tft.fillScreen(TFT_BLACK);
- tft.setSwapBytes(true);
- for(byte i = 1; i <=8; i ++) Bulb(i, 0); // wyłącz wszystkie żarówki
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- E X P A N D E R I N I T ------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void ExpanderInit()
- {
- rWire.begin(5, 4);
- rWire.setClock(100000L);
- expander.begin();
- RelayOff(0); // all Relay OFF (send 0)
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M A I N S E T U P ------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void setup()
- {
- SerialInit(); // check and init serial monitor
- WiFiInit(); // start WiFi Connection
- WiFiStat(); // show WiFi Status
- WiFiReconect(); // check WiFi Connection and conect if loss !!!
- timeClient.begin(); // start timeClient
- DrawLine(60,'*'); // Draw 60x separator *
- if ( WiFi.status() == WL_CONNECTED ) // if WiFi is OK
- {
- while(!NTPInit()); // init NTP server
- myRTC_Update(); // update RTC module
- MyClock_Update(); // copy Time & Data to structure MyClock
- Show_NTP(); // show NTP time & data
- }
- ExpanderInit(); // ExpanderInit
- TFTInit(); // Init TFT Display
- TaskInit(); // Init Task Scheluder
- timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
- Blynk.config(AUTH);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- M A I N L O O P --------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void loop()
- {
- runner.execute(); // scheluder
- Blynk.run(); // Blynk
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- //
- //
- // *** T F T f u n c t i o n s ***
- //
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- T F T B U L B O N / O F F --------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void Bulb(byte nr, byte state)
- {
- switch(nr)
- {
- case 1 : {
- if (!state) tft.pushImage(5, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(5, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 2 : {
- if (!state) tft.pushImage(45, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(45, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 3 : {
- if (!state) tft.pushImage(85, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(85, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 4 : {
- if (!state) tft.pushImage(125, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(125, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 5 : {
- if (!state) tft.pushImage(165, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(165, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 6 : {
- if (!state) tft.pushImage(205, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(205, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 7 : {
- if (!state) tft.pushImage(245, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(245, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- case 8 : {
- if (!state) tft.pushImage(285, 10, bulb_off_Width, bulb_off_Height, bulb_off);
- else tft.pushImage(285, 10, bulb_off_Width, bulb_off_Height, bulb_on);
- break;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- //
- //
- // *** R E L A Y S ***
- //
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Relay to OFF -------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void RelayOff(byte nr)
- {
- if (!nr) // if nr = 0 all relay set to OFF (RelayMin, RelayMax)
- {
- for (byte i = RelayMin; i <= RelayMax; i++) expander.write(RELAY[i], HIGH);
- #if SERIAL
- DrawLine(60,'-'); // draw line
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Wyłączono wszystkie przekaźniki");
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] Turn off all relay");
- #endif
- #endif
- }
- else
- {
- if ( nr >= RelayMin && nr <= RelayMax ) // only value (1-8)
- {
- if ( !expander.read(RELAY[nr]) ) // if Relay is ON
- {
- expander.write(RELAY[nr], HIGH); // turn off Relay
- Bulb(nr, 0);
- #if SERIAL
- DrawLine(60,'-'); // draw line
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Wyłączono przekaźnik nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] Turn off relay no. %d", nr);
- #endif
- #endif
- }
- else // relay is off, re-off
- {
- #if SERIAL
- DrawLine(60,'-');
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Wykryto próbę ponownego wyłączenia przekaźnika nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] An attempt to re-engage the relay has been detected no. %d", nr);
- #endif
- #endif
- }
- }
- else // ERROR !!! Relay does not exist (jeśli taki przekaźnik nie istnieje)
- {
- #if SERIAL
- DrawLine(60,'-');
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Nie ma takiego przekaźnika, nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] There is no such relay, no. %d", nr);
- #endif
- #endif
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // --- Relay to OFF -------------------------------------------------------------------------------------------------- //
- // ------------------------------------------------------------------------------------------------------------------- //
- void RelayOn(byte nr)
- {
- if (!nr) // if nr = 0 all relay set to ON (RelayMin, RelayMax)
- {
- for (byte i = RelayMin; i <= RelayMax; i++) expander.write(RELAY[i], LOW);
- #if SERIAL
- DrawLine(60,'-'); // draw line
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Włączono wszystkie przekaźniki");
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] Turn on all relay");
- #endif
- #endif
- }
- else
- {
- if ( nr >= RelayMin && nr <= RelayMax ) // only value (1-8)
- {
- if ( expander.read(RELAY[nr]) ) // if Relay is OFF
- {
- expander.write(RELAY[nr], LOW); // turn on Relay
- Bulb(nr, 1);
- #if SERIAL
- DrawLine(60,'-'); // draw line
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Włączono przekaźnik nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] Turn on relay no. %d", nr);
- #endif
- #endif
- }
- else // relay is on, re-on
- {
- #if SERIAL
- DrawLine(60,'-');
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Wykryto próbę ponownego włączenia przekaźnika nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] An attempt to re-engage the relay has been detected no. %d", nr);
- #endif
- #endif
- }
- }
- else
- {
- #if SERIAL
- DrawLine(60,'-'); // draw line
- #if POLISH
- Serial.printf("\n[ESP] [RELAY] Nie ma takiego przekaźnika, nr. %d", nr);
- #endif
- #if ENGLISH
- Serial.printf("\n[ESP] [RELAY] There is no such relay, no. %d", nr);
- #endif
- #endif
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- //
- //
- // B L Y N K S E C T I O N
- //
- //
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK_CONNECTED
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_CONNECTED()
- {
- Blynk.syncVirtual(VPIN_BUTTON_1);
- Blynk.syncVirtual(VPIN_BUTTON_2);
- Blynk.syncVirtual(VPIN_BUTTON_3);
- Blynk.syncVirtual(VPIN_BUTTON_4);
- Blynk.syncVirtual(VPIN_BUTTON_5);
- Blynk.syncVirtual(VPIN_BUTTON_6);
- Blynk.syncVirtual(VPIN_BUTTON_7);
- Blynk.syncVirtual(VPIN_BUTTON_8);
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 1
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_1)
- {
- toggleState_1 = param.asInt();
- if(toggleState_1)
- {
- RelayOn(1);
- }
- else
- {
- RelayOff(1);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 2
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_2)
- {
- toggleState_2 = param.asInt();
- if(toggleState_2)
- {
- RelayOn(2);
- }
- else
- {
- RelayOff(2);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 3
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_3)
- {
- toggleState_3 = param.asInt();
- if(toggleState_3)
- {
- RelayOn(3);
- }
- else
- {
- RelayOff(3);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 4
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_4)
- {
- toggleState_4 = param.asInt();
- if(toggleState_4)
- {
- RelayOn(4);
- }
- else
- {
- RelayOff(4);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 5
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_5)
- {
- toggleState_5 = param.asInt();
- if(toggleState_5)
- {
- RelayOn(5);
- }
- else
- {
- RelayOff(5);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 6
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_6)
- {
- toggleState_6 = param.asInt();
- if(toggleState_6)
- {
- RelayOn(6);
- }
- else
- {
- RelayOff(6);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 7
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_7)
- {
- toggleState_7 = param.asInt();
- if(toggleState_7)
- {
- RelayOn(7);
- }
- else
- {
- RelayOff(7);
- }
- }
- // ------------------------------------------------------------------------------------------------------------------- //
- // BLYNK BUTTON 8
- // ------------------------------------------------------------------------------------------------------------------- //
- BLYNK_WRITE(VPIN_BUTTON_8)
- {
- toggleState_8 = param.asInt();
- if(toggleState_8)
- {
- RelayOn(8);
- }
- else
- {
- RelayOff(8);
- }
- }
- void checkBlynkStatus()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment