Skip to content

Instantly share code, notes, and snippets.

@dashbad
Created September 13, 2016 10:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dashbad/4e94af0bec5291543dbe641dc63b39ab to your computer and use it in GitHub Desktop.
Save dashbad/4e94af0bec5291543dbe641dc63b39ab to your computer and use it in GitHub Desktop.
Push DHT22 raspberry Pi readings to influxdb
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT
import requests
import time
# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22
# Example using a Beaglebone Black with DHT sensor
# connected to pin P8_11.
#pin = 'P8_11'
# Example using a Raspberry Pi with DHT sensor
# connected to GPIO23.
pin = 4
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
timestamp = time.time()
if humidity is not None and temperature is not None:
#Write the data to the database
#data = 'server_stack_temp,host=rpi1,location=basement value=' + str(temperature) + ' ' + str(timestamp)
data = 'temp,host=rpi1,location=roaming value=' + str(temperature)
print data
output = requests.post('http://192.168.1.5:8086/write?db=main', data=data)
print output
data = 'humidity,host=rpi1,location=roaming value=' + str(humidity)
print data
output = requests.post('http://192.168.1.5:8086/write?db=main', data=data)
print output
#Output the data to the screen
print 'Temp={0:0.1f}*F Humidity={1:0.1f}%'.format(temperature, humidity)
else:
print 'Failed to get reading. Try again!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment