maximkulkin / esp-homekit

Apple HomeKit accessory server library for ESP-OPEN-RTOS
MIT License
1.1k stars 168 forks source link

HOMEKIT_SERVICE_HUMIDIFIER_DEHUMIDIFIER #210

Open densh1k opened 1 year ago

densh1k commented 1 year ago

After the firmware and connection. Homekit shows a constant update of the device. The port monitor does not display any changes. Where did I make a mistake?


#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>

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

void setup() {
    Serial.begin(115200);
    wifi_connect(); // in wifi_info.h
  //homekit_storage_reset();
    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_humidity;
extern "C" homekit_characteristic_t cha_current_humidity_cur;
extern "C" homekit_characteristic_t cha_current_humidity_tar;
extern "C" homekit_characteristic_t cha_current_humidity_rel;

static uint32_t next_heap_millis = 0;
static uint32_t next_report_millis = 0;

#define PIN_SWITCH 2

void state(const homekit_value_t value){
  uint8_t state = value.float_value;
  cha_current_humidity_tar.value.float_value = 0;
  cha_current_humidity_tar.value.float_value = state;
  Serial.print("1:  ");
  Serial.println(state);

}

void state_cur(const homekit_value_t value){
  uint8_t state_cur = value.float_value;
  cha_current_humidity_cur.value.float_value = state_cur;
  Serial.print("2:  ");
  Serial.println(state_cur);

}

void rel(const homekit_value_t value){
  uint8_t rel = value.float_value;
  cha_current_humidity_cur.value.float_value = rel;
  Serial.print("3:  ");
  Serial.println(rel);

}

void my_homekit_setup() {
  cha_current_humidity_tar.setter = state;
  cha_current_humidity_cur.setter = state_cur;
  cha_current_humidity_rel.setter = rel;
    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() {
    cha_current_humidity.value.float_value = 50.0;
    homekit_characteristic_notify(&cha_current_humidity, cha_current_humidity.value);

}

Accessory:

/*
 * my_accessory.c
 * Define the accessory in C language using the Macro in characteristics.h
 *
 *  Created on: 2020-05-15
 *      Author: Mixiaoxiao (Wang Bin)
 */

#include <homekit/homekit.h>
#include <homekit/characteristics.h>

// Called to identify this accessory. See HAP section 6.7.6 Identify Routine
// Generally this is called when paired successfully or click the "Identify Accessory" button in Home APP.
void my_accessory_identify(homekit_value_t _value) {
    printf("accessory identify\n");
}

// (required) format: float; HAP section 9.35; min 0, max 100, step 0.1, unit celsius
homekit_characteristic_t cha_current_humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0);

homekit_characteristic_t cha_current_humidity_cur = HOMEKIT_CHARACTERISTIC_(CURRENT_HUMIDIFIER_DEHUMIDIFIER_STATE, 0);
homekit_characteristic_t cha_current_humidity_tar = HOMEKIT_CHARACTERISTIC_(TARGET_HUMIDIFIER_DEHUMIDIFIER_STATE, 0);
homekit_characteristic_t cha_current_humidity_rel = HOMEKIT_CHARACTERISTIC_(RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD, 0);

// (optional) format: string; HAP section 9.62; max length 64
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Humidifiers");

homekit_accessory_t *accessories[] = {
    HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) {
        HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
            HOMEKIT_CHARACTERISTIC(NAME, "Humidifiers"),
            HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
            HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
            HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
            HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
            HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
            NULL
        }),
        HOMEKIT_SERVICE(HUMIDIFIER_DEHUMIDIFIER, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
            &cha_current_humidity, 
            &cha_name,//optional
      &cha_current_humidity_cur,
      &cha_current_humidity_tar,
      &cha_current_humidity_rel,
            NULL
        }),
        NULL
    }),
    NULL
};

homekit_server_config_t config = {
        .accessories = accessories,
        .password = "111-11-111"
};

IMG_2693