Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Getting Started with Microsoft IoT Edge

2.60/5 (2 votes)
18 Sep 2017CPOL4 min read 13.4K  
Here's how you can get started with Azure IoT edge. Learn how you can integrated SensorTag with IoT edge running on a Raspberry Pi

Azure IoT edge is your own premise gateway that helps you communicate your data to cloud, while simultaneously filtering the data it is sending.

Often, people don’t understand the difference of IoT edge computing vs fog computing. With IoT edge, processing happens at the edge, whereas in a fog computing architecture, the processing happens at a centralised computing device.

What Will You Learn in this Article?

  • What is IoT Edge, how it differs from Fog
  • How Azure IoT edge works
  • Implement Azure IoT edge with Raspberry Pi and TI sensor Tag

Note: For those of you who aren't familiar with Raspberry Pi, it is a Single board computer that you can use to build your IoT Proof of concepts. I frequently use Raspberry Pi as a Proof of concept gateway to quickly build and test my prototypes. Building on top of Raspberry Pi is surprisingly fast because of the available libraries that have been generously donated by open source community.

How Azure IoT Edge Works?

Let’s first see what components entail Azure IoT edge:

  • IoT devices sending data
  • IoT protocols facilitating data transfer to the IoT gateway
  • IoT Gateway with: Protocol investor, Filter, cloud communication capabilities

Further, it is important to understand this vast ecosystem. It has these functionalities with the following features:

  • Device to cloud communication: Helps you select between device to cloud messages, digital twin’s reported properties, etc.
  • Cloud to device communication: Select direct methods, digital twin’s desired properties, etc.
  • Device to cloud and cloud to device messaging with IoT Hub that: Sends device to cloud messages using IoT hub, reads device messages from the built in end-point
  • Create custom end points and routing rules for devices to cloud messages
  • Create and read IoT hub messages

Using Azure IoT Hub, you can:

  • Get files from a device
  • Manage device identities
  • Control who can access what
  • Synchronize states and configs
  • Schedule a job on multiple devices
  • Message routing, restrict quotas and throttling

This is one of my example builds of Azure IoT Edge.

Left side terminal shows actual data sent from the device and right side terminal displays monitored value using iothub-explorer monitor:

iothub-explorer monitor-events --login "HostName={Your iot hub name}.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey={Your IoT Hub key}"

Image 1Image 2

Protocol Ingestion Module Details in Azure IoT Edge

  1. Module reads data from physical devices
  2. Add a property with a MAC address to the message

The above forms a protocol ingestion module. This module is responsible for receiving data from devices, and pushing it to cloud from the IoT gateway.

In the steps that follow, we will learn how to run IoT edge on Raspberry Pi that collects data from a TI Sensor Tag using BLE protocol.

Installing the Pre-requisites for IoT Edge on Raspberry Pi

  • Open your shell and enter:
    sudo-apt-get update
  • Install all dependencies by:>
    sudo apt-get install curl build-essential libcurl4-openssl-dev
    	git cmake libssl-dev uuid-dev valgrind libglib2.0-dev libtool autoconf

On your Azure Subscription, Do the Following

  • Create an IoT Hub
  • Add two devices to your IoT Hub, and note their ids and keys. Use iothub-explorer tool to do that.

Now, let’s clone the Azure IoT Edge repository to your local machine (Raspberry Pi) by:

git clone https://github.com/Azure/iot-edge.git

Note that even though this is Microsoft’s own tech, I faced a huge amount of problems dealing with Windows, and the same worked seamlessly with Linux.

If you have never used Raspberry Pi before, I would recommend going through the following tutorial first. Make sure that Raspbian is properly installed.

Once it is installed, you can easily SSH into your Raspberry Pi.

Integrating BLE Sensor Tag with Raspberry Pi

BlueZ stack is one of the most commonly used libraries and is required to get TI Sensor tag work with your Raspberry Pi.

Open up console on your Raspberry Pi and enter the following commands:

sudo apt-get update<br /> sudo apt-get install bluetooth blues-tools 
build-essential autoconf glib2.0 libglib2.0-dev libdbus-1-dev libudev-dev libical-dev libreadline—dev

wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.37.tar.xz

Now, go to the directory you just created and configure BlueZ code:

cd blues-5.37 ./configure —disable-dev —disable-systems —enable-experimental

make sudo make install

Now, change systems service config to for BLE:

ExecStart=/usr/local/libexec/bluetooth/bluetoothd -E

Now, you can connect your Raspberry pi with Sensor Tag.

In the previous notes, I wrote about adding your devices to your IoT Hub. Here, you can add your BLE sensor Tag over here, let’s call it My_sensor_tag.

Configuring IoT Edge on your Raspberry Pi

Installing dependencies:

sudo apt-get install cmake uuid-dev curl libcurl4-openssl-dev libssl-dev

We have already cloned IoT Edge and all required dependencies in our previous notes.

Let’s build the repo:

cd ~/iot-edge ./tools/build.sh  --disable-native-remote-modules

Configuring Each IoT Edge Module to Run BLE on your Raspberry Pi 3

Configuring your logger located in /home/pi/iot-edge/

The module should look something like this:

JavaScript
{
 "name": "Logger",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path" : "build/modules/logger/liblogger.so"
   }
 },
 "args":
 {
   "filename": "<</path/to/log-file.log>>"
 }
}

Configuration of the BLE module:

JavaScript
{
 "name": "SensorTag",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/ble/libble.so"
   }
 },
 "args": {
   "controller_index": 0,
   "device_mac_address": "<<AA:BB:CC:DD:EE:FF>>",
   "instructions": [
     {
       "type": "read_once",
       "characteristic_uuid": "00002A24-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A25-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A26-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A27-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A28-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "read_once",
       "characteristic_uuid": "00002A29-0000-1000-8000-00805F9B34FB"
     },
     {
       "type": "write_at_init",
       "characteristic_uuid": "F000AA02-0451-4000-B000-000000000000",
       "data": "AQ=="
     },
     {
       "type": "read_periodic",
       "characteristic_uuid": "F000AA01-0451-4000-B000-000000000000",
       "interval_in_ms": 1000
     },
     {
       "type": "write_at_exit",
       "characteristic_uuid": "F000AA02-0451-4000-B000-000000000000",
       "data": "AA=="
     }
   ]
 }
}

IoT Hub Module configuration:

JavaScript
{
 "name": "IoTHub",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/iothub/libiothub.so"
   }
 },
 "args": {
   "IoTHubName": "<<Azure IoT Hub Name>>",
   "IoTHubSuffix": "<<Azure IoT Hub Suffix>>",
   "Transport" : "amqp"
 }
}

Identity management configuration:

JavaScript
{
 "name": "mapping",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/identitymap/libidentity_map.so"
   }
 },
 "args": [
   {
     "macAddress": "<<AA:BB:CC:DD:EE:FF>>",
     "deviceId": "<<Azure IoT Hub Device ID>>",
     "deviceKey": "<<Azure IoT Hub Device Key>>"
   }
 ]
}

BLE printer module configuration:

JavaScript
{
 "name": "BLE Printer",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/samples/ble_gateway/ble_printer/libble_printer.so"
   }
 },
 "args": null
}

BLEC2D Module config:

JavaScript
{
 "name": "BLEC2D",
 "loader": {
   "name" : "native",
   "entrypoint" : {
     "module.path": "build/modules/ble/libble_c2d.so"
   }
 },
 "args": null
}

Routing Module config should look something like:

JavaScript
"links" : [
   {"source" : "*", "sink" : "Logger" },
   {"source" : "SensorTag", "sink" : "mapping" },
   {"source" : "SensorTag", "sink" : "BLE Printer" },
   {"source" : "mapping", "sink" : "IoTHub" },
   {"source" : "IoTHub", "sink" : "mapping" },
   {"source" : "mapping", "sink" : "BLEC2D" },
   {"source" : "BLEC2D", "sink" : "SensorTag"}
]

Now, in order to run it, pass the path to the JSON config file in form of a paramter to ble_gateway binary. If you are using gateway_sample.json, run the following on your Raspberry Pi:

./build/samples/ble_gateway/ble_gateway./samples/ble_gateway/src/gateway_sample.json

You can monitor device to cloud messages using the following command:

iothub-explorer monitor-events --login "HostName={Your iot hub name}.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey={Your IoT Hub key}"

There you go! Your first IoT Edge gateway implementation using Azure IoT edge!

Image 3

I am exploring a few other data models around IoT Edge as well, in case you have any perspectives, I would love to learn from you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)