Skip to content
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

Closed
fabiom91 opened this issue Dec 8, 2022 · 3 comments
Closed

Wrong Temperature and Humidity Readings on DHT22 #196

fabiom91 opened this issue Dec 8, 2022 · 3 comments

Comments

@fabiom91
Copy link

fabiom91 commented Dec 8, 2022

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

//Variables
float hum;  //Stores humidity value
float 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.c
extern "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?

@Bighoneypot
Copy link

Bighoneypot commented Dec 8, 2022 via email

@fabiom91
Copy link
Author

fabiom91 commented Dec 8, 2022

Dear, add pinMode(your DHTpin, INPUT); in setup();

Oh 🤦‍♂️
I added dht.begin() in the setup() and now it's working. Apologies.

@fabiom91 fabiom91 closed this as completed Dec 8, 2022
@Bighoneypot
Copy link

Dear, add pinMode(your DHTpin, INPUT); in setup();

Oh 🤦‍♂️
I added dht.begin() in the setup() and now it's working. Apologies.

Dear @fabiom91 I noticed that too but from my smartphone I forgot to write it 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants