Skip to content

Commit af751cb

Browse files
committed
add bthome_vibration
1 parent c8a939a commit af751cb

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

apps.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,25 @@
283283
"icon": "icon.png",
284284
"version":"0.01",
285285
"description": "Turn your Espruino device into a BTHome/Home Assistant compatible energy monitor by sensing flashes from your electricty meter (needs an LDR wired between D1 and D2)",
286-
"tags": "bluetooth,homeassistant,energy,power,electricity",
286+
"tags": "bluetooth,homeassistant,energy,power,electricity,sensor",
287287
"readme": "README.md",
288288
"needsFeatures":["BLE"],
289289
"storage": [
290290
{"name":".bootcde","url":"app.js"}
291291
]
292292
},
293+
{ "id": "bthome_vibration",
294+
"name": "BTHome Vibration Sensor",
295+
"icon": "icon.png",
296+
"version":"0.01",
297+
"description": "Turn your Puck.js into a BTHome/Home Assistant compatible vibration/movement sensor. When it moves, it'll send that information as a BTHome packet which will then appear in HomeAssistant.",
298+
"tags": "bluetooth,homeassistant,vibration,movement,sensor",
299+
"readme": "README.md",
300+
"needsFeatures":["BLE","ACCEL"],
301+
"storage": [
302+
{"name":".bootcde","url":"app.js"}
303+
]
304+
},
293305
{ "id": "ha_led",
294306
"name": "Home Assistant Bluetooth LED",
295307
"icon": "icon.png",

apps/bthome_vibration/ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: New App!

apps/bthome_vibration/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# BTHome Vibration Sensor
2+
3+
This advertises battery level, temperature, and whether the Puck is moving or not in a [BTHome (https://bthome.io/)](https://bthome.io/) compatible format, which can then be used in https://www.home-assistant.io/
4+
5+
This is based on https://www.espruino.com/BTHome and https://www.espruino.com/Puck.js#accelerometer-gyro
6+
7+
The code is available on https://www.espruino.com/Puck.js+Vibration+Sensor
8+
9+
## Usage
10+
11+
Install the app and disconnect, then attach the Puck.js running this code onto something that can move (such as a washing machine).
12+
13+
The current battery level, temperature, and whether the Puck is moving or not will then be sent to Home Assistant.

apps/bthome_vibration/app.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const TIMEOUT = 10000; // 10s
2+
// using code from https://www.espruino.com/Puck.js#accelerometer-gyro
3+
// and https://www.espruino.com/BTHome
4+
5+
// Timeout to consider movement stopped
6+
var idleTimeout;
7+
// Are we moving?
8+
var isMoving = false;
9+
10+
function updateAdvertising() {
11+
NRF.setAdvertising(require("BTHome").getAdvertisement([
12+
{
13+
type : "battery",
14+
v : E.getBattery()
15+
},
16+
{
17+
type : "temperature",
18+
v : E.getTemperature()
19+
},
20+
{
21+
type : "moving",
22+
v : isMoving
23+
},
24+
]), {
25+
name : "Movement",
26+
interval: 600,
27+
// not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
28+
//connectable : false, scannable : false,
29+
});
30+
}
31+
32+
function onIdle() {
33+
idleTimeout = undefined;
34+
isMoving = false;
35+
updateAdvertising();
36+
//LED.reset();
37+
}
38+
39+
require("puckjsv2-accel-movement").on();
40+
Puck.on('accel',function(a) { "ram"
41+
//LED.set();
42+
if (idleTimeout) clearTimeout(idleTimeout);
43+
else {
44+
isMoving = true;
45+
updateAdvertising();
46+
}
47+
idleTimeout = setTimeout(onIdle, TIMEOUT);
48+
});
49+
// turn off with require("puckjsv2-accel-movement").off();
50+
51+
// Update advertising state every 2 minutes to update battery/temp
52+
setInterval(updateAdvertising, 2*60000);

apps/bthome_vibration/icon.png

10 KB
Loading

0 commit comments

Comments
 (0)