Hello
At first everything worked fine, but then I made some changes to the code but I don't remember which ones and the humidity and temperature sensor stopped working, I tried everything. Can someone tell me where the error is?
float voltage;
CloudLight led;
CloudTemperatureSensor temperature;
int bat_percentage;
CloudRelativeHumidity humidity;
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"
#include "DHT.h"
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN D4 //DHT22 Pin D4(GPIO 2)
#define led D2
DHT dht(DHTPIN, DHTTYPE);
int analogInPin = A0; // Analog input pin
int sensorValue;
float calibration = 0.40; // Check Battery voltage using multimeter & add/subtract the value
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
dht.begin();
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(led, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
sensorValue = analogRead(analogInPin);
voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
bat_percentage = mapfloat(voltage, 2.8, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
if (bat_percentage >= 100)
{
bat_percentage = 100;
}
if (bat_percentage <= 0)
{
bat_percentage = 1;
}
Serial.print("Analog Value = ");
Serial.print(sensorValue);
Serial.print("t Output Voltage = ");
Serial.print(voltage);
Serial.print("t Battery Percentage = ");
Serial.println(bat_percentage);
delay(1000);
float hm= dht.readHumidity();
Serial.print("Humidity ");
Serial.println(hm);
float temp=dht.readTemperature();
Serial.print("Temperature ");
Serial.println(temp);
humidity=hm;
temperature=temp;
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*
Since Temperature is READ_WRITE variable, onTemperatureChange() is
executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange() {
// Add your code here to act upon Temperature change
}
/*
Since Humidity is READ_WRITE variable, onHumidityChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHumidityChange() {
// Add your code here to act upon Humidity change
}
/*
/*
Since Led is READ_WRITE variable, onLedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLedChange() {
// Add your code here to act upon Led change
if(led)
{
digitalWrite(led,HIGH);
Serial.println("ON");
}
else
{
digitalWrite(led,LOW);
Serial.println("OFF");
}
}
/*
Since Temp is READ_WRITE variable, onTempChange() is
executed every time a new value is received from IoT Cloud.
*/
void onTempChange() {
// Add your code here to act upon Temp change
}
Please help