Dataworks Blog

    IoT: A STEP BY STEP GUIDE ON HOW TO CONNECT DEVICES TO THE CLOUD

  • Dataworks as a company had been involved in a number of discussions in recent times with our customer base regarding IoT (Internet of Things) and collecting and streaming data to the cloud. Normally our input into the development of these IoT solutions would stop at the source device and as a consequence we were only conceptually aware of what is involved at the device level. We decided to change that by doing an internal proof of concept (POC) where we would take an off the shelf embedded system and get it to stream its sensor data to the Azure cloud. The aim was to give us a more rounded understanding of the entire IoT data chain. This article describes how we did this and what we learned from this exercise.

    What is IoT

    Anyone who has got this far into the article will probably already be very familiar with the term IoT. Even still, we wanted to include a definition in this blog which best described IoT from the perspective of our POC and found the following which we believe does this quite well.

    “IoT is a network of physical devices, containing embedded systems with network connectivity which enable these devices to collect and exchange data”

    POC Data Sources

    We looked for some relevant data sources to use in our POC and settled on environmental data (temperature and humidity) as one of our source types. This is something that is often of interest in a manufacturing context where process engineering look to correlate environmental conditions with production metrics such as yield. We also wanted to use some faster moving data to show the real time capabilities of Azure. Accelerometer and Gyroscopic data were included in our source types for that reason.

    Device Selection

    Having had some experience of working with ST Microcontrollers in the past it seemed like the natural place to start our search for a suitable device for our POC. ST offered a solution which was exactly what we required. The P-Nucleo-CLD1 (shown below) is an evaluation kit which, as well as providing the necessary hardware (temperature and humidity sensors, accelerometer and gyroscope along with WiFi), came with full source code for the IoT solution. For the remainder of this blog we will refer to this as the ‘device’ for simplicity.

    Cloud Solution Provider Selection

    The selection of Microsoft Azure Cloud Solution was a straight forward decision for a cloud solution provider, given Dataworks long history of developing solutions using Microsoft technologies. Azure’s IoT Hub is a fully managed service that enables reliable and secure bidirectional communications between millions of IoT devices and a solution back end and was the final piece of our POC puzzle.

    Microsoft Azure Applications

    As well as the IoT hub, which would manage our IoT device, we wanted to explore other Azure applications which would showcase the kind of things you could do with a device which steamed data to the cloud.

    Azures Stream Analytics was the first application that we wanted to look at. Stream Analytics performs actions on real-time data, such as updating an external database with processed information. The significance of this application deserves emphasis. Traditionally a solution for monitoring a data stream might involve taking all incoming data and writing this data to a repository such as a database. Periodic queries would then be run on the data repository to look for a specific type of event. Obviously an application like Stream Analytics, which acts on the incoming data stream has huge advantages over the traditional solution in terms of storage and response time and is a vital tool in any IoT solution.

    Azure provides a number of cloud data storage options. For our POC we chose a SQL server database as the destination for our streaming data.

    Time Series Insights was the final Azure application that we felt required was worth looking at in an IoT context. It is used to visualise the data being streamed from our IoT device and gave a tangible demonstration of the power of IoT.

    Linking our Device to Azure IoT Hub

    With all the hardware and software selected the next step was to integrate the various pieces into our IoT solution. To begin, we setup an Azure cloud account and created the IoT Hub which would be the destination for our streaming data and would also be the source to which our Azure storage and analytics applications would connect. The screenshot below shows the IoT Hub setup screen and the highlighted section in the bottom right hand corner contains the connection string which uniquely identifies our IoT hub.

    Microsoft conveniently provide a desktop application called Device Explorer which allows you to setup your device to connect to your Azure IoT hub. It also provides some diagnostic and interrogation tools for use with your configured device. The screenshot below shows the configuration tab of Device Explorer where the IoT Hub connection string from above is entered.

    Once this is entered your specific device is created under the management tab (screenshot below) and the ‘Copy connection string for selected device’ option is selected which provides the string which will be used by your device to make the connection by it to the IoT hub.

    Device Setup

    The source code provided with our device from ST does not have any knowledge of our specific IoT hub so this information needs to be edited into this source and the demo project needs to be rebuilt with the connection string from Device Explorer. We used the free System Workbench compiler from ST to perform the build and the free programming software STM32 ST-LINK to program the microcontroller in the device.

    The screenshot below shows the sections in the include file where our specific device connection string was added and the setup of our specific wireless network which the device will use to connect to Azure.

    The ST-LINK programming utility (screenshot below) takes the bin file created by System Workbench and programs the microcontroller via the USB port on the board.

     

    Testing the device connection to your Azure IoT Hub

    In most embedded systems, where a display or keyboard is not provided as part of the solution, an alternative Input/Output method is required. When we connected our device to our PC via USB it automatically sets up a virtual serial communication port through which board diagnostic information could be displayed. We used the serial port terminal application Tera Term to connect to our device. Configuring Tera Term with the following serial port properties allows the device to output its setup and diagnostic information and for the user to confirm that the connection to your Azure IoT hub has been successfully made.

    The following screen shots show the information output via the virtual serial port after device reset. The first shows the results of internal tests on the device hardware and software.

    The screenshot below shows the connection being made to the default wireless network we programmed into the device. You can see there is also a method to alter this connection via a button on the device and data input via Tera Term.

    The final screenshot below shows the IoT hub connection being made and a message being output every time a sample is sent to the IoT Hub.

    Device Explorer Commands

    Once our device is running and successfully connected to our IoT Hub, the Device Explorer application can be used to perform some interesting tests and setup on the device. It is important to understand that any interaction with the device using Device Explorer at this point is being done via its connection to the Azure IoT Hub. The screen shot below shows the ‘Message to Device’ tab which allows messages to be sent to the IoT device. The message to be sent is setup with the format shown in the ‘Message’ entry box shown in the screenshot. The supported messages in our device included ‘Led on’ and ‘Led off’ which toggles an LED on our device. Obviously this could be any physical device output which could be altered via the Azure  IoT hub. Also supported is the play and pause option which stops and starts the device streaming data to Azure.

    The screen shot of the corresponding Tera Term output is shown below. You can see confirmation of the received command, that was executed via Device Explorer, on the device via the terminal window.

    Altering IoT ‘Device Twin’ file via Device Explorer

    Device Twins are JSON documents that store device state information including metadata, configurations, and conditions. Azure IoT Hub maintains a device twin for each device that you connect to IoT Hub. Device specific Metadata information such as the physical location of the device is normally held in the Device Twin as well as configuration information. The Device Explorer application allows us to alter this persistent Device Twin file and in the example shown below we are altered the device sensor sample rate (DesiredTelemetryInterval) using this feature.

    Once the command is received, a confirmation message is shown on Tera Term application and the device will continue to sample at this rate even after a power cycle as it effectively is initialised to this value at start up by the Device Twin File.

    Device Explorer ‘Call Method’ feature

    Another useful feature of the Device Explorer application is the ‘Call Method on Device’ tab. This allows the user to call supported methods or features on the Device. In the example below you can see the ‘Reboot’ method being called and the Tera Term application confirming the method call and performing a  restart on the device which can be seen by the start-up information being displayed.

     

    The device also supports the Firmware Update method where the ‘Payload’ of the message has a URL containing the new binary file that will be programmed into the microcontroller of the device. This solves a long standing problem with embedded systems where any issues with the existing firmware can lead to expense recalls if the device cannot be programmed in the field. Using this method individual devices can be programmed in situ with whatever firmware update is required by that specific device.

    Device Explorer Data Monitor

    The final useful feature we will look at in the Device Explorer Application is the Data Monitor. The screenshot below shows the Device Explorer ‘Data’ tab with its ‘Event Hub Data’ Dialog outputting a string containing the data payload every time a sample is received by the IoT hub from the device. In our device this data consists of the Temperature, Humidity, Accelerometer and Gyroscopic information which we are streaming to Azure.

     

    Azure Setup

    Having successfully setup our IoT Hub in Azure and having altered and configured our device to stream sensor data to it, we were now in a position to setup the various Azure applications to consume the data. The screen shot below shows our Azure customizable dashboard which gives an overview of the various applications we configured.

    Stream Analytics Setup

    We setup Stream Analytics to pipe our device sensor data to a cloud based SQL Server database. There were three distinct  parts to this setup namely Input , Query and Output. This is shown in the highlighted section of the screenshot below and is known as the ‘Job Topology’ in Azure.

    The input in our case was the IoT hub which was setup to receive IoT data from our device and its configuration can be seen in the screen shot below.

    The output in this example was a SQL server instance that we had created in the Azure cloud. The configuration of this Output connection can be seen in the screenshot below where the SQL Server database and table are configured and the credentials for access are entered.

    Finally the Query section is configured where the decision as to what data to log to what field in the table is  built using a Stream Analytics Query. In the example shown below we simply pass all the data from the IoT device record into the table but we could have built more complex queries to only write specific types or combinations of data , or data which meets certain criteria into the destination database. This , as we alluded to earlier in the blog, is the real power of Stream Analytics.

    The screen shot below shows a SQL query of the destination table in our Azure cloud database. You can see a an individual timestamped record for each sample taken of our Devices Sensors.

    There are other options for Output data storage, as shown in the screenshot below, including data blobs, files and tables.

    Azure Time Series Insights

    Another interesting way of digesting and displaying device data is the Time Series Insights application. At time of writing this was a new feature to Azure and was created to allow visualisation of time series data such as the type of data being collected from our device. The screenshot below shows the preview screen which shows some real-time metrics on device connection and collection.

    In order to setup Time Series Insights you must configure a suitable source which again in our case was our IoT Hub. You can see the  configured IoT Hub source in the Time Series Insights configuration screen shot below.

    Clicking on the ‘Go to Environment’ button in the preview screen takes you to a setup screen where graphical displays can be created to show the ingested data from the device. This is connected directly to the streaming data and not accessing the stored database information. The first screen shot shows our Temperature and Humidity data being graphed together while the second shows the three axis of our accelerometer reacting to a bang of the table on which the device was placed.

     

    Iconics IoTWorX

    Finally, we thought we would conclude this blog by sharing some information on an exciting new product from Iconics, IoTWorX, which was released in 2017.

    Iconics have been Dataworks strategic partner for over 10 years,  providing us with the tools to create sophisticated applications to visualize , analyse and collect data from multiple data source types and delivering these applications securely to our customers on any device.

    Traditionally Iconics products communicate with plant equipment open standard such as OPC. Iconics new product, IoTWorX, provides a bridge between plant infrastructure and the Azure cloud. This effectively allows any piece of connected equipment in your plant become an IoT device similar to the one described in this blog. As well as exposing your plant data to cloud based Iconics Visualisation Solutions,  IoTWorX can also unleash the power of Azure on your live plant data and deliver your IoT solution with no significant hardware investment.

     

     

    Conclusion

    Hopefully we have succeeded in our goal of demystifying the process of connecting a device to the Azure Cloud. We also hope that by showing you a little of what can be done in Azure, it may help you envisage how an IoT based solution can help you solve your particular business challenge.

    Further Information

    Dataworks have extensive experience working with companies operating in the Medical Device and Pharmaceutical sectors to help them improve their business processes and increase efficenices with our innovative solutions. If you would like to discuss how our IoT knowledge and capabilities can benefit your business Contact Us today.

     

     

  • Back to Blogs