├── qrcode.png ├── DHT11_bb.png ├── DHT22_bb.png ├── HomeKit Temperature & Humidity Sensor.jpg ├── .github └── FUNDING.yml ├── scr ├── Makefile ├── ota-api.h ├── ota-api.c ├── dht22.c └── dht11.c ├── .gitignore ├── LICENSE └── README.md /qrcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaltyLeo/ESP8266-HomeKit-Temperature-Humidity-Sensor/master/qrcode.png -------------------------------------------------------------------------------- /DHT11_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaltyLeo/ESP8266-HomeKit-Temperature-Humidity-Sensor/master/DHT11_bb.png -------------------------------------------------------------------------------- /DHT22_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaltyLeo/ESP8266-HomeKit-Temperature-Humidity-Sensor/master/DHT22_bb.png -------------------------------------------------------------------------------- /HomeKit Temperature & Humidity Sensor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaltyLeo/ESP8266-HomeKit-Temperature-Humidity-Sensor/master/HomeKit Temperature & Humidity Sensor.jpg -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [AchimPieters] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 2 | custom: ["https://www.paypal.me/AJFPieters ", studiopieters.nl] 3 | -------------------------------------------------------------------------------- /scr/Makefile: -------------------------------------------------------------------------------- 1 | PROGRAM = main 2 | 3 | EXTRA_COMPONENTS = \ 4 | extras/http-parser \ 5 | extras/dhcpserver \ 6 | extras/dht \ 7 | extras/rboot-ota \ 8 | $(abspath ../../components/esp8266-open-rtos/wifi_config) \ 9 | $(abspath ../../components/esp8266-open-rtos/cJSON) \ 10 | $(abspath ../../components/common/button) \ 11 | $(abspath ../../components/common/wolfssl) \ 12 | $(abspath ../../components/common/homekit) 13 | 14 | FLASH_SIZE ?= 32 15 | 16 | 17 | EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS 18 | 19 | include $(SDK_PATH)/common.mk 20 | 21 | monitor: 22 | $(FILTEROUTPUT) --port $(ESPPORT) --baud 115200 --elf $(PROGRAM_OUT) 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /scr/ota-api.h: -------------------------------------------------------------------------------- 1 | #ifndef __HOMEKIT_CUSTOM_CHARACTERISTICS__ 2 | #define __HOMEKIT_CUSTOM_CHARACTERISTICS__ 3 | 4 | #define HOMEKIT_CUSTOM_UUID(value) (value "-0e36-4a42-ad11-745a73b84f2b") 5 | 6 | #define HOMEKIT_SERVICE_CUSTOM_SETUP HOMEKIT_CUSTOM_UUID("000000FF") 7 | 8 | #define HOMEKIT_CHARACTERISTIC_CUSTOM_OTA_TRIGGER HOMEKIT_CUSTOM_UUID("F0000001") 9 | #define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_OTA_TRIGGER(_value, ...) \ 10 | .type = HOMEKIT_CHARACTERISTIC_CUSTOM_OTA_TRIGGER, \ 11 | .description = "Update Firmware", \ 12 | .format = homekit_format_bool, \ 13 | .permissions = homekit_permissions_paired_read \ 14 | | homekit_permissions_paired_write \ 15 | | homekit_permissions_notify, \ 16 | .value = HOMEKIT_BOOL_(_value), \ 17 | ##__VA_ARGS__ 18 | 19 | unsigned int ota_read_sysparam(char **manufacturer,char **serial,char **model,char **revision); 20 | 21 | void ota_update(void *arg); 22 | 23 | void ota_set(homekit_value_t value); 24 | 25 | #define API_OTA_TRIGGER HOMEKIT_CHARACTERISTIC_(CUSTOM_OTA_TRIGGER, false, .setter=ota_set) 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Achim Pieters 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-HomeKit-Temperature-Humidity-Sensor 2 | ESP8266 – HomeKit Temperature & Humidity Sensor 3 | 4 | ![GitHub release (latest by date)](https://img.shields.io/github/v/release/achimpieters/ESP8266-HomeKit-Temperature-Humidity-Sensor) 5 | ![GitHub All Releases](https://img.shields.io/github/downloads/achimpieters/ESP8266-HomeKit-Temperature-Humidity-Sensor/total) 6 | [![GitHub license](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/hyperion-project/hyperion.ng/master/LICENSE) 7 | [![Donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://paypal.me/AJFPieters) 8 | 9 | 10 | For More Information Visit http://www.studiopieters.nl/ 11 | 12 | 13 | 14 | 15 | 16 | For more information visit:https://www.studiopieters.nl/esp8266-homekit-…-humidity-sensor/ ‎ 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | ###### HomeKit Accessory Protocol (HAP) is Apple’s proprietary protocol that enables third-party accessories in the home (e.g., lights, thermostats and door locks) and Apple products to communicate with each other. HAP supports two transports, IP and Bluetooth LE. The information provided in the HomeKit Accessory Protocol Specification (Non-Commercial Version) describes how to implement HAP in an accessory that you create for non-commercial use and that will not be distributed or sold. 30 | 31 | ###### The HomeKit Accessory Protocol Specification (Non-Commercial Version) can be downloaded from the [HomeKit Apple Developer page.](https://developer.apple.com/homekit/) 32 | 33 | ###### Copyright © 2019 Apple Inc. All rights reserved. 34 | -------------------------------------------------------------------------------- /scr/ota-api.c: -------------------------------------------------------------------------------- 1 | #include //for printf 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // the first function is the ONLY thing needed for a repo to support ota after having started with ota-boot 11 | // in ota-boot the user gets to set the wifi and the repository details and it then installs the ota-main binary 12 | 13 | void ota_update(void *arg) { //arg not used 14 | rboot_set_temp_rom(1); //select the OTA main routine 15 | sdk_system_restart(); //#include 16 | // there is a bug in the esp SDK such that if you do not power cycle the chip after serial flashing, restart is unreliable 17 | } 18 | 19 | // this function is optional to couple Homekit parameters to the sysparam variables and github parameters 20 | unsigned int ota_read_sysparam(char **manufacturer,char **serial,char **model,char **revision) { 21 | sysparam_status_t status; 22 | char *value; 23 | 24 | status = sysparam_get_string("ota_repo", &value); 25 | if (status == SYSPARAM_OK) { 26 | strchr(value,'/')[0]=0; 27 | *manufacturer=value; 28 | *model=value+strlen(value)+1; 29 | } else { 30 | *manufacturer="manuf_unknown"; 31 | *model="model_unknown"; 32 | } 33 | status = sysparam_get_string("ota_version", &value); 34 | if (status == SYSPARAM_OK) { 35 | *revision=value; 36 | } else *revision="0.0.0"; 37 | 38 | uint8_t macaddr[6]; 39 | sdk_wifi_get_macaddr(STATION_IF, macaddr); 40 | *serial=malloc(18); 41 | sprintf(*serial,"%02X:%02X:%02X:%02X:%02X:%02X",macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]); 42 | 43 | unsigned int c_hash=0; 44 | char version[16]; 45 | char* rev=version; 46 | char* dot; 47 | strncpy(rev,*revision,16); 48 | if ((dot=strchr(rev,'.'))) {dot[0]=0; c_hash= atoi(rev); rev=dot+1;} 49 | if ((dot=strchr(rev,'.'))) {dot[0]=0; c_hash=c_hash*1000+atoi(rev); rev=dot+1;} 50 | c_hash=c_hash*1000+atoi(rev); 51 | //c_hash=c_hash*10 +configuration_variant; //possible future extension 52 | printf("manuf=\'%s\' serial=\'%s\' model=\'%s\' revision=\'%s\' c#=%d\n",*manufacturer,*serial,*model,*revision,c_hash); 53 | return c_hash; 54 | } 55 | 56 | 57 | 58 | 59 | #include 60 | #include 61 | #include 62 | 63 | static ETSTimer update_timer; 64 | 65 | void ota_set(homekit_value_t value) { 66 | if (value.format != homekit_format_bool) { 67 | printf("Invalid ota-value format: %d\n", value.format); 68 | return; 69 | } 70 | if (value.bool_value) { 71 | //make a distinct light pattern or other feedback to the user = call identify routine 72 | sdk_os_timer_setfn(&update_timer, ota_update, NULL); 73 | sdk_os_timer_arm(&update_timer, 500, 0); //wait 0.5 seconds to trigger the reboot so gui can update and events sent 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /scr/dht22.c: -------------------------------------------------------------------------------- 1 | /** Copyright 2019 Achim Pieters | StudioPieters® 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 17 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | 20 | Build upon: ESP-HomeKit - MIT License - Copyright (c) 2017 Maxim Kulkin 21 | **/ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | #include 40 | #include "ota-api.h" 41 | 42 | const int status_tampered_gpio = 14; 43 | 44 | #define LED_INBUILT_GPIO 2 // this is the onboard LED used to show on/off only 45 | #define SENSOR_PIN 4 46 | bool led_on = false; 47 | bool active = false; 48 | int state; 49 | 50 | void led_write(bool on) { 51 | gpio_write(LED_INBUILT_GPIO, on ? 0 : 1); 52 | } 53 | 54 | void led_init() { 55 | gpio_enable(LED_INBUILT_GPIO, GPIO_OUTPUT); 56 | led_write(led_on); 57 | } 58 | 59 | void sensor_identify_task(void *_args) { 60 | for (int i=0; i<3; i++) { 61 | for (int j=0; j<2; j++) { 62 | led_write(true); 63 | vTaskDelay(100 / portTICK_PERIOD_MS); 64 | led_write(false); 65 | vTaskDelay(100 / portTICK_PERIOD_MS); 66 | } 67 | vTaskDelay(250 / portTICK_PERIOD_MS); 68 | } 69 | led_write(led_on); 70 | vTaskDelete(NULL); 71 | } 72 | 73 | void sensor_identify(homekit_value_t _value) { 74 | printf("Sensor identify\n"); 75 | xTaskCreate(sensor_identify_task, "Sensor identify", 128, NULL, 2, NULL); 76 | } 77 | 78 | homekit_characteristic_t ota_trigger = API_OTA_TRIGGER; 79 | homekit_characteristic_t temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0); 80 | homekit_characteristic_t humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0); 81 | homekit_characteristic_t status_active = HOMEKIT_CHARACTERISTIC_(STATUS_ACTIVE, 0); 82 | homekit_characteristic_t status_fault = HOMEKIT_CHARACTERISTIC_(STATUS_FAULT, 0); 83 | homekit_characteristic_t status_tampered = HOMEKIT_CHARACTERISTIC_(STATUS_TAMPERED, 0); 84 | homekit_characteristic_t status_low_battery = HOMEKIT_CHARACTERISTIC_(STATUS_LOW_BATTERY, 0); 85 | 86 | void temperature_sensor_task(void *_args) { 87 | gpio_set_pullup(SENSOR_PIN, false, false); 88 | 89 | float humidity_value, temperature_value; 90 | while (1) { 91 | bool success = dht_read_float_data( 92 | DHT_TYPE_DHT22, SENSOR_PIN, 93 | &humidity_value, &temperature_value 94 | ); 95 | if (success) { 96 | temperature.value.float_value = temperature_value; 97 | humidity.value.float_value = humidity_value; 98 | bool active = true; 99 | homekit_characteristic_notify(&temperature, HOMEKIT_FLOAT(temperature_value)); 100 | homekit_characteristic_notify(&humidity, HOMEKIT_FLOAT(humidity_value)); 101 | homekit_characteristic_notify(&status_active, HOMEKIT_BOOL(active)); 102 | } else { 103 | printf("Couldnt read data from sensor\n"); 104 | bool state = true; 105 | homekit_characteristic_notify(&status_fault, HOMEKIT_UINT8 (state)); 106 | } 107 | vTaskDelay(3000 / portTICK_PERIOD_MS); 108 | } 109 | } 110 | 111 | void temperature_sensor_init() { 112 | xTaskCreate(temperature_sensor_task, "Temperatore Sensor", 256, NULL, 2, NULL); 113 | } 114 | 115 | void status_tampered_callback(bool high, void *context) { 116 | status_tampered.value = HOMEKIT_UINT8(high ? 1 : 0); // switch from 1:0 to 0:1 to inverse signal. 117 | homekit_characteristic_notify(&status_tampered, status_tampered.value); 118 | } 119 | 120 | 121 | int low_battery_value; 122 | float battery_value; 123 | 124 | void battery_low_task(void *_args) { 125 | while (1) { 126 | battery_value = sdk_system_adc_read(); 127 | printf ("ADC voltage is %.3f\n", 1.0 / 1024 * sdk_system_adc_read()* 4.2); 128 | vTaskDelay(3000 / portTICK_PERIOD_MS); 129 | if (1.0 / 1024 * battery_value * 4.2 < 3.900) { 130 | printf ("Battery value is low.\n"); 131 | low_battery_value = 1; 132 | } 133 | else{ 134 | printf ("Battery value is not low.\n"); 135 | } 136 | homekit_characteristic_notify(&status_low_battery, HOMEKIT_UINT8(low_battery_value)); 137 | vTaskDelay(3000 / portTICK_PERIOD_MS); 138 | } 139 | } 140 | void battery_low_init() { 141 | xTaskCreate(battery_low_task, "Battery Low", 256, NULL, 2, NULL); 142 | } 143 | 144 | homekit_accessory_t *accessories[] = { 145 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]){ 146 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){ 147 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 148 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "StudioPieters®"), 149 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "C39LDDQZFFD"), 150 | HOMEKIT_CHARACTERISTIC(MODEL, "HKSP1SE/T"), 151 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.0.2"), 152 | HOMEKIT_CHARACTERISTIC(IDENTIFY, sensor_identify), 153 | NULL 154 | }), 155 | HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) { 156 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 157 | &temperature, 158 | NULL 159 | }), 160 | HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) { 161 | HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"), 162 | &humidity, 163 | &status_active, 164 | &status_fault, 165 | &status_tampered, 166 | &status_low_battery, 167 | &ota_trigger, 168 | NULL 169 | }), 170 | NULL 171 | }), 172 | NULL 173 | }; 174 | 175 | homekit_server_config_t config = { 176 | .accessories = accessories, 177 | .password = "156-45-568", 178 | .setupId="1TH8", 179 | }; 180 | 181 | void on_wifi_ready() { 182 | } 183 | 184 | void user_init(void) { 185 | uart_set_baud(0, 115200); 186 | homekit_server_init(&config); 187 | 188 | led_init(); 189 | temperature_sensor_init(); 190 | battery_low_init(); 191 | 192 | if (toggle_create(status_tampered_gpio, status_tampered_callback, NULL)) { 193 | printf("Tampered with sensor\n"); 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /scr/dht11.c: -------------------------------------------------------------------------------- 1 | /** Copyright 2019 Achim Pieters | StudioPieters® 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 17 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | 20 | Build upon: ESP-HomeKit - MIT License - Copyright (c) 2017 Maxim Kulkin 21 | **/ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | #include 40 | 41 | #include "ota-api.h" 42 | 43 | const int status_tampered_gpio = 14; 44 | 45 | #define LED_INBUILT_GPIO 2 // this is the onboard LED used to show on/off only 46 | #define SENSOR_PIN 4 47 | bool led_on = false; 48 | bool active = false; 49 | int state; 50 | 51 | void led_write(bool on) { 52 | gpio_write(LED_INBUILT_GPIO, on ? 0 : 1); 53 | } 54 | 55 | void led_init() { 56 | gpio_enable(LED_INBUILT_GPIO, GPIO_OUTPUT); 57 | led_write(led_on); 58 | } 59 | 60 | void sensor_identify_task(void *_args) { 61 | for (int i=0; i<3; i++) { 62 | for (int j=0; j<2; j++) { 63 | led_write(true); 64 | vTaskDelay(100 / portTICK_PERIOD_MS); 65 | led_write(false); 66 | vTaskDelay(100 / portTICK_PERIOD_MS); 67 | } 68 | vTaskDelay(250 / portTICK_PERIOD_MS); 69 | } 70 | led_write(led_on); 71 | vTaskDelete(NULL); 72 | } 73 | 74 | void sensor_identify(homekit_value_t _value) { 75 | printf("Sensor identify\n"); 76 | xTaskCreate(sensor_identify_task, "Sensor identify", 128, NULL, 2, NULL); 77 | } 78 | 79 | homekit_characteristic_t ota_trigger = API_OTA_TRIGGER; 80 | homekit_characteristic_t temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0); 81 | homekit_characteristic_t humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0); 82 | homekit_characteristic_t status_active = HOMEKIT_CHARACTERISTIC_(STATUS_ACTIVE, 0); 83 | homekit_characteristic_t status_fault = HOMEKIT_CHARACTERISTIC_(STATUS_FAULT, 0); 84 | homekit_characteristic_t status_tampered = HOMEKIT_CHARACTERISTIC_(STATUS_TAMPERED, 0); 85 | homekit_characteristic_t status_low_battery = HOMEKIT_CHARACTERISTIC_(STATUS_LOW_BATTERY, 0); 86 | 87 | void temperature_sensor_task(void *_args) { 88 | gpio_set_pullup(SENSOR_PIN, false, false); 89 | 90 | float humidity_value, temperature_value; 91 | while (1) { 92 | bool success = dht_read_float_data( 93 | DHT_TYPE_DHT11, SENSOR_PIN, 94 | &humidity_value, &temperature_value 95 | ); 96 | if (success) { 97 | temperature.value.float_value = temperature_value; 98 | humidity.value.float_value = humidity_value; 99 | bool active = true; 100 | homekit_characteristic_notify(&temperature, HOMEKIT_FLOAT(temperature_value)); 101 | homekit_characteristic_notify(&humidity, HOMEKIT_FLOAT(humidity_value)); 102 | homekit_characteristic_notify(&status_active, HOMEKIT_BOOL(active)); 103 | } else { 104 | printf("Couldnt read data from sensor\n"); 105 | bool state = true; 106 | homekit_characteristic_notify(&status_fault, HOMEKIT_UINT8 (state)); 107 | } 108 | vTaskDelay(3000 / portTICK_PERIOD_MS); 109 | } 110 | } 111 | 112 | void temperature_sensor_init() { 113 | xTaskCreate(temperature_sensor_task, "Temperatore Sensor", 256, NULL, 2, NULL); 114 | } 115 | 116 | void status_tampered_callback(bool high, void *context) { 117 | status_tampered.value = HOMEKIT_UINT8(high ? 1 : 0); // switch from 1:0 to 0:1 to inverse signal. 118 | homekit_characteristic_notify(&status_tampered, status_tampered.value); 119 | } 120 | 121 | 122 | int low_battery_value; 123 | float battery_value; 124 | 125 | void battery_low_task(void *_args) { 126 | while (1) { 127 | battery_value = sdk_system_adc_read(); 128 | printf ("ADC voltage is %.3f\n", 1.0 / 1024 * sdk_system_adc_read()* 4.2); 129 | vTaskDelay(3000 / portTICK_PERIOD_MS); 130 | if (1.0 / 1024 * battery_value * 4.2 < 3.900) { 131 | printf ("Battery value is low.\n"); 132 | low_battery_value = 1; 133 | } 134 | else{ 135 | printf ("Battery value is not low.\n"); 136 | } 137 | homekit_characteristic_notify(&status_low_battery, HOMEKIT_UINT8(low_battery_value)); 138 | vTaskDelay(3000 / portTICK_PERIOD_MS); 139 | } 140 | } 141 | void battery_low_init() { 142 | xTaskCreate(battery_low_task, "Battery Low", 256, NULL, 2, NULL); 143 | } 144 | 145 | homekit_accessory_t *accessories[] = { 146 | HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]){ 147 | HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){ 148 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 149 | HOMEKIT_CHARACTERISTIC(MANUFACTURER, "StudioPieters®"), 150 | HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "C39LDDQZFFD"), 151 | HOMEKIT_CHARACTERISTIC(MODEL, "HKSP1SE/T"), 152 | HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.0.2"), 153 | HOMEKIT_CHARACTERISTIC(IDENTIFY, sensor_identify), 154 | NULL 155 | }), 156 | HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) { 157 | HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"), 158 | &temperature, 159 | NULL 160 | }), 161 | HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) { 162 | HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"), 163 | &humidity, 164 | &status_active, 165 | &status_fault, 166 | &status_tampered, 167 | &status_low_battery, 168 | &ota_trigger, 169 | NULL 170 | }), 171 | NULL 172 | }), 173 | NULL 174 | }; 175 | 176 | homekit_server_config_t config = { 177 | .accessories = accessories, 178 | .password = "156-45-568", 179 | .setupId="1TH8", 180 | }; 181 | 182 | void on_wifi_ready() { 183 | } 184 | 185 | void user_init(void) { 186 | uart_set_baud(0, 115200); 187 | homekit_server_init(&config); 188 | 189 | led_init(); 190 | temperature_sensor_init(); 191 | battery_low_init(); 192 | 193 | if (toggle_create(status_tampered_gpio, status_tampered_callback, NULL)) { 194 | printf("Tampered with sensor\n"); 195 | } 196 | } 197 | --------------------------------------------------------------------------------