Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import time
  2. import serial, re, os
  3. from datetime import datetime
  4. from time import strftime
  5. from influxdb import InfluxDBClient
  6. import geohash
  7.  
  8. # Set this variables, influxDB should be localhost on Pi
  9. host = "localhost"
  10. port = 8086
  11. user = "root"
  12. password = "root"
  13.  
  14. # The database we created
  15. dbname = "logger"
  16.  
  17. # Create the InfluxDB object
  18. client = InfluxDBClient(host, port, user, password, dbname)
  19.  
  20. # set location
  21. lat = -32
  22. lon = 143
  23. location = geohash.encode(lat, lon)
  24.  
  25. # set serial connection to sensor
  26. ser = serial.Serial('/dev/ttyS0',
  27. 9600,
  28. 8,
  29. 'N',
  30. 1,
  31. timeout = 0.5)
  32. i = 0
  33.  
  34. #the main sensor reading loop
  35. try:
  36. while True:
  37. out = ser.readline()
  38. print(out)
  39. if len(out) == 10:
  40. PM25 = (list(out)[3]*256) + (list(out)[2] /10)
  41. PM10 = (list(out)[5]*256) + (list(out)[4] /10)
  42. iso = time.ctime()
  43. json_body = [{"measurement": 'air_quality',
  44. "tags": {"type": 'particles',
  45. "location": location},
  46. #"time": iso,
  47. "fields": {"PM25" : PM25,
  48. "PM10" : PM10}}]
  49. print(PM10, PM25)
  50.  
  51. # Write JSON to InfluxDB
  52. try:
  53. assert PM10 < 9999, 'PM10 invalid value'
  54. assert PM25 < 9999, 'PM25 invalid value'
  55. client.write_points(json_body)
  56. except AssertionError as e:
  57. print(e)
  58. pass
  59. except:
  60. print('db error passing')
  61. pass
  62.  
  63. i += 1
  64. # delay between stream posts time.sleep(0.25)
  65. except KeyboardInterrupt:
  66. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement