Close

Remote access / publishing : ThingSpeak & PubNub

A project log for SEELablet : Instrument Cluster For Laboratories

Minimal, yet powerful combination of control and measurement tools integrated with an SBC running Python based UIs for science & Engg. Expts

jithinJithin 07/12/2016 at 10:120 Comments

being able to remotely access the instrument and connected sensors opens up several new possibilities such as weather monitoring systems.
From an implementation point of view, I tried out two platforms.

A ) Thingspeak.com : This allows uploading sensor data to your channel, and their website auto-generates nice plots that you may embed on websites. You can also retrieve the data, all through http requests. There's a restriction of 15 seconds between successive uploads of datapoints, so you're limited to monitoring slow parameters.

In the above example, I created a channel called HMC5883L , and uploaded a few datapoints to it. The plot is available here .

result = I.get_voltage('CH1')  # upload a voltage reading
params = urllib.urlencode(
{'field1': float(result),
'key':<your key here>})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.read()

B) PubNub :

With PubNub, one can send and receive data on predefined channels. Simply bind a callback function to a channel(string) , and the PubNub thread will automatically invoke it whenever a message is received over a subscribed channel. The free accounts are limited to 1 million messages.

Figure : Utility to access devices remotely

pubnub = Pubnub(publish_key = <your key>,subscribe_key = <your key>)   #  Create a pubnub communication thread
def callback(msg,channel):
    print msg,channel
pubnub.subscribe('C1',callback = callback) #subscribe to a channel called C1, and specify the callback function

pubnub.publish(channel = 'C1',message = 'hello') #send a message over C1 . Anyone listening over C1 will be able to read it.

Figure : Thingspeak publishing.


get_voltage('CH1') is executed every 15 seconds, and the data is pushed to the cloud.

Discussions