Created
May 6, 2018 09:37
-
-
Save unixweb/13bffca302fc7c421e18c567eb6cd613 to your computer and use it in GitHub Desktop.
Read Sensors Data from DHT11 / DHT22 and push the data into a InfluxDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 Raspberry Pi with DHT sensor | |
# connected to GPIO7. | |
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.100:8086/write?db=sensors', data=data) | |
print output | |
data = 'humidity,host=rpi1,location=roaming value=' + str(humidity) | |
print data | |
output = requests.post('http://192.168.1.100:8086/write?db=sensors', 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!' |
Author
unixweb
commented
May 6, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment