Skip to content

Instantly share code, notes, and snippets.

@parasquid
Last active October 19, 2020 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parasquid/b693069545710bab384a49c3913b16a8 to your computer and use it in GitHub Desktop.
Save parasquid/b693069545710bab384a49c3913b16a8 to your computer and use it in GitHub Desktop.
Espruino code for home automation
let pressCount = 0;
const batteryPercentage = () => (NRF.getBattery() - 2) * 100;
const LIGHT = 0xFFFF;
const FAN = 0xFFFE;
const LIGHT_FAN_ON = 0xFFFD;
const LIGHT_FAN_OFF = 0xFFFC;
E.on("init", () => {
NRF.setAdvertising({
0x180F: [batteryPercentage()]
}, { interval: 1000 });
});
setInterval(() => {
NRF.setAdvertising({
0x180F: [batteryPercentage()]
}, { interval: 5000 });
}, 30000);
const changeAdvertising = (btn, device) => {
console.log(`button ${btn.pin} pressed!`);
pressCount++;
const data = {};
data[device] = [pressCount];
NRF.setAdvertising(data, { interval: 200 });
digitalPulse(LED, true, 50);
};
setWatch((btn) => changeAdvertising(btn, LIGHT), BTN1, { repeat: true, edge: 'rising' });
setWatch((btn) => changeAdvertising(btn, FAN), BTN2, { repeat: true, edge: 'rising' });
setWatch((btn) => changeAdvertising(btn, LIGHT_FAN_ON), BTN3, { repeat: true, edge: 'rising' });
setWatch((btn) => changeAdvertising(btn, LIGHT_FAN_OFF), BTN4, { repeat: true, edge: 'rising' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment