Skip to content

Instantly share code, notes, and snippets.

@valllllll2000 valllllll2000/dht22.py
Last active Oct 27, 2018

Embed
What would you like to do?
Send raspberry pi data to thinkspeak
"""
dht22.py
Temperature/Humidity monitor using Raspberry Pi and DHT22.
Data is displayed at thingspeak.com
Original author: Mahesh Venkitachalam at electronut.in
Modified by Adam Garbo on December 1, 2016
"""
import sys
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT
import urllib2
myAPI = "THINGSPEAK WRITE API_KEY"
def getSensorData():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
return (str(round(RH, 1)), str(round(T,1)))
def main():
print 'starting...'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
while True:
try:
RH, T = getSensorData()
print RH, T
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (RH, T))
print f.read()
f.close()
sleep(300) #uploads DHT22 sensor values every 5 minutes
except:
print 'exiting with error'
break
# call main
if __name__ == '__main__':
main()
#! /bin/sh
pgrep -f dht22.py || sudo nohup python /home/pi/thingspeak/dht22.py /dev/null &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.