Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
  2. from bluepy.btle import Peripheral
  3. import time
  4. from influxdb import InfluxDBClient
  5. from influxdb import SeriesHelper
  6.  
  7. # InfluxDB connections settings
  8. host = ''
  9. port = ''
  10. user = ''
  11. password = ''
  12. dbname = ''
  13.  
  14. myclient = InfluxDBClient(host, port, user, password, dbname)
  15. # bluetooth connection
  16. devices = [('DEVICE_NAME','MAC')]
  17.  
  18. class SeriesHelper(SeriesHelper):
  19. # Meta class stores time series helper configuration.
  20. class Meta:
  21. client = myclient
  22. series_name = 'environment'
  23. fields = ['temp', 'pressure', 'uv', 'light', 'sound', 'humidity', 'co2', 'voc']
  24. tags = ['location']
  25. bulk_size = 5
  26. # autocommit must be set to True when using bulk_size
  27. autocommit = True
  28.  
  29. def debug_print(location, data):
  30. print ('### {} ###'.format(location))
  31. for key, value in data.items():
  32. print ('{}:\t{}'.format(key,value))
  33. print('')
  34.  
  35. def push_to_influx(data, location):
  36. SeriesHelper(location=location, temp=data['temperature'],
  37. pressure=data['pressure'], uv=data['uv'],
  38. light=data['light'], sound=data['sound'],
  39. humidity=data['humidity'], co2=data['co2'], voc=data['voc'])
  40. SeriesHelper.commit()
  41.  
  42. def read_sensor_data(p):
  43. sen_data = {}
  44. for service in p.getServices():
  45. # environmental service
  46. if '181a' in str(service.uuid):
  47. for ch in service.getCharacteristics():
  48. if '2a76' in str(ch.uuid):
  49. sen_data['uv'] = int.from_bytes(ch.read(), byteorder='little')
  50. if '2a6d' in str(ch.uuid):
  51. sen_data['pressure'] = int.from_bytes(ch.read(), byteorder='little') / 1000
  52. if '2a6e' in str(ch.uuid):
  53. sen_data['temperature'] = int.from_bytes(ch.read(), byteorder='little') / 100
  54. if '2a6f' in str(ch.uuid):
  55. sen_data['humidity'] = int.from_bytes(ch.read(), byteorder='little') / 100
  56. if 'c8546913-bfd9-45eb-8dde-9f8754f4a32e' in str(ch.uuid):
  57. sen_data['light'] = int.from_bytes(ch.read(), byteorder='little')
  58. if 'c8546913-bf02-45eb-8dde-9f8754f4a32e' in str(ch.uuid):
  59. sen_data['sound'] = int.from_bytes(ch.read(), byteorder='little') / 100
  60.  
  61. # air quality service
  62. if 'efd658ae-c400-ef33-76e7-91b00019103b' in str(service.uuid):
  63. for ch in service.getCharacteristics():
  64. if 'c401' in str(ch.uuid):
  65. sen_data['co2'] = int.from_bytes(ch.read(), byteorder='little')
  66. if 'c402'in str(ch.uuid):
  67. sen_data['voc'] = int.from_bytes(ch.read(), byteorder='little')
  68. return sen_data
  69.  
  70.  
  71. def main():
  72. print('try btle connect')
  73. btDevice = {}
  74. for device in devices:
  75. p = Peripheral(device[1])
  76. if p is not None:
  77. print ('{} connected'.format(device[0]))
  78. btDevice[device[0]] = p
  79. while 1:
  80. for key, val in btDevice.items():
  81. data = read_sensor_data(val)
  82. debug_print(key, data)
  83. push_to_influx(data, key)
  84. time.sleep(2)
  85.  
  86.  
  87. if __name__ == '__main__':
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement