text: #include <stdio.h>
  #include <espressif/esp_wifi.h>
  #include <espressif/esp_sta.h>
  #include <esp/uart.h>
  #include <esp8266.h>
  #include <FreeRTOS.h>
  #include <task.h>
  
  #include <homekit/homekit.h>
  #include <homekit/characteristics.h>
  #include "wifi.h"
  
  #include <dht/dht.h>
  
  
  #ifndef SENSOR_PIN
  #error SENSOR_PIN is not specified
  #endif
  
  
  static void wifi_init() {
      struct sdk_station_config wifi_config = {
          .ssid = WIFI_SSID,
          .password = WIFI_PASSWORD,
      };
  
      sdk_wifi_set_opmode(STATION_MODE);
      sdk_wifi_station_set_config(&wifi_config);
      sdk_wifi_station_connect();
  }
  
  
  void temperature_sensor_identify(homekit_value_t _value) {
      printf("Temperature sensor identify\n");
  }
  
  homekit_characteristic_t temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0);
  homekit_characteristic_t humidity    = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0);
  homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, "TempSensor");
  
  void temperature_sensor_task(void *_args) {
      gpio_set_pullup(SENSOR_PIN, false, false);
  
      float humidity_value, temperature_value;
      while (1) {
          bool success = dht_read_float_data(
              DHT_TYPE_DHT22, SENSOR_PIN,
              &humidity_value, &temperature_value
          );
          if (success) {
              temperature.value.float_value = temperature_value;
              humidity.value.float_value = humidity_value;
  
              homekit_characteristic_notify(&temperature, HOMEKIT_FLOAT(temperature_value));
              homekit_characteristic_notify(&humidity, HOMEKIT_FLOAT(humidity_value));
          } else {
              printf("Couldnt read data from sensor\n");
          }
  
          vTaskDelay(3000 / portTICK_PERIOD_MS);
      }
  }
  
  void temperature_sensor_init() {
      xTaskCreate(temperature_sensor_task, "Temperature Sensor", 256, NULL, 2, NULL);
  }
  
  
  homekit_accessory_t *accessories[] = {
      HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_thermostat, .services=(homekit_service_t*[]) {
          HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
              &name,
              HOMEKIT_CHARACTERISTIC(MANUFACTURER, "HaPK"),
              HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0012345"),
              HOMEKIT_CHARACTERISTIC(MODEL, "MyTemperatureSensor"),
              HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
              HOMEKIT_CHARACTERISTIC(IDENTIFY, temperature_sensor_identify),
              NULL
          }),
          HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
              HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"),
              &temperature,
              NULL
          }),
          HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) {
              HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"),
              &humidity,
              NULL
          }),
          NULL
      }),
      NULL
  };
  
  homekit_server_config_t config = {
      .accessories = accessories,
      .password = "111-11-111"
  };
  
  void user_init(void) {
      uart_set_baud(0, 115200);
  
      uint8_t macaddr[6];
          sdk_wifi_get_macaddr(STATION_IF, macaddr);
  
          int name_len = snprintf(NULL, 0, "TempSensor-%02X%02X%02X",
                                  macaddr[3], macaddr[4], macaddr[5]);
          char *name_value = malloc(name_len+1);
          snprintf(name_value, name_len+1, "TempSensor-%02X%02X%02X",
                   macaddr[3], macaddr[4], macaddr[5]);
  
          name.value = HOMEKIT_STRING(name_value);
  
      wifi_init();
      temperature_sensor_init();
      homekit_server_init(&config);
  }
  