Skip to content

Commit 97606ef

Browse files
committed
Example02_Switch
1 parent 5204b75 commit 97606ef

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
void my_accessory_identify(homekit_value_t _value) {
13+
printf("accessory identify\n");
14+
}
15+
16+
// Switch (HAP section 8.38)
17+
// required: ON
18+
// optional: NAME
19+
20+
// format: bool; HAP section 9.70; write the .setter function to get the switch-event sent from iOS Home APP.
21+
homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);
22+
23+
// format: string; HAP section 9.62; max length 64
24+
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Switch");
25+
26+
homekit_accessory_t *accessories[] = {
27+
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) {
28+
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
29+
HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
30+
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
31+
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
32+
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
33+
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
34+
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
35+
NULL
36+
}),
37+
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
38+
&cha_switch_on,
39+
&cha_name,
40+
NULL
41+
}),
42+
NULL
43+
}),
44+
NULL
45+
};
46+
47+
homekit_server_config_t config = {
48+
.accessories = accessories,
49+
.password = "111-11-111"
50+
};
51+
52+

examples/Example02_Switch/switch.ino

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* switch.ino
3+
*
4+
* This example shows how to:
5+
* 1. define a switch accessory and its characteristics (in my_accessory.c).
6+
* 2. get the switch-event sent from iOS Home APP.
7+
* 3. report the switch value to HomeKit
8+
*
9+
* Created on: 2020-05-15
10+
* Author: Mixiaoxiao (Wang Bin)
11+
*
12+
*/
13+
14+
#include <Arduino.h>
15+
#include <arduino_homekit_server.h>
16+
#include "wifi_info.h"
17+
18+
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
wifi_connect(); // in wifi_info.h
23+
my_homekit_setup();
24+
}
25+
26+
void loop() {
27+
my_homekit_loop();
28+
delay(10);
29+
}
30+
31+
//==============================
32+
// Homekit setup and loop
33+
//==============================
34+
35+
// access your HomeKit characteristics defined in my_accessory.c
36+
extern "C" homekit_server_config_t config;
37+
extern "C" homekit_characteristic_t cha_switch_on;
38+
39+
static uint32_t next_heap_millis = 0;
40+
41+
#define PIN_SWITCH 2
42+
43+
//Called when the switch value is changed by iOS Home APP
44+
void cha_switch_on_setter(const homekit_value_t value){
45+
bool on = value.bool_value;
46+
cha_switch_on.value.bool_value = on;//sync the value
47+
LOG_D("Switch: %s", on ? "ON" : "OFF");
48+
digitalWrite(PIN_SWITCH, on ? LOW : HIGH);
49+
}
50+
51+
void my_homekit_setup() {
52+
pinMode(PIN_SWITCH, OUTPUT);
53+
digitalWrite(PIN_SWITCH, HIGH);
54+
55+
//Add the .setter function to get the switch-event sent from iOS Home APP.
56+
//The .setter should be added before arduino_homekit_setup.
57+
//HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
58+
//Maybe this is a legacy design issue in the original esp-homekit library,
59+
//and I have no reason to modify this "feature".
60+
cha_switch_on.setter = cha_switch_on_setter;
61+
arduino_homekit_setup(&config);
62+
63+
//report the switch value to HomeKit if it is changed (e.g. by a physical button)
64+
//bool switch_is_on = true/false;
65+
//cha_switch_on.value.bool_value = switch_is_on;
66+
//homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
67+
}
68+
69+
void my_homekit_loop() {
70+
arduino_homekit_loop();
71+
const uint32_t t = millis();
72+
if (t > next_heap_millis) {
73+
// show heap info every 5 seconds
74+
next_heap_millis = t + 5 * 1000;
75+
LOG_D("Free heap: %d, HomeKit clients: %d",
76+
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
77+
78+
}
79+
}

examples/Example02_Switch/wifi_info.h

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)