Raspberry Pi + ThingSpeak

[A] Send Raspberry Pi CPU temperature data to ThingSpeak for visualization.

Requirement :
a. Raspberry Pi device
b. Free account in http://www.thingspeak.com


  1. Login to thingspeak.com > Channels > my Challan > new Channel >
    Name : “Raspberry Pi CPU temparature”
    Field1 : “Temp”
    Save the Channel
  2. Go to API keys tab > copy the Key
  3. Open python editor in Raspberry Pi > type below code:
import httplib
import urllib
import time
key = "XXXXXXXXXXXXXXXX"  # copy API Key 
def raspberryPi_CPU_temp():
    while True:
        temp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3 
        params = urllib.urlencode({'field1': temp, 'key':key }) 
        headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
        conn = httplib.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:
            print "connection failed"
        break
if __name__ == "__main__":
        while True:
                raspberryPi_CPU_temp()
 
sudo python raspberryPi_CPU_temp


Ref: https://iotdesignpro.com/projects/how-to-send-data-to-thingspeak-cloud-using-raspberry-pi


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.