Skip to content

Commit 6b1b343

Browse files
committed
added makesmart lock-mechanism
Addes makesmart lock-mechanism with two easy to use functions: open_lock(), close_lock()
1 parent 592b427 commit 6b1b343

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* makesmart_lock.ino
3+
*
4+
* Created on: 2021-02-05
5+
* Author: cooper @ makesmart.net
6+
* Thank you for this great library!
7+
*
8+
* This example shows how to:
9+
* 1. define a lock accessory and its characteristics in my_accessory.c
10+
* 2. get the target-state sent from iOS Home APP.
11+
* 3. report the current-state value to HomeKit.
12+
*
13+
* you can use both:
14+
* void open_lock(){}
15+
* and
16+
* void close_lock(){}
17+
*
18+
* at the end of this file to let the lock-mechanism do whatever you want.
19+
*
20+
*
21+
* Pairing Code: 123-45-678
22+
*
23+
*
24+
* You should:
25+
* 1. read and use the Example01_TemperatureSensor with detailed comments
26+
* to know the basic concept and usage of this library before other examples。
27+
* 2. erase the full flash or call homekit_storage_reset() in setup()
28+
* to remove the previous HomeKit pairing storage and
29+
* enable the pairing with the new accessory of this new HomeKit example.
30+
*
31+
*/
32+
33+
#include <Arduino.h>
34+
#include <arduino_homekit_server.h>
35+
#include "wifi_info.h"
36+
37+
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
wifi_connect();
42+
homekit_storage_reset();
43+
my_homekit_setup();
44+
}
45+
46+
void loop() {
47+
my_homekit_loop();
48+
delay(10);
49+
}
50+
51+
//==============================
52+
// HomeKit setup and loop
53+
//==============================
54+
55+
// access lock-mechanism HomeKit characteristics defined in my_accessory.c
56+
extern "C" homekit_server_config_t config;
57+
58+
extern "C" homekit_characteristic_t cha_lock_current_state;
59+
extern "C" homekit_characteristic_t cha_lock_target_state;
60+
61+
static uint32_t next_heap_millis = 0;
62+
63+
64+
// called when the lock-mechanism target-set is changed by iOS Home APP
65+
void set_lock(const homekit_value_t value) {
66+
67+
uint8_t state = value.int_value;
68+
cha_lock_current_state.value.int_value = state;
69+
70+
if(state == 0){
71+
// lock-mechanism was unsecured by iOS Home APP
72+
open_lock();
73+
}
74+
if(state == 1){
75+
// lock-mechanism was secured by iOS Home APP
76+
close_lock();
77+
}
78+
79+
//report the lock-mechanism current-sate to HomeKit
80+
homekit_characteristic_notify(&cha_lock_current_state, cha_lock_current_state.value);
81+
82+
}
83+
84+
void my_homekit_setup() {
85+
86+
cha_lock_target_state.setter = set_lock;
87+
arduino_homekit_setup(&config);
88+
89+
90+
}
91+
92+
93+
void my_homekit_loop() {
94+
arduino_homekit_loop();
95+
const uint32_t t = millis();
96+
if (t > next_heap_millis) {
97+
// show heap info every 30 seconds
98+
next_heap_millis = t + 30 * 1000;
99+
LOG_D("Free heap: %d, HomeKit clients: %d",
100+
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
101+
102+
}
103+
}
104+
105+
106+
107+
/* use this functions to let your lock mechanism do whatever yoi want */
108+
void open_lock(){
109+
Serial.println("unsecure");
110+
// add your code here eg switch a relay or whatever
111+
}
112+
113+
void close_lock(){
114+
Serial.println("secure");
115+
// add your code here eg switch a relay or whatever
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* my_accessory.c
3+
* Define the accessory in C language using the Macro in characteristics.h
4+
*
5+
* Created on: 2021-02-05
6+
* Author: cooper @ makesmart.net
7+
* Thank you for this great library!
8+
*
9+
*/
10+
11+
#include <homekit/homekit.h>
12+
#include <homekit/characteristics.h>
13+
14+
void my_accessory_identify(homekit_value_t _value) {
15+
printf("accessory identify\n");
16+
}
17+
18+
19+
// characteristics of the lock-mechanism
20+
// format: uint8_t // 0 is unsecured // 1 is secured
21+
homekit_characteristic_t cha_lock_current_state = HOMEKIT_CHARACTERISTIC_(LOCK_CURRENT_STATE, 1);
22+
homekit_characteristic_t cha_lock_target_state = HOMEKIT_CHARACTERISTIC_(LOCK_TARGET_STATE, 1);
23+
24+
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Lock");
25+
26+
homekit_accessory_t *accessories[] = {
27+
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_door_lock, .services=(homekit_service_t*[]) {
28+
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
29+
HOMEKIT_CHARACTERISTIC(NAME, "Lock"),
30+
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "makesmart Community"),
31+
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "1111111"),
32+
HOMEKIT_CHARACTERISTIC(MODEL, "makesmart Lock"),
33+
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
34+
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
35+
NULL
36+
}),
37+
HOMEKIT_SERVICE(LOCK_MECHANISM, .primary=true, .characteristics=(homekit_characteristic_t*[]){
38+
&cha_lock_current_state,
39+
&cha_lock_target_state,
40+
&cha_name,
41+
NULL
42+
}),
43+
NULL
44+
}),
45+
NULL
46+
};
47+
48+
homekit_server_config_t config = {
49+
.accessories = accessories,
50+
.password = "123-45-678"
51+
};
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

0 commit comments

Comments
 (0)