Last active
October 19, 2020 06:40
-
-
Save parasquid/b693069545710bab384a49c3913b16a8 to your computer and use it in GitHub Desktop.
Espruino code for home automation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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