-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature.py
36 lines (31 loc) · 852 Bytes
/
temperature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sys
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT
import urllib3
def getSensorData():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 23)
# return dict
return (str(RH), str(T))
# main() function
def main():
# use sys.argv if needed
if len(sys.argv) < 2:
print('Usage: python tstest.py PRIVATE_KEY')
exit(0)
print('starting...')
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % sys.argv[1]
while True:
try:
RH, T = getSensorData()
http = urllib3.PoolManager()
f = http.request('GET', baseURL + "&field1=%s&field2=%s" % (RH, T))
print(f.read())
f.close()
sleep(15)
except:
print('exiting.')
break
# call main
if __name__ == '__main__':
main()