Skip to content

Commit

Permalink
add rotation sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Dec 11, 2024
1 parent f04c17b commit 93404a8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/bthome_rotate/ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.01: New App!
12 changes: 12 additions & 0 deletions apps/bthome_rotate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# BTHome Rotation

This advertises temperature, battery and rotation in a [BTHome (https://bthome.io/)](https://bthome.io/) compatible format, which can then be used in https://www.home-assistant.io/

There are two rotations advertised, for the X and Y axes. When the Puck.js is completely flat on a desk (aerial up, button down) a value of `0,0` will be reported, and will vary +/- 180 degrees. Rotation updates once per minute.

This is based on https://www.espruino.com/BTHome


## Usage

Install the app and disconnect, then Puck.js will appear in Home Assistant as a new device
38 changes: 38 additions & 0 deletions apps/bthome_rotate/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var a,rx,ry;

// https://www.espruino.com/BTHome
// Update the data we're advertising here
function updateAdvertising() {
// read accelerometer
a = Puck.accel().acc;
// calculate angle
rx = Math.atan2(a.x,a.z)*180/Math.PI;
ry = Math.atan2(a.y,a.z)*180/Math.PI;

NRF.setAdvertising(require("BTHome").getAdvertisement([

Check warning on line 12 in apps/bthome_rotate/app.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 spaces but found 3
{
type : "battery",
v : E.getBattery()
},
{
type : "temperature",
v : E.getTemperature()
},
{
type : "rotation",
v : rx
},
{
type : "rotation",
v : ry
},
]), {
name : "Rot", interval:1000,
// not being connectable/scannable saves power (but you'll need to reboot to connect again with the IDE!)
//connectable : false, scannable : false,
});
}

NRF.setTxPower(4);
updateAdvertising();
setInterval(updateAdvertising, 60000); // 1 minute
Binary file added apps/bthome_rotate/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions apps/bthome_rotate/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"id": "bthome_rotate",
"name": "BTHome Rotation Sensor",
"icon": "icon.png",
"version": "0.01",
"description": "Turn your Puck.js into a BTHome/Home Assistant compatible angle/rotation sensor",
"tags": "bluetooth,homeassistant,angle,rotation",
"readme": "README.md",
"needsFeatures": [
"BLE",
"ACCEL"
],
"storage": [
{
"name": ".bootcde",
"url": "app.js"
}
]
}

0 comments on commit 93404a8

Please sign in to comment.