The age of makers

When I was 10, I was fascinated by electronics and robotics. I remember dismantling my old electro-mechanical toys in order to reuse some of their parts, and combining them to Lego blocks to build my own prototypes. Unfortunately, this was mainly basic tinkering that never really lead to something working. I think that one of the main obstacles to this was that, understanding as well as manipulating electronic components, was quite complicated for kids, even when resorting to dedicated electronic kits (such as “Science Fair electronic project kits”).

This was a long time ago and since, I worked my way in the software industry – but that’s another story. Yet, I can see that today, many kids – including my own – still share this same interest to making things, as it is also the case of many people of different age categories. There is actually a movement – the “maker” movement – that is gaining in importance, thanks to a “democratization” of technology, notably in the field of prototyping technologies such as 3D printing, cheap but powerful boards (Raspberry Pi, Arduino, etc.) and electronic components kits such as Littlebits’.

DIY with littlebits

Littlebits is a company that designs and sells magnetized electronics components that you can snap together to easily build electronic circuits. Littlebits brings you a large diversity of components, with detailed step-by-step instructions and practical projects to get you started. Littlebits kits are what I wished I had when I was a kid: their Lego-like approach is ideal to learn electronics while having fun. It is also an excellent choice for makers who want to rapidly build prototypes.

Beyond hardware

Animus imperat corpori, the mind controls the body: when building prototypes by assembling components, for fun or to realize a business idea, you reach a place where you find that you need more than physical parts and electronic circuits. For example, you might need to run calculations based on the history of values measured by a given sensor before taking a decision and sending instructions to your device. Or you might need to send messages to alert your device’s users – or other devices; post a message to a social network or include the outcome of a remote service into your own process… This is all done by incorporating software into the equation.

Comparing the software to the mind and the hardware to the body is of course, a metaphor. Yet, adding software to your prototype, notably when making Internet of Things applications, greatly extends its capabilities and brings you more control over what you can do. This is why Littlebits exposes APIs on its Cloud platform (cloudbits) so you can send instructions to your components or subscribe to events they can generate. This is also why, at scriptr.io, we wrapped these APIs into a connector that you can directly use in your scriptr.io scripts making it thus super-simple to interact with your littlebits prototypes.

Try it!

Let us illustrate how easy it is to use littlebits with scriptr.io by making our own IoT prototype, using the cloubit starter kit.

Components of our prototype

Our prototype will be composed of the following:

A sound trigger: it listens to the noise level and sends an ON signal when the noise reaches a certain threshold (the latter can be adjusted).
sound-sensor
A servo: it is a motor that can swing back and forth (“swing” mode), or execute a rotation of a given angle (“turn” mode). In the former case, the input controls how fast the motor can swing, whereas in the latter, it determines the rotation angle.
servo
The cloud module: it uses wifi to connect the electronic circuit to your network and thus, to littlebits’ APIs and vice-versa.
cloud-module
A USB power that we use with a USB Wall Adapter: they allow the power to flow into the circuit.
usb-charger

Pre-requisites
Do not forget to first configure your cloud module, which gives it access to your wifi network and also generates an authentication token that you will use in the below to communicate with littlebits APIs.

Features of our prototype

Our prototype will use the sound trigger to detect the ambient sound level and notify scriptr.io when the sound level threshold has been reached. In return, scriptr.io will send a notification email and issue an output towards our circuit, in order to trigger the servo.

Build the body

Setting up our circuit is really child’s play: snap the USB power to the sound trigger and snap the latter to the cloud module that you snap to the servo. Plug the USB wall adapter and wait for the led on the cloud module to turn green – which means you are connected to the wifi network – and you are set!
circuit (2)

Build the mind

Make sure to checkout our littlebit connector into your scriptr.io workspace. Once done, configure the connector. Open the “littlebits/config” file and paste the value of your littlebits authentication token as the value of the “token” variable.

// Your Cloudbits platform OAuth token. Is used by default when no other token is specified
var token = "911d587eg774f38e543608b507h6c97a93167fde80bc8a357cdbff5e1bb8230w"; // example

Also replace the value of the “defaultNotificationEmail” variable with your email address:

// Default email address to use by the default event handler (DefaultHandler)
var defaultNotificationEmail = "iotmaker@somemail.com";

In the scriptr.io dashboard, create a new script called “initialize” (or any name you prefer), then type the following instructions:

// require the cloudbits module
var cloudbitsModule = require("modules/littlebits/cloudbits.js");

// create an instance of the Cloudbits class, passing your littlebits user id
var cloudbits = new cloudbitsModule.Cloudbits({userid:"your_id"}); // replace with your user id

// obtain an instance of the Device class, to control your 'cloud module' (passing the module's id)
var myCloudModule = cloudbits.getDevice("00f06c213b7a");

// subscribe to events emitted by the cloud module (in our case, when the sound level is higher that the threshold
// which translates to a voltage jump in the circuit)
var mappings = require("modules/littlebits/mappings.js"); // this contains event definitions
myCloudModule.addSubscriber({events:[mappings.events.VOLTAGE_JUMP]});

When ran, this script subscribes the ‘littlebits/api/handleEvent’ script to voltage jump events issued by the circuit, which will occur whenever the sound trigger detect a loud sound.

Let’s now write the logic (handler) to react to this event. Since ‘littlebits/api/handleEvent’ uses the ‘littlebits/notifications/DefaultHandler’ script by default, we will just reuse the latter for now and update it. Insert the following lines at the beginning of the ‘handle’ function:

// obtain an instance of the Device class, to control your 'cloud module' (passing the module's id)
var cloudbitsModule = require("modules/littlebits/cloudbits.js");
var cloudbits = new cloudbitsModule.Cloudbits({userid:"your_id"}); // replace with your user id
var myCloudModule = cloudbits.getDevice("00f06c213b7a"); // replace with your cloud's module id

// instruct the cloud module to propagate output at 80% of amplitude for 5 seconds,
// this results in instructing the servo to swing at 80% of its speed for that duration
myCloudModule.write({percent: 80, duration_ms: 5000});

That’s it.

You can try it by running the “initialize” script once (if not already done) then speaking loud near the sound trigger. After speaking, you should observe the servo swinging for 5 seconds and receive notifications at the email address you used in the configuration part (note that depending on your network’s quality, this might require a few seconds).