Hi, I’m encountering an issue with connecting to the IoT cloud. Initially, everything functions correctly, but when I disconnect and reconnect my board, it fails to connect. I’ve found a temporary workaround by uploading the example ‘cloud blink’ sketch, which allows the device to connect and appear on the devices dashboard. Afterward, I re-upload my original sketch and hope it connects. Could you help me understand why this is happening?
Here is my code
#include <DHT.h>
#include <DHT_U.h>
#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 6 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define LIGHT_PIN 5 // Pin connected to the light control
DHT_Unified dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool lightState = false;
RTC_HandleTypeDef hrtc;
unsigned long timerStart = 0; // When the timer started (in millis)
unsigned long previousMillis = 0;
const long sensorInterval = 20000; // Interval to read the sensors (20 seconds)
void setup() {
pinMode(LIGHT_PIN, OUTPUT);
dht.begin();
lcd.init();
lcd.backlight();
lcd.clear();
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false);
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
// Initialize the RTC
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
Serial.begin(9600);
}
void loop() {
ArduinoCloud.update();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= sensorInterval) {
readSensorsAndUpdate();
previousMillis = currentMillis;
}
manageLightScheduler();
manageTimerDisplay();
}
void readSensorsAndUpdate() {
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
float temperatureF = (event.temperature * 9.0 / 5.0) + 32; // Convert to Fahrenheit
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperatureF);
lcd.print(" F ");
temperature = temperatureF; // Update cloud variable
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
} else {
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(event.relative_humidity);
lcd.print("% ");
humidity = event.relative_humidity; // Update cloud variable
}
}
void manageLightScheduler() {
// Get the current date and time
RTC_TimeTypeDef gTime;
RTC_DateTypeDef gDate;
HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN); // Must be called after GetTime
// Adjust for EST (UTC-5)
int currentHour = gTime.Hours - 5;
if (currentHour < 0) {
currentHour += 24; // Adjust day rollover
}
// Scheduler logic
if (scheduler) {
if (currentHour >= 7 && currentHour < 19) {
if (!lightState) {
digitalWrite(LIGHT_PIN, HIGH);
lightState = true;
timerStart = millis();
}
} else {
if (lightState) {
digitalWrite(LIGHT_PIN, LOW);
lightState = false;
}
}
}
// Display the scheduler status on the LCD
lcd.setCursor(0, 3);
lcd.print(scheduler ? "Scheduler: ON " : "Scheduler: OFF");
}
void manageTimerDisplay() {
if (lightState) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - timerStart; // Elapsed time in milliseconds
unsigned long elapsedSeconds = (elapsedTime / 1000) % 60; // Elapsed seconds
unsigned long elapsedMinutes = (elapsedTime / 1000) / 60; // Elapsed minutes
unsigned long elapsedHours = elapsedMinutes / 60; // Elapsed hours
elapsedMinutes %= 60; // Remaining minutes after converting to hours
lcd.setCursor(0, 2);
lcd.print("Time: ");
if (elapsedHours > 0) {
lcd.print(elapsedHours);
lcd.print("H ");
}
lcd.print(elapsedMinutes);
lcd.print("M ");
lcd.print(elapsedSeconds);
lcd.print("S "); // Clear extra characters
} else {
lcd.setCursor(0, 2);
lcd.print("Light OFF ");
}
}
void onLightChange() {
digitalWrite(LIGHT_PIN, light ? HIGH : LOW);
lightState = light;
if (lightState) {
timerStart = millis();
}
Serial.print("Light changed to: ");
Serial.println(light ? "ON" : "OFF");
}
void onSchedulerChange() {
Serial.print("Scheduler changed to: ");
Serial.println(scheduler ? "ON" : "OFF");
}
// Implement these functions if you need to act upon changes in humidity, temperature, or other properties
void onHumidityChange() {
// Act upon Humidity change
}
void onTemperatureChange() {
// Act upon Temperature change
}