From fa55c0a61da7b4d5025cf23dc0f205f5d0dd9a12 Mon Sep 17 00:00:00 2001 From: TRN Date: Fri, 11 Apr 2025 22:37:46 +0300 Subject: [PATCH] Add variants --- src/main.cpp | 72 +++++++----------------------- src/temp-clock.txt | 106 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 57 deletions(-) create mode 100644 src/temp-clock.txt diff --git a/src/main.cpp b/src/main.cpp index fb51646..6b79f68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,45 +1,35 @@ -#include // https://github.com/johnrickman/LiquidCrystal_I2C -#include // https://github.com/arduino-libraries/NTPClient +#include +#include #include #include -#include "DHTesp.h" // https://github.com/beegee-tokyo/DHTesp -#define buzzerPin 15 // define buzzer connected pin -#define dhtPin 2 // define dht connected pin -#define dhtType DHTesp::DHT11 // Define dht sensor type +const char *ssid = ""; +const char *password = ""; -const char *ssid = ""; // Wifi SSID -const char *password = ""; // Wifi Password +const long utcOffsetInSeconds = 10800; -const long utcOffsetInSeconds = 10800; // Set your timezone 1 hour = 3600 second +const char *ntpServerUrl = "pool.ntp.org"; -bool executed = false; -bool Fahrenheit = false; // If you want change temperature unit to Fahrenheit set true -const char *ntpServerUrl = "pool.ntp.org"; // Set npt server url - // https://www.ntppool.org/en/ +String weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; -DHTesp dht; WiFiUDP ntpUDP; LiquidCrystal_I2C lcd(0x27, 16, 2); NTPClient timeClient(ntpUDP, ntpServerUrl, utcOffsetInSeconds); void lcdClockDate(); -void TempHum(); -void ringBuzzer(); void setup() { - pinMode(buzzerPin, OUTPUT); lcd.init(); lcd.backlight(); WiFi.begin(ssid, password); - dht.setup(dhtPin, dhtType); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(0, 0); lcd.print("Connecting to"); - lcd.setCursor(3, 1); + int startPos = (16 - String(ssid).length()) / 2; + lcd.setCursor(startPos, 1); lcd.print(ssid); } lcd.clear(); @@ -50,14 +40,13 @@ void loop() { timeClient.update(); lcdClockDate(); - TempHum(); - ringBuzzer(); delay(2000); } void lcdClockDate() { time_t epochTime = timeClient.getEpochTime(); String formattedTime = timeClient.getFormattedTime().substring(0, 5); + String weekDay = weekDays[timeClient.getDay()]; struct tm *ptm = gmtime((time_t *)&epochTime); int monthDay = ptm->tm_mday; String currentMonth = (ptm->tm_mon + 1) < 10 ? "0" + String(ptm->tm_mon + 1) : String(ptm->tm_mon + 1); @@ -67,40 +56,9 @@ void lcdClockDate() lcd.print(CurrentDate); lcd.setCursor(11, 0); lcd.print(formattedTime); -} -void TempHum() -{ - float humidity = dht.getHumidity(); - float temperature = dht.getTemperature(); - if (Fahrenheit) - { - lcd.setCursor(0, 1); - lcd.print(String(dht.toFahrenheit(temperature), 1) + char(223) + "F"); - lcd.setCursor(11, 1); - lcd.print("%" + String(humidity, 1)); - } - else - { - - lcd.setCursor(0, 1); - lcd.print(String(temperature, 1) + char(223) + "C"); - lcd.setCursor(11, 1); - lcd.print("%" + String(humidity, 1)); - } -} - -void ringBuzzer() // This function activates the buzzer only once per hour -{ - int currentMinute = timeClient.getMinutes(); - if (currentMinute == 0 && executed == false) - { - digitalWrite(buzzerPin, HIGH); - delay(200); - digitalWrite(buzzerPin, LOW); - executed = true; - } - if (currentMinute != 0) - { - executed = false; - } + lcd.setCursor(0, 1); + lcd.print(" "); + int startPos = (16 - weekDay.length()) / 2; + lcd.setCursor(startPos, 1); + lcd.print(weekDay); } diff --git a/src/temp-clock.txt b/src/temp-clock.txt new file mode 100644 index 0000000..fb51646 --- /dev/null +++ b/src/temp-clock.txt @@ -0,0 +1,106 @@ +#include // https://github.com/johnrickman/LiquidCrystal_I2C +#include // https://github.com/arduino-libraries/NTPClient +#include +#include +#include "DHTesp.h" // https://github.com/beegee-tokyo/DHTesp + +#define buzzerPin 15 // define buzzer connected pin +#define dhtPin 2 // define dht connected pin +#define dhtType DHTesp::DHT11 // Define dht sensor type + +const char *ssid = ""; // Wifi SSID +const char *password = ""; // Wifi Password + +const long utcOffsetInSeconds = 10800; // Set your timezone 1 hour = 3600 second + +bool executed = false; +bool Fahrenheit = false; // If you want change temperature unit to Fahrenheit set true +const char *ntpServerUrl = "pool.ntp.org"; // Set npt server url + // https://www.ntppool.org/en/ + +DHTesp dht; +WiFiUDP ntpUDP; +LiquidCrystal_I2C lcd(0x27, 16, 2); +NTPClient timeClient(ntpUDP, ntpServerUrl, utcOffsetInSeconds); + +void lcdClockDate(); +void TempHum(); +void ringBuzzer(); + +void setup() +{ + pinMode(buzzerPin, OUTPUT); + lcd.init(); + lcd.backlight(); + WiFi.begin(ssid, password); + dht.setup(dhtPin, dhtType); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + lcd.setCursor(0, 0); + lcd.print("Connecting to"); + lcd.setCursor(3, 1); + lcd.print(ssid); + } + lcd.clear(); + timeClient.begin(); +} + +void loop() +{ + timeClient.update(); + lcdClockDate(); + TempHum(); + ringBuzzer(); + delay(2000); +} +void lcdClockDate() +{ + time_t epochTime = timeClient.getEpochTime(); + String formattedTime = timeClient.getFormattedTime().substring(0, 5); + struct tm *ptm = gmtime((time_t *)&epochTime); + int monthDay = ptm->tm_mday; + String currentMonth = (ptm->tm_mon + 1) < 10 ? "0" + String(ptm->tm_mon + 1) : String(ptm->tm_mon + 1); + int currentYear = ptm->tm_year - 100; + String CurrentDate = String(monthDay) + "/" + String(currentMonth) + "/" + String(currentYear); + lcd.setCursor(0, 0); + lcd.print(CurrentDate); + lcd.setCursor(11, 0); + lcd.print(formattedTime); +} +void TempHum() +{ + float humidity = dht.getHumidity(); + float temperature = dht.getTemperature(); + if (Fahrenheit) + { + lcd.setCursor(0, 1); + lcd.print(String(dht.toFahrenheit(temperature), 1) + char(223) + "F"); + lcd.setCursor(11, 1); + lcd.print("%" + String(humidity, 1)); + } + else + { + + lcd.setCursor(0, 1); + lcd.print(String(temperature, 1) + char(223) + "C"); + lcd.setCursor(11, 1); + lcd.print("%" + String(humidity, 1)); + } +} + +void ringBuzzer() // This function activates the buzzer only once per hour +{ + int currentMinute = timeClient.getMinutes(); + if (currentMinute == 0 && executed == false) + { + digitalWrite(buzzerPin, HIGH); + delay(200); + digitalWrite(buzzerPin, LOW); + executed = true; + } + if (currentMinute != 0) + { + executed = false; + } +}