Skip to content

Instantly share code, notes, and snippets.

@dbonino
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbonino/10271299 to your computer and use it in GitHub Desktop.
Save dbonino/10271299 to your computer and use it in GitHub Desktop.
Python script to detect button pressing through the RaspberryPi GPIO and to send an "associate" command to Dog. A led attached to another GPIO port provides feedback on the current gateway status according to Dog.
'''
Created on Apr 4, 2014
@author: bonino
@author: de russis
Copyright (c) 2014 Dario Bonino and Luigi De Russis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
'''
import rest,time, RPi.GPIO as GPIO
def button_to_gw (RCpin):
# the base url
base_url = 'http://localhost:8080/api/v1/'
# gateway URL
gw_associate_url = base_url+'devices/zwave-gateway/commands/associate'
gw_state_url = base_url+'devices/zwave-gateway/status'
# request body
body = '{}'
#setup the gpio output
GPIO.setmode(GPIO.BOARD)
GPIO.setup(RCpin, GPIO.IN)
GPIO.setup(18,GPIO.OUT)
# This takes about 1 millisecond per loop cycle
status = 'idle'
while (True):
status = rest.send(url=gw_state_url)
print status
if status['status']['DeviceAssociationState'][0]['value'] == 'associating':
GPIO.output(18,GPIO.HIGH)
else:
GPIO.output(18,GPIO.LOW)
if(GPIO.input(RCpin) == False):
try:
rest.send('PUT', gw_associate_url, body, { 'Content-Type':'application/json' })
print 'sending associate'
except:
pass
time.sleep(0.1)
return
if __name__ == '__main__':
button_to_gw(22)
'''
Created on Apr 4, 2014
@author: bonino
Copyright (c) 2014 Dario Bonino
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
'''
import urllib2, json
def send(method = 'GET', url=None, data = None, headers = {}):
#the response dictionary, initially empty
response_dict = dict()
#check that the url is not empty
if(url!=None):
#build the request
req = urllib2.Request(url, data, headers)
req.get_method = lambda: method
#try to call the url
result = None
try:
#get the result
result = urllib2.urlopen(req)
except urllib2.URLError, e:
#print the error
print e.reason
#check result
if(result != None):
#decode the result
result_as_string = result.read().decode('utf8')
#convert into a dict
response_dict = json.loads(result_as_string)
return response_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment