Mixiaoxiao / Arduino-HomeKit-ESP8266

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

Doesn't show up on homeapp #193

Open wasweisic opened 1 year ago

wasweisic commented 1 year ago
void setup() { 
  Serial.begin(115200);
  Serial.println();
 // attempt to connect to Wifi network:
  Serial.print("Connecting to Wifi SSID ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nWiFi connected. IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Retrieving time: ");
  configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  time_t now = time(nullptr);
  while (now < 24 * 3600)
  {
    Serial.print(".");
    delay(100);
    now = time(nullptr);
  }
  Serial.println(now);
  homekit_storage_reset();
  my_homekit_setup();
}

void loop() {
  my_homekit_loop();
  delay(10);
}

// Optional Sensor Stuff
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN D8 //13
#define DHTTYPE    DHT11
DHT dht(DHTPIN, DHTTYPE);
float t = 0.0;
float h = 0.0;

//Pins
const int inputPin = D3; // This one has an internal pullup for Bell
const int relayPin = D1; // for Relay and Opener
const int led = LED_BUILTIN;
int c = 0; //Status on Bell

extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_switch_on;
extern "C" homekit_characteristic_t cha_temperature;
extern "C" homekit_characteristic_t cha_humidity;
extern "C" homekit_characteristic_t cha_contact;

#define HOMEKIT_CONTACT_SENSOR_DETECTED       0
#define HOMEKIT_CONTACT_SENSOR_NOT_DETECTED   1

void my_homekit_setup() {
  pinMode(DHTPIN, INPUT);
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  pinMode(relayPin, OUTPUT);
  delay(10);
  digitalWrite(relayPin, HIGH); // initialize pin as on
  cha_switch_on.setter = cha_switch_on_setter;
  arduino_homekit_setup(&config);

  bool switch_is_on = true/false;
  cha_switch_on.value.bool_value = switch_is_on;
  homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
}

void cha_switch_on_setter(const homekit_value_t value) {
  bool on = value.bool_value;
  cha_switch_on.value.bool_value = on;
  LOG_D("Switch: %s", on ? "ON" : "OFF");
  digitalWrite(relayPin, on ? HIGH : LOW);
}

static uint32_t next_heap_millis = 0;
static uint32_t next_report_millis = 0;
int readDelay = 100;
int buttonPressDelay = 0;

void my_homekit_loop() {
  arduino_homekit_loop();
  c = digitalRead(inputPin)< 1 ?
     HOMEKIT_CONTACT_SENSOR_DETECTED : HOMEKIT_CONTACT_SENSOR_NOT_DETECTED;
  digitalWrite(led, c);
  delay(readDelay);
  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 t = dht.readTemperature();
  float h = dht.readHumidity(); 
  c = digitalRead(inputPin);
  cha_contact.value.uint8_value = c;
  homekit_characteristic_notify(&cha_contact, cha_contact.value);
  cha_temperature.value.float_value = t;
  homekit_characteristic_notify(&cha_temperature, cha_temperature.value);
  cha_humidity.value.float_value = h;
  homekit_characteristic_notify(&cha_humidity, cha_humidity.value);
  LOG_D("t %.1f, h %.1f,  c %u", t, h, c);
}

ACCESSORY FILE

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

void my_accessory_identify(homekit_value_t _value) {
  printf("accessory identify\n");
}

// format: float; min 0, max 100, step 0.1, unit celsius
homekit_characteristic_t cha_temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 1);
// format: float; min 0, max 100, step 1
homekit_characteristic_t cha_humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 1);
// format: uint8; 0 ”Contact is detected”, 1 ”Contact is not detected”
homekit_characteristic_t cha_contact = HOMEKIT_CHARACTERISTIC_(CONTACT_SENSOR_STATE, 0);
// format: bool; HAP section 9.70; write the .setter function to get the switch-event sent from iOS Home APP.
homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, true);

homekit_accessory_t *accessories[] = {
    HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_bridge, .services=(homekit_service_t*[]) {
      HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
            HOMEKIT_CHARACTERISTIC(NAME, "MultipleSensors"),
            HOMEKIT_CHARACTERISTIC(MANUFACTURER, "makesmart Communityt"),
            HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "123456"),
            HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266 D1 Mini"),
            HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
            HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
            NULL
        }),
        NULL
    }),
  HOMEKIT_ACCESSORY(.id=2, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) {
      HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL
    }),
      HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Temperature"),
      &cha_temperature,
      NULL
    }),
    NULL
  }),
  HOMEKIT_ACCESSORY(.id=3, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) {
      HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL
    }),
      HOMEKIT_SERVICE(HUMIDITY_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Humidity"),
      &cha_humidity,
      NULL
    }),
    NULL
  }),
  HOMEKIT_ACCESSORY(.id=4, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) {
      HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Schalter"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL
    }),
      HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Schalter"),
      &cha_switch_on,
      NULL
    }),
    NULL
  }),
  HOMEKIT_ACCESSORY(.id=5, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) {
      HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Contact Sensor"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL
    }),
      HOMEKIT_SERVICE(CONTACT_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
      HOMEKIT_CHARACTERISTIC(NAME, "Contact"),
      &cha_contact,
      NULL
    }),
    NULL
  }),
    NULL
};

homekit_server_config_t config = {
    .accessories = accessories,
    .password = "111-11-111"
};
N5NMR commented 1 year ago

Hey. Same Problem here. Are there any solutions so far?

Thx.

John