Skip to content

Instantly share code, notes, and snippets.

@jamesabruce
Created August 19, 2015 11:53
Show Gist options
  • Save jamesabruce/dbb593a597acb2c0b93b to your computer and use it in GitHub Desktop.
Save jamesabruce/dbb593a597acb2c0b93b to your computer and use it in GitHub Desktop.
OpenHAB on Raspberry Pi Tutorial: Detect User Presence via Bluetooth, report to RESTful OpenHAB interface
#!/usr/bin/python
import bluetooth
import time
import requests
from requests.exceptions import ConnectionError
payload =''
while True:
print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5)
if (result != None and payload!='ON'):
print "User present"
payload ='ON'
try:
r = requests.put("http://localhost:8080/rest/items/JamesInOffice/state",data=payload)
except ConnectionError as e:
print e
r = "No Response"
payload = ''
elif (payload != 'OFF' and result == None):
print "User out of range"
payload ='OFF'
try:
r = requests.put("http://localhost:8080/rest/items/JamesInOffice/state",data=payload)
except ConnectionError as e:
print e
r = "No Response"
payload = ''
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment