Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import blescan, sys, requests, time, OpenSSL
  2.  
  3. import bluetooth._bluetooth as bluez
  4.  
  5. # public key roM51Nwag0IgYROoJ99b
  6. url = "https://data.sparkfun.com/input/roM51Nwag0IgYROoJ99b?private_key=aaaaaaaaa&gravity=%.3f&temperature=%.1f"
  7.  
  8. dev_id = 0
  9. try:
  10.         sock = bluez.hci_open_dev(dev_id)
  11.         print "ble thread started"
  12.  
  13. except:
  14.         print "error accessing bluetooth device..."
  15.         sys.exit(1)
  16.  
  17. blescan.hci_le_set_scan_parameters(sock)
  18. blescan.hci_enable_le_scan(sock)
  19.  
  20. while True:
  21.         returnedList = blescan.parse_events(sock, 10)
  22.         for beacon in returnedList:
  23.                 b = beacon.split(",") #88:c2:55:ac:23:6a
  24.                 if (b[0][0:5] != '88:c2'):
  25.                         continue
  26.                 sg = float(b[3])/1000
  27.                 temperature = (float(b[2])-32)/1.8
  28.                 if (sg > 0.9 and sg < 1.2):
  29.                         try:
  30.                                 r = requests.get(url % (sg, temperature))
  31.                         except OpenSSL.SSL.ZeroReturnError:
  32.                                 print "Whoops, ZeroResultError, sparkfun is forked."
  33.                         except:
  34.                                 print "Whoops, some error"
  35.  
  36.                         print "%d: Temperature: %.1f, Gravity: %.3f (%s)" % (r.status_code, temperature, sg, b[3])
  37.                         break
  38.  
  39.         time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement