Skip to content

Commit e8532e4

Browse files
committed
Merge branch 'feature/homekit' into 'master'
homekit_switch: Added an example for RainMaker + HomeKit See merge request app-frameworks/esp-rainmaker!168
2 parents 46f805c + e45bf60 commit e8532e4

15 files changed

+1027
-0
lines changed

.gitlab-ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ before_script:
137137
- echo Copy gpio.bin to esp-rainmaker-bins-${CI_JOB_ID}/
138138
- cp $CI_PROJECT_DIR/examples/gpio/build/gpio.bin $CI_PROJECT_DIR/esp-rainmaker-bins-${CI_JOB_ID}/
139139

140+
.build_homekit_switch: &build_homekit_switch
141+
# Building homekit_switch app - Started
142+
- cd $CI_PROJECT_DIR/examples/homekit_switch
143+
- mkdir components
144+
- cd components
145+
- echo Cloning esp-homekit-sdk
146+
- git clone --recursive --branch master --depth 1 https://github.com/espressif/esp-homekit-sdk.git
147+
- cd ..
148+
- export HOMEKIT_PATH=$PWD/components/esp-homekit-sdk
149+
- echo Building homekit_switch app - Started
150+
- echo Running idf.py
151+
- idf.py build
152+
- echo Running make
153+
- rm -rf build/ sdkconfig
154+
- make defconfig
155+
- make $MAKEFLAGS
156+
- echo Building homekit_switch app - Done
157+
# Building homekit_switch app - Done
158+
# Copying homekit_switch.bin to esp-rainmaker-bins-${CI_JOB_ID}/
159+
- echo Copy homekit_switch.bin to esp-rainmaker-bins-${CI_JOB_ID}/
160+
- cp $CI_PROJECT_DIR/examples/homekit_switch/build/homekit_switch.bin $CI_PROJECT_DIR/esp-rainmaker-bins-${CI_JOB_ID}/
161+
140162
build_app:
141163
stage: build
142164
image: $CI_DOCKER_REGISTRY/esp32-ci-env
@@ -167,6 +189,9 @@ build_app:
167189
- if [[ "$APP_BUILD" == "all" || "$APP" == "gpio" ]]; then
168190
- *build_gpio
169191
- fi
192+
- if [[ "$APP_BUILD" == "all" || "$APP" == "homekit_switch" ]]; then
193+
- *build_homekit_switch
194+
- fi
170195
- done
171196
- echo Build Complete for $APP_BUILD
172197
# Generating zip file for binaries generated
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
if(NOT DEFINED ENV{HOMEKIT_PATH})
6+
message(FATAL_ERROR "Please set HOMEKIT_PATH to esp-homekit-sdk repo")
7+
endif(NOT DEFINED ENV{HOMEKIT_PATH})
8+
9+
# Add RainMaker components and other common application components
10+
set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common $ENV{HOMEKIT_PATH}/components/homekit)
11+
12+
set(PROJECT_VER "1.0")
13+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
14+
project(homekit_switch)

examples/homekit_switch/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
ifeq ($(HOMEKIT_PATH),)
7+
$(error Please set HOMEKIT_PATH to esp-homekit-sdk repo)
8+
endif
9+
10+
PROJECT_NAME := homekit_switch
11+
PROJECT_VER := 1.0
12+
13+
# Add RainMaker components and other common application components
14+
EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/../../components $(PROJECT_PATH)/../common $(HOMEKIT_PATH)/components/homekit
15+
16+
include $(IDF_PATH)/make/project.mk

examples/homekit_switch/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# HomeKit Switch Example
2+
3+
This example demonstrates ESP RaimMaker + HomeKit integration using the [esp-homekit-sdk](https://github.com/espressif/esp-homekit-sdk).
4+
5+
## Setup
6+
7+
Compiling this example would need the [esp-homekit-sdk](https://github.com/espressif/esp-homekit-sdk) repository. Clone it at a suitable location using:
8+
9+
```
10+
git clone --recursive https://github.com/espressif/esp-homekit-sdk.git
11+
```
12+
13+
Export the path to this repository using:
14+
15+
```
16+
export HOMEKIT_PATH=/path/to/esp-homekit-sdk/
17+
```
18+
19+
## Build and Flash firmware
20+
21+
Follow the ESP RainMaker Documentation [Get Started](https://rainmaker.espressif.com/docs/get-started.html) section to build and flash this firmware. Just note the path of this example. Note that, on bootup, the serial terminal will show 2 QR codes, one small and the other large. The smaller QR code is to be used for HomeKit pairing from the iOS Home app, whereas the larger one is supposed to be used with ESP RainMaker app.
22+
23+
> Note: HomeKit pairing can be done only after the device connects to the Wi-Fi network. However, if you have the MFi variant of the ESP HomeKit SDK, the QR code can be used for WAC Provisioning as well.
24+
> The same QR code may be shown multiple times, so that it is available on screen whenever the device is in HomeKit pairing mode.
25+
26+
## What to expect in this example?
27+
28+
- This example uses the BOOT button and RGB LED on the ESP32-S2-Saola-1 board to demonstrate a switch with HomeKit integration.
29+
- The LED state (green color) indicates the state of the switch.
30+
- Pressing the BOOT button will toggle the state of the switch and hence the LED. This will also reflect on the phone app.
31+
- Toggling the button on the phone app should toggle the LED on your board, and also print messages like these on the ESP32-S2 monitor:
32+
33+
```
34+
I (16073) app_main: Received value = true for Switch - power
35+
```
36+
- Once the board is set up, it can also be paired from iOS Home App. Follow the steps as given in next section.
37+
38+
## Using with iOS Home app
39+
Open the Home app on your iPhone/iPad and follow these steps:
40+
41+
- Tap on "Add Accessory" and scan the small QR code mentioned above.
42+
- If QR code is not visible correctly, you may use the link printed on the serial terminal or follow these steps:
43+
- Choose the "I Don't Have a Code or Cannot Scan" option.
44+
- Tap on "ESP RainMaker Device" in the list of Nearby Accessories.
45+
- Select the "Add Anyway" option for the "Uncertified Accessory" prompt.
46+
- Enter 11122333 as the Setup code.
47+
- You should eventually see the "ESP RainMaker Device added" message.
48+
- Give a custom name, assign to a room, create scenes as required and you are done.
49+
50+
Now, any changes from ESP RainMaker will reflect on HomeKit and vice-versa. Changes from push button will reflect on both.
51+
52+
### LED not working?
53+
54+
The ESP32-S2-Saola-1 board has the RGB LED connected to GPIO 18. However, a few earlier boards may have it on GPIO 17. Please use `CONFIG_WS2812_LED_GPIO` to set the appropriate value.
55+
56+
### Reset to Factory
57+
58+
Press and hold the BOOT button for more than 3 seconds to reset the board to factory defaults. You will have to provision the board again to use it.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS ./app_driver.c ./app_main.c ./app_homekit.c ./app_wifi_with_homekit.c
2+
INCLUDE_DIRS ".")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
menu "Example Configuration"
2+
3+
config EXAMPLE_USE_HARDCODED_SETUP_CODE
4+
bool "Use hard-coded setup code"
5+
default y
6+
help
7+
HomeKit does not recommend having the setup code programmed in the accessory as is.
8+
Instead, it expects setup info (salt-verifier pair) generated for the given setup code.
9+
Use this option only for testing purposes. For production, the setup info should be
10+
used.
11+
12+
config EXAMPLE_SETUP_CODE
13+
string "HomeKit Setup Code"
14+
default "111-22-333"
15+
depends on EXAMPLE_USE_HARDCODED_SETUP_CODE
16+
help
17+
Setup code to be used for HomeKot pairing, if hard-coded setup code is enabled.
18+
19+
config EXAMPLE_SETUP_ID
20+
string "HomeKit Setup Id"
21+
default "ES32"
22+
depends on EXAMPLE_USE_HARDCODED_SETUP_CODE
23+
help
24+
Setup id to be used for HomeKot pairing, if hard-coded setup code is enabled.
25+
26+
endmenu
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Switch demo implementation using button and RGB LED
2+
3+
This example code is in the Public Domain (or CC0 licensed, at your option.)
4+
5+
Unless required by applicable law or agreed to in writing, this
6+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7+
CONDITIONS OF ANY KIND, either express or implied.
8+
*/
9+
10+
#include <sdkconfig.h>
11+
12+
#include <iot_button.h>
13+
#include <esp_rmaker_core.h>
14+
#include <esp_rmaker_standard_params.h>
15+
16+
#include <app_reset.h>
17+
#include <ws2812_led.h>
18+
#include "app_priv.h"
19+
20+
/* This is the button that is used for toggling the power */
21+
#define BUTTON_GPIO 0
22+
#define BUTTON_ACTIVE_LEVEL 0
23+
24+
/* This is the GPIO on which the power will be set */
25+
#define OUTPUT_GPIO 19
26+
static bool g_power_state = DEFAULT_POWER;
27+
28+
/* These values correspoind to H,S,V = 120,100,10 */
29+
#define DEFAULT_RED 0
30+
#define DEFAULT_GREEN 25
31+
#define DEFAULT_BLUE 0
32+
33+
#define WIFI_RESET_BUTTON_TIMEOUT 3
34+
#define FACTORY_RESET_BUTTON_TIMEOUT 10
35+
36+
void app_indicator_set(bool state)
37+
{
38+
if (state) {
39+
ws2812_led_set_rgb(DEFAULT_RED, DEFAULT_GREEN, DEFAULT_BLUE);
40+
} else {
41+
ws2812_led_clear();
42+
}
43+
}
44+
45+
static void app_indicator_init(void)
46+
{
47+
ws2812_led_init();
48+
app_indicator_set(g_power_state);
49+
}
50+
static void push_btn_cb(void *arg)
51+
{
52+
bool new_state = !g_power_state;
53+
app_driver_set_state(new_state);
54+
esp_rmaker_param_update_and_report(
55+
esp_rmaker_device_get_param_by_name(switch_device, "power"),
56+
esp_rmaker_bool(new_state));
57+
app_homekit_update_state(new_state);
58+
}
59+
60+
static void set_power_state(bool target)
61+
{
62+
gpio_set_level(OUTPUT_GPIO, target);
63+
app_indicator_set(target);
64+
}
65+
66+
void app_driver_init()
67+
{
68+
button_handle_t btn_handle = iot_button_create(BUTTON_GPIO, BUTTON_ACTIVE_LEVEL);
69+
if (btn_handle) {
70+
/* Register a callback for a button tap (short press) event */
71+
iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, push_btn_cb, NULL);
72+
/* Register Wi-Fi reset and factory reset functionality on same button */
73+
app_reset_button_register(btn_handle, WIFI_RESET_BUTTON_TIMEOUT, FACTORY_RESET_BUTTON_TIMEOUT);
74+
}
75+
76+
/* Configure power */
77+
gpio_config_t io_conf = {
78+
.mode = GPIO_MODE_OUTPUT,
79+
.pull_up_en = 1,
80+
};
81+
io_conf.pin_bit_mask = ((uint64_t)1 << OUTPUT_GPIO);
82+
/* Configure the GPIO */
83+
gpio_config(&io_conf);
84+
app_indicator_init();
85+
}
86+
87+
int IRAM_ATTR app_driver_set_state(bool state)
88+
{
89+
if(g_power_state != state) {
90+
g_power_state = state;
91+
set_power_state(g_power_state);
92+
}
93+
return ESP_OK;
94+
}
95+
96+
bool app_driver_get_state(void)
97+
{
98+
return g_power_state;
99+
}

0 commit comments

Comments
 (0)