-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong Temperature and Humidity Readings on DHT22 #196
Comments
Dear,
add pinMode(your DHTpin, INPUT); in setup();
…On Thu, 8 Dec 2022, 10:41 Fabio Magarelli, ***@***.***> wrote:
Following your TemperatureSensor example, I'm getting the wrong reading
for temperature and humidity.
I'm using a Sparkfun ESP8266 Thing Dev, a DHT22 Temperature and Humidity
sensor with the DHT library from Adafruit (v1.4.4).
This is the code I used before without HomeKit to read temperature and
humidity:
#include <DHT.h>
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variablesfloat hum; //Stores humidity valuefloat temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
delay(2000);
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
}
Which prints the correct temperature and humidity values:
Humidity: 58.90 %, Temp: 19.40 Celsius
Humidity: 58.90 %, Temp: 19.50 Celsius
Humidity: 58.90 %, Temp: 19.50 Celsius
...
Now in the Arduino-Homekit TemperatureSensor example (.ino file):
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include "wifi_info.h"//include the Arduino library for your real sensor here, e.g. <DHT.h>
#include <DHT.h>
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
float temperature_value;float humidity_value;
void setup() {
temperature_value = 0.0;
humidity_value = 0.0;
Serial.begin(115200);
wifi_connect(); // in wifi_info.h
my_homekit_setup();
}
void loop() {
my_homekit_loop();
delay(10);
}
//==============================// Homekit setup and loop//==============================
// access your homekit characteristics defined in my_accessory.cextern "C" homekit_server_config_t config;extern "C" homekit_characteristic_t cha_current_temperature;extern "C" homekit_characteristic_t cha_humidity;
static uint32_t next_heap_millis = 0;static uint32_t next_report_millis = 0;
void my_homekit_setup() {
arduino_homekit_setup(&config);
}
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_report_millis) {
// report sensor values every 10 seconds
next_report_millis = t + 10 * 1000;
my_homekit_report();
}
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
}
}
void my_homekit_report() {
//Read data and store it to variables hum and temp
temperature_value = dht.readTemperature();
humidity_value = dht.readHumidity();
cha_current_temperature.value.float_value = temperature_value;
cha_humidity.value.float_value = humidity_value;
LOG_D("Current temperature: %.1f", temperature_value);
LOG_D("Current humidity: %.1f", humidity_value);
homekit_characteristic_notify(&cha_current_temperature, cha_current_temperature.value);
homekit_characteristic_notify(&cha_humidity, cha_humidity.value);
}
But I'm getting this output:
Current temperature: 9.7
Current humidity: 29.6
...
Current temperature: -9.7
Current humidity: 29.3
...
What am I doing wrong?
—
Reply to this email directly, view it on GitHub
<#196>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIPMDFC2AFV76S3C7DZVLMLWMGUN3ANCNFSM6AAAAAASX5C7OM>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Oh 🤦♂️ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following your TemperatureSensor example, I'm getting the wrong reading for temperature and humidity.
I'm using a Sparkfun ESP8266 Thing Dev, a DHT22 Temperature and Humidity sensor with the DHT library from Adafruit (v1.4.4).
This is the code I used before without HomeKit to read temperature and humidity:
Which prints the correct temperature and humidity values:
Now in the Arduino-Homekit TemperatureSensor example (
.ino
file):But I'm getting this output:
What am I doing wrong?
The text was updated successfully, but these errors were encountered: