This is the fourth installment of 5 part series Connecting Things to Internet – a step-by-step guide on how to get things connected to the Internet using low cost devices. In this fourth article, I will focus on getting Windows Azure IoT SDK installed on on your gateway device, The Rasbberry Pi 2, and configure the device in IoT hub that was create in the second article. As mentioned in the earlier article, I will be using Node implementation of Azure IoT SDK in this series.
Installing Azure IoT Node SDK
It is assumed that you already have Node(0.12.x or later) and serialport package installed in your RPi. If not done so, please refer to the previous article and have the gateway device ready before proceeding with the steps in this article. There are 2 ways of installing Azure IoT in your RPi.
Option 1: Installing IoT suite by compiling the source code.
- Navigate to https://github.com/Azure/azure-iot-sdks and copy the git url to the clipboard
- Connect to your RPi using Bitvise SSH Client and clone the project to your working directory
1git clone https://github.com/Azure/azure-iot-sdks.git
- Now you should have the project cloned in your working directory
- Navigate to the node folder in your local copy of this repository (azure-iot-sdks). Run the build/dev-setup.sh script to prepare your development environment. it will take a while to complete the build. so, please be patient.
- Then run the build/build.sh script to verify your installation. if the build is completed successfully you should not see any errors.
Option 2: Installing IoT suite by using npm
- Go to your working directory and execute the following commands in the given order
1234npm install -g azure-iot-device@latestnpm install -g azure-iot-device-amqp@latestnpm install -g azure-iot-device-http@latestnpm install -g azure-iot-device-mqtt@latest
Configuring Remote Device
After installing Windows IoT Azure SDK on your RPi, the next step of the process is to configure the device that you added in the second article. Follow the steps to have the device configured in the IoT hub.
- Copy the following javascript code in to your text editor and save the file, configure-device.js, in the working directory of your RPi. make sure that you replace IoT hub name, device id and the device key in the code with the values recorded from the device creation step. you may add dummy metadata from line 28 to 40. however, make sure to have a valid DeviceID at line 27.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960'use strict';var device = require('azure-iot-device'); //load azure-iot-device module//ConnectionString comprises of 3 parts;you can replace each part with the value you recorded from//step 3 of 'Provisioning remote monitoring devices' section in article 2.var connectionString = "HostName=xxxxxxxxxx.azure-devices.net;DeviceId=newyork-1;SharedAccessKey=xys0m8cu1azPwRrHv5W9uQ==";var deviceId = device.ConnectionString.parse(connectionString).DeviceId;// Create IoT Hub client//var client = new device.Client.fromConnectionString(connectionString);var client = device.Client.fromConnectionString(connectionString, device.Amqp);// Helper function to print results for an operationfunction printErrorFor(op) {return function printError(err) {if (err) console.log(op + ' error: ' + err.toString());};}// Configure device meta data. please note that these are dummy metadata except the DeviceIDvar deviceMetaData = {"ObjectType":"DeviceInfo","IsSimulatedDevice":0,"Version":"1.0","DeviceProperties": {"DeviceID":deviceId,"HubEnabledState":1,"CreatedTime":"2015-09-21T20:28:55.5448990Z","DeviceState":"normal","UpdatedTime":null,"Manufacturer":"Raspberry Pi Inc.","ModelNumber":"RPi2 B","SerialNumber":"E305654","FirmwareVersion":"1.10","Platform":"ARM7","Processor":"A 900MHz quad-core ARM Cortex-A7","InstalledRAM":"1 GB","Latitude":40.710962, //latitude of the device location"Longitude":-74.008473 //longitude of the device location},"Commands": [{"Name": "SetTemperature","Parameters": [{"Name": "Temperature","Type": "double"}]},{"Name": "SetHumidity","Parameters": [{"Name": "Humidity","Type": "double"}]}]};//Initialize the device with metadataconsole.log("Sending device metadata:\n" + JSON.stringify(deviceMetaData));client.sendEvent(new device.Message(JSON.stringify(deviceMetaData)), printErrorFor('send metadata')); - Execute the following command
1node configure-device.js - Log in to the IoT hub, select the newly added device and verify the device metadata defined in the above code.
This concludes the fourth article in the series. In the fifth and last article, I will explain how to send the sensor data captured from Arduino to the Iot Hub.