Home automation as a weekend project using IBM’s clouds – part 1

I decided to make myself familiar with the IBM’s cloud services by creating a home automation system based on these services:

  • Internet of Things Cloud to interconnect sensors (temperature and motion detector) and actuators (light switches) with the application running in cloud.
  • Bluemix to process the events and act in an appropriate way
    • I plan to use Node-RED to feed a database with the events
    • and Business rules service to implement logic.

Before implementing such a system I started by testing components that I plan to use.

I am documenting my tests and learning by publishing simple articles like this one. Simply it’s my external memory.

Let’s go forward and “wire” a first part of this test setup:

Screen Shot 2014-08-04 at 14.54.59

Figure 1 – Test architecture overview

Connect simulated device to IoT cloud

First we need an IBM Internet of things account. Go here


and sign up for one.

Then register new device by going into Dashboard and selecting “Create a device type” and entering “sensor-type” and fill your MAC address into last field:

Screen Shot 2014-08-04 at 15.00.34

Figure 2 – Registering a new device

Copy the fields generated by the registration process. Example what the registration process returns – use your own not this example:


Now customize the following ruby script publisher.rb by replacing _place-holders_ with values from the list above:

require 'mqtt' 
class Pub 
    def self.sendIt 
        p = 80 
        t = 37
        r = Random.new 
        client = MQTT::Client.new remote_host: '_ORG_.messaging.internetofthings.ibmcloud.com', 
            remote_port: 1883, 
            username: 'use-token-auth', 
            password: _AUTH-TOKEN_' 

        client.connect 'd:_ORG_:sensor-type:_ID_' do |c| 
            loop { 
                puts "Publishing Temp: #{t}, Pulse: #{p}" 
                c.publish 'iot-2/evt/event1/fmt/json', " { \"d\": { \"temp\": #{t}, \"pulse\": #{p} } }" 
                puts 'Waiting 5 seconds' 
                sleep 5 
                p += r.rand(-0.5..0.5) 
                t += r.rand(-0.5..0.5) 

Save the file publisher.rb somewhere and install Ruby if you do not have any. I suggest using RVM for rubies management: https://rvm.io

Install needed gem ‘mqtt’ by issuing

gem install mqtt

Start the script and if you see something like this then everything is ok:

$ ruby publisher.rb 
Publishing Temp: 37, Pulse: 80 
Waiting 5 seconds 
Publishing Temp: 37.24603750728819, Pulse: 79.80073214626277 
Waiting 5 seconds 
Publishing Temp: 37.24132599127924, Pulse: 79.36174810882044 
Waiting 5 seconds 
Publishing Temp: 37.71265703067605, Pulse: 79.31680888876201 
Waiting 5 seconds 

If the answer is like below:

MQTT::ProtocolException: Connection refused: not authorised

or another exception then the values obtained during registration process are not inserted into the script in the correct way.

Ok, now we have connected the simulated device to the IoT Cloud! Before leaving the Internet of Things dashboard generate an API key for future access from Bluemix. You will receive something like this:

Key: a:kbrhg:7xeayt2adc 
Auth Token: 1ECDokPIRkx_XBSueF

Catch the events using Node-RED app running at Bluemix

Obtain an account if you do not have any:


From the Catalog choose Boilerplate „Internet if Things Starter“ and name it „IoT“.

Add service „Internet of Things“ into the „IoT“ Application using Dashboard with Service name „SensorData“.

Use IoT API credentials obtained second before. Start Node-RED editor by clicking the link after “Routes:” and clisk “Go to…” button on next page.

Click the button in the right upper corner and select “Import from…”, “…clipboard” menu item.

Copy the following text into the field:

 [{"id":"6b68d19b.94973","type":"iot-app-in","name":"IoT App In","topic":"iot-2/type/sensor-type/id/843835542d90/evt/event1/fmt/json","x":178,"y":187,"z":"4940fee9.b6bf","wires":[["cc9f2f21.3360d"]]},{"id":"cc9f2f21.3360d","type":"debug","name":"","active":true,"console":false,"complete":false,"x":462,"y":186,"z":"4940fee9.b6bf","wires":[]}]

and place two nodes somewhere into the workplace. Open the “IoT App In” node by double-clicking and enter topic name:


where you replace the _MAC_ placeholder by your MAC address (also called ID by IoT Cloud).

Deploy the flow using the red button and switch to debug tab. If your simulated device is running you should see the new events in the debug tab.

Screen Shot 2014-08-04 at 14.55.10

Figure 3 – Node-RED GUI with debug showing the events from simulated device

So far so good, we have a simulated device (or a real device if you run the script on a RaspberryPi ☺ ) connected to the IoT cloud and we are consuming the events by Node.js application running on Bluemix.

In the next article I will connect Node-RED to a hosted MongoDB and after that I will build a simple mobile web app using Bluemix RapidApps.

Edited 7.8.2014:

Topic format changed to:


Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *