Controlling cloudBit using Raspberry Pi 2

Written by on September 28, 2015 in Electronics, Internet of Things (IoT), Mathematica with 0 Comments

I have had Raspberry Pi for two years now and badly wanted to get the newer Raspberry Pi 2. I finally got it last week and decided to tinker with it during my spare time.

Setting it up was pretty straightforward. Just connected the power source, keyboard, mouse, HDMI monitor, and wifi dongle, and the system booted normally.

Connecting Raspberry Pi 2

Connecting Raspberry Pi 2

When I was looking for a simple experiment, it occurred to me that I could try and control my littleBits cloudBit module using Raspi.

So this is what I did. I put together a very simple cloudBit project: Power -> Push button -> cloudBit -> Number Display

cloudBit Setup

cloudBit Setup

I made sure that my cloudBit setup was connected to the same wifi network as the Raspberry Pi.

The good news is that cloudBit can be controlled through HTTP requests, so any programming environment that supports this can be used. Since I am a huge fan of Mathematica and since there is a version of Mathematica that comes as part of Raspian bundle, I decided to write a simple program in Mathematica to interact with the cloudBit module.

I wrote two functions, one to read the status of the cloudBit and the other to control the output (Number Display) of cloudBit. The functions are given below (I have masked the cloudBit authentication key):

CloudBitFetch[] := ImportString[

URLFetch[“https://api-http.littlebitscloud.cc/devices/00e04c0383a1”,

“Headers” -> {“Authorization” -> “Bearer <key>“,

“Accept” -> “application/vnd.littlebits.v2+json”}], “JSON”]

The above function uses the GET method to read the status of the device and returns the results as a JSON object. My device ID is hardcoded for convenience.

CloudBitSend[value_, durn_] := 

URLFetch[“https://api-http.littlebitscloud.cc/devices/00e04c0383a1/output”, “Method” -> “Post”, “Headers” -> {“Authorization” -> “Bearer <Key>”, “Accept” -> “application/vnd.littlebits.v2+json”}, “Parameters” -> { “percent” -> ToString[value], “duration_ms” -> ToString[durn]}]

This function takes a value (0 to 99) and duration in millis and sends them to the output of cloudBit (using Post method). This causes the Number Display to display the value for the specified duration.

Here is how we can use these functions (result from Mathematica is shown below each call):

In[2] = devicedata = CloudBitFetch[]

Out[2] = {label -> RangaCloudBit, user_id -> 49762, 

 input_interval_ms -> 750, id -> 00e04c0383a1, 

 ap -> {ssid -> mmsindia, strength -> 100, 

   mac -> 48:EE:0C:CF:E6:99, server_id -> 4kR9rW-kx, 

   socket_id -> NyQkwnZ1g, status -> 2}, 

 is_connected -> True, subscriptions”-> {}, subscribers -> {}}

In[3]:= “label” /. devicedata

Out[3]= RangaCloudBit

In[4]:= CloudBitSend[30, 4000]

Out[4]= {“success”:true}

If you observe carefully, you can see that the display reads “31” whereas the value I sent was “30”. I can’t understand why! Whatever value I send, the display reads one more than that value. This may require further investigation, which I will probably take up later.

One of the other popular experiments with cloudBit involves setting up IFTTT so that interactions/notifications between cloudBit and IFTTT-enabled channels can take place. In my case, I created two recipes in IFTTT, one to send me an email via gmail when the Push button at the input of cloudBit is pressed, and the other to send me an SMS when the Push button is pressed. Both these worked as expected!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top