Jython
Deutsch   English 

11. Cloud Computing


In cloud computing, computer services are outsourced to an external server that is accessed via the Internet. As demonstrated in this chapter, the storage of sensor data in the cloud is of particular interest, because this data can then be accessed from anywhere via the Internet.

 


Here we use the cloud server from ThingSpeak, which is available free of charge for small data volumes. This cloud service is offered by Mathworks (distributor of Matlab . It is very easy to retrieve the stored data and display it graphically.in a Web browser.

 

To use the services, you need to create a personal account at ThingSpeak (www.thingspeak.com). ). A short tutorial can be found here: Create a ThingSpeak account.

 

Example 1: Store values of the distance sensor in the cloud

To store the distance measurements in the cloud, the mbRobot acts as HTTP client and sends HTTP requests in a the required format to the cloud server. In the following program a new value is generated every 15 seconds (for a free account you can't do more than every 15 seconds, for a paid account every second). The required API key is obtained when creating the ThingSpeak channel.

 

# ThingSpeakDist.py
from microbit import *
from linkup import *
from mbrobot import *

key = "456FPOXESR6L8WYX"
print("Sending AP connection command")
connectAP("mySSID", "myPassword")
while True:
    dist = getDistance()
    print("Sending new distance:", dist)
    url = "http://api.thingspeak.com/update?api_key=" + key + "&field1=" + str(dist)
    httpGet(url)    
    delay(15000)
► Copy to clipboard
 

 

After some time you will see the following graphic on the ThingSpeak website:

The cloud server provides tools to insert the stored data in a HTML page (as table or graphics).

Explanations of the program code:

key = "7893POXESR6L8WUX": Write key of the ThingSpeak channel
httpGet(url): Sends the current sensor value with a GET request


Example 2
: Store acceleration sensor readings in the cloud

The acceleration sensor of the micro:bit is used to measure the x-component of the acceleration. If the mbRobot is positioned on a horizontal surface, the value is 0; if it is tilted to the right, the values are positive; if it is tilted to the left, the values are negative.

In order to save the acceleration, the mbRobot acts as HTTP client and sends an HTTP request to the cloud server every 15 seconds, where the value is stored.
 

# ThingSpeakAcc.py
from microbit import *
from linkup import *
from mbrobot import *

key = "7567FPOXESR6L8WXY"
print("Sending AP connection command")
connectAP("mySSID", "myPassword")
while True:
    acc = accelerometer.get_x()
    print("Sending acc:", acc)
    url = "http://api.thingspeak.com/update?api_key=" + key + "&field1=" + str(acc)
    response = httpGet(url)
    print("Response:", response)
    delay(15000)
► Copy to clipboard
 

 


After some time you will see the following graphic on the ThingSpeak website:

 

Example 3: Retrieving data from the cloud programmatically

With a GET request, you can retrieve data stored on ThinkSpeak. You need to know the read key and the channel ID, which you can get after logging into your account under the menu option API Keys. The format of the URL is the following:

http://api.thingspeak.com/channels/channelID/feeds.json?api_key=readKey&results=100

where channelD and readKey must be replaced by the current values. The value of results corresponds to the maximum number of values returned (a maximum of 8000 values can be stored). The return value is a JSON-formatted string containing all the information of the channel.

If you type this URL into a browser, the information will be displayed (both in raw format and JSON formatted).

In this example, the data is displayed graphically with a GPanel. The return string is converted into a Python dictionary with eval(). Under the key feeds you get a list with the stored values (each  value again as a dictionary). The field1 key returns the value as a string.                         

 

 

 

 

# ThingSpeakFeeds.py

import urllib2
from gpanel import *

makeGPanel(-2, 22, -600, 600)
drawGrid(0, 20, -500, 500)
x = 0

readKey = "LGHJ1UDKXABYCIJ"
channelID = "4924629"

url = "http://api.thingspeak.com/channels/"+channelID+"/feeds.json?api_key="+readKey+"&results=100" 
data = urllib2.urlopen(url).read()
dataDict = eval(data)
print dataDict
feeds = dataDict['feeds']
for feed in feeds:
    a = feed['field1']
    y = int(a)
    if x == 0:
        pos(x, y)
    else:
        draw(x, y)
    x += 1
► Copy to clipboard

With a HTTP DELETE request you can delete all data of the channel programmatically. You have to know the user key that you find in your ThinkSpeak account under the option My Profile.

# ThingSpeekDel.py

from linkup import *

channelId = "123456"
userKey = "A4YKXGJEIC8J1L48"

connectAP("myssid", "mypassword")
url = "https://api.thingspeak.com/channels/" + channelId + "/feeds.json?api_key=" + userKey
httpDelete(url)
► Copy to clipboard