Skip to content

Commit d7334c5

Browse files
committed
Add BLE door open detector
1 parent 570b976 commit d7334c5

File tree

8 files changed

+92
-2
lines changed

8 files changed

+92
-2
lines changed

apps.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,25 @@
257257
{ "id": "bthome_button",
258258
"name": "BTHome button",
259259
"icon": "icon.png",
260-
"version":"0.01",
260+
"version":"0.02",
261261
"description": "Turn your device into a BTHome/Home Assistant compatible button",
262262
"tags": "bluetooth,homeassistant",
263263
"readme": "README.md",
264264
"needsFeatures":["BLE"],
265265
"storage": [
266266
{"name":".bootcde","url":"app.js"}
267267
]
268+
},
269+
{ "id": "bthome_door",
270+
"name": "BTHome Door Sensor",
271+
"icon": "icon.png",
272+
"version":"0.01",
273+
"description": "Turn your Puck.js into a BTHome/Home Assistant compatible door open detector",
274+
"tags": "bluetooth,homeassistant,door",
275+
"readme": "README.md",
276+
"needsFeatures":["BLE","MAG"],
277+
"storage": [
278+
{"name":".bootcde","url":"app.js"}
279+
]
268280
}
269281
]

apps/bthome_button/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
0.01: New App!
2+
0.02: Handle long/short press

apps/bthome_button/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ On a Bluetooth LE device, this advertises temperature and battery in a [BTHome (
44

55
When you press the button, a 'button event' will be advertised which can be used to trigger tasks in https://www.home-assistant.io/.
66

7+
It reports `press` for a short press, or `long_press` for pressing over 0.5s
8+
79
## Usage
810

911
Install the app and disconnect - no configuration is needed.

apps/bthome_button/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ if (E.getBattery===undefined)
22
E.getBattery = ()=>100;
33

44
var slowTimeout; //< After 60s we revert to slow advertising
5+
//https://www.espruino.com/BTHome
56

67
// Update the data we're advertising here
78
function updateAdvertising(buttonState) {
@@ -22,7 +23,7 @@ function updateAdvertising(buttonState) {
2223
name : "Sensor",
2324
interval: buttonState?20:2000, // fast when we have a button press, slow otherwise
2425
// not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
25-
//connectable : false, scannable : false,
26+
//connectable : false, scannable : false,
2627
});
2728
/* After 60s, call updateAdvertising again to update battery/temp
2829
and to ensure we're advertising slowly */

apps/bthome_door/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: New App!

apps/bthome_door/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# BTHome Door Sensor
2+
3+
On a Bluetooth LE device, this advertises temperature and battery in a [BTHome (https://bthome.io/)](https://bthome.io/) compatible format, which can then be used in https://www.home-assistant.io/
4+
5+
If you attach a Puck.js running this code near a door, and then put a magnet on the door such that it's near the Puck when the door is closed, this will fire a `long_press` button event to Home Assistant and flash the red LED
6+
when the door is opened, and a `press` event (and green flash) when it is closed.
7+
8+
This is based on https://www.espruino.com/BTHome and https://www.espruino.com/Puck.js+Door+Light
9+
10+
11+
## Usage
12+
13+
Install the app and disconnect, then place the Puck.js running this code near a door (on the door frame), and then put a magnet on the door such that it's near the Puck when the door is closed

apps/bthome_door/app.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
if (E.getBattery===undefined)
2+
E.getBattery = ()=>100;
3+
4+
var slowTimeout; //< After 60s we revert to slow advertising
5+
6+
// https://www.espruino.com/BTHome
7+
// Update the data we're advertising here
8+
function updateAdvertising(buttonState) {
9+
NRF.setAdvertising(require("BTHome").getAdvertisement([
10+
{
11+
type : "battery",
12+
v : E.getBattery()
13+
},
14+
{
15+
type : "temperature",
16+
v : E.getTemperature()
17+
},
18+
{
19+
type: "button_event",
20+
v: buttonState
21+
},
22+
]), {
23+
name : "Door",
24+
interval: (buttonState!="none")?20:2000, // fast when we have a button press, slow otherwise
25+
// not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
26+
//connectable : false, scannable : false,
27+
});
28+
/* After 60s, call updateAdvertising again to update battery/temp
29+
and to ensure we're advertising slowly */
30+
if (slowTimeout) clearTimeout(slowTimeout);
31+
slowTimeout = setTimeout(function() {
32+
slowTimeout = undefined;
33+
updateAdvertising("none" /* no button pressed */);
34+
}, 60000);
35+
}
36+
37+
// Update advertising now
38+
updateAdvertising("none");
39+
40+
// Enable highest power advertising (4 on nRF52, 8 on nRF52840)
41+
NRF.setTxPower(4);
42+
43+
44+
// https://www.espruino.com/Puck.js+Door+Light
45+
var zero = Puck.mag();
46+
var doorOpen = false;
47+
function onMag(p) {
48+
p.x -= zero.x;
49+
p.y -= zero.y;
50+
p.z -= zero.z;
51+
var s = Math.sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
52+
var open = s<1000;
53+
if (open!=doorOpen) {
54+
doorOpen = open;
55+
digitalPulse(open ? LED1 : LED2, 1,1000);
56+
updateAdvertising(open ? "long_press" : "press");
57+
}
58+
}
59+
Puck.on('mag', onMag);
60+
Puck.magOn();

apps/bthome_door/icon.png

10 KB
Loading

0 commit comments

Comments
 (0)