|
| 1 | +/* |
| 2 | + * temperature_sensor.ino |
| 3 | + * |
| 4 | + * This example shows how to: |
| 5 | + * 1. define a temperature sensor accessory and its characteristics (in my_accessory.c). |
| 6 | + * 2. report the sensor value to HomeKit (just random value here, you need to change it to your real sensor value). |
| 7 | + * |
| 8 | + * Created on: 2020-05-15 |
| 9 | + * Author: Mixiaoxiao (Wang Bin) |
| 10 | + * |
| 11 | + * Note: |
| 12 | + * |
| 13 | + * You are recommended to read the Apple's HAP doc before using this library. |
| 14 | + * https://developer.apple.com/support/homekit-accessory-protocol/ |
| 15 | + * |
| 16 | + * This HomeKit library is mostly written in C, |
| 17 | + * you can define your accessory/service/characteristic in a .c file, |
| 18 | + * since the library provides convenient Macro (C only, CPP can not compile) to do this. |
| 19 | + * But it is possible to do this in .cpp or .ino (just not so conveniently), do it yourself if you like. |
| 20 | + * Check out homekit/characteristics.h and use the Macro provided to define your accessory. |
| 21 | + * |
| 22 | + * Generally, the Arduino libraries (e.g. sensors, ws2812) are written in cpp, |
| 23 | + * you can include and use them in a .ino or a .cpp file (but can NOT in .c). |
| 24 | + * A .ino is a .cpp indeed. |
| 25 | + * |
| 26 | + * You can define some variables in a .c file, e.g. int my_value = 1;, |
| 27 | + * and you can access this variable in a .ino or a .cpp by writing extern "C" int my_value;. |
| 28 | + * |
| 29 | + * So, if you want use this HomeKit library and other Arduino Libraries together, |
| 30 | + * 1. define your HomeKit accessory/service/characteristic in a .c file |
| 31 | + * 2. in your .ino, include some Arduino Libraries and you can use them normally |
| 32 | + * write extern "C" homekit_characteristic_t xxxx; to access the characteristic defined in your .c file |
| 33 | + * write your logic code (eg. read sensors) and |
| 34 | + * report your data by writing your_characteristic.value.xxxx_value = some_data; homekit_characteristic_notify(..., ...) |
| 35 | + * done. |
| 36 | + */ |
| 37 | + |
| 38 | +#include <Arduino.h> |
| 39 | +#include <arduino_homekit_server.h> |
| 40 | +#include "wifi_info.h" |
| 41 | +//include the Arduino library for your real sensor here, e.g. <DHT.h> |
| 42 | + |
| 43 | +#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__); |
| 44 | + |
| 45 | +void setup() { |
| 46 | + Serial.begin(115200); |
| 47 | + wifi_connect(); // in wifi_info.h |
| 48 | + my_homekit_setup(); |
| 49 | +} |
| 50 | + |
| 51 | +void loop() { |
| 52 | + my_homekit_loop(); |
| 53 | + delay(10); |
| 54 | +} |
| 55 | + |
| 56 | +//============================== |
| 57 | +// Homekit setup and loop |
| 58 | +//============================== |
| 59 | + |
| 60 | +// access your homekit characteristics defined in my_accessory.c |
| 61 | +extern "C" homekit_server_config_t config; |
| 62 | +extern "C" homekit_characteristic_t cha_current_temperature; |
| 63 | + |
| 64 | +static uint32_t next_heap_millis = 0; |
| 65 | +static uint32_t next_report_millis = 0; |
| 66 | + |
| 67 | +void my_homekit_setup() { |
| 68 | + arduino_homekit_setup(&config); |
| 69 | +} |
| 70 | + |
| 71 | +void my_homekit_loop() { |
| 72 | + arduino_homekit_loop(); |
| 73 | + const uint32_t t = millis(); |
| 74 | + if (t > next_report_millis) { |
| 75 | + // report sensor values every 10 seconds |
| 76 | + next_report_millis = t + 10 * 1000; |
| 77 | + my_homekit_report(); |
| 78 | + } |
| 79 | + if (t > next_heap_millis) { |
| 80 | + // show heap info every 5 seconds |
| 81 | + next_heap_millis = t + 5 * 1000; |
| 82 | + LOG_D("Free heap: %d, HomeKit clients: %d", |
| 83 | + ESP.getFreeHeap(), arduino_homekit_connected_clients_count()); |
| 84 | + |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void my_homekit_report() { |
| 89 | + float temperature_value = random_value(10, 30); // FIXME, read your real sensor here. |
| 90 | + cha_current_temperature.value.float_value = temperature_value; |
| 91 | + LOG_D("Current temperature: %.1f", temperature_value); |
| 92 | + homekit_characteristic_notify(&cha_current_temperature, cha_current_temperature.value); |
| 93 | +} |
| 94 | + |
| 95 | +int random_value(int min, int max) { |
| 96 | + return min + random(max - min); |
| 97 | +} |
0 commit comments