Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import sys
- import datetime
- from influxdb import InfluxDBClient
- import Adafruit_DHT
- import json
- # Set this variables, influxDB should be localhost on Pi
- host = "localhost"
- port = 8086
- user = "pi"
- password = "Missyoujack1."
- # The database we created
- dbname = "logger"
- # Sample period (s)
- interval = 1
- # Allow user to set session and runno via args otherwise auto-generate
- if len(sys.argv) > 1:
- if (len(sys.argv) < 3):
- print "Must define session and runNo!!"
- sys.exit()
- else:
- session = sys.argv[1]
- runNo = sys.argv[2]
- else:
- session = "dev"
- now = datetime.datetime.now()
- runNo = now.strftime("%Y%m%d%H%M")
- print "Session: ", session
- print "runNo: ", runNo
- # Create the InfluxDB object
- client = InfluxDBClient(host, port, user, password, dbname)
- # Run until keyboard out
- try:
- while True:
- # This gets a dict of the three values
- iso = time.ctime()
- humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
- if humidity is not None:
- humidity_1 = humidity
- if temperature is not None:
- temperature_1 = temperature
- json_body = [
- {
- "measurement": session,
- "tags": {
- "run": runNo,
- },
- "time": iso,
- "fields": {
- "humidity" : format(humidity_1, '.2f'),
- "temperature" : format(temperature_1, '.2f')
- }
- }
- ]
- # Write JSON to InfluxDB
- client.write_points(json_body)
- # Wait for next sample
- time.sleep(interval)
- except KeyboardInterrupt:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement