I’ve been reading this board quite a bit and the helpfulness on this board amazes me compared to others I must say! kudos! Very nice to see people taking the time to help one another!
With that said I currently have my garage setup as follows,
http://ip/status.php shows a 0 or 1 based on if the door is open or closed.
http:///ip/garage=1 opens and closes the door.
I am looking to create a http sensor and switch that basically shows the following based on the above info,
Garage door: OPEN
OPEN/CLOSE Button
Is this doable? can someone get me started on this then i should be able to replicate going forward…
I’ve a very similar setup. You need two Items, a Switch (to control the door) and a Contact (to get the status). I use HTTP to control the door and MQTT to report the door’s status but you could easily change it to use the HTTP binding or use the HTTP actions in a rule. I trigger the doors through a Rule rather than using the HTTP binding.
Here is my setup unedited to make it simpler. There are a lot of advanced concepts in the code below so ask questions.
The theory of operations is as follows:
I have two garage door openers and each opener has a reed switch wired up to a raspberry pi and a relay wired up to that same raspberry pi which is creatively named “garage”. On garage I have a script that polls the GPIO pins and reports any change in state of the reed switches to OH using MQTT. (For your approach you will need to instead set up an Item bound to an HTTP Binding that polls your status URL). To trigger a relay I issue a REST API call similar to what you describe. The big difference is because I have two openers I do some advanced rules stuff (lambdas) so I only have to write the code once. On the sitemap I use the visibility feature to change the icon to indicate whether the door is open or not and use mappings to provide just the one button to press instead of having a switch.
Items:
Switch T_D_Garage1 "Garage Door 1" <garagedoor>
Switch T_D_Garage2 "Garage Door 2" <garagedoor>
Contact N_D_GarageDoor1 "Garage Door 1 [MAP(en.map):%s]" <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door1:state:default]" }
Contact N_D_GarageDoor2 "Garage Door 2 [MAP(en.map):%s]" <garagedoor> (gDoorSensors, gRemindDoorSensors, gGarageSensors) { mqtt="<[mosquitto:entry_sensors/main/garage/door2:state:default]" }
Rules:
import org.openhab.core.library.types.*
import org.openahb.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.eclipse.xtext.xbase.lib.*
val Functions$Function1 openGarage = [ String garageNum |
logInfo("Garage Controller", "The garage door " + garageNum + " has been triggered")
var url = "http://192.168.1.201:8000/GPIO/"
switch garageNum {
case "1" : url = url + "17"
case "2" : url = url + "22"
default : url = null
}
if (url == null) {
Notification_Proxy_Info.postUpdate("Received cmd to trigger unknown garage door: " + garageNum)
}
else {
url = url + "/sequence/500,01"
logDebug("Garage Controller", "The URL is " + url)
logInfo("Garage Controller", "HTTP Post result " + sendHttpPostRequest(url).toString)
if (Present.state != ON) {
Notification_Proxy_Alarm.postUpdate("Garage door " + garageNum + " has been triggered.")
}
if (gGarageNet.state != ON) {
Notification_Proxy_Alarm.postUpdate("The garage controller may be offline, trigger may have failed!")
}
}
true
]
rule "Trigger Garage Door Opener 1"
when
Item T_D_Garage1 received command
// Item T_D_Dash_Tide received update or
// Item T_D_Dash_Bounty received update
then
openGarage.apply("1")
end
rule "Trigger Garage Door Opener 2"
when
Item T_D_Garage2 received command
then
openGarage.apply("2")
end
Sitemap fragment:
// Garage Door Openers
Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-closed" mappings=[ON=Open] visibility=[N_D_GarageDoor1!="OPEN"]
Switch item=T_D_Garage1 label="Garage Door 1" icon="garagedoor-open" mappings=[ON=Close] visibility=[N_D_GarageDoor1=="OPEN"]
Switch item=T_D_Garage2 label="Garage Door 2" icon="garagedoor-closed" mappings=[ON=Open] visibility=[N_D_GarageDoor2!="OPEN"]
Switch item=T_D_Garage2 label="Garage Door 2" icon="garagedoor-open" mappings=[ON=Close] visibility=[N_D_GarageDoor2=="OPEN"]
Well maybe, maybe there is a different approach based on your setup since they are similar,
So my garage door is controlled with a banana Pro and GPIO. GPIO pin 23 activates a relay to open and close the door. Another GPIO senses 3v from a magnet switch on the door. I also have the php scripts just running the gpio commands for additional integration. It sounds like you have the same setup with the PI reporting to OH via MQTT which I know nothing about. lol
SO maybe there is another approach lol mqtt stuff?
<?php
// Set up valid status list
$state[1] = "-=Open=-";
$state[0] = "-=Closed=-";
?>
Door is currently:
<?php $pinStatus = trim(shell_exec("gpio -g read 23"));
//returns 0 = low; 1 = high
echo $state[$pinStatus];?>
You can find my MQTT reporting script here. It is written in Python and depends on WebIOPi for both the Python script and the REST API that OH calls to trigger the relay.
AHHH ok scratch that idea. WebIOPi wont compile on banana due to cross compiler issues. Guess its HTTP then lol. Do you have any HTTP samples for the GET of the door open/close and status?
There is a PHP MQTT library (probably more than one, this is just the first I found). You already have the PHP/GPIO stuff working so you can just update your script to report the changes instead of requiring a poll.
Or you can configure your PHP script to push the updates to OH through OH’s REST API.
I’m firmly of the opinion that it is far better to push data than poll for data over the network. Also, with MQTT you can set up a Last Will and Testament message so if your client ever goes offline the MQTT Broker will send a message and your OH can pick that up and alert you to your controller being offline. This has been a life saver for me.
SO yea the REST API seems A LOT easier. I’m thinking after i create the items in open hab I should be able to update the status as such “http://localhost:8080/CMD?Garage=OPEN” ? kinda sorta. Then I just have to create a bash loop script with curl or wget to send the update as i rather not use the overhead of Apache and PHP if I don’t have to.
Yes, that is one of the reasons I went with my Python script. I can set my loop to be really tight (hundreds of milliseconds) so when it changes state I find out almost immediately yet I’m not using up lots of network bandwidth and server processing time with polling.
Once you get it to work you might consider doing the little extra work necessary to make it run as a service. Personally I found it easier to make it a service, keep track of state so I know when it changes, and monitor and control it using a higher level language. I chose Python but you could do the same in most anything (Ruby, Perl, Node.js, etc.).
Yea agreed. I can just convert it to a daemon with error and crash reporting and some watchdog. No biggy there. The simplicity of the API just blows my mind though after all the php and other crap i have had to do to get this going lol. That seems a hell of a lot easier.