Skip to content

Instantly share code, notes, and snippets.

@moritzmhmk
Created January 3, 2017 12:29
Show Gist options
  • Save moritzmhmk/47269695d60f56e0e46d65241c243102 to your computer and use it in GitHub Desktop.
Save moritzmhmk/47269695d60f56e0e46d65241c243102 to your computer and use it in GitHub Desktop.
control a 433Mhz switch with nodejs using rpio
const rpio = require('rpio')
rpio.init({mapping: 'gpio'})
rpio.open(17, rpio.OUTPUT)
const defaultProtocol = {
tristate: true,
pulseLength: 350,
zero: {high: 1, low: 3},
one: {high: 3, low: 1},
sync: {high: 1, low: 31}
}
let send = (code, protocol = {}) => {
protocol = Object.assign({}, defaultProtocol, protocol)
transmit(protocol.sync, protocol.pulseLength)
for (let i = 0; i < code.length; i++) {
const char = code.charAt(i)
if (char === '0') { transmit(protocol.zero, protocol.pulseLength) }
if (char === '0' && protocol.tristate) { transmit(protocol.zero, protocol.pulseLength) }
if (char === '1') { transmit(protocol.one, protocol.pulseLength) }
if (char === '1' && protocol.tristate) { transmit(protocol.one, protocol.pulseLength) }
if (char === 'F' && protocol.tristate) { transmit(protocol.zero, protocol.pulseLength) }
if (char === 'F' && protocol.tristate) { transmit(protocol.one, protocol.pulseLength) }
}
}
let transmit = (pulses, pulseLength) => {
rpio.write(17, rpio.HIGH)
rpio.usleep(pulseLength * pulses.high)
rpio.write(17, rpio.LOW)
rpio.usleep(pulseLength * pulses.low)
}
for (let i = 0; i < 10; i++) {
send('0F00F' + 'FF0FF' + '0F', {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment