Last active
October 27, 2018 12:46
-
-
Save valllllll2000/6feaf746a0e5664394362c295da7b0e8 to your computer and use it in GitHub Desktop.
Send raspberry pi data to thinkspeak
This file contains 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
""" | |
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() |
This file contains 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
#! /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