· 6 years ago · Mar 28, 2020, 02:54 PM
1import httplib
2import urllib
3import time
4key = "J9070YKLDYVL" # THNGSPEAK WRITE API KEY
5def thermometer():
6 while True:
7 #Calculate CPU temperature of Raspberry Pi in Degrees C
8 temp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3 # Get Raspberry Pi CPU temp
9 params = urllib.urlencode({'field1': temp, 'key':key })
10 headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
11 conn = httplib.HTTPConnection("api.thingspeak.com:80")
12 try:
13 conn.request("POST", "/update", params, headers)
14 response = conn.getresponse()
15 print temp
16 print response.status, response.reason
17 data = response.read()
18 conn.close()
19 except:
20 print "connection failed"
21 break
22if __name__ == "__main__":
23 while True:
24 thermometer()