Pushover notifications with Tilt Hydrometer
10 Apr 2020
This article will take 1 minute to read.
Today I modified atlefren/pytilt in order to send push notifications to my phone, instead of sending data to remote server. It uses Pushover and Advanced Python Scheduler to send one notification per hour for every Tilt nearby.
import sys
import datetime
import time
import httplib
import urllib
from apscheduler.schedulers.blocking import BlockingScheduler
import bluetooth._bluetooth as bluez
import blescan
TILTS = {
'a495bb10c5b14b44b5121370f02d74de' : 'Red' ,
'a495bb20c5b14b44b5121370f02d74de' : 'Green' ,
'a495bb30c5b14b44b5121370f02d74de' : 'Black' ,
'a495bb40c5b14b44b5121370f02d74de' : 'Purple' ,
'a495bb50c5b14b44b5121370f02d74de' : 'Orange' ,
'a495bb60c5b14b44b5121370f02d74de' : 'Blue' ,
'a495bb70c5b14b44b5121370f02d74de' : 'Yellow' ,
'a495bb80c5b14b44b5121370f02d74de' : 'Pink' ,
}
def distinct ( objects ):
seen = set ()
unique = []
for obj in objects :
if obj [ 'uuid' ] not in seen :
unique . append ( obj )
seen . add ( obj [ 'uuid' ])
return unique
def to_celsius ( fahrenheit ):
return round (( fahrenheit - 32.0 ) / 1.8 , 2 )
def notify ( msg ):
conn = httplib . HTTPSConnection ( "api.pushover.net:443" )
conn . request ( "POST" , "/1/messages.json" ,
urllib . urlencode ({
"token" : "af6bai9bodcfbexieswjpennj4fxxd" ,
"user" : "uxeuanyhid5k143uru7ecx3g9ghyqi" ,
"message" : msg ,
}), { "Content-type" : "application/x-www-form-urlencoded" })
conn . getresponse ()
def monitor_tilt ():
ret = False
while True :
beacons = distinct ( blescan . parse_events ( sock , 10 ))
for beacon in beacons :
if beacon [ 'uuid' ] in TILTS . keys ():
print ( 'notify' )
notify ({
'color' : TILTS [ beacon [ 'uuid' ]],
'temp' : to_celsius ( beacon [ 'major' ]),
'gravity' : beacon [ 'minor' ]
})
ret = True ;
if ret == True :
return ;
time . sleep ( 10 )
if __name__ == '__main__' :
dev_id = 0
try :
sock = bluez . hci_open_dev ( dev_id )
print 'Starting pytilt logger'
except :
print 'error accessing bluetooth device...'
sys . exit ( 1 )
blescan . hci_le_set_scan_parameters ( sock )
blescan . hci_enable_le_scan ( sock )
monitor_tilt ()
scheduler = BlockingScheduler ()
scheduler . add_job ( monitor_tilt , 'interval' , hours = 1 )
scheduler . start ()