import http.client
import urllib.parse
import time
key = "WK3MUELXXVM8VE7F" # Put your API Key here
def thermometer():
while True:
# Calculate CPU temperature of Raspberry Pi in Degrees C
temp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000 # Get Raspberry Pi CPU temp
params = urllib.parse.urlencode({'field1': temp, 'key': key})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = http.client.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print(temp)
print(response.status, response.reason)
data = response.read()
conn.close()
except Exception as e:
print("Connection failed:", str(e))
time.sleep(1) # Wait for 15 seconds before posting the next value
if __name__ == "__main__":
while True:
thermometer()