JTA - Workshop

advertisement
Manage devices using IoT Foundation and MQTT protocol in IBM Bluemix.
Methods to connect devices using Internet of Things (IoT) Foundation, MQTT protocol and NodeRED flow editor. This
facilitates collection of real-time data from physical and virtual devices, enabling us to react in real-time along with
analytics on historian data.
References
https://internetofthings.ibmcloud.com/
https://quickstart.internetofthings.ibmcloud.com/#/
What is IoT?
Internet of Things Foundation is a fully managed, cloud-hosted service that makes it simple to derive value
from Internet of Things (IoT) devices.
IoT API Interfaces



Real-Time APIs:
o Device API - Lets a device connect and send real-time data, and lets it receive commands
o Application API - Lets you subscribe to receive live streams of data from devices, and lets you send
commands to devices
Management APIs - Lets you register or deregister devices, and view details of your IoTF organization
Historical data API - Lets you query the historical data store to retrieve data sent from devices in the past
Internet of Things Foundation in IBM Bluemix
Internet of Things provides ability to collect and store data from a device in the cloud. This provides the ability
to gain insight on the physical world and drive actions to connected devices.
How to create an IoT application:






Select the Internet of Things Foundation boilerplate.
The next page shows details for the application. On the right side of the page, provide a name for the
application (ex. iotf-starter). This will produce a default route of http://iotf-starter.mybluemix.net for the
application. Click Create.
After creating the application, the application dashboard will load and your application will be
automatically started.
Once your application has finished staging, add the Internet of Things service: from the application
dashboard, click Add Service, and then select the Internet of Things Foundation (near the
bottom). NOTE: If you already have an IoT service created, click Bind Service instead of Add Service.
On the next screen, give the service a name (ex. iot-starter).
Next, click on the Internet of Things service in the application dashboard. This will open the
configuration page for the service. From here, you can click the 'launch' button to open the Internet of
Things Foundation dashboard.
MQTT Flow with devices registered with IoT
Why use MQTT protocol useful in collecting IoT data?



It is an open standard protocol
It is lightweight protocol, sending data in small packets (kb)
It is a Publish and Subscribe protocol and offers reliable Quality of Service over unreliable networks
REGISTER YOUR DEVICE
Organizations: When you register with the Internet of Things Foundation, you are given an organization ID;
this is a unique 6 character identifier for your account. Organizations ensure your data is only accessible from
your devices and applications.
Devices: A device can be anything that is generating sensor data which can be mined for real time analysis
as well as historian data available in time series database. A Device needs to register with the IoT Foundation.
The IoT Foundation returns an Authentication Token as a unique identifier for the device.
IBM Internet of Things is powered by IBM MessageSight technology, it allows:
o
o
o
o
o
Secure Device Registration
Scalable Device Registration
Historian Data in Time Series database
Visual Wiring
PAYG SaaS Pricing
IBM Internet of Things allows you to easily compose IoT solutions:
o
o
o
o
o
o
Select from a growing list of device recipes
Simply connect & “recognize” device types
Visualize real-time data stream
Visually define logic flows
Mix with other services in Bluemix to create applications
Connect to the device
Use MQTT Client Connection







Connect MQTT clients to org_id.messaging.internetofthings.ibmcloud.
A Device must authenticate using a client ID in the following format: d:org_id:device_type:device_id
When connecting to the QuickStart service no authentication (or registration) is required
Be registered in an organization and establish an MQTT connection
Subscribe to: iot-2/cmd/<cmd-type>/fmt/<format-id>
You should implement specific code to take action on receipt of the command.
You should have this code publish an event containing the outcome of the action.
o Where <cmd-type> is used to distinguish between different types of command. When a device
receives an MQTT message, this part of the topic name is used to resolve the command type.
o <format-id> is used to distinguish different payload encodings.
o The <cmd-type> and <format-id> values can be up to 32 characters in length and contain the
characters A-Z a-z 0-9 ‘–’ ‘_’ and ‘.’
o <cmd-type> and <format-id> can also be wild carded with the MQTT ‘+’ wildcard, that matches
exactly 1 level in topic tree. For example, subscribing to iot-2/cmd/+/fmt/+ allows a device to
subscribe to any command sent to it in any format.
Security between the devices
Secure messaging patterns are baked in. Once authenticated, devices are only authorized to publish and
subscribe to a restricted topic space:
o /iot-2/evt/+/fmt/+
o /iot-2/cmd/+
All devices work with the same topic space. The authentication credentials provided by the client connecting
dictate to which device this topic space will be scoped by the IOT Foundation. This prevents devices from
being able to impersonate another device.
NOTE: DEVICES USING QUICKSTART CONNECTION CANNOT SUBSCRIBE
What is Node-Red?




Node-RED in its simplest form is an open source visual editor for wiring the internet of things created by
IBM.
Node-RED provides a browser-based environment for visually wiring together hardware devices, APIs
and online services in new and interesting ways
Node-RED is capable of running on low cost hardware such as the IoT Devices (ex: Raspberry PI,
Arduino, etc...) in the cloud and aims to enable developers to "focus on the task at hand and not worry
about the plumbing".
Node-RED's user interface offers multiple workspaces to edit different flows in parallel, import of flows
from and export to the clipboard and a built-in library. Flows are represented in JSON to ease
serialization and exchange.
Creating an MQTT subscriber with Node-RED
Once you are in your app, click on “Go to your Node-RED flow editor” button. This will take you to the NodeRED flow editor environment where you can start creating your cloud-hosted app. We can do so by simply
dragging in the MQTT input node into the sandbox area. Double-click on the MQTT node that you just dragged
in to configure it. We need to do 2 things:








Add the MQTT broker information
Next, we need to tell the MQTT node which topic to subscribe to.
Now, our MQTT node is properly configured!
To start seeing data from our LaunchPad, drag in the DEBUG node in the Node-RED palette.
Now, wire up the MQTT node to the debug node
Once connected, press the “DEPLOY” button on the top-right corner of Node-RED.
Next, click on the “Debug” tab in the side-bar to the right.
At this point, you should start to see sensor values from your LaunchPad streaming in!
In Node RED flow editors, available node types cover a wide range of functionality and are presented in the
palette under different categories depending on whether they offer inputs, outputs, functions or specific
integrations, for example:






Network – HTTP, TCP, UDP, MQTT, WebSocket
Parsers – CSV, JSON, XML
Transformations – JavaScript Functions, Mustache Templates
Social – Twitter, Twillio, Email, IRC, RSS, XMPP
Storage – Filesystem, MongoDB, MySQL, PostgreSQL, Redis
Analysis – Sentiment, Statistics
Following the above steps, you will have an application reading sensor data from devices, acting on the sensor
data in real time as well as storing that data in time series database for future analytics.
Download