I'm working on a project to control a relay with a button and to have the status of the device when it's running when the conditions from 2 sensors our met. The logic works with the sensors but this ESP32 only gives me ArduinoIoTCloudTCP::handle_ConnectMqttBroker will not connect to the cloud.
I have no idea how to troubleshoot this error.
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-up.iot.arduino.cc:8884
I have 4 other devices connected to the same network that report basic temp and humidity.
I'm able to ping this new device when it connects to my wifi.
Without any error logs or anything more descriptive. I have no idea how to troubleshoot this.
This is my code.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/8b87fa5f-06e8-4d6c-be06-b85140709ed4
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
bool pump1;
bool relayState;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
// Define pin assignments
#define RELAY_PIN 23
#define UPPER_SENSOR_PIN 2
#define LOWER_SENSOR_PIN 22
// Relay state and timers
unsigned long pumpStartTime = 0; // When the pump was last started
unsigned long cooldownStartTime = 0; // When the pump was last stopped
const unsigned long pumpMaxDuration = 10000; // Max pump run-time in ms (10 seconds)
const unsigned long cooldownDuration = 10000; // Cooldown time before restarting in ms (10 seconds)
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Print free heap at startup
Serial.print("Initial Free Heap: ");
Serial.println(ESP.getFreeHeap());
// Initialize other settings
pinMode(RELAY_PIN, OUTPUT);
pinMode(UPPER_SENSOR_PIN, INPUT_PULLUP);
pinMode(LOWER_SENSOR_PIN, INPUT);
deactivatePump("Initial setup");
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
Serial.println("Setup complete.");
}
void loop() {
ArduinoCloud.update();
// Monitor free memory periodically
static unsigned long lastHeapCheck = 0;
if (millis() - lastHeapCheck > 10000) { // Check every 60 seconds
lastHeapCheck = millis();
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Normal loop logic
// Read sensor states (read multiple times for stability)
bool upperSensorStable = true;
bool lowerSensorStable = true;
for (int i = 0; i < 3; i++) {
if (digitalRead(UPPER_SENSOR_PIN) == LOW) upperSensorStable = false;
if (digitalRead(LOWER_SENSOR_PIN) == HIGH) lowerSensorStable = false; // Lower sensor is stable when LOW
delay(10); // Small delay for stability
}
// Debugging sensor states with timestamps
Serial.print("[millis: ");
Serial.print(millis());
Serial.print("] Upper Sensor: ");
Serial.print(upperSensorStable ? "ON" : "OFF");
Serial.print(" | Lower Sensor: ");
Serial.print(lowerSensorStable ? "ON" : "OFF");
Serial.print(" | Relay State: ");
Serial.println(relayState ? "ON" : "OFF");
// Pump logic
if (relayState) {
// Check if the pump should turn off
if (!lowerSensorStable) { // Pump turns off when lower sensor is LOW
deactivatePump("Lower sensor LOW (water level low)");
} else if (millis() - pumpStartTime >= pumpMaxDuration) {
deactivatePump("Max duration reached");
}
} else {
// Check if the pump should turn on
if (upperSensorStable && (millis() - cooldownStartTime >= cooldownDuration)) {
Serial.print("Pump activation condition met at millis: ");
Serial.println(millis());
activatePump();
}
}
// Short delay for serial debugging clarity
delay(500);
}
// Function to handle button press from IoT Cloud
void onPump1Change() {
if (pump1) {
Serial.println("Manual pump activation requested via IoT Cloud.");
activatePump(); // Run pump for up to 10 seconds
}
}
void onrelayStateChange() {
// Add your code here to act upon RelayState change
}
// Function to activate the pump
void activatePump() {
if (!relayState && (millis() - cooldownStartTime >= cooldownDuration)) {
Serial.print("Activating pump at millis: ");
Serial.println(millis());
digitalWrite(RELAY_PIN, HIGH);
relayState = true;
pumpStartTime = millis();
} else if (relayState) {
Serial.println("Pump already running.");
} else {
Serial.println("Cooldown period active. Cannot start pump.");
}
}
// Function to deactivate the pump
void deactivatePump(String reason) {
if (relayState) {
Serial.print("Deactivating pump at millis: ");
Serial.println(millis());
Serial.print("Reason: ");
Serial.println(reason);
digitalWrite(RELAY_PIN, LOW);
relayState = false;
cooldownStartTime = millis();
}
I have tried deleting the "thing" from the IoT dashboard and recreating it with a new key.
I tried various delays and clearing the memory.
I've tried different pins for the relay and sensors.
I've also tried 4 different boards.
I don't think it's a hardware or network issue. I just can't find enough information to continue troubleshooting this.
Updated error.