Skip to content

Commit

Permalink
Merge pull request #9 from rsporny/issue-6-reed-switch
Browse files Browse the repository at this point in the history
Issue 6 reed switch
  • Loading branch information
rsporny authored May 17, 2018
2 parents 7be261e + c39e7d1 commit 01113f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Sample accessory:
"pinDown": 11,
"durationUp": 13000,
"durationDown": 13000,
"pinClosed": 17,
"pinOpen": 18,
"activeLow": false
}
]
Expand All @@ -35,7 +37,10 @@ Fields:
- `pinDown` pin for moving down
- `durationUp` milliseconds to open blinds completely
- `durationDown` milliseconds to close blinds completely
- `activeLow` set to false if your relay is activated by high state (default: *true*)
- `pinClosed` [optional] pin connected to reed switch which is active when blind is closed, see *reedActiveLow*
- `pinOpen` [optional] pin connected to reed switch which is active when blind is open, see *reedActiveLow*
- `activeLow` [optional, default: *true*] true: relay activated by low state (0), false: relay activated by high state (1), affects *pinUp*, *pinDown*
- `reedSwitchActiveLow` [optional, default: *true*] true: reed switch activated by low state (0), false: reed switch activated by high state (1), affects *pinClosed*, *pinOpen*

## Raspberry Pi setup
- Raspberry Pi 3 (should work with all versions)
Expand Down
2 changes: 2 additions & 0 deletions config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"pinDown": 3,
"durationUp": 27000,
"durationDown": 25000,
"pinClosed":17,
"pinOpen":18,
"activeLow": false
}
]
Expand Down
55 changes: 43 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require("underscore");
var rpio = require("rpio");
var _ = require('underscore');
var rpio = require('rpio');
var Service, Characteristic;

const STATE_DECREASING = 0;
Expand All @@ -10,20 +10,23 @@ module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;

homebridge.registerAccessory("homebridge-gpio-blinds", "Blinds", BlindsAccessory);
homebridge.registerAccessory('homebridge-gpio-blinds', 'Blinds', BlindsAccessory);
}

function BlindsAccessory(log, config) {
_.defaults(config, {activeLow: true});
_.defaults(config, {activeLow: true, reedSwitchActiveLow: true});

this.log = log;
this.name = config['name'];
this.pinUp = config["pinUp"];
this.pinDown = config["pinDown"];
this.pinUp = config['pinUp'];
this.pinDown = config['pinDown'];
this.durationUp = config['durationUp'];
this.durationDown = config['durationDown'];
this.pinClosed = config['pinClosed'];
this.pinOpen = config['pinOpen'];
this.initialState = config['activeLow'] ? rpio.HIGH : rpio.LOW;
this.activeState = config['activeLow'] ? rpio.LOW : rpio.HIGH;
this.reedSwitchActiveState = config['reedSwitchActiveLow'] ? rpio.LOW : rpio.HIGH;

this.currentPosition = 0; // down by default
this.targetPosition = 0; // down by default
Expand All @@ -33,16 +36,18 @@ function BlindsAccessory(log, config) {

this.infoService = new Service.AccessoryInformation();
this.infoService
.setCharacteristic(Characteristic.Manufacturer, "Radoslaw Sporny")
.setCharacteristic(Characteristic.Model, "RaspberryPi GPIO Blinds")
.setCharacteristic(Characteristic.SerialNumber, "Version 1.0.0");
.setCharacteristic(Characteristic.Manufacturer, 'Radoslaw Sporny')
.setCharacteristic(Characteristic.Model, 'RaspberryPi GPIO Blinds')
.setCharacteristic(Characteristic.SerialNumber, 'Version 1.0.1');

// use gpio pin numbering
rpio.init({
mapping: 'gpio'
});
rpio.open(this.pinUp, rpio.OUTPUT, this.initialState);
rpio.open(this.pinDown, rpio.OUTPUT, this.initialState);
if (this.pinClosed) rpio.open(this.pinClosed, rpio.INPUT, rpio.PULL_UP);
if (this.pinOpen) rpio.open(this.pinOpen, rpio.INPUT, rpio.PULL_UP);

this.service
.getCharacteristic(Characteristic.CurrentPosition)
Expand All @@ -69,6 +74,19 @@ BlindsAccessory.prototype.getCurrentPosition = function(callback) {
}

BlindsAccessory.prototype.getTargetPosition = function(callback) {
if (this.closedAndOutOfSync()) {
this.log("Current position is out of sync, setting to 0");
this.currentPosition = 0;
this.targetPosition = 0;
} else if (this.openAndOutOfSync()) {
this.log("Current position is out of sync, setting to 100");
this.currentPosition = 100;
this.targetPosition = 100;
} else if (this.partiallyOpenAndOutOfSync()) {
this.log("Current position is out of sync, setting to 50");
this.currentPosition = 50;
this.targetPosition = 50;
}
this.log("Target position: %s", this.targetPosition);
callback(null, this.targetPosition);
}
Expand All @@ -77,13 +95,13 @@ BlindsAccessory.prototype.setTargetPosition = function(position, callback) {
this.log("Setting target position to %s", position);

if (this.positionState != STATE_STOPPED) {
this.log("Blinds are moving. You need to wait. I will do nothing.");
this.log('Blinds are moving. You need to wait. I will do nothing.');
callback();
return false;
}

if (this.currentPosition == position) {
this.log("Current position already matches target position. There is nothing to do.");
this.log('Current position already matches target position. There is nothing to do.');
callback();
return true;
}
Expand All @@ -98,7 +116,7 @@ BlindsAccessory.prototype.setTargetPosition = function(position, callback) {
}

this.log("Duration: %s ms", duration);
this.log(moveUp ? "Moving up" : "Moving down");
this.log(moveUp ? 'Moving up' : 'Moving down');

this.service.setCharacteristic(Characteristic.PositionState, (moveUp ? STATE_INCREASING : STATE_DECREASING));
this.positionState = (moveUp ? STATE_INCREASING : STATE_DECREASING);
Expand All @@ -125,6 +143,19 @@ BlindsAccessory.prototype.setFinalBlindsState = function() {
this.log("Successfully moved to target position: %s", this.targetPosition);
}

BlindsAccessory.prototype.closedAndOutOfSync = function() {
return this.currentPosition != 0 && this.pinClosed && (rpio.read(this.pinClosed) == this.reedSwitchActiveState);
}

BlindsAccessory.prototype.openAndOutOfSync = function() {
return this.currentPosition != 100 && this.pinOpen && (rpio.read(this.pinOpen) == this.reedSwitchActiveState);
}

BlindsAccessory.prototype.partiallyOpenAndOutOfSync = function() {
return (this.currentPosition == 0 && this.pinClosed && (rpio.read(this.pinClosed) != this.reedSwitchActiveState)) ||
(this.currentPosition == 100 && this.pinOpen && (rpio.read(this.pinOpen) != this.reedSwitchActiveState));
}

BlindsAccessory.prototype.getServices = function() {
return [this.infoService, this.service];
}

0 comments on commit 01113f2

Please sign in to comment.