Skip to content

Commit 5204b75

Browse files
committed
Example01_TemperatureSensor
1 parent 19df21e commit 5204b75

File tree

9 files changed

+213
-0
lines changed

9 files changed

+213
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* my_accessory.c
3+
* Define the accessory in C language using the Macro in characteristics.h
4+
*
5+
* Created on: 2020-05-15
6+
* Author: Mixiaoxiao (Wang Bin)
7+
*/
8+
9+
#include <homekit/homekit.h>
10+
#include <homekit/characteristics.h>
11+
12+
// Called to identify this accessory. See HAP section 6.7.6 Identify Routine
13+
// Generally this is called when paired successfully or click the "Identify Accessory" button in Home APP.
14+
void my_accessory_identify(homekit_value_t _value) {
15+
printf("accessory identify\n");
16+
}
17+
18+
// For TEMPERATURE_SENSOR,
19+
// the required characteristics are: CURRENT_TEMPERATURE
20+
// the optional characteristics are: NAME, STATUS_ACTIVE, STATUS_FAULT, STATUS_TAMPERED, STATUS_LOW_BATTERY
21+
// See HAP section 8.41 and characteristics.h
22+
23+
// (required) format: float; HAP section 9.35; min 0, max 100, step 0.1, unit celsius
24+
homekit_characteristic_t cha_current_temperature = HOMEKIT_CHARACTERISTIC_(CURRENT_TEMPERATURE, 0);
25+
26+
// (optional) format: string; HAP section 9.62; max length 64
27+
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Temperature Sensor");
28+
29+
30+
// (optional) format: bool; HAP section 9.96
31+
// homekit_characteristic_t cha_status_active = HOMEKIT_CHARACTERISTIC_(STATUS_ACTIVE, true);
32+
33+
// (optional) format: uint8; HAP section 9.97; 0 "No Fault", 1 "General Fault"
34+
// homekit_characteristic_t cha_status_fault = HOMEKIT_CHARACTERISTIC_(STATUS_FAULT, 0);
35+
36+
// (optional) format: uint8; HAP section 9.100; 0 "Accessory is not tampered", 1 "Accessory is tampered with"
37+
// homekit_characteristic_t cha_status_tampered = HOMEKIT_CHARACTERISTIC_(STATUS_TAMPERED, 0);
38+
39+
// (optional) format: uint8; HAP section 9.99; 0 "Battery level is normal", 1 "Battery level is low"
40+
// homekit_characteristic_t cha_status_low_battery = HOMEKIT_CHARACTERISTIC_(STATUS_LOW_BATTERY, 0);
41+
42+
// example for humidity
43+
// homekit_characteristic_t cha_humidity = HOMEKIT_CHARACTERISTIC_(CURRENT_RELATIVE_HUMIDITY, 0);
44+
45+
homekit_accessory_t *accessories[] = {
46+
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_sensor, .services=(homekit_service_t*[]) {
47+
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
48+
HOMEKIT_CHARACTERISTIC(NAME, "Temperature Sensor"),
49+
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
50+
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
51+
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
52+
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
53+
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
54+
NULL
55+
}),
56+
HOMEKIT_SERVICE(TEMPERATURE_SENSOR, .primary=true, .characteristics=(homekit_characteristic_t*[]) {
57+
&cha_current_temperature,
58+
&cha_name,//optional
59+
//&cha_status_active,//optional
60+
//&cha_status_fault,//optional
61+
//&cha_status_tampered,//optional
62+
//&cha_status_low_battery,//optional
63+
NULL
64+
}),
65+
// Add this HOMEKIT_SERVICE if you has a HUMIDITY_SENSOR together
66+
/*
67+
HOMEKIT_SERVICE(HUMIDITY_SENSOR, .characteristics=(homekit_characteristic_t*[]) {
68+
HOMEKIT_CHARACTERISTIC(NAME, "Humidity Sensor"),
69+
&cha_humidity,
70+
NULL
71+
}),*/
72+
NULL
73+
}),
74+
NULL
75+
};
76+
77+
homekit_server_config_t config = {
78+
.accessories = accessories,
79+
.password = "111-11-111"
80+
};
81+
82+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* wifi_info.h
3+
*
4+
* Created on: 2020-05-15
5+
* Author: Mixiaoxiao (Wang Bin)
6+
*/
7+
8+
#ifndef WIFI_INFO_H_
9+
#define WIFI_INFO_H_
10+
11+
#if defined(ESP8266)
12+
#include <ESP8266WiFi.h>
13+
#elif defined(ESP32)
14+
#include <WiFi.h>
15+
#endif
16+
17+
const char *ssid = "your-ssid";
18+
const char *password = "your-password";
19+
20+
void wifi_connect() {
21+
WiFi.persistent(false);
22+
WiFi.mode(WIFI_STA);
23+
WiFi.setAutoReconnect(true);
24+
WiFi.begin(ssid, password);
25+
Serial.println("WiFi connecting...");
26+
while (!WiFi.isConnected()) {
27+
delay(100);
28+
Serial.print(".");
29+
}
30+
Serial.print("\n");
31+
Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());
32+
}
33+
34+
#endif /* WIFI_INFO_H_ */

0 commit comments

Comments
 (0)