Skip to content

Instantly share code, notes, and snippets.

@valllllll2000
Last active October 27, 2018 12:46
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 valllllll2000/6feaf746a0e5664394362c295da7b0e8 to your computer and use it in GitHub Desktop.
Save valllllll2000/6feaf746a0e5664394362c295da7b0e8 to your computer and use it in GitHub Desktop.
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