July 8, 2015 | Written by: BENJAMIN RONDEAU
Share this post:
If you want to learn how to connect an Internet of Things (IoT) device to the cloud and utilize the wealth of services available in today’s cloud marketplace, a couple of searches is all it takes. But what about connecting 100K+ devices to the cloud? How do you handle that level of traffic and scale your services to meet that demand?
What does the “Internet of Things” mean?
Simply put, the Internet of Things (IoT) is an ecosystem of connected devices: things like “smart” traditionally-unconnected objects (think desk lamp, washing machine), simple sensors (temperature, motion), and traditional internet-facing electronic devices. With this ecosystem comes new and exciting use cases: objects from everyday life like a carbon monoxide sensor can send you a text message through the internet if dangerous levels of CO are detected, helping keep you and your family safe. For a more formal definition, see the IBM Internet of Things Foundation.
Resources on this topic are slim and many IoT service providers will not handle this well. This leaves a developer digging through API documentation and crossing their fingers that the configuration they set up will work.
That is the challenge this blog series will solve: getting a single device connected to the cloud, then scaling it to thousands of users with confidence. In this first installment, I’ll cover the basics of the Internet of Things (IoT) Foundation service on Bluemix and how to connect a device to it.
Don’t have an IoT device? No problem! A simple ready-to-deploy smartphone web app is provided and will be used throughout this series to demonstrate how to address scalability without getting mired in details. In subsequent installments, you will learn about useful services to expand the capabilities of your Bluemix-hosted IoT application and how to scale it to handle thousands of simultaneously connected devices.
Let’s get started!
Quick overview of IBM’s IoT Service
Before diving straight into the how-to part of this entry, some background on the Internet of Things (IoT) Foundation service is necessary to understand how it all works.
IoT Foundation is the hub that connects your IoT devices to the cloud. It serves as an abstraction layer to simplify communication with devices and manage them in one place. The diagram below visualizes it well:
For more details, see IBM Internet of Things Foundation
Without this critical service, devices would need individual configuration and management, leading to a maintenance nightmare and making any sort of scale nearly impossible. IoT Foundation also handles critical features like device registration and authentication, secure and fast data transmission using the MQTT protocol with TLS, logging of device data with time-series historical APIs and much more. The IoT Foundation service will be the power behind scaling your IoT application to thousands of devices.
Deploy and run the IoT Phone sample application
For this entire blog series, our IoT device will be a smartphone running a web application named IoT Phone. This app will access smartphone acceleration, orientation, and location; think of it as a portable motion and movement tracker.
Note: If you have an IoT device of your own and want to connect it to Bluemix through the IoT Foundation, check out the Quick Start Recipes.
To get started with the IoT Foundation service, click on the ‘Deploy to Bluemix’ button below:
Once the deployment page loads, you can login to your Bluemix account (or create a new one) to host this application. If you want to review the code, see to the IOT-HTML5-Phone-Application on GitHub.
After logging in, name your application and select the region, organization and space you want your application to deploy to (it’s okay to leave everything as default if you’d like). Click ‘Deploy’ and watch your application spin-up. Behind the scenes, Bluemix is deploying the application code in a Node.js runtime and creating a new Cloudant database and IoT Foundation service organization.
Once your application is successfully deployed to Bluemix, you will see this confirmation screen:
Click on the ‘View Your App’ button to open the custom URL created for this application. Grab a smartphone and open this URL in a mobile web browser (an accelerometer and motion data from a desktop browser isn’t very, uh… exciting). But don’t worry about the particulars of the smartphone app’s display, it is intentionally simple so we can focus on the more interesting problem—scaling your IoT application.
Once opened on a mobile browser, you will see a dialog box asking for a device ID. Name your device something unique (ex. – “My First Device”) and click ‘OK’. A device with this name will be created in IoT Foundation.
After entering a device ID, you will see data from your phone being captured and published at a fast rate (10 msg/sec) to IoT Foundation:
Well done – your IoT Application is now up and running! Your device is interfacing with the IoT Foundation service and publishing the sensor data generated by your phone’s movement. And if desired, you can add tens of thousands of devices to your IoT Foundation organization without any extra configuration; the IBM Internet of Things Foundation service can scale right out of the box.
Quick tour of your deployed IoT application
At this point you have deployed an application in Bluemix, created an IoT Foundation organization and added a device to the organization. To dive deeper into the architecture of what an application and service are in Bluemix, please visit our Bluemix developer site and the Internet of Things site.
Now let’s explore the application and services you just created. Open your Bluemix Dashboard and click on the application you just created. It should look like this tile, but with the unique name you gave your application:
Once the application’s homepage opens, you will see the vital information about your application and the following components that makeup your app:
- Node.js Runtime. Our ‘IoT device’ is a web app running on a smartphone, so we need a web server to host the application code. The Node.js runtime hosts this app and also provides an API for the device to register itself with IoT Foundation
- Cloudant NoSQL DB. The Node.js runtime uses this database to store and retrieve IoT Foundation device credentials. This database is managed, secured and scaled to meet your needs without you lifting a finger
- Internet of Things Foundation. This is the hub for your IoT device connectivity. IoT Foundation is where you can setup and manage your devices as well as access realtime and historical data APIs.
Viewing your IoT application’s events and data
To view the data created by your smartphone’s browser, click on the ‘Internet of Things’ service tile:
This will open the transition page with links to the organization dashboard, and documentation for extending and building your IoT application. Click on the ‘Launch Dashboard’ button to open your organization dashboard page for the service:
Once the dashboard loads, click on the ‘Devices’ link and you will see a listing of the devices that have connected to the service (in this case it will be the name(s) entered in the browser window prompt when visiting the application’s URL on a smartphone). Clicking on the plus symbol (+) on an device’s row will show the last 10 inbound events:
Clicking on any of the events will display a modal dialog with the sensor data payload for that event:
Once the data is in IoT Foundation, it’s easy to access from other Bluemix applications and services with the realtime and historian APIs that IoT Foundation provides… but we’ll save that for part 2! In the meantime, think about the data we are collecting from the smartphone and what could be done with it. Motion sensor for a door? Activity monitor? Location tracker?
This wraps up the first installment of this series; we explored the IoT Foundation service on Bluemix and successfully connected a device. In future entries, we will build on this basic application to add third party services (like analytics, DevOps, etc.) and demonstrate how to scale them to thousands of users. Until then, happy hacking!