homie-esp8266: DHT22 read failure
Hi all, I’m trying to explore the “world of Homie” but I’ve a strange issue using DHT22 sensor. If I read temperature value during loop function, with interval set to 300 seconds, it’s ok. Instead if I’m trying to read temperature after a received MQTT message, using a settable property and associated handler, it fails always. Here is the sketch I’m using:
#include <Homie.h>
#include <DHT.h>
#define DHTPIN 4 // GPIO4 pin 2 of ESP8266`
#define DHTTYPE DHT22 // DHT 22 (AM2302)`
const int TEMPERATURE_INTERVAL = 300;
unsigned long lastTemperatureSent = 0;
HomieNode temperatureNode("temperature", "temperature");
DHT dht(DHTPIN, DHTTYPE);
void setupHandler() {
Homie.setNodeProperty(temperatureNode, "unit").send("c");
}
float readTemp(){
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read temperature from DHT sensor!");
return t;
}
Serial.print("Read Temperature: ");
Serial.print(t);
Serial.println(" °C");
return t;
}
void getSendTemp() {
if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) {
float t = readTemp();
if (isnan(t)) {
Serial.println("Failed getSendTemp!");
return;
}
Homie.setNodeProperty(temperatureNode, "degrees").send(String(t));
lastTemperatureSent = millis();
}
}
bool tempReadHandler(HomieRange range, String value) {
if (value == "true") {
float t = readTemp();
if (isnan(t)) {
Serial.println("Failed ReadTemp!");
return true;
}
Homie.setNodeProperty(temperatureNode, "read").send("true");
Serial.print("Send Temperature: ");
Serial.print(t);
Serial.println(" °C");
Homie.setNodeProperty(temperatureNode, "degrees").send(String(t));
return true;
} else {
return false;
}
}
void loopHandler() {
getSendTemp();
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
dht.begin();
Homie_setFirmware("my-dht22", "1.0.0");
temperatureNode.advertise("read").settable(tempReadHandler);
temperatureNode.advertise("unit");
temperatureNode.advertise("degrees");
Homie.setSetupFunction(setupHandler).setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}
For example if i send a message with
mosquitto_pub -t 'devices/012092e1/temperature/read/set' -m true
the output is
Calling global input handler...
Calling node input handler...
Calling property input handler...
Failed to read temperature from DHT sensor!
during the loopHandler the output is
Read Temperature: 25.60 °C
Thank you
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 21 (6 by maintainers)
There is a piece of code:
which explicit calls
delay
function. Have a look at the exp8266/Arduino project documentation note about delays: link