Hi. I am using my Arduino to make an IoT project, and its connection to the internet is good. I programmed it to make an alarm go off at 8:00 and 20:00 (8:00 PM). Despite, it goes off at 7:33 and 19:33 (7:33 PM). If I make it print the time over serial, it is accurate, there is no time drift. If I reset the Arduino occasionally (press the reset button), then it works fine. Sometimes it even goes off at 7:33 and 8:00. Is anyone able to spot my oversight? Thanks for your time.
Here is my main code (thingProperties.h doesn't matter):
#include "thingProperties.h"
#include <DHT.h>
#define DHTPIN 2 // Pin where DHT11 is connected
#define DHTTYPE DHT11 // Sensor type
int BUZZER = 3; // Buzzer
int BTN = 4; // Interface button
bool isTime = false;
DHT dht(DHTPIN, DHTTYPE);
float tempAdjust = 5;
float humidAdjust = -5;
bool rang = false;
String MorningAlarmTime = "08:00";
String AfternoonAlarmTime = "20:00"; // Example for 3:00 PM
void checkAlarm() {
time_t now = ArduinoCloud.getLocalTime();
struct tm *timeinfo = localtime(&now);
int currentHour = timeinfo->tm_hour;
int currentMinute = timeinfo->tm_min;
int morningHour = MorningAlarmTime.substring(0, 2).toInt();
int morningMinute = MorningAlarmTime.substring(3, 5).toInt();
int afternoonHour = AfternoonAlarmTime.substring(0, 2).toInt();
int afternoonMinute = AfternoonAlarmTime.substring(3, 5).toInt();
if (serialOutput) {
Serial.print("Time is ");
Serial.print(currentHour);
Serial.print(":");
Serial.println(currentMinute);
}
bool matchMorning = (
(currentHour == morningHour || currentHour == morningHour + 12) &&
currentMinute == morningMinute
);
bool matchAfternoon = (
(currentHour == afternoonHour || currentHour == afternoonHour + 12) &&
currentMinute == afternoonMinute
);
bool match = matchMorning || matchAfternoon;
if (match && !rang) {
isTime = true;
} else if (!match) {
isTime = false;
rang = false;
}
}
void setup() {
pinMode(BTN, INPUT);
pinMode(BUZZER, OUTPUT);
// Initialize serial and wait for port to open
Serial.begin(115200);
delay(1500);
dht.begin(); // Start DHT11 sensor
// Initialize IoT Cloud properties
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Debugging level (0 = errors only, 4 = max details)
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update(); // Sync with IoT Cloud
checkAlarm();
if (isTime && !rang) {
while (digitalRead(BTN) == LOW) { // HIGH means button is is pressed
if (serialOutput) {
Serial.println("BEEPING!");
}
digitalWrite(BUZZER, HIGH);
delay(250);
digitalWrite(BUZZER, LOW);
delay(250);
digitalWrite(BUZZER, HIGH);
delay(250);
digitalWrite(BUZZER, LOW);
delay(1000);
}
rang = true; // Only mark it as "rang" after button press
}
humidAdjust = humidOffset;
tempAdjust = tempOffset;
// Read from DHT11 sensor
float newTemp = dht.readTemperature();
float newHum = dht.readHumidity() + humidAdjust;
// if (newHum == humidity) {
// humidity += 0.01; // Temporary change
// }
if (isnan(newTemp) || isnan(newHum)) {
if (serialOutput) {
Serial.println("❌ Failed to read from DHT11 sensor!");
}
return;
}
float temperatureF = ((newTemp * 9.0 / 5.0) + 32) + tempAdjust;
// Print values to Serial Monitor
if(serialOutput) {
Serial.print("🌡 Temp: ");
Serial.print(temperatureF);
Serial.print("°F | 💧 Humidity: ");
Serial.print(newHum);
Serial.println("%");
}
// Update IoT Cloud variables
temperature = temperatureF;
humidity = newHum;
delay(5000); // Wait 5 seconds before next read
}