homebridge-rc433-motion-sensor
Version:
A Homebridge plugin for a 433 MHz motion sensor, that transmit data to a 433 MHz receiver of a Raspberry Pi.
59 lines (45 loc) • 1.73 kB
JavaScript
var Service, Characteristic;
var motionDetected = false;
var rpi433 = require('rpi-433');
var pin;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-433-MHz-motion-sensor", "MotionSensor433MHz", MotionSensor433MHz);
};
function MotionSensor433MHz(log, config) {
//config
this.name = config["name"];
pin = config["pin"];
this.on = config["on"];
this.off = config["off"];
if (this.name == undefined || pin == undefined || this.on == undefined || this.off == undefined ) {
throw "Specify name, pin, on code and off code in config file.";
}
//setup
this.log = log;
this.service = new Service.MotionSensor(this.name);
this.service
.getCharacteristic(Characteristic.MotionDetected)
.on('get', this.getState.bind(this));
this.rfSniffer = rpi433.sniffer({
pin: pin,
debounceDelay: 100
});
this.rfSniffer.on('data', function (data) {
console.log('Code received: '+data.code+' pulse length : '+data.pulseLength);
if(data.code == this.on && !motionDetected){
motionDetected = true;
this.service.setCharacteristic(Characteristic.MotionDetected, motionDetected);
}else if(data.code == this.off && motionDetected){
motionDetected = false;
this.service.setCharacteristic(Characteristic.MotionDetected, motionDetected);
}
}.bind(this));
}
MotionSensor433MHz.prototype.getState = function (callback) {
callback(null, motionDetected);
};
MotionSensor433MHz.prototype.getServices = function () {
return [this.service];
};