Friday, April 3, 2015

IoT at dweet.io, Python on RPi 2 to send data to Cloud

This example modify from my previous example of "Display Raspberry Pi CPU temperature graphically, using Python 2 with Matplotlib", add the function to send the temperature to cloud, dweet.io.



dweet.io doesn't require any setup or sign-up— just publish and go.

dweet.io is simple publishing and subscribing for machines, sensors, devices, robots, and gadgets (we just call them things). We call published messages ‘dweets’. It’s helpful to think of dweet.io as a Twitter for things, in fact.



This example code run on Python 2, Raspberry Pi 2/Raspbian, get CPU temperature, plot the graph on local screen, and send to dweet.io, with API like this:
https://dweet.io/dweet/for/helloRaspberryPi_RPi2_vcgencmd?measure_temp=xx.x

Where helloRaspberryPi_RPi2_vcgencmd is my-thing-name. To view my thing online, visit:
http://dweet.io/follow/helloRaspberryPi_RPi2_vcgencmd

Please notice that it is just a trial experience, not a completed example.

View on dweet.io

view on Raspberry Pi 2

#$ sudo pip install requests
import requests
import os
import matplotlib.pyplot as plt
from drawnow import *

# This example send the core temperature of Raspberry Pi
# to http://dweet.io/, a IoT on Cloud.
# with name = helloRaspberryPi_RPi2_vcgencmd
#
# To check this dweet, visit here on browser
# http://dweet.io/follow/helloRaspberryPi_RPi2_vcgencmd
#
dweetIO = "https://dweet.io/dweet/for/"
myName = "helloRaspberryPi_RPi2_vcgencmd"
myKey = "measure_temp"

tempC = []

plt.ion()
cnt=0

def plotTempC():
    plt.ylim(20,80)
    plt.title('Raspberry Pi core temperture')
    plt.grid(True)
    plt.ylabel('Temp C')
    plt.plot(tempC, 'rx-', label='Degrees C')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,26):
    tempC.append(0)
    
while True:

    ostemp = os.popen('vcgencmd measure_temp').readline()
    temp = (ostemp.replace("temp=", "").replace("'C\n", ""))
    print(temp)
    tempC.append(temp)
    tempC.pop(0)
    drawnow(plotTempC)

    #Send to Cloud, dweet.io
    rqsString = dweetIO+myName+'?'+myKey+'='+str(temp)
    print(rqsString)
    rqs = requests.get(rqsString)
    print rqs.status_code
    print rqs.headers
    print rqs.content
    
    plt.pause(.5)


Next:
Create dashboards for dweet.io things with freeboard.io
Read dweet.io JSON using Java

Related example on Arduino:
Arduino Uno + Ethernet Shield send data to dweet.io and freeboard.io

No comments: