├── 4way-switch-with-temp-humi.ino ├── README.md ├── my_accessory.c └── wifi_info.h /4way-switch-with-temp-humi.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "wifi_info.h" 6 | 7 | #define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); 8 | 9 | int pinDHT11 = 14; //define pin of DHT11 10 | SimpleDHT11 dht11(pinDHT11); // define device 11 | 12 | void setup() { 13 | Serial.begin(115200); 14 | wifi_connect(); // in wifi_info.h 15 | //homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example 16 | my_homekit_setup(); 17 | } 18 | 19 | void loop() { 20 | my_homekit_loop(); 21 | delay(10); 22 | } 23 | 24 | //============================== 25 | // HomeKit setup and loop 26 | //============================== 27 | 28 | // access your HomeKit characteristics defined in my_accessory.c 29 | extern "C" homekit_server_config_t config; 30 | extern "C" homekit_characteristic_t cha_switch_on1; //The firt relay 31 | extern "C" homekit_characteristic_t cha_switch_on2; //The secondry relay 32 | extern "C" homekit_characteristic_t cha_switch_on3; //The third relay 33 | extern "C" homekit_characteristic_t cha_switch_on4; //The fouth relay 34 | extern "C" homekit_characteristic_t temperature; //temperature 35 | extern "C" homekit_characteristic_t humidity; //humidity 36 | static uint32_t next_heap_millis = 0; 37 | 38 | #define PIN_SWITCH1 16 //The first relay 39 | #define PIN_SWITCH2 5 //The secondry relay 40 | #define PIN_SWITCH3 4 //The third relay 41 | #define PIN_SWITCH4 0 //The fouth relay 42 | 43 | //Called when the switch value is changed by iOS Home APP 44 | void cha_switch_on_setter1(const homekit_value_t value) { 45 | bool on = value.bool_value; 46 | cha_switch_on1.value.bool_value = on; //sync the value 47 | LOG_D("Switch1: %s", on ? "ON" : "OFF"); 48 | digitalWrite(PIN_SWITCH1, on ? HIGH : LOW); 49 | } 50 | 51 | void cha_switch_on_setter2(const homekit_value_t value) { 52 | bool on = value.bool_value; 53 | cha_switch_on2.value.bool_value = on; //sync the value 54 | LOG_D("Switch2: %s", on ? "ON" : "OFF"); 55 | digitalWrite(PIN_SWITCH2, on ? HIGH : LOW); 56 | } 57 | void cha_switch_on_setter3(const homekit_value_t value) { 58 | bool on = value.bool_value; 59 | cha_switch_on3.value.bool_value = on; //sync the value 60 | LOG_D("Switch3: %s", on ? "ON" : "OFF"); 61 | digitalWrite(PIN_SWITCH3, on ? HIGH : LOW); 62 | } 63 | 64 | void cha_switch_on_setter4(const homekit_value_t value) { 65 | bool on = value.bool_value; 66 | cha_switch_on4.value.bool_value = on; //sync the value 67 | LOG_D("Switch4: %s", on ? "ON" : "OFF"); 68 | digitalWrite(PIN_SWITCH4, on ? HIGH : LOW); 69 | } 70 | 71 | void my_homekit_setup() { 72 | 73 | pinMode(PIN_SWITCH1, OUTPUT); 74 | digitalWrite(PIN_SWITCH1, LOW); 75 | pinMode(PIN_SWITCH2, OUTPUT); 76 | digitalWrite(PIN_SWITCH2, LOW); 77 | pinMode(PIN_SWITCH3, OUTPUT); 78 | digitalWrite(PIN_SWITCH3, LOW); 79 | pinMode(PIN_SWITCH4, OUTPUT); 80 | digitalWrite(PIN_SWITCH4, LOW); 81 | 82 | //Add the .setter function to get the switch-event sent from iOS Home APP. 83 | //The .setter should be added before arduino_homekit_setup. 84 | //HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function. 85 | //Maybe this is a legacy design issue in the original esp-homekit library, 86 | //and I have no reason to modify this "feature". 87 | cha_switch_on1.setter = cha_switch_on_setter1; 88 | cha_switch_on2.setter = cha_switch_on_setter2; 89 | cha_switch_on3.setter = cha_switch_on_setter3; 90 | cha_switch_on4.setter = cha_switch_on_setter4; 91 | arduino_homekit_setup(&config); 92 | 93 | //report the switch value to HomeKit if it is changed (e.g. by a physical button) 94 | //bool switch_is_on = true/false; 95 | //cha_switch_on.value.bool_value = switch_is_on; 96 | //homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value); 97 | } 98 | //get temperature and humidity first.then give parm to HomeKit 99 | void get_sensor() 100 | { 101 | byte temperature_value = 0; 102 | byte humidity_value = 0; 103 | int err = SimpleDHTErrSuccess; 104 | delay(1000); 105 | if ((err = dht11.read(&temperature_value, &humidity_value, NULL)) != SimpleDHTErrSuccess) { 106 | Serial.print("Read DHT11 failed, err="); Serial.println(err); 107 | delay(1000); 108 | } 109 | temperature.value.float_value = temperature_value; 110 | humidity.value.float_value = humidity_value; 111 | homekit_characteristic_notify(&temperature, temperature.value); 112 | homekit_characteristic_notify(&humidity, humidity.value); 113 | // Serial.print(temperature_value); Serial.print(" *C, "); 114 | // Serial.print(humidity_value); Serial.println(" H"); 115 | } 116 | 117 | void my_homekit_loop() { 118 | arduino_homekit_loop(); 119 | const uint32_t t = millis(); 120 | if(GPIO_INPUT_GET(GPIO_ID_PIN(12)) == 0x01) 121 | { 122 | delay(3000); 123 | if(GPIO_INPUT_GET(GPIO_ID_PIN(12)) == 0x01) 124 | {homekit_server_reset(); 125 | delay(500); 126 | WiFi.disconnect(); 127 | delay(500); 128 | wifi_connect(); // in wifi_info.h 129 | } 130 | } 131 | if (t > next_heap_millis) { 132 | // show heap info every 5 seconds 133 | next_heap_millis = t + 5 * 1000; 134 | LOG_D("Free heap: %d, HomeKit clients: %d", 135 | ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); 136 | get_sensor(); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arduino-ESP8266-HomeKit 2 | 4路继电器+温湿度(4way-switch+temperature+humidity) 3 | 4 | 1、配网使用wifimanager进行WEB配网,4分钟内没有配网会重启,配置过wifi会自动连,~~如果需要重新连新的SSID, 5 | 需要把原来的路由关闭一会,再重启模块,手机连接模块再重新配置,以后考虑使用按钮解决~~已添加GPIO12,高电平有效,进行重新配置WiFi。 6 | 7 | 2、4个开关+温湿度传感器结合在一个设备里,Homekit里可以分开,配置码为11-111-111 8 | 9 | 3、测试使用的是nodemcu,基于esp8266的都大同小异,pin默认低电平,高电平导通,可修改引脚和初始化电平 10 | 11 | 4、温湿度不是很准确,没有实体温湿度计就没有做进一步优化,有个缺点就是温度没有小数位 12 | -------------------------------------------------------------------------------- /my_accessory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void my_accessory_identify(homekit_value_t _value) { 5 | printf("accessory identify\n"); 6 | } 7 | 8 | // Switch (HAP section 8.38) 9 | // required: ON 10 | // optional: NAME 11 | 12 | // format: bool; HAP section 9.70; write the .setter function to get the switch-event sent from iOS Home APP. 13 | homekit_characteristic_t cha_switch_on1 = HOMEKIT_CHARACTERISTIC_(ON, false); 14 | homekit_characteristic_t cha_switch_on2 = HOMEKIT_CHARACTERISTIC_(ON, false); 15 | homekit_characteristic_t cha_switch_on3 = HOMEKIT_CHARACTERISTIC_(ON, false); 16 | homekit_characteristic_t cha_switch_on4 = HOMEKIT_CHARACTERISTIC_(ON, false); 17 | homekit_characteristic_t temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0); 18 | homekit_characteristic_t humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0); 19 | // format: string; HAP section 9.62; max length 64 20 | homekit_characteristic_t cha_name1 = HOMEKIT_CHARACTERISTIC_(NAME, "Switch"); 21 | homekit_characteristic_t cha_name2 = HOMEKIT_CHARACTERISTIC_(NAME, "Switch2"); 22 | homekit_characteristic_t cha_name3 = HOMEKIT_CHARACTERISTIC_(NAME, "Switch3"); 23 | homekit_characteristic_t cha_name4 = HOMEKIT_CHARACTERISTIC_(NAME, "Switch4"); 24 | homekit_accessory_t *accessories[] = { 25 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) { 26 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) { 27 | HOMEKIT_CHARACTERISTIC(NAME, "Controler"), 28 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Nature"), 29 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "20200718"), 30 | HOMEKIT_CHARACTERISTIC(MODEL, "Light_UP"), 31 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"), 32 | HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify), 33 | NULL 34 | }), 35 | HOMEKIT_SERVICE(SWITCH, .characteristics=(homekit_characteristic_t*[]){ 36 | &cha_switch_on1, 37 | &cha_name1, 38 | NULL 39 | }), 40 | HOMEKIT_SERVICE(SWITCH, .characteristics=(homekit_characteristic_t*[]){ 41 | &cha_switch_on2, 42 | &cha_name2, 43 | NULL 44 | }), 45 | HOMEKIT_SERVICE(SWITCH, .characteristics=(homekit_characteristic_t*[]){ 46 | &cha_switch_on3, 47 | &cha_name3, 48 | NULL 49 | }), 50 | HOMEKIT_SERVICE(SWITCH, .characteristics=(homekit_characteristic_t*[]){ 51 | &cha_switch_on4, 52 | &cha_name4, 53 | NULL 54 | }), 55 | HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .characteristics=(homekit_characteristic_t*[]) { 56 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 57 | &temperature, 58 | NULL 59 | }), 60 | HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) { 61 | HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"), 62 | &humidity, 63 | NULL 64 | }), 65 | NULL 66 | }), 67 | NULL 68 | }; 69 | 70 | homekit_server_config_t config = { 71 | .accessories = accessories, 72 | .password = "111-11-111" 73 | }; 74 | -------------------------------------------------------------------------------- /wifi_info.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFI_INFO_H_ 2 | #define WIFI_INFO_H_ 3 | 4 | 5 | 6 | #include 7 | #include 8 | #include "FS.h" 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | void wifi_connect() { 15 | WiFiManager wifiManager; 16 | wifiManager.setConfigPortalTimeout(240); 17 | if(!wifiManager.autoConnect("HomeKit", "")) 18 | { 19 | Serial.println(F("Failed to connect. Reset and try again. . .")); 20 | delay(3000); 21 | //重置并重试 22 | ESP.reset(); 23 | delay(5000); 24 | } 25 | else 26 | WiFi.mode(WIFI_STA); 27 | } 28 | 29 | #endif /* WIFI_INFO_H_ */ 30 | --------------------------------------------------------------------------------