I am having problems with the arduino cloud. I am currently using the esp32 wroom with a dht 11 sensor. But every time I get this error code;
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
Humidity: 50.00% Temperature: 26.10°C
Humidity: 50.00% Temperature: 26.10°C
Humidity: 50.00% Temperature: 26.10°C
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8883 Error: -2
Humidity: 51.00% Temperature: 26.10°C
This is the code I am using:
#include "DHT.h"
#include "WiFi.h"
#include "ArduinoIoTCloud.h"
#include "Arduino_ConnectionHandler.h"
// Replace these with your network credentials
const char SSID[] = "*****";
const char PASS[] = "****";
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
CloudLight led;
CloudTemperatureSensor temperature;
CloudRelativeHumidity humidity;
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
// Initialize properties and configure the device
void initProperties() {
ArduinoCloud.setThingId("*****");
ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(led, READWRITE, ON_CHANGE, onLedChange);
}
// This function will be called when the 'led' property changes
void onLedChange() {
if (led == 1) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
}
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(2, OUTPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Initialize properties
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Set debug message level
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Start the sensor
dht.begin();
}
void loop() {
ArduinoCloud.update();
delay(2000);
// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Update the cloud variables
humidity = h;
temperature = t;
// Print data to serial monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.println("°C");
}
Hi! Code reported here is not handling correcly authentication for a secret based board (like ESP32). For example, it is missing definition of secret device key:
Device key is generated at baord provisioning time.
You can also remove .setThingId() call.
I suggest you to start from a code template generated in Wed Editor and take a look at auto-generated thingProperties.h file. Cloud connection code is automatically genarated based on your board by cloud web editor.
I tried another code but I still get the same error code.
Code:
#include <DHT.h>
#include "thingProperties.h"
// Initialize DHT sensor
#define DHTPIN 12 // Pin where the DHT11 sensor is connected
#define DHTTYPE DHT11 // Define the type of DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
delay(1500);
dht.begin();
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(4); // Set debug level to get detailed info
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Read temperature and humidity
float temperatureValue = dht.readTemperature(); // Read temperature as Celsius
float humidityValue = dht.readHumidity(); // Read humidity as percentage
// Check if any reads failed and exit early (to try again).
if (isnan(temperatureValue) || isnan(humidityValue)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Update cloud variables
temperature = temperatureValue;
humidity = humidityValue;
// Print values to serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureValue);
Serial.print(" °C");
Serial.print("\tHumidity: ");
Serial.print(humidityValue);
Serial.println(" %");
// Delay between reads
delay(2000);
}
Error code:
Connected to "Upstairs"
Temperature: 29.10 °C Humidity: 52.00 %
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
TimeServiceClass::sync Drift: 0 RTC value: 1722346012
Temperature: 29.10 °C Humidity: 52.00 %
Temperature: 29.10 °C Humidity: 52.00 %
ArduinoIoTCloudTCP::handle_SyncTime could not get valid time. Retrying now.
Temperature: 29.10 °C Humidity: 52.00 %
Temperature: 29.10 °C Humidity: 52.00 %
TimeServiceClass::sync Drift: 0 RTC value: 1722346025
Temperature: 29.10 °C Humidity: 52.00 %
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
Temperature: 29.10 °C Humidity: 52.00 %