Hi Folks
I thought I had a nice piece of code: After connecting to the internet to get the right Date and time, it shows date and time and it also uses a DHT22 sensor to show Temperature and Humidity. It works fine.
But strange enough at time 00:01 it stops working8
Here is my code, any suggestions are welcome.
#include <Wire.h>
#include <WiFi.h>
#include <TFT_eSPI.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <DHT.h>
// Replace with your network credentials
const char* ssid = "";
const char* password = "*";
// Setup for the display
TFT_eSPI tft = TFT_eSPI(); // Create TFT object
// DHT22 sensor setup
#define DHTPIN 15 // Pin connected to the DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// NTP setup
WiFiUDP udp;
NTPClient timeClient(udp, "pool.ntp.org", 7200, 60000); // 7200 seconds offset for GMT +2
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1); // Adjust rotation if needed
tft.fillScreen(TFT_BLACK);
// Initialize DHT sensor
dht.begin();
// Connect to Wi-Fi
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
tft.println("Connecting to WiFi.");
tft.setCursor(10, 75);
WiFi.begin(ssid, password);
bool toggleColor = false; // Flag to alternate color
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
// Alternate background color
if (toggleColor) {
tft.fillScreen(TFT_RED);
} else {
tft.fillScreen(TFT_GREEN);
}
toggleColor = !toggleColor; // Toggle the color flag
tft.setCursor(10, 75);
tft.setTextColor(TFT_WHITE); // Set text color to white for visibility
tft.print(".");
}
tft.println("\nConnected to WiFi");
// Start NTP Client
timeClient.begin();
timeClient.forceUpdate(); // Force immediate update after Wi-Fi connection
// Set initial time
unsigned long epochTime = timeClient.getEpochTime();
setTime(epochTime); // Set the internal time
displayDateTime();
}
void loop() {
// Update the time client
if (hour() == 0 && minute() == 1) { // Force NTP update at 00:01
timeClient.forceUpdate();
}
if (timeClient.update()) {
unsigned long epochTime = timeClient.getEpochTime();
setTime(epochTime); // Update internal time
displayDateTime();
}
delay(2000); // Update every 2 seconds
}
void displayDateTime() {
// Clear the display
tft.fillScreen(TFT_YELLOW);
// Get the current time
int currentHour = hour();
int currentMinute = minute();
// Format the time as hh:mm
String formattedHour = (currentHour < 10) ? "0" + String(currentHour) : String(currentHour);
String formattedMinute = (currentMinute < 10) ? "0" + String(currentMinute) : String(currentMinute);
String formattedTime = formattedHour + ":" + formattedMinute; // Format: HH:MM
// Get current date
int currentDay = day();
int currentMonth = month();
int currentYear = year();
// Format the date
String formattedDay = (currentDay < 10) ? "0" + String(currentDay) : String(currentDay);
String formattedMonth = (currentMonth < 10) ? "0" + String(currentMonth) : String(currentMonth);
String formattedDate = formattedDay + "/" + formattedMonth + "/" + String(currentYear % 100); // Format: DD/MM/YY
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if reading failed
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Print time, date, temperature, and humidity for debugging
Serial.print("Current Time: ");
Serial.println(formattedTime);
Serial.print("Current Date: ");
Serial.println(formattedDate);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
// Display the date, time, temperature, and humidity on the screen
tft.setTextColor(TFT_RED);
tft.setTextSize(3);
tft.setCursor(3, 10);
tft.println("Date:");
tft.setCursor(96, 10);
tft.println(formattedDate);
tft.setCursor(3, 35);
tft.println("Time:");
tft.setCursor(98, 35);
tft.println(formattedTime);
tft.drawLine(0, 62, tft.width(), 62, TFT_GREEN);
tft.drawLine(0, 63, tft.width(), 63, TFT_GREEN);
tft.drawLine(0, 64, tft.width(), 65, TFT_GREEN);
tft.drawLine(0, 65, tft.width(), 65, TFT_GREEN);
tft.setTextColor(TFT_BLUE);
tft.setCursor(3, 70);
tft.println("Temp: ");
tft.setCursor(98, 70);
tft.println(String(temperature,1) + " C"); // Alternative method
tft.setCursor(3, 95);
tft.println("Hum: ");
tft.setCursor(98, 95);
tft.println(String(humidity,1) + " %");
// Go to light sleep for 60 seconds
esp_sleep_enable_timer_wakeup(60 * 1000000); // Sleep for 60 seconds (in microseconds)
esp_light_sleep_start(); // Enter light sleep
}