Skip to content

Instantly share code, notes, and snippets.

@OliPassey
Forked from dashbad/temp_hum_influx.py
Last active January 2, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliPassey/9bd890ce6605d3e13b289bf9c4b6a335 to your computer and use it in GitHub Desktop.
Save OliPassey/9bd890ce6605d3e13b289bf9c4b6a335 to your computer and use it in GitHub Desktop.
Push DHT22 raspberry Pi readings to Hosted Graphite
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Forked & Amended: Oli Passey & Steven Gould (2017)
# 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
import socket
# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22
# 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 succesful in grabbing a reading, send to Hosted Graphite
if humidity is not None and temperature is not None:
conn = socket.create_connection(("statsd.hostedgraphite.com", 2003))
conn.send("API-KEY-REMOVED.server_room.temperature "+str(temperature)+"\n")
conn.send("API-KEY-REMOVED.server_room.humidity "+str(humidity)+"\n")
conn.close()
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