I have developed an IoT system that integrates sensors and actuators using an Arduino Uno4 WiFi board. The data from the sensors can be displayed on the serial monitor, but it does not appear on the dashboard. I have already verified that the variables in my code match those configured in the IoT cloud.
According to the serial port output, the Arduino Nano is successfully connected to my WiFi network and is also linked to the IoT cloud. I can't understand what could be wrong? I have re-installed the Cloud Agent several times and deleted and recreated the Device and the Thing and the Dashboard and the Cloud Variable several times but nothing seems to help.
Any help would be welcome.
Here is the sketch for reference:
#include <BH1750FVI.h>
#include <PreMo.h>
#include <PID_v1.h>
#include <DHT.h>
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);
#define DHTpin A0
#define DHTTYPE DHT11
DHT dht(DHTpin, DHTTYPE);
int soil_moisture_sensor = A5;
int dimmerPin = 11;
int relay_1 = 13;
bool useCloudControl = false;
const int moistureLowThreshold = 20;
const int moistureHighThreshold = 30;
double tempSetpoint = 22, tempInput, tempOutput;
double tempKp = 2, tempKi = 0.1, tempKd = 0.01;
PID tempPID(&tempInput, &tempOutput, &tempSetpoint, tempKp, tempKi, tempKd, DIRECT);
#include "thingProperties.h"
unsigned long previousMillis = 0;
const long interval = 500;
void setup() {
Serial.begin(9600);
delay(1500);
// Initialize BH1750 light sensor
LightSensor.begin(); // No return value to check
Serial.println("BH1750 initialized.");
// Initialize DHT sensor
dht.begin();
// Initialize PID controller
tempPID.SetMode(AUTOMATIC);
tempPID.SetOutputLimits(0, 100);
// Set pin modes
pinMode(dimmerPin, OUTPUT);
pinMode(relay_1, OUTPUT);
// Initialize Arduino Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
ArduinoCloud.update();
// Declare dimmerLevel at the top of the loop
int dimmerLevel = 0;
// Read soil moisture sensor and map to percentage
int rawSoilMoisture = analogRead(soil_moisture_sensor);
sm_sensor = constrain(map(rawSoilMoisture, 1000, 2725, 0, 100), 0, 100);
int soilInput = sm_sensor;
// Read light intensity in lux and scale it
int lux = LightSensor.GetLightIntensity();
light_intensity = map(lux, 0, 65535, 0, 100);
Serial.print("Light: ");
Serial.println(lux);
// Read temperature from DHT sensor and compute PID
float temp = dht.readTemperature();
if (!isnan(temp)) {
temperature = temp;
tempInput = temp;
// Compute PID output
tempPID.Compute();
// Map PID output (0–100%) to dimmer level (0–255 for PWM)
dimmerLevel = map((int)tempOutput, 0, 100, 0, 255);
analogWrite(dimmerPin, dimmerLevel);
} else {
Serial.println("Failed to read temperature from DHT sensor!");
}
// Automatic pump control logic
if (!useCloudControl) {
if (soilInput < moistureLowThreshold) {
digitalWrite(relay_1, LOW); // Turn on pump
relay = true;
} else if (soilInput > moistureHighThreshold) {
digitalWrite(relay_1, HIGH); // Turn off pump
relay = false;
}
} else {
onRelayChange(); // Handle manual control via cloud
}
// Serial debugging output
Serial.print("Temperature: ");
Serial.print(tempInput);
Serial.print("°C, PID Output: ");
Serial.print(tempOutput);
Serial.print(", Dimmer Level: ");
Serial.println(dimmerLevel);
Serial.print("Light Intensity (%): ");
Serial.println(light_intensity);
Serial.print("Soil Moisture (%): ");
Serial.println(sm_sensor);
}
}
// Cloud control callback functions
void onSmSensorChange() {
// Add your code here to act upon SmSensor change
}
void onLightIntensityChange() {
// Add your code here to act upon LightIntensity change
}
void onTemperatureChange() {
// Add your code here to act upon Temperature change
}
void onRelayChange() {
// Add your code here to act upon Relay change
if (relay) {
digitalWrite(relay_1, LOW); // Turn on the relay
Serial.println("Pump ON (Manual mode)");
} else {
digitalWrite(relay_1, HIGH); // Turn off the relay
Serial.println("Pump OFF (Manual mode)");
}
}
/*
Since LightSensor is READ_WRITE variable, onLightSensorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLightSensorChange() {
// Add your code here to act upon LightSensor change
}