Bridging REST to MQTT – Web apps talking to constrained devices

If you are into connecting your embedded devices to the Internet, you have most likely heard (and probably used) of the Message Queueing Telemetry Transport protocol, mostly known as MQTT,  which recently became also an OASIS standard. There are quite a few client implementations of MQTT for embedded devices:

MQTT is quite handy especially in those cases where you want to send data to your device (e.g., control commands) without having the device to continuously poll the server for updates or by implementing a server on the device (e.g., a web server) and accessing it remotely (which is a bad idea, both for security reasons, but also due to firewall-nat restrictions). However, your browser does not ‘talk’ MQTT, while in many cases you would like to a) send data from a browser and deliver them to device(s) through MQTT and b) receive data in the browser from devices that communicate over MQTT.

Fortunately there are ways to overcome this issue, by using a MQTT-to-REST bridge. To put it simply, you need a server that translates MQTT to WebSockets. You can write the server script on your own, or use an existing implementation, such as the Eclipse Ponte.

Ponte is an open source framework based on Node.JS that allows you to publish and receive the data using any protocol: HTTP, MQTT and CoAP. You can mix and match: submit with CoAP and subscribe via MQTT.

For example, you can do (assuming you have installed Ponte locally):

$ curl -X PUT -d 'world' http://localhost:3000/resources/hello

do send data over HTTP to the topic “hello:, and to receive through MQTT, you subscribe to the same topic:

$ mosquitto_sub -t "hello" -v

Additionally, you can use a simple Node.JS script (if you simply need a MQTT to WS bridge) that implements the following:

  • connects to a MQTT broker and subscribes to a topic
  • implements a WebSocket server
  • when there is data received over MQTT it sends the data to the connected WebSocket clients
  • when there is data from a WebSocket client, it dispatches the data through MQTT to a specific topic


You can find such a script here.

Simply edit and add the server endpoints, install the dependencies and you are ready to go!

Further reading:

Overview of Pub/Sub protocols

MQTT vs HTTPs performance on mobiles

The popularity of the ‘Internet of Things’

From Wikipedia page on Internet of Things

We are all aware of the hype and the great media attraction on the ‘Internet of Things': predictions about zillions of connected objects, white papers, infographics, acquisitions, startups, crowdfunded campaigns, conventions and various events, endless debates about the terminology, etc.

But all this did not exist 2 or 3 years ago! So when did this media attention really start to happen and why?

I have been in this domain, both for doing research and for personal interest (which in both cases led to building cool connected projects) for the past 4 years, and I am pretty sure we all agree on the fact that most of the technologies that enable us to build connected objects existed at the time. What has changed since then?

Let’s take a look at some graphs that can provide some information on when the media started to get attracted on the IoT:


This first graph is from the search activity on Google for the IoT term. The first mentions date back to July 2007 (first mentions of the term also in research papers – source: The first rise takes place around the beginning of 2010, while in mid 2012 the search rate grows higher and explodes after mid 2013. In 2010 there have been reports about the value and the upcoming growth of the IoT (like this one), but also something important happened back then in the Arduino world: At the end 2009, the first Arduino Ethernet shield became the mainstream way to connect your Arduino t the Internet, and a bit later, the first library for building a WiFi-based web client was published and vendors started providing WiFi-enabled Arduinos!

The second graph is from Gigaom and is generated every time you search on their website for the ‘internet of things’ term.


It displays the number of articles published around the IoT for each month, for the last 2 years. In 2012 there are less than 10 articles on IoT and one of them was about SmartThings (still active campaign on kickstarter back then). If you look at the early SmartThings prototype photos you will recognise an Arduino board ;). In the following months the published articles are on startups and connected objects, big vendors announcing their interest on IoT, acquisitions and crowd-funding campaigns. Of course wearables played an important role on this, with Pebble being one of the most successful kickstarter campaigns ever (ended on May 2012). Pebble smartwatch was also prototyped using an Arduino.

Where we stand now? Dozens of reports, events and press articles every month. On the product/project side, more than 180 connected devices for the Connected Home, 130 wearables in the Connected Body category and 40 projects for the Connected City (as indexed by Postscapes), mainly developed by startups and communities.

While this is not an extensive research, I am sure that this all started thanks to the makers movement and the Arduino who allowed people to start prototyping with connected objects.

Thank you Arduino and OSHW!


For those still wondering what the Internet of Things really is, here is the term I like to use most: “IoT is technologies and notions allowing connected objects to communicate with Internet services for the benefit of the user“. And if you are building a project/service/platform and you are wondering whether it can be considered as IoT or not, here is a link by @iotwatch that can help you on that.


Update on Philips Hue Node-RED Node

Based on recent communication with guys from Node-RED, the Hue node has been modified to include the following features:

  • You can now set also the brightness level (0->100) of a lamp
  • By setting the status to AUTO, you can set the lamp parameters using the message on the input node as follows:
    • msg.lamp sets the lamp ID
    • msg.color sets the lamp color (e.g., msg.color=”DF0101″ will set the color to red)
    • msg.brightness sets the lamp brightness (e.g., msg.brightness=50)
    • msg.payload is used to se the lamp status (on/off/alert) (e.g., msg.payload=”alert” will flash the Lamp once

    Please note, by setting the status to AUTO on the node configuration, the rest of the node parameters are ignored, you need to set all parameters through the message input.

You can find the current version of Hue node here (hardware->hue).

By the way, current Node-RED version is 0.7.1, which includes some nice new features, including headless mode, UI updates and more!

Also, make sure you check the flows published by users here!

So, here is a nice example for flashing a Hue Lamp when #iot is used on Twitter:

[{"id":"e973b4e.f168c48","type":"twitter in","twitter":"","tags":"#iot","user":"false","name":"iot","topic":"tweets","x":111,"y":128,"z":"d192ee66.2e6d1","wires":[["a2a6b979.5d5948","74b8bc6d.8b4744"]]},{"id":"a2a6b979.5d5948","type":"function","name":"set_payload","func":"msg.payload = \"alert\";\nmsg.lamp = 2;\n\nreturn msg;","outputs":1,"x":286,"y":127,"z":"d192ee66.2e6d1","wires":[["aef74415.5108b8"]]},{"id":"aef74415.5108b8","type":"HueNode","name":"FlashHue","username":"","discovery_mode":"","lamp_id":"2","color":"EBF5FF","brightness":"100","lamp_status":"AUTO","x":467,"y":127,"z":"d192ee66.2e6d1","wires":[[]]},{"id":"74b8bc6d.8b4744","type":"debug","name":"","active":true,"console":false,"complete":false,"x":254,"y":224,"z":"d192ee66.2e6d1","wires":[]}]

Makerland 2014

Makerland 2014, the first event of this Makerland series, took place in Warsaw, Poland between 17-19 March. I have to say it was definitely a big success and a positive experience for anyone that has participated! 350 makers have attended and made stuff by either participating in one of the numerous tutorials or by hacking and making things on the last day.

The venue has been great, and the Makerland team has done their best! Many local and international companies were also there demonstrating their products.

The first two days were full of keynotes and workshops. Participants learned to do wearable computing:

Learned how to use the RaspberryPi to detect when hens have eggs:

Played with micro-drones:

Controlled Sphero Robot balls using Arduinos and JavaScript (thanks to cylonjs):

And of course, learned how to use Node-RED to control the Philips Hue!

The venue was also full of 3D printers that printed cool stuff all the time!

There were even portable 3D scanners tested on participants!

The last day the hackathon took place, where almost 300 people had the chance to play with all available hardware and goodie’s from workshops and sponsors and create cool projects!

UPDATE: Check some of the cool hackathon projects here!

Overall it was an awesome experience, I had a great time, met wonderful and talented people and made new friend and of course learned new things and making and hacking stuff!

A big thanks to the Makerland team and looking forward to the next event!

RoboPet: Building a connected Robot for interacting with Pets remotely

Recently there has been an Arduino Robot competition organised by RobotChallenge. The best 10 ideas were rewarded with an Arduino Robot for building and presenting their project at the event (RobotChallenge 2014, Vienna, Austria). My team (Massimo, Riccardo, Ivan, Luca, Antonio, Roberto and me) in CREATE-NET submitted the RoboPet, which made it to the 10 finalists!. The idea is simple:

1) The Arduino Robot is equipped with a laser pointer that can be pointed to different directions using two servos.

2) An Android phone in interfaced with the Arduino Robot for allowing the remote control of the robot and the laser pointer.

3) The phone provides also live video feed of the robot.

We decided to use WebSockets for the communication between the Android phone and for making the video feed available in the user’s browser (through WebRTC).

This is how the architecture of the system looks like:

And here is the demo video we created for the project. Please, be kind to ‘like‘ the video, the more likes we get the higher the chances to present RoboPet at RobotChallenge in Vienna!

After a very short time our proposal was accepted and we received the Arduino Robot:

The first thing we tried was check how to move the robot in different directions. It turned out this was the easiest part. The following Arduino code snippet demonstrates how to move the robot forward, left, right and backward:

//read the motor speed from the potentiometer on board:

int val=map(Robot.knobRead(),0,1023,-255,255);
Robot.motorsWrite(val,val); // move forward

Robot.motorsStop(); // fast stop

Robot.motorsWrite(-val,-val); // backward

Robot.motorsWrite(0,0); // slow stop

Robot.motorsWrite(-val,val); // turn left

Robot.motorsStop(); // fast stop

Robot.motorsWrite(val,-val); // turn right

Note on Arduino Robot wheels movement: Despite it being easy to program, after some time we noticed that the Arduino Robot has issues with the wheels, preventing it from moving properly. We have tried different surfaces, performed wheel calibration but the Robot in many cases fails to move properly (especially to turn left or right). Also, looking at the Arduino forum, the wheels movement issue does not seem very uncommon, see here and here.

Next step was to find ways to interface the Robot with the servo motors and the Bluetooth module for communication with the Android phone. This was the toughest part and one of the main reasons we had to ask for a delivery date extension from the competition organisers. The reasons:

a) serious lack of documentation on Arduino Robot (we even posted questions in the official Arduino forum but got no feedback until this day). The only online resources available for the Arduino Robot is this page, that contains generic info on what the Robot can do, but without proper documentation on how to do it! For example, there is a reference about 6 PWM pins available on the control board and 2 on the motor board, but there is no indication where to find these pins!

b) Although Arduino Robot in fact consists of two Arduino boards connected to each other, there is a lack of available pins for interfacing with networking modules, especially when it comes to serial communication. Many digital pins are available, but SoftSerial on Arduino Leonardo (same MCU chip with the Arduino Robot) operates (the Rx) only on specific pins, that on the Arduino Robot happen to be used by the TFT screen.

So the only way to proceed was to remove the screen and use the MOSI pin to connect it as Rx pin to the Bluetooth module. We used a BlueSMiRF module for the wireless communication between the Arduino Control board and the phone. Initially the SoftSerial library was throwing compilation errors when selecting the Arduino Control as a target device, so we had to tinker a bit with the library itself and some header files for the Arduino Robot.

In the meantime, Riccardo was preparing the laser pointer by disassembling an ordinary laser pointer, attaching wires to get power from the robot board and mounting it on top of 2 servos for pointing it at X and Y axis: 

To keep the complexity low, Riccardo added a random laser movement function in the Arduino sketch. We then mounted the servos with the laser pointer on the Arduino Robot. We also added a metal holder for attaching the phone:


The phone runs a background service that connects to the Bluetooth module and sends commands that are received over a WebSocket connection. At the same time it runs a webpage connected to a WebRTC video streaming session that is transmits the video from the front phone camera to any client subscribed to that stream (a webpage or another smartphone device).

The Arduino receives the commands over the SoftSerial pins through the Bluetooth module and interprets them to move forward, backward, left, right and on/off for starting the laser pointer.

All source code for the Arduino, Android and the WebRTC/WebSockets part can be found here. A demo video will follow soon.

Counting Twitter hashtags with Philips Hue

This a script I wrote some time ago for a demonstration at an IoT workshop. The idea was to use a commercial IoT product such as the Hue (which had been just released during that time) and combine it with an existing service on the Internet (like Twitter).

In this case counting Twitter hashtags is visualised through the Hue: When a new hashtag (e.g., #iot) is detected the lamp will blink once and an internal counter is increased. When a defined threshold is reached the lamp color will change to blue.

You don’t need to install any library to use the script and control the Hue. The Hue REST API is used by sending the appropriate requests.

You can find the script on github:

Some updates on the IoT (open source) World

I would have created many separate posts for the following news, but unfortunately time is short. So here is a list with some updates about open source software and hardware on the IoT World:

– Node-RED is currently at version 0.4.0 (updated:) 0.5.0

It features new configuration options for npm updates and new nodes (helpful when installing a new Node-RED version), new graphical improvements and more. Also new nodes have been added in the node-red-nodes repository, including a wemo node (for controlling Belkin Wemo sockets and switches), as well as two Philips Hue nodes (see earlier post). You can check the new nodes here.

– openPicus new software and hardware releases

openPicus has recently released the FlyportPRO which targets more professional users. It comes in a very small factor, for professional users that want to integrate it directly to their product. More information here.

Flyport Light has been introduced at a very competitive price for projects that do not require any connectivity.

There is a new version 2.5 of the IDE.

MQTT will become available as a library for the Flyport as well!

Finally, a great Xmas project is hosted at the FCO Airport in Rome, powered by Flyport! There is an interactive Christmas tree that you can control the lights from your browser! More about it here.

– Arduino Intel Galileo on MacOSX

As you probably already know the Intel Galileo has been released. Here is an interesting post/step-by-step tutorial on how to use the board on a Mac.


– The ‘Open Informant’ wearable badge

A very interesting open source project has been developed by Superflux for Wearable Futures 2013 Conference. The concept behind the project is to visualise communication that may contain NSA trigger words. A smartphone app is used to scan through your messages and identify keywords that could trigger a NSA investigation on you! The words are then displayed on a e-ink wearable display, powered by an Arduino Mini and BLE. More info here.

– The ThingFul Project

You probably have heard of ThingFul project by Usman Haque. As stated on their website, “Thingful is a discoverability engine for The Public Internet of Things, providing a geographical index of where things are, who owns them, and how and why they are used.”. It is an interesting concept, although to my opinion, IoT object and service discovery is not that simple, and for sure an API would be more useful for discovering and identifying objects.

For sure it is worth trying registering your devices, hopefully there will be more features revealed/enabled in the near future.

– Building your own IoT service with open source tools

On December 12th I was invited to speak at the BDigital IoT Forum in Barcelona. I presented the concept of COMPOSE european project and more specifically, how makers and entrepreneurs can benefit from open source components of the project for building their own IoT services and applications. You can check the slides of my presentation here.

Creating a node for Node-RED: The Philips Hue Node

Guys at Node-RED are working hard to provide updates and bug fixes. Currently the most recent version is v.03.0 which adds some neat features like tabs to the workspace and the very much useful ‘switch‘ node which can route messages according to simple rules defined in the UI (like If msg.payload != true send to 1).

There are still things to do and there is always room for improvement, but Node-RED is a great tool as it is, and besides, if you need something that does not exist, you can always add it by implementing your own node. As in my previous post, where I implemented a Bluetooth 4.0 device scanning node.

There is not a lot of documentation yet on how to create your own node, and although there is the 99-sample.js.demo sample file, it might not be very clear on how to do it. So I decided to write down in this post the steps for creating a new node.

Note: Creating a new node is a good practise when you can build something that can be re-used from the community. For instance, porting an existing Node.js library to Node-RED and exposing all (or most) of its functionality. If you need only to import an module and use it on a specific workflow, you can do it through the function node using the built-in os module:


Node-RED node essentials:

Each Node-RED node consists of two source files: The nodename.js and nodename.html files. The former contains the execution logic of the node and the latter the graphical element html code, which also defines parameters like what are the inputs, etc.

Both files need to be placed inside a folder name nodename under Node-RED/nodes/node-red-nodes/hardware, software, …

An Example -Building the Philips Hue Node:

I wanted to create a node that controls a Philips Hue wireless lamp through the Hue API. I made a search first online and luckily I found node-hue-api, by Peter Murray, which turned out the simplified the process a lot. Node-hue-api is a great Node.js library that exposes the full Hue API, so that you can build your own Node.js app to look for a bridge, get registered lights, register new lights, change the light status of a lamp, etc.

So, I started with the node samples provided by Node-RED. You can find them in ../nodes/node-red-nodes and the filenames are 99-sample.html.demo and 99-sample.js.demo. I saved them in a folder named ‘philips_hue’ and renamed them to 103-philips_hue.js and 103-philips_hue.html.

The sample code in 103-philips_hue.js (from 99-sample.js.demo) is more or less self-explanatory. It starts with importing the main RED module:

and then implements the main node function. In the 99-sample.js.demo file it is called SampleNode(n) so I changed it to HueNode(n).

Inside this function you write the main code of your node’s logic. The sample comes with a simple “Hello world !” message sent to the output of the node. No matter what you implement in this function, you need to start with initialising the RED node with the following:


Also, keep in mind that any code inside this main function will be executed upon registering the node in Node-RED, that is when and every time you start Node-RED. It will also be executed every time you provide an ‘input’ to the node.

If you need your node’s code to be executed only upon receiving an input, you have to add the following ‘event’ listener:

and add your code accordingly.

Designing the Node – the philips_hue.html

Before jumping into code implementation, it is good to identify first what the node inputs will be. In my case, I wanted the node to be able to act in dual mode: a) provide a scanning feature for identifying the IP address of a Philips Hue bridge in the local network and then querying it for registered lamps and b) provide a way to turn on/off and flash a particular lamp. So I decided to go with a checkbox in the philips_hue node configuration window. Also, one mandatory input to the node should be the authorization id or username that will authenticate the node as valid to use the Philips Hue API and perform commands on the bridge. More information on that and how to use the Philips API can be found here.

I have modified the default code from 99-sample.html.demo to look like the following:

The first edit was to change the data-template-name to “HueNode”. This is what my new node is called and will be also mapped within the code.

Secondly, I added the textfield for entering the ‘username’ used for authentication with the bridge. When adding your input parameter, make sure you change the id to “node-input-parametername” and the placeholder to “parametername“.

Next, I added the the html segment for the checkbox that will enable bridge scanning:

I added the input fields for the Lamp ID variable and for selecting the status of the lamp the same way. The status was a list with options like AUTO, ON and OFF. AUTO refers to when user wants to set the status of the specific lamp using the msg.payload, e.g., msg.payload = “ON”.

Finally I modified the javascript code at the end of the file by adding the variable’s I am using for input from the user:

You can see that on top of the code the ‘HueNode‘ is registered and under the defaults, each input parameter is defined accordingly.

You can check the complete code for philips_hue.html here.

The Code Implementation – the philips_hue.js

After having defined the inputs next step is the code implementation. Back to the philips_hue.js file, I have imported in the beginning of the file the node-hue-api library that will be used through two variables, hue and hueAPI.

Inside the main HueNode(n) function the user input parameters are saved into variables.

Then on “input” the code checks for bridge discovery mode and performs so using the node-hue-api methods.

When bridge discovery is done, the IP address of the located bridge is stored and then the lamp query begins for identifying registered lamps. At the end, the output of the discovery (JSON formatted string with information about the light names and IDs) is stored in a msg.payload and sent to the node output.

In a similar way, when discovery_mode!=1 the code uses the node-hue-api to change the state of a registered lamp through its lamp ID (1, 2, 3, …). The code segment for controlling the lamp status performs bridge discovery again so there is no need to use the discovery mode first if you want just to control the lamp.

You can find the complete code for philips_hue.js here.

The following Node-RED sketch demonstrates how to turn on lights when it is getting dark:

[{"id":"235afb71.dca504","type":"HueNode","name":"Turn Lamp Off","



I am running Node-RED on my RaspberryPi and now I can easily turn on/off the lights based on proximity detection (using the ScanBLE node). Full instructions and code will follow soon!


Fast IoT prototyping with SODAQ

I recently came across SODAQ (SOlar Data AcQuisition), an interesting Arduino-based board that promises to make IoT prototyping faster and easier.

What do you need for an IoT project? A microcontroller platform, some sensors and actuators, power and of course a wireless interface. SODAQ provides all these in one board, featuring Grove sensor interfaces and a xBee socket.In addition, there two power connectors, one for a rechargeable battery and one for a solar panel (hence the name). These mean basically two things:

1) You can easily add any Grove compatible (and Seeedstudio provides a huge list!) sensor or actuator by simply plugging it on the board. No needs for soldering or ‘breadboarding’.

2) You can select the best wireless interface that suits your projects (RF, Bluetooth, WiFi, GPRS).

The board is quite small and compact, it is the size of a RaspberryPi, so any Pi-case can do for SODAQ too. People behind SODAQ claim that it draws about 100 times less power than a Pi to connect your things. 

 SODAQ comes with the following specs:

  • Atmega Microcontroller running at 3.3V and 8MHz
  • Arduino Compatible
  • Power supply by LiPo battery (3.7V) or USB cable
  • Solar charge controller with JST connector for Solar Panel up to 2.5W
  • Battery Monitor
  • DS3231 Real Time Clock and Temperature sensor, clock backup powered by LiPo battery
  • 8 MBit data flash module (AT45DB)
  • Micro USB connector
  • 12 Grove connectors connecting Digital, Analog and I2C pins
  • On/Off switch. With the switch in Off position the solar charge circuit is still active and the RTC clock is still powered.
  • ICSP programming header
  • Bee socket for Xbee, GPRSbee or other bee style modules
  • Same size as Raspberry Pi

The board is open source and  looks very promising! Wish I can have one soon to build my next project! You can order yours here.

Using Node-RED to scan for BLE devices

After playing around with Node-RED, I decided to use it for the next project I had in mind. The idea is to use a BLE (aka Bluetooth Low Energy or Bluetooth 4.0) device to automate some actions at home or on my computer. There are a couple (or more) ways to scan for BLE devices, one of them is to use directly some command line tools, like the hcitool, part of BlueZ (assuming that you are on Linux). The other is to use an existing library for your favorite developing environment, like noble for Node.js.

One of the first projects that came in mind was to use my Fitbit Flex for locking my screen/session when I am leaving my computer. The idea is not unique (see here and here for example), but since Fitbit is something I carry always on me, it looks more easy-to-use (and less pervasive) than using a smartphone.

The first step was to write a special node for Node-RED that would do the scanning and checking for a specific BLE device (by name and UUID). Writing the module was quite easy (after spending some time to understand the logic, documentation is still a bit poor on this. The 99-sample.js.demo is much more helpful). I also decided to share the new node to the community by making a pull request at the node-red-nodes repository which is dedicated for hosting nodes created by the community. The request was accepted (btw I was the first non-IBM-er to pull to  the repository) and now you can find the scanBLE node here.  The node icon looks like this:

Many thanks to Dave C-J  for adding the Bluetooth icon on the node.

So to use scanBLE in your flow, you install it first (check here for short instructions) and add it in your canvas. Doubleclick and enter the required information (BLE device name for the name of the Bluetooth device you are looking for, and UUID for its UUID). The node will scan all BLE devices and will check for the name and UUID you have entered. When found, it will trigger the message output with the name of the device as payload. For example, if you are looking for your ‘Fitbit’ device, then msg.payload=’Fitbit’.

To have the computer screen locked when the Fitbit is not around you can use the following flow:



{"id":"d3947a09.2c6b88","type":"scanBLE","name":"ScanBLE","ble_name":"Fitbit","ble_uuid":"123455555","x":271,"y":203,"wires":[["31e80866.ce17f8"]]},{"id":"31e80866.ce17f8","type":"function","name":"Check","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n//   console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n//   context = {};\n\nif(msg.payload!='Fitbit'){\n\t//exec('/System/Library/CoreServices/Menu\\ Extras/ -suspend');\n\treturn msg;\n}\n","outputs":1,"x":417,"y":130,"wires":[["d1efa92e.2e1058"]]},

{"id":"d1efa92e.2e1058","type":"exec","command":"/System/Library/CoreServices/Menu\\ Extras/ -suspend'","append":"","useSpawn":"","name":"Lock()","x":556,"y":219,"wires":[["6ccbcca2.933434"],["6ccbcca2.933434"],


EDIT: Of course you need to execute a shell command for locking your screen. The flow above executes a command that will lock your Mac session:

 /System/Library/CoreServices/Menu\ Extras/ -suspend 

If you are on Linux you can try something like

  gnome-screensaver-command -l 

Note: You need a BLE interface for your Mac/Linux computer, I have used this Bluetooth 4.0 Adapter from TRUST.

1 2 3 4 8