Mixiaoxiao / Arduino-HomeKit-ESP8266

Native Apple HomeKit accessory implementation for the ESP8266 Arduino core.
MIT License
1.51k stars 277 forks source link

Help with temperature example #115

Closed gio-dude closed 3 years ago

gio-dude commented 3 years ago

I'm trying to add the DHT22 sensor. I was able to get temperature working but I'm having issue with humidity. I've new to programing and still surprised I was able to get temperature to work. I've included my modifications to the example code. Any guidance would be much appropriate. I get errors at cha_humidity.value.float_value = humidity_value;

`/*

include

include

include "wifi_info.h"

include "DHT.h"

//include the Arduino library for your real sensor here, e.g.

define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);

define DHTPIN 2

define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() { Serial.begin(115200); wifi_connect(); // in wifi_info.h my_homekit_setup();

dht.begin(); }

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;

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() { float temperature_value = dht.readTemperature(true); // FIXME, read your real sensor here. cha_current_temperature.value.float_value = temperature_value; LOG_D("Current temperature: %.1f", temperature_value); homekit_characteristic_notify(&cha_current_temperature, cha_current_temperature.value);

float humidity_value = dht.readHumidity(); // FIXME, read your real sensor here. cha_humidity.value.float_value = humidity_value; LOG_D("Current humidity: %.1f", humidity_value); homekit_characteristic_notify(&cha_humidity, cha_humidity.value); }

int random_value(int min, int max) { return min + random(max - min); }`

Varogkorn commented 3 years ago

You can get some idea from my work. DHT_Switch.zip I have 2 LED and Report Temperature with Humidity from DHT22.

gio-dude commented 3 years ago

You can get some idea from my work. DHT_Switch.zip I have 2 LED and Report Temperature with Humidity from DHT22.

Thank you so much after looking at your code I left out the extern "C" homekit_characteristic_t cha_humidity; line

alexandrerochasalles commented 3 years ago

You can get some idea from my work. DHT_Switch.zip I have 2 LED and Report Temperature with Humidity from DHT22.

This have also solved my issue, thanks a lot.

Just let me know something, how should I know that in the accessory.c the HomeKit_characteristic_t cha_humidity should be (CURRENT_RELATIVE_HUMIDITY) ??? Because I was trying for a while without the "relative" in the middle, and always getting an error...

Varogkorn commented 3 years ago

You can get some idea from my work. DHT_Switch.zip I have 2 LED and Report Temperature with Humidity from DHT22.

This have also solved my issue, thanks a lot.

Just let me know something, how should I know that in the accessory.c the HomeKit_characteristic_t cha_humidity should be (CURRENT_RELATIVE_HUMIDITY) ??? Because I was trying for a while without the "relative" in the middle, and always getting an error...

image