iot-15-minutes-1

IoT Prototype in 15 minutes

Grzegorz Hajdukiewicz

IoT - Internet of Things has been a hot topic in IT industry for some time. It's currently considered to be the fastest growing market, so why not have a closer look about how to build your first prototype, using cutting edge technologies for creating IoT proof of concept projects.

Build your first prototype using Node-RED, mqtt and Samsung ARTIK in 15 minutes with our tutorial! Make it applicable in the field by deploying to Raspberry PI and see how to attach some real-world measurement sensors to it.

Ready, steady, go!

Architecture

Let's start with some prerequisites for our micro-project, so that we share a clear vision of the goal we want to achieve, technologies we want to use, and what reason stands behind it.

We will divide this tutorial into two steps. The first will lead you through the process of building proof of concept software and the second will show how to deploy it to real hardware. In IoT projects it's important to know the final technologies being used in your platform, so that you can start with correct assumptions and choose the right solutions.

We'll build a simple temperature monitoring system, with real-time charting and overheating email alerts. These kinds of systems are being used everywhere from alarm systems, to hobbies like brewing, and even to complex industrial systems for monitoring technological processes and machinery.

alt-text

We want our system to be sending a single floating point number per sensor, let's say, every 10 minutes to the cloud (you can do it more often if you wish, I basically find it unnecessary). The data should be stored, displayed as a chart and compared to threshold value so that our system can trigger email notifications if something goes wrong. It needs to to be able to work under any conditions, also using a GSM network to connect to the cloud - low data rates are possible, so a light-weight protocol like MQTT seems to be a good choice. Another reason to use protocols with low data overhead is that in GSM communication, you usually pay per single kilobyte transferred.

ARTIK - The Cloud Platform

We need a place where we'll be sending our data to store and process. A First thought would be to build a simple, custom web app with a charting interface, but there are already multiple, ready to go cloud platforms that offer features which facilitate the setup of a system like ours. In this tutorial we'll be using ARTIK Cloud by Samsung, which I find to be the most user friendly. The tool will let us speed things up while ensuring scalability for our system and a full support. Also there's a free plan which perfectly fits our needs!

Let's sum up.

System Specification

  1. Single floating point number being sent per sensor
  2. Data update period - 10 minutes
  3. Real-time charting
  4. Email notifications once critical value is reached
  5. MQTT Protocol - for low data-rate GSM connections

Tutorial

ARTIK Setup

  1. First go to https://developer.artik.cloud/dashboard and click Sign up to setup an account. The free plan will be good enough for the sake of this example.

First we will create a new "Device type". It's a general container for all information related to a group of devices with the same features. In our case it will define what a temperature sensing unit is. Once tagged and published it can be later reused by other users so let's make sure it's meaningful!

  1. Navigate to the developer dashboard (https://developer.artik.cloud/dashboard) this is where you define the entire "backend" part of your system. You can also check error console, configure APIs, etc. It's a good place to check if something goes wrong.
  2. From "Dashboard" dropdown select "Your device types"
  3. Create a new device type and give it a name - I used "MQTT Temperature Sensor" with "mqtt.temperature" unique name

Now we need to let ARTIK know what kind of data will be sent by our device. We need to create a “manifest” in order to achieve that.

  1. Create new manifest
  2. Create one file named "temp" for example
  3. Set data type to Double (we need a floating point number) with *C as a unit

alt-text

  1. Save and progress to the next step

Our device can't perform any actions, and thus will not be receiving any data from the cloud.

  1. Skip the "actions" step and activate your manifest

Congrats! You just defined your first IoT Device type! We should be able to send some data soon. It should look more or less like this.

alt-text

Now it's time to create a new device of a type we just defined.

  1. Navigate to cloud section (alternatively use the link https://my.artik.cloud/)

This is your "front-end" dashboard, where you can manage all your devices, rules (more about it later) see incoming data on charts and so on and so forth.

  1. Select "Devices" from "My ARTIK Cloud" dropdown and create a new device, choose the device type you just created and give it a fancy name. Something like below (I'm not good in fancy names).

alt-text

  1. Click on "Connect device". It should appear on the list.

alt-text

Well done! Click on the cogwheel in the bottom right corner of your device's tile. There are two important things you need in order to properly connect to your device - Device ID and Device Token. While the first one is constant, just copy it and store for further steps, the token can be regenerated at any time (for example in case our fire alarm's security was compromised). Generate yourself a token and save it for future (I recommend not to write it down on a sticker!).

Node-RED

Node-RED is a great tool from IBM which allows you to quickly prototype any kind of simple communication and data processing, with graphical editor and ready to go blocks (nodes). Since multiple new nodes are being created by the community, it's now a perfect tool for prototyping the IoT systems - you can simply change the way things work without too much effort.

First we install Node-RED on a local machine, mock sensor data and send it to ARTIK via MQTT just to see if things are working properly. There is a great guide on Node-RED's website showing how to do it on any platform - just go here and follow the steps http://nodered.org/docs/getting-started/installation.html.

Once installed, run it with the Node-RED start command and you should get more or less the following output:

Welcome to Node-RED
===================

26 Aug 01:43:45 - [info] Node-RED version: v0.14.6
26 Aug 01:43:45 - [info] Node.js  version: v5.10.1
26 Aug 01:43:45 - [info] Darwin 15.6.0 x64 LE
26 Aug 01:43:45 - [info] Loading palette nodes
26 Aug 01:43:47 - [warn] ------------------------------------------------------
26 Aug 01:43:47 - [warn] [rpi-gpio] Info : Ignoring Raspberry Pi specific node
26 Aug 01:43:47 - [warn] ------------------------------------------------------
26 Aug 01:43:47 - [info] Settings file  : /Users/grzegorz/.node-red/settings.js
26 Aug 01:43:47 - [info] User directory : /Users/grzegorz/.node-red
26 Aug 01:43:47 - [info] Flows file     : /Users/grzegorz/.node-red/start
26 Aug 01:43:47 - [info] Creating new flow file
26 Aug 01:43:47 - [info] Starting flows
26 Aug 01:43:47 - [info] Started flows
26 Aug 01:43:47 - [info] Server now running at http://127.0.0.1:1880/

Open your browser and connect to localhost at port 1880 (default for node red) - type in the address bar: http://127.0.0.1:1880/ .

Now, in the browser we can start building our application. On the left side of the screen you will see a sidebar with multiple nodes divided into subgroups. Lets start with adding an "inject" node. Drag and drop it to the workspace on the right.

alt-text

This little fellow allows us to mock the sensor data. (spoiler - our sensor will be sending us simple, numeric data representing temperature. Nothing fancy). Double click it, change "payload" field to "numeric" and enter some value. Close the modal window, now the block should look more or less like this:

alt-text

Ok, now since we have our data, let's prepare the complete message to be sent via MQTT. As you can read in the ARTIK documentation, the message needs nothing more than a topic and a payload. We need to add our device id (the one we saved for future use from ARTIK) so that ARTIK's MQTT broker knows where to send the data. So we need something like below:

msg.topic = "/v1.1/messages/_your_DEVICE_ID_goes_here_"

In order to prepare our message, we will be using the "Function" Node. Find it in the list of nodes, drag & drop to the working area and double click to edit. The node we just added will receive a msg object from whatever is connected to it's input. We can use the data within the object, modify it and then return the same object to be passed by the "output" of our node. In our case msg.payload will contain the mocked temperature data. We need our "output" payload to look more or less like this:

{ "temp": 19.03 }

So the code of our function node will be as simple as:

var t = msg.payload

msg.topic = "/v1.1/messages/_your_DEVICE_ID_goes_here_"
msg.payload = {
  "temp": t
  }

return msg;

The last thing we need is to send the message we just prepared through MQTT to ARTIK. Let's use MQTT output node for that - it's build into Node-RED by default. Looks like this on the list:

alt-text

Again, drag and drop it, and connect its input to the output of our function node. Now we just need to put in some credentials so that ARTIK's broker allows us to push data and puts it in the right place. Double click the node to edit it and then click on the Pencil button next to "Server" Dropdown menu. Broker's server address is api.artik.cloud and it communicates on port 8883. Also tick the Enable secure (SSL/TLS) connection checkbox (leave TLS Configuration field empty).

alt-text

In the security tab, set the username to your Device ID (the same you used in message header) and password to Device Token (the one you got with device ID in last step of ARTIK's part of the tutorial).

Confirm with the red Add Button and a red Done right after that. Now our flow is ready to go. To make it work just click Deploy button. Your MQTT node should have a green dot and state “Connected” a few seconds after deploy.

alt-text

If the connection is fine you can simply use a blue button on our injection node to force our mocked data to be sent to ARTIK.

alt-text

Do it, then go to artik.cloud @ My Devices and click on your device name. Select the data series that is in our area of interest and see your data being added to the chart every time you inject it. Beautiful!

alt-text alt-text

You can play around with your Node-RED flow, add some more injection nodes with multiple differing values and, optionally, a debug node to see a message being sent if you wish. For testing I ended up with something like this:

alt-text

Which resulted in ARTIK drawing a fancy chart - what a bobby dazzler!

alt-text

OK, one thing left for this part of tutorial - email notifications. It's fairly simple to do this in ARTIK

Email Notifications

As you may remember from the first part of this tutorial, we want our system to send us email alerts when the temperature raises above certain level - for example in an unlikely event of fire. ARTIK has some build in mechanisms - so called rules, which allow you to set conditions and trigger some actions if they are met. Lets see how it works.

Go to https://my.artik.cloud/ and click on "Rules" tile. Then go to the "Create New Rule" screen. Once you're there you will see that a rule contains of a few definitions:

  • Schedule
  • Conditions
  • Actions
  • Description

In our case the rule should work at any time so we can leave the Schedule section as is. Conditions is our main area of interest - this is the section below "Choose device activity to monitor" header. Select your device from the dropdown, then click on "if" input one more time to select which data field is to be monitored - it will show everything you defined in your Device Type manifest. The input will show device and data field selected in a really nice, descriptive way. Mine looks like below.

alt-text

Then move to setting the rule. Let's assume we need the alerts to be triggered every time the temperature value rises above 100 degrees. We need our condition to look exactly like this:

alt-text

Now we can move on to defining an action which should be triggered if the above condition is true. Once you click inside the "Then" input, you will see that it can operate on any data from any device you defined, but in our case we'll use the "Send Email" option. Select this one, give it a title, recipients and body. Mine for example looks like that:

alt-text

Now just give your rule a name and save it. We're done. Voila !

Time for some testing. Set your injection node in Node-RED to mock a value higher than 100. Then force it to send data through our flow (the same way we did it before) and see if you receive an email. I did! You should as well :)

Summary

In this tutorial we set up our first device type and its manifest in ARTIK cloud, created a first device of the type and generated an access token for it. Then we used Node-RED to create a simple flow sending mocked data through MQTT to our ARTIK device. As a last step we set a rule to send a polite email notification every time the temperature raises above 100 Celsius degrees.

Grzegorz Hajdukiewicz avatar
Grzegorz Hajdukiewicz