Microsoft Azure IoT Client Implementation

Connecting IoT Devices to Azure IoT Hub

Configuring Azure cloud platform

Azure IoT Hub

Creating IoT hub

You need to create an IoT hub to connect your devices with the Azure cloud platform.

  1. Sign in to the Azure portal
  2. Select New > Internet of Things > IoT Hub CreateIoTHub

Device registry (device provisioning)

  1. Choose IoT Devices under your created IoT Hub.
  2. On top choose add to registry new device.
  3. Give deviceId and choose your authentication type (in this example we take a symmetric key with auto generate keys) deviceRegistry

Configuring publish data from device

General

There are four languages we can use to implement the client code;

  • .NET
  • Java
  • Node.js
  • Python

With IP-capable devices you can use the protocols AMQP, MQTT and HTTPS (which are standard implemented in the SDK of Azure)

Example of client code

This example (Based on MQTT Implementation) will push data from a RaspberryPi3 with a GrovePi configured Light Sensor to our IoT hub. Below you can see the configuration of your client code by using the SDK’s of Azure.

'use strict'

// you can change protocols by uncomment/comment the different protocols
var Protocol = require('azure-iot-device-mqtt').Mqtt
// var Protocol = require('azure-iot-device-amqp').AmqpWs;
// var Protocol = require('azure-iot-device-http').Http;
// var Protocol = require('azure-iot-device-amqp').Amqp

var Client = require('azure-iot-device').Client
var Message = require('azure-iot-device').Message

// with the connectionstring of a device you can only connect to one particular device
// var connectionString = '<connectionstring of deviceId>'

// with this connectionstring you can connect with all the devices you want. You only need to change the deviceId and you need a shared acces policy that have the right permissions. iothubowner default has all the permissions
var connectionString = '<connectionString of shared acces policies;DeviceId=<this deviceId>>'
var client = Client.fromConnectionString(connectionString, Protocol)

const GrovePi = require('node-grovepi').GrovePi
let LightAnalogSensor = GrovePi.sensors.AirQualityAnalog
  • To get the connection string of your device go to your IoT hub choose IoT Devicesand select your device. On your right you will see the connectionstring.
  • To get the connection string of your shared acces policies go to your IoT hub choose shared acces pliciesand select your policy. On your right you will see the connectionstring.

The publishing of the data looks something like this:

var sendInterval = setInterval(function () {
      const sensorValue = LightAnalogSensor.read().toString()
      const data = JSON.stringify({
        'lightValue': sensorValue
      })

      var message = new Message(data)
      console.log('Sending message: ' + message.getData())

      // Client Sends the message
      client.sendEvent(message)
}, 3000)

As you can see we will get the value of the ligthsensor every 3 seconds and send it to the IoT hub.