IN THIS ARTICLE
Subscribe to Our Newsletter
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 realtime IoT application.
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 realtime. This article will walk through all of them and show how a Data Stream Network 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?
Solution:
After a little (actually a lot) 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 Arduino |
Home Automation | Central Processing device Remote Sensing Nodes Great Connectivity Low Power Draw | ESP8266 Photon WiPy |
Wearables | Board Size Battery Powered Computing Power | Flora Gemma Xadow-Edison |
I wanted to build an application that senses the environment and charts out values in realtime. This is an integral part of home automation, medical wearables or even Industrial IoT applications.
My IoT Application specifications:
- Monitor the hardware devices and the values they sense from anywhere in the world
- Visualize the sensor data in a browser as a realtime chart
- 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.
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 realtime. I tried to emulate a real-life scenario as much as possible.
Realtime 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 realtime bi-directional communication between my devices?
Solution:
Either you can take an open source solution based on WebSockets like Socket.IO and build a realtime 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 realtime 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 realtime. The PubNub network will take care of message routing in realtime, 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++
SAMD21: C
Browser: JavaScript
Solution:
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:
- 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.
- 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.
- The PubNub APIs are the same irrespective of the SDK – you use
publish()
to send data andsubscribe()
to receive data. This uniformity is helpful when you are dealing with a couple or more SDKs.
// say hello! pubnub.publish({ channel : 'my_channel', message : 'hello!' }); pubnub.subscribe({ 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?
Solution:
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.
pubnub.subscribe({ 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. pubnub.here_now({ channel : 'my_channel', callback : function(m){console.log(m)} });
Realtime Data Visualization
How can I easily visualize the data streaming from devices through charts and graphs?
Solution:
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 realtime. 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 realtime chart.
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.