Getting started with IoT in Real time

6 min read Michael Carroll on May 10, 2016

In the previous article, I broke IoT down to simpler components like software, hardware and network, and went on to categorize the pain points in IoT. Moving forward, I will show you how to overcome these challenges when building a


Whether you are building a home automation app, an asset tracking app or a wearable, you might face the same pain points as me. It could be to remotely monitor and control devices in the wild, or tracking them as they move and charting the values they send to your dashboard in real time. This article will walk through all of them and show how a Real-time Communication Platform like PubNub will help solve the above challenges.

Choosing a Microcontroller Board

There are so many microcontroller boards, each one better than the other. Which one do I pick?


After a little (actually a lot) of digging, I realized that this depends on what you are trying to build.

Use Case Requirements Boards
Education Visual OS
Online Resources
Affordable Sandbox
Raspberry Pi
Home Automation Central Processing device
Remote Sensing Nodes
Great Connectivity
Low Power Draw
Wearables Board Size
Battery Powered
Computing Power


I wanted to build an application that senses the environment and charts out values in real time. This is an integral part of home automation, medical wearables or even Industrial IoT applications.

My IoT Application specifications:

  1. Monitor the hardware devices and the values they sense from anywhere in the world
  2. Visualize the sensor data in a browser as a real-time chart
  3. Get notified if the sensor values exceed a threshold

For this project, I chose a Pi, the Arduino Uno and an Atmel microcontroller. I purposely chose disparate devices running different SDKs to communicate with a browser to show how they can all work together.

IoT prototype setup

IoT prototype setup

The Atmel microcontroller, SAMD21 is connected to a wifi hotspot, the Arduino locally connected to my laptop, and the Pi connected through ethernet. This is very typical of an application where different devices can be connected to the internet in various ways, but the aim is still to communicate in real time. I tried to emulate a real-life scenario as much as possible.

Real-time Bi-Directional Device Communication

Irrespective of whether I have a few devices to control, or where I am in the world, the messages between the devices have to be delivered instantly. How can I achieve that real-time bi-directional communication between my devices?

Moving from "request/response" architecture to "always-on" Data Streams

Moving from “request/response” architecture to “always-on” Data Streams


Either you can take an open source solution based on WebSockets like Socket.IO and build a real-time data stream network over that, or you can use a pre-built solution that takes care of this for you. By taking the latter route, you free up some time for yourself for actual application development than maintaining a real-time network. If you are still undecided on which to use, this blog will make it clearer on whether to build it yourself or not.

By using PubNub’s SDKs in your application code, you are instantly connected to a global network, and every message sent and received is in real time. The PubNub network will take care of message routing in real time, with no effort on your side. It auto-switches the protocol and removes socket-level complexities making it easy for developers to build apps that can scale easily by leveraging the highly available PubNub network.

Disparate Devices, Disparate Platforms

Each of these 4 devices have different platforms to code on. How will they understand each other?

Raspberry Pi: Python
Arduino: Arduino programming language based on C/C++
Browser: JavaScript

Real-time Communication for Mobile, Web and IoT

Real-time Communication for Mobile, Web and IoT


A person speaking in Japanese will not understand someone speaking in English. As simple as that. Devices and microcontrollers are the same. They need to agree upon a method to communicate, and there are several protocols like MQTT, AllJoyn, HTTP Long Polling, WebSockets etc. to do so. Choosing which protocol to use when is like going down a rabbit hole. Each one has its own pros and cons. Here are my findings:

  1. PubNub makes that decision easier. First of all, PubNub provides SDKs, with very easy to use APIs. The APIs abstract all of the lower level details of opening and maintaining a socket connection between devices to send data. So I can use the PubNub Python, C and JS libraries for my different devices.
  2. PubNub has used a variety of protocols over time, like WebSockets, MQTT, COMET, BOSH, long polling and others. The bottom line is that PubNub will use the best protocol to get connectivity through any environment. As a developer, I did not have to worry about which protocol to use when, PubNub took care of this for me. Speaking the same protocol, these devices can now understand and communicate with each other.
  3. The PubNub APIs are the same irrespective of the SDK – you use publish() to send data and subscribe() to receive data. This uniformity is helpful when you are dealing with a couple or more SDKs.
// say hello!
  channel : 'my_channel',
  message : 'hello!'
  channel : 'my_channel',
  message : function(m) {console.log(m)}

Remote Device Monitoring

How do I know the state of my devices remotely at every instant?


One key criteria when I deploy my devices in different places is to remotely monitor them. I can’t physically check on each of them, and a central dashboard telling me the states of all the devices will be helpful. The PubNub presence() API  solves this for me, notifying me when devices join, leave or timeout from the network. This is similar Skype, where a green dot indicates a contact is online, and gray indicates offline.

  channel: "my_channel",
  presence: function(m) {console.log(m)},
  message: function(m) {console.log(m)}

With the here_now() call, I can see which devices are currently on a particular channel.

// Get List of Occupants and Occupancy Count.
  channel : 'my_channel',
  callback : function(m){console.log(m)}

Real-time Data Visualization

How can I easily visualize the data streaming from devices through charts and graphs?


Most IoT applications need a dashboard to go with, a partner application that streams the data visually. This can be a health app displaying blood pressure, heart rate, number of steps walked or a social app that maps where your kid is in real time. What ever the application is, an easy way to hook up your devices to the dashboard is crucial. PubNub Eon does exactly that – lets you build live updating charts and maps. It is built over C3.js and provides flexibility to choose either spline, bar, pie or guage chart. Best of all it is plug-and-play and you can stream data in minutes. With the new chart builder, you can hook up the data from all the devices to a real-time chart.

Plotting Sensor Data in Real time

Plotting Sensor Data in Real time

So if you were planning to build a green house monitoring system with sensors measuring temperature and humidity, you can use this technology. Or you could be building a medical wearable that relays information about your body to your mobile device. The same application can be modified to control different devices in your house. With all these common pain points covered, I hope you find it easy to work on your IoT prototype. Irrespective of the board you use, you can use the corresponding PubNub SDK and benefit from all the features.

The code for this application is in the iot-101 repository.  In my next tutorial, I will be covering security and how to make the same application much smarter and more secure. Stay tuned for everything from network to home to data and device security.