hi there this is my code.
my Cloud variables are all correct and the time zones are all set correctly. i however can not seem to get the scheduler to activate.
#include "WiFiSetup.h"
#include "RTCSetup.h"
#include "WaterLevelMonitor.h"
#include "PumpControl.h"
#include "IoTCloud.h"
#include "Debug.h"
#include "LEDControl.h"
#include "DIPSwitchHandler.h"
void setup() {
setupDebug();
debugMessage("Starting Setup...", INFO);
setupPumpControl();
setupWiFi();
setupIoTCloud();
setupRTC();
setupWaterLevelMonitor();
setupDIPSwitchHandler();
initLEDs();
debugMessage("Setup Complete", INFO);
}
void loop() {
checkTime();
ArduinoCloud.update();
handleSwitch();
if (pauseLoop || dipPauseLoop) {
stopPump();
debugNotification("Program Paused");
while (pauseLoop || dipPauseLoop) {
ArduinoCloud.update();
handleSwitch();
}
debugNotification("Program Restarted");
}
// ✅ Debug if schedules are detected
bool scheduleActive;
if (isScheduleActive()) {
debugMessage("A schedule is currently active.", INFO);
scheduleActive = true;
} else {
scheduleActive = false;
}
monitorWaterLevel();
ArduinoCloud.update();
if (!waterLow && (pumpControl || dipPumpControl || scheduleActive)) {
activatePump();
} else if (waterLow || (!pumpControl && !dipPumpControl && !scheduleActive)) {
stopPump();
}
}
#ifndef DIPSWITCHHANDLER_H
#define DIPSWITCHHANDLER_H
#include <Arduino.h>
#include "GlobalVariables.h"
#include "PumpControl.h"
#include "Debug.h"
void onSwitchPump();
void onSwitchPause();
volatile bool dipPumpStateChanged = false;
volatile bool dipPauseStateChanged = false;
void setupDIPSwitchHandler() {
pinMode(SWITCH_PIN_PUMP, INPUT_PULLUP);
pinMode(SWITCH_PIN_PROGRAMPAUSE, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN_PUMP), onSwitchPump, CHANGE);
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN_PROGRAMPAUSE), onSwitchPause, CHANGE);
}
void onSwitchPump() {
dipPumpStateChanged = true;
}
void onSwitchPause() {
dipPauseStateChanged = true;
}
void handleSwitch() {
if (dipPumpStateChanged) {
dipPumpControl = !digitalRead(SWITCH_PIN_PUMP);
debugNotification(dipPumpControl ? "DIP Switch Pump Activated" : "DIP Switch Pump Deactivated");
dipPumpStateChanged = false;
}
if (dipPauseStateChanged) {
dipPauseLoop = !digitalRead(SWITCH_PIN_PROGRAMPAUSE);
debugNotification(dipPauseLoop ? "DIP Switch Pause Activated" : "DIP Switch Pause Deactivated");
dipPauseStateChanged = false;
}
}
#endif
#ifndef DEBUG_H
#define DEBUG_H
#include <Arduino.h>
#include "GlobalVariables.h"
// Debug level constants
#define DEBUG 3
#define INFO 2
#define NOTIFY 1
// Set debug level here
#ifndef CURRENT_DEBUG_LEVEL
#define CURRENT_DEBUG_LEVEL DEBUG
#endif
// Initialization function for Serial
void setupDebug() {
Serial.begin(9600);
delay(5000);
}
// General debug output function
void debugMessage(const String &message, int level) {
if (level <= CURRENT_DEBUG_LEVEL) {
if (level == DEBUG) Serial.println("[DEBUG] " + message);
else if (level == INFO) Serial.println("[INFO] " + message);
else if (level == NOTIFY) Serial.println("[NOTIFY] " + message);
}
}
// Specialized function for notifications
inline void debugNotification(const String &message) {
static String lastNotification;
if (lastNotification != message) {
lastNotification = message;
notification = "[NOTIFY] " + message;
debugMessage(message, NOTIFY);
}
}
#endif
#include "GlobalVariables.h"
#include <ArduinoIoTCloud.h>
// ✅ Wi-Fi Credentials
const char *ssid = "";
const char *password = "";
WiFiConnectionHandler ArduinoIoTPreferredConnection(ssid, password);
// ✅ Pump & Water Level Control
bool waterLow = false;
bool pumpActive = false;
bool pauseLoop = false;
bool dipPauseLoop = false;
bool dipPumpControl = false;
// ✅ Cloud Variables
CloudSwitch pumpControl;
CloudString notification;
CloudTime time_read;
// ✅ Define 20 CloudSchedule Variables
CloudSchedule pumpSchedules[20];
#ifndef GLOBALVARIABLES_H
#define GLOBALVARIABLES_H
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
// ✅ Wi-Fi Credentials
extern const char *ssid;
extern const char *password;
extern WiFiConnectionHandler ArduinoIoTPreferredConnection;
// ✅ Pin Definitions
#define WATER_SENSOR_PIN 4
#define PUMP_PIN 5
#define SWITCH_PIN_PUMP 2
#define SWITCH_PIN_PROGRAMPAUSE 3
// ✅ Pump & Water Level Control
extern bool waterLow;
extern bool pumpActive;
extern bool pauseLoop;
extern bool dipPauseLoop;
extern bool dipPumpControl;
extern CloudSwitch pumpControl;
extern CloudString notification;
extern CloudTime time_read;
// ✅ Define 20 CloudSchedule Variables
extern CloudSchedule pumpSchedules[20];
#endif
#ifndef IOTCLOUD_H
#define IOTCLOUD_H
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include "GlobalVariables.h"
void setupIoTCloud() {
if (WiFi.status() != WL_CONNECTED) {
debugMessage("WiFi not connected. Waiting before connecting to Arduino Cloud.", INFO);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// ✅ Cloud Properties
ArduinoCloud.addProperty(pumpControl, READWRITE);
ArduinoCloud.addProperty(notification, READ);
ArduinoCloud.addProperty(pauseLoop, READWRITE);
ArduinoCloud.addProperty(time_read, READ);
// ✅ Register 20 Schedules in Arduino Cloud using a loop
for (int i = 0; i < 20; i++) {
ArduinoCloud.addProperty(pumpSchedules[i], READWRITE, ON_CHANGE);
}
}
void checkTime() {
if (ArduinoCloud.connected()) {
debugNotification("Cloud Local Time: " + ArduinoCloud.getLocalTime());
debugNotification("Cloud Internal Time: " + ArduinoCloud.getInternalTime());
} else {
debugNotification("Arduino Cloud is not connected. Time may not be accurate.");
}
}
#endif
#ifndef LEDCONTROL_H
#define LEDCONTROL_H
#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include "Images.h"
// Create an instance of the LED matrix
ArduinoLEDMatrix matrix;
// Initialize the LED matrix
inline void initLEDs() {
matrix.begin(); // Initialize the matrix
matrix.clear(); // Clear the display
}
// Display the Pump ON image
inline void pumpOnImage() {
matrix.clear();
matrix.loadFrame(Tick);
}
// Display the Pump OFF image
inline void pumpOffImage() {
matrix.clear();
matrix.loadFrame(Cross);
}
inline void imageClear() {
matrix.clear();
}
#endif // LEDCONTROL_H
#ifndef PUMPCONTROL_H
#define PUMPCONTROL_H
#include <Arduino.h>
#include "GlobalVariables.h"
#include "Debug.h"
#include "LEDControl.h"
void setupPumpControl() {
pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, LOW);
debugMessage("Pump Control Initialized.", INFO);
}
void activatePump() {
if (!pumpActive) {
debugMessage("Pump activated.", INFO);
digitalWrite(PUMP_PIN, HIGH);
pumpActive = true;
pumpOnImage();
}
}
void stopPump() {
if (pumpActive) {
debugMessage("Pump deactivated.", INFO);
digitalWrite(PUMP_PIN, LOW);
pumpActive = false;
pumpOffImage();
}
}
// ✅ Checks if any cloud schedule is active
bool isScheduleActive() {
ArduinoCloud.update();
for (int i = 0; i < 20; i++) {
if (pumpSchedules[i].isActive()) {
return true;
}
}
return false;
}
#endif
#ifndef RTCSETUP_H
#define RTCSETUP_H
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "GlobalVariables.h"
#include "Debug.h"
WiFiUDP ntpUDP;
const long utcOffsetSeconds = 8 * 3600;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetSeconds);
void setupRTC() {
debugMessage("Initializing RTC...", INFO);
timeClient.begin();
int attempts = 0;
while (!timeClient.update() && attempts < 5) {
debugMessage("RTC sync failed, retrying...", INFO);
delay(2000);
attempts++;
}
if (!timeClient.update()) {
debugNotification("RTC sync failed. Using fallback time.");
} else {
debugMessage("RTC synchronized: " + timeClient.getFormattedTime(), INFO);
}
}
void updateRTC() {
if (!timeClient.update()) {
debugMessage("Failed to update RTC from NTP.", INFO);
}
}
String getCurrentTime() {
String currentTime = timeClient.getFormattedTime();
if (currentTime == "") {
debugMessage("RTC error: No valid time data available.", INFO);
}
return currentTime;
}
#endif
#ifndef WATERLEVELMONITOR_H
#define WATERLEVELMONITOR_H
#include "GlobalVariables.h"
#include "Debug.h"
void setupWaterLevelMonitor() {
pinMode(WATER_SENSOR_PIN, INPUT_PULLUP);
debugMessage("Water Level Monitor Initialized.", INFO);
}
void monitorWaterLevel() {
static unsigned long lastMessageTime = 0;
const unsigned long messageInterval = 30 * 60 * 1000; // 30 minutes in milliseconds
bool newState = digitalRead(WATER_SENSOR_PIN);
// ✅ Always update waterLow state
if (newState != waterLow) {
waterLow = newState;
debugNotification(waterLow ? "Water level is LOW! Pump control disabled." : "Water level is GOOD.");
debugMessage(waterLow ? "Water level is LOW!" : "Water level is GOOD.", INFO);
lastMessageTime = millis(); // ✅ Reset timer on change
}
// ✅ Send a status update every 30 minutes if water is NOT low
if (!waterLow && millis() - lastMessageTime >= messageInterval) {
debugNotification("Water level is GOOD.");
debugMessage("Water level is GOOD.", INFO);
lastMessageTime = millis(); // ✅ Reset timer
}
}
#endif
const uint32_t Cross[] =
{
0x30c1980f,
0x600600,
0xf019830c,
66
};
const uint32_t Tick[] =
{
0x300600,
0xc818c306,
0x603c0180,
66
};
#ifndef WIFISETUP_H
#define WIFISETUP_H
#include <WiFiS3.h>
#include "Debug.h"
#include "GlobalVariables.h"
void setupWiFi() {
debugMessage("Connecting to Wi-Fi...", INFO);
WiFi.begin(ssid, password);
unsigned long startTime = millis();
const unsigned long timeout = 30000;
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startTime >= timeout) {
debugNotification("Wi-Fi connection failed: Timeout.");
return;
}
delay(1000);
}
debugNotification("Wi-Fi connected successfully.");
}
#endif