Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import time
  2. import sys
  3. import datetime
  4. from influxdb import InfluxDBClient
  5. import pigpio
  6.  
  7. # Set this variables, influxDB should be localhost on Pi
  8. host = "localhost"
  9. port = 8086
  10. user = ""
  11. password = ""
  12.  
  13. # The database we created
  14. dbname = "poele"
  15. # Sample period (s)
  16. interval = 15
  17.  
  18. pi = pigpio.pi()
  19.  
  20. if not pi.connected:
  21. exit(0)
  22.  
  23. sensor = pi.spi_open(0, 1000000, 0) # CE0 on main SPI
  24.  
  25. # Allow user to set session and runno via args otherwise auto-generate
  26. if len(sys.argv) > 1:
  27. if (len(sys.argv) < 3):
  28. print "Must define session and runNo!!"
  29. sys.exit()
  30. else:
  31. session = sys.argv[1]
  32. runNo = sys.argv[2]
  33. else:
  34. session = "temperature"
  35. now = datetime.datetime.now()
  36. runNo = now.strftime("%Y%m%d")
  37.  
  38. # Create the InfluxDB object
  39. client = InfluxDBClient(host, port, user, password, dbname)
  40.  
  41. # Run until keyboard out
  42. try:
  43. while True:
  44. c, d = pi.spi_read(sensor, 2)
  45. if c == 2:
  46. word = (d[0]<<8) | d[1]
  47. if (word & 0x8006) == 0: # Bits 15, 2, and 1 should be zero.
  48. t = (word >> 3)/4.0
  49. cel = "{:.2f}".format(t)
  50. celcius = float(cel)
  51. far = (t*1.8)+32
  52.  
  53. iso = time.ctime()
  54. #iso = time.strftime('%Y-%m-%dT%H:%M:%SZ',time.localtime(time.time()))
  55.  
  56. client = InfluxDBClient(host='127.0.0.1', port=8086, username='', password='', ssl=False, verify_ssl=False)
  57. client.write(['interface,path=address,elementss=link value=3'],{'db':'yourdb'},204,'line')
  58.  
  59.  
  60. client.write_points(json_body)
  61. # Wait for next sample
  62. time.sleep(interval)
  63.  
  64. except KeyboardInterrupt:
  65. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement