benlamonica / homebridge-rasppi-gpio-garagedoor

Plugin for HomeBridge for a Raspberry Pi GPIO Garage Door Opener
63 stars 40 forks source link

change switching value of relay / make it configurable #8

Closed michis0806 closed 7 years ago

michis0806 commented 8 years ago

Hi benlamonica,

I like your module very much. But I'd like to change the switching value of the relay-switch. With the current settings, the relay switches when it's powerless, so when the rasppi reboots, my garage door opens.

I switched the value in #74 to 1 and in #103 to 0 in the configuration. so the door stays closed when the system is powerless...

Thanks! Michael

muhackl commented 7 years ago

Hi @michis0806 and @benlamonica , I'm also interested in what @michis0806 asked. I also would be happy, if I could edit the time the relay switches. At the moment, it's 1000 ms. How can I edit it, after I installed the plugin? Many thanks and best regards Muhackl

benlamonica commented 7 years ago

I will make the switch time configurable and also if the should be high or low to trigger it. I'll submit the new build sometime today.

--Ben L

On Oct 10, 2016, at 2:29 AM, muhackl notifications@github.com wrote:

Hi @michis0806 and @benlamonica , I'm also interested in what @michis0806 asked. I also would be happy, if I could edit the time the relay switches. At the moment, it's 1000 ms. How can I edit it, after I installed the plugin? Many thanks and best regards Muhackl

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

muhackl commented 7 years ago

Sounds great. Really looking forward to it and appreciate your work!

muhackl commented 7 years ago

@benlamonica Did you have the chance to work on the switch and the trigger? Really looking forward to it.

ghost commented 7 years ago

@benlamonica I Appriciate your work, got my garage door all setup and wife is happy! I have two questions. When i ask siri The status of the garage door, when its closed/open she replies closing/opening is there away to fix that(p.s on home app everything looks good). And last one, could you possibly add another sensor on the opposite side, like it triggers then the door is open, so if the door got stopped in between to sensors it would tell you opening/closing/partially opened.

fiddypal commented 7 years ago

Guys i was running into the same problem and found the 1/0's i needed to flip in the index.js file. I think i see how the variables are getting pulled from the config.json (i have them hardcoded right now) so let me play around with it over the holidays this week and see if i can get it working with insert variables in the config.json.

fiddypal commented 7 years ago

OK i was able to figure it out.

Use this file for index.js in the plugin folder:

var fs = require("fs");
var Service, Characteristic, DoorState; // set in the module.exports, from homebridge

module.exports = function(homebridge) {
  Service = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;
  DoorState = homebridge.hap.Characteristic.CurrentDoorState;

  homebridge.registerAccessory("homebridge-rasppi-gpio-garagedoor", "RaspPiGPIOGarageDoor", RaspPiGPIOGarageDoorAccessory);
}

function RaspPiGPIOGarageDoorAccessory(log, config) {
  this.log = log;
  this.name = config["name"];
  this.defaultSensorValue = config["defaultSensorValue"];
  this.relayDefaultValue = config["relayDefaultValue"];
  this.relaySwitchValue = config["relaySwitchValue"];
  this.doorSwitchPin = config["doorSwitchPin"];
  this.doorSensorPin = config["doorSensorPin"];
  this.doorPollInMs = config["doorPollInMs"];
  this.doorOpensInSeconds = config["doorOpensInSeconds"];
  log("Door Switch Pin: " + this.doorSwitchPin);
  log("Door Sensor Pin: " + this.doorSensorPin);
  log("Door Poll in ms: " + this.doorPollInMs);
  log("Door Opens in seconds: " + this.doorOpensInSeconds);
  this.initService();
  setTimeout(this.monitorDoorState.bind(this), this.doorPollInMs);
}

RaspPiGPIOGarageDoorAccessory.prototype = {

  monitorDoorState: function() {
     var isClosed = this.isClosed();
     if (isClosed != this.wasClosed) {
       this.wasClosed = isClosed;
       var state = isClosed ? DoorState.CLOSED : DoorState.OPEN;       
       this.log("Door state changed to " + (isClosed ? "CLOSED" : "OPEN"));
       if (!this.operating) {
         this.currentDoorState.setValue(state);
         this.targetDoorState.setValue(state);
         this.targetState = state;
       }
     }
     setTimeout(this.monitorDoorState.bind(this), this.doorPollInMs);
  },

  initService: function() {
    this.garageDoorOpener = new Service.GarageDoorOpener(this.name,this.name);
    this.currentDoorState = this.garageDoorOpener.getCharacteristic(DoorState);
    this.currentDoorState.on('get', this.getState.bind(this));
    this.targetDoorState = this.garageDoorOpener.getCharacteristic(Characteristic.TargetDoorState);
    this.targetDoorState.on('set', this.setState.bind(this));
    this.targetDoorState.on('get', this.getTargetState.bind(this));
    var isClosed = this.isClosed();
    this.currentDoorState.setValue(isClosed ? DoorState.CLOSED : DoorState.OPEN);
    this.targetDoorState.setValue(isClosed ? DoorState.CLOSED : DoorState.OPEN);
    this.infoService = new Service.AccessoryInformation();
    this.infoService
      .setCharacteristic(Characteristic.Manufacturer, "Opensource Community")
      .setCharacteristic(Characteristic.Model, "RaspPi GPIO GarageDoor")
      .setCharacteristic(Characteristic.SerialNumber, "Version 1.0.0");

    this.wasClosed = isClosed;
    this.operating = false;
    setTimeout(this.monitorDoorState.bind(this), this.doorPollInMs);
  },

  getTargetState: function(callback) {
    callback(null, this.targetState);
  },

  isClosed: function() {
    return fs.readFileSync("/sys/class/gpio/gpio"+this.doorSensorPin+"/value", "utf8").trim() == this.defaultSensorValue;
  },

  switchOff: function() {
    fs.writeFileSync("/sys/class/gpio/gpio"+this.doorSwitchPin+"/value", this.relayDefaultValue);
    this.log("Turning off GarageDoor Relay");
  },

  setFinalDoorState: function() {
    var isClosed = this.isClosed();
    if ((this.targetState == DoorState.CLOSED && !isClosed) || (this.targetState == DoorState.OPEN && isClosed)) {
      this.log("Was trying to " + (this.targetState == DoorState.CLOSED ? " CLOSE " : " OPEN ") + "the door, but it is still " + (isClosed ? "CLOSED":"OPEN"));
      this.currentDoorState.setValue(DoorState.STOPPED);
      this.targetDoorState.setValue(isClosed ? DoorState.CLOSED : DoorState.OPEN);
    } else {
      this.currentDoorState.setValue(this.targetState);
    }
    this.operating = false;
  },

  setState: function(state, callback) {
    this.log("Setting state to " + state);
    this.targetState = state;
    var isClosed = this.isClosed();
    if ((state == DoorState.OPEN && isClosed) || (state == DoorState.CLOSED && !isClosed)) {
        this.log("Triggering GarageDoor Relay");
        this.operating = true; 
        if (state == DoorState.OPEN) {
            this.currentDoorState.setValue(DoorState.OPENING);
        } else {
            this.currentDoorState.setValue(DoorState.CLOSING);
        }
        setTimeout(this.setFinalDoorState.bind(this), this.doorOpensInSeconds * 1000);
        fs.writeFileSync("/sys/class/gpio/gpio"+this.doorSwitchPin+"/value", this.relaySwitchValue);
        setTimeout(this.switchOff.bind(this), 1000);
    }

    callback();
    return true;
  },

  getState: function(callback) {
    var isClosed = this.isClosed();
    this.log("GarageDoor is " + (isClosed ? "CLOSED ("+DoorState.CLOSED+")" : "OPEN ("+DoorState.OPEN+")")); 
    callback(null, (isClosed ? DoorState.CLOSED : DoorState.OPEN));
  },

  getServices: function() {
    return [this.infoService, this.garageDoorOpener];
  }
};

Here is example how to setup your config.json in homebridge:

        "accessories": [{
                "accessory": "RaspPiGPIOGarageDoor",
                "name": "Garage Door",
                "defaultSensorValue": "0",
                "relayDefaultValue": "1",
                "relaySwitchValue": "0",
                "doorSwitchPin": 7,
                "doorSensorPin": 18,
                "doorPollInMs": 4000,
                "doorOpensInSeconds": 14
        }, 

deafultSensorValue - Set this to 0 if using an NC sensor, 1 if using NO sensor. relayDefaultValue - Set to 1 if your relay is jamming in the "ON" position when not in use (you also need to set the relaySwitchValue to the invese of whatever this value is) relaySwitchValue - Set to inverse of relayDefaultValue

benlamonica commented 7 years ago

@sogseal - have you upgraded to iOS 10 and the latest version of homebridge? When I just recently upgraded it fixed the status messages.

simonrb2000 commented 7 years ago

I have the same as above. Been using this for over a year and I used to show people asking Siri if my garage door is open? She would reply closed. However past few weeks she has been saying closing when its closed and opening when its open. I have latest iOS 10 and latest Homebridge.

Is this what your fix above fixes fiddypal?

ghost commented 7 years ago

@sogseal, i have latest ios and installed homebridge a week ago. Here is my homebridge version: pi@raspberrypi:~ $ npm -v homebridge 2.15.5 I also noticed in home app that there is a option for "obstruction detected: no" any idead how to implement that?

benlamonica commented 7 years ago

I’ll take a look at it.

On Nov 26, 2016, at 1:46 PM, sog notifications@github.com wrote:

@sogseal https://github.com/sogseal, i have latest ios and installed homebridge a week ago.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/benlamonica/homebridge-rasppi-gpio-garagedoor/issues/8#issuecomment-263082003, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHlWNDjZzRn1GG0tdQ-1XcZlq7gEWm4ks5rCIyKgaJpZM4J1VIU.

simonrb2000 commented 7 years ago

It's only Siri who gets the status wrong. The Home App shows it as opening when it's opening and as open once it's open. Same as the closing as it closes and closed once it's closed. Only Siri will say it's closing when asked even though it's closed. (Same with opening) hope thats a bit clearer :-)

fiddypal commented 7 years ago

Same problem here. Status works correctly in the home app, but when you ask Siri 'is my garage open ' she will respond with 'your garage is opening ' when it's open and 'your garage is closing' when it's closed. Never the correct open or closed status. Had the same problem when running straight off hap-nodejs and other plugins as well.

fiddypal commented 7 years ago

@simonrb2000

My fix is for a couple things. One was that I am using an NC contact switch, the stock code is hard coded for an NO contact switch. This was causing incorrect open and close reading.

The other problem I had was the relay was jamming in the on position at rest. This would only allow you to use the app to open and close, it blocked out the switch on the wall.

So instead of hard coding the opposite values to fix these issues I pulled them out into the config to make it a lot easier to toggle for people.

benlamonica commented 7 years ago

I’ve spent a lot of time implementing everything everyone has asked for today.

Configurable pins, an open door sensor (though I can’t test that one), configurable time to press the button, and I tried to fix the bug with Siri, but I think it’s an apple bug, because when you query Siri, you see in the logs that homebridge is reporting CLOSED to siri, but then siri turns around and says Closing.

I tried migrating to wiring-pi in order to have rootless homebridge, but couldn’t get it to work in nodejs (works fine from command line…). So I switched back to the filesystem based pin triggering. I did add an error if you try to start without being root so that at least you know it’s needed up front.

I’ll get these changes published either tonight or on Monday.

On Nov 26, 2016, at 5:39 PM, fiddypal notifications@github.com wrote:

@simonrb2000 https://github.com/simonrb2000 My fix is for a couple things. One was that I am using an NC contact switch, the stock code is hard coded for an NO contact switch. This was causing incorrect open and close reading.

The other problem I had was the relay was jamming in the on position at rest. This would only allow you to use the app to open and close, it blocked out the switch on the wall.

So instead of hard coding the opposite values to fix these issues I pulled them out into the config to make it a lot easier to toggle for people.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/benlamonica/homebridge-rasppi-gpio-garagedoor/issues/8#issuecomment-263092616, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHlWJkNrmY8cNVv72jFfKWpx-bORvJSks5rCMMcgaJpZM4J1VIU.

simonrb2000 commented 7 years ago

Appreciate all your hard work mate, we all do. It had been working fine, Siri saying it's closed when it was closed. It's only been recently it's changed and I hadn't changed anything in my setup so I suspect your right, it's an Apple thing.

Thanks for looking :-)

On 27 Nov 2016, at 01:01, Ben La Monica notifications@github.com<mailto:notifications@github.com> wrote:

I've spent a lot of time implementing everything everyone has asked for today.

Configurable pins, an open door sensor (though I can't test that one), configurable time to press the button, and I tried to fix the bug with Siri, but I think it's an apple bug, because when you query Siri, you see in the logs that homebridge is reporting CLOSED to siri, but then siri turns around and says Closing.

I tried migrating to wiring-pi in order to have rootless homebridge, but couldn't get it to work in nodejs (works fine from command line...). So I switched back to the filesystem based pin triggering. I did add an error if you try to start without being root so that at least you know it's needed up front.

I'll get these changes published either tonight or on Monday.

On Nov 26, 2016, at 5:39 PM, fiddypal notifications@github.com<mailto:notifications@github.com> wrote:

@simonrb2000 https://github.com/simonrb2000 My fix is for a couple things. One was that I am using an NC contact switch, the stock code is hard coded for an NO contact switch. This was causing incorrect open and close reading.

The other problem I had was the relay was jamming in the on position at rest. This would only allow you to use the app to open and close, it blocked out the switch on the wall.

So instead of hard coding the opposite values to fix these issues I pulled them out into the config to make it a lot easier to toggle for people.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/benlamonica/homebridge-rasppi-gpio-garagedoor/issues/8#issuecomment-263092616, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHlWJkNrmY8cNVv72jFfKWpx-bORvJSks5rCMMcgaJpZM4J1VIU.

- You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/benlamonica/homebridge-rasppi-gpio-garagedoor/issues/8#issuecomment-263095491, or mute the threadhttps://github.com/notifications/unsubscribe-auth/APQCAKYKMOhCk22oQ8COv_3AWQYzgmYFks5rCNZKgaJpZM4J1VIU.

ghost commented 7 years ago

@benlamonica you are my hero man! love what you did, using it everyday, my pregnant wife is happy as well(happy wife, happy life). Now i need to buy apple TV to control home away.

fiddypal commented 7 years ago

Ben I also want to thank you for this awesome software. It's been an awesome and very rewarding project for learning Linux and some basic coding skills.

@sogseal

I believe the obtrusion detection input is for the little 'eyes' at the bottom of the door that form an invisible trip wire to stop the door from closing if you are walking under it. I have not wired this up yet but plan to in the future, I'm sure it's a simple job garage door wiring is not very complex.

ghost commented 7 years ago

@fiddypal yes, i was wondering how to implemet those 'eyes' to a pi and the config, i have no js knowlegde, but a little python. this is all so much fun!

fiddypal commented 7 years ago

It should be as simple as copying the sensor detection code as used by the open/close sensor and just have it trigger the obstruction detected characteristic to true when your garage 'eyes' sees an obstruction. My guess is this has embedded functionality in the HomeKit framework to update the door Status to 'jammed' or such when you set the flag to true. Let me go hook up multi meter to the eyes and see what kind of signal I get back when I block them.

https://developer.apple.com/reference/homekit/hmcharacteristictypeobstructiondetected

benlamonica commented 7 years ago

The software part should be pretty easy. I think intercepting the sensor will be more difficult, as you’ll have to tie the two electrical systems together. If you want to give it a try on the software side, you can code it and submit a pull request. I’d be happy to offer advice too.

On Nov 27, 2016, at 8:43 AM, fiddypal notifications@github.com wrote:

It should be as simple as copying the sensor detection code as used by the open/close sensor and just have it trigger the obstruction detected characteristic to true when your garage 'eyes' sees an obstruction. My guess is this has embedded functionality in the HomeKit framework to update the door Status to 'jammed' or such when you set the flag to true. Let me go hook up multi meter to the eyes and see what kind of signal I get back when I block them.

https://developer.apple.com/reference/homekit/hmcharacteristictypeobstructiondetected https://developer.apple.com/reference/homekit/hmcharacteristictypeobstructiondetected — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/benlamonica/homebridge-rasppi-gpio-garagedoor/issues/8#issuecomment-263126038, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHlWFc2QcnIi4zVpsLjezvrOiM5mZYlks5rCZcTgaJpZM4J1VIU.

benlamonica commented 7 years ago

I've committed the changes, so if you want to make changes, please pull down master before you do, as I've done some refactoring. Once I've fully tested those changes I'll tag and push the release to npm.

fiddypal commented 7 years ago

@benlamonica You are right, after a quick google it seems the sensors are sending a looping signal back to the garage unit when unblocked, then when you block them one wire is 6v above the other, so I'd imagine some intermediate hardware would be required as it can't be wired straight to the gpio pins on the pi. Unfortuntely I'm not that skilled with hardware yet but hopefully someone can chime in and move us along, I can certainly help on the software side.

ghost commented 7 years ago

ill check on the hw, ill play with my garage door 'eyes' to get my calculations, from there ill see if i have supplies to add them to pi/gpio's.

ghost commented 7 years ago

so pi dont have Analog pins if im not mistaken. i check out the 'eyes', when blocked i get 6.150V and when no obstructions i get 5.840V, very fine difference. Prolly need some kind of analog converter so the pi can read voltage. *Edit need ADC(Analog Digital Converter) https://www.adafruit.com/product/856

fiddypal commented 7 years ago

@benlamonica can you please share example of new config.json since you have updated the code base.

Thanks!

benlamonica commented 7 years ago

Good point. Forgot to update the docs. Will do that tomorrow.

--Ben L

On Nov 27, 2016, at 5:30 PM, fiddypal notifications@github.com wrote:

@benlamonica can you please share example of new config.json since you have updated the code base.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

benlamonica commented 7 years ago

I've released version 1.0.3 of the plugin. This makes all of the pins configurable to be active_low or active_high. Please see the readme. Closing this Issue. Please open a new issue if you have other problems.