Publishing Temperature Sensor Data to the Microsoft Azure* IoT Hub

In this task, we'll update the Intel® NUC with some packages to help it talk to our Microsoft Azure* IoT Hub, then we'll modify the Flow 3 flow we created earlier to publish the temperature sensor data to our Azure IoT Hub.

1. Open an ssh connection to your Intel® NUC using the method described previously. From the prompt, run the following commands:

Note: MAKE SURE YOU ARE RUNNING THESE COMMANDS ON THE INTEL NUC VIA AN SSH SESSION.

Note: If you are using PuTTY, you can copy the command below to your computer's clipboard, then right-click on the PuTTY window to paste it into the remote ssh command prompt. Other ssh clients should offer a similar paste option.

The commands will not return any result unless there was a problem. They are simply adding some public key values used by the apt-get package repositories we will be installing from in the coming steps:

rpm --import http://iotdk.intel.com/misc/iot_pub.key
rpm --import http://iotdk.intel.com/misc/iot_pub2.key

2. Next, install the node-red-contrib-os npm package globally on the Intel® NUC. This package (link) adds some special Node-RED* nodes that allow you to get information from the local Operating System (OS) where the Node-RED flow is running. Things like OS Info, Drive Info, Memory Info, etc. Install it on the NUC using the following statement:

You will see a number of WARN unmet dependency messages appear. You can safely ignore these.

npm install node-red-contrib-os -g

Keep your ssh connection open, you'll need it later.

Next, we need to add an rpm package repository to the system. In your browser, navigate to the  IP Address of your Intel® NUC and login as root. Then navigate to the Packages page, and click the Add Repo + button.

4. In the Manage Repositories window, in the fields under Add New Repository enter the following, and click the Add Repository button:

Note: The IoT_Cloud repo is provided by Intel and includes some packages for working with Azure's IoT services. Once we add a reference to the repo (which is what we are doing here), we can later install packages from it using apt-get.

  • Name - IoT_Cloud
  • URL - http://iotdk.intel.com/repos/iot-cloud/wrlinux7/rcpl13
  • Username - leave blank
  • Password - leave blank

5. You will see the following message, indicating that this update may take a few minutes. And it does, so be patient:

***Adding repository IoT_Cloud. Package list will be updated. This may take a few minutes...***

6. Once the update is complete, you should see the following message. Click on the Update Repositories button. Again, this will take a few minutes to complete:

7. When the button states that the repositories have been updated (this will also take a minute or so to update), you can click on the X in the top right corner of the Manage Repositories window to close it:

8. Next, click the Add Packages + button:

9. In the Add New Packages window, in the search box, type cloud-azure, then click the Install button next to the packagegroup-cloud-azure package. Again, this takes a few minutes so be patient:

Note: You can see what all is installed with the packagegroup-cloud-azure package here: link

Basically it is all of the npm packages for the various Azure IoT Hub sdks. It also includes a Node-RED node for working with Azure IoT Hubs (link).

10. Once the package disappears from the list, you can click on the X icon to close the Add New Packages window.

11. Back in your ssh connection to the NUC, run the following command to restart the Node-RED environment on the Intel® NUC. This is necessary because the package that we just installed updated the resources available to Node-RED so it needs to be re-initialized:

systemctl restart node-red-experience

12. Now, from your computer open the Node-RED development environment in the browser (Remember you can just point your browser to port 1880 on your NUC, eg: http://your.nucs.ip.address:1880 where your.nucs.ip.address is the IP Address of your Intel® NUC). If you already had it open, make sure to refresh it. In the list of nodes on the left, you should see a new cloud category, and within it the azureiothub node:

Note: if the cloud category and azureiothubnode node don't appear, you may need to manually install the node-red-control-azureiothubnode package on the Intel® NUC. If that is necessary, ssh into the Intel® NUC, and from the prompt run the following two commands:

npm install -g node-red-contrib-azureiothubnode
systemctl restart node-red-experience

13. Open the myresources.txt file, and copy the IoT Hub Device Connection String you pasted in after creating the Azure IoT Hub Device Identity.

Note: Make sure to copy the IoT Hub Device Connection String and not the connection strings for the iothubowner or service SAS policies. You'll know you have the right one if you can see the DeviceId=<name>IntelIoTGateway device id (or whatever device Id you used) that you created earlier.

14. Drag the azureiothub node onto the Node-RED visual editor, connect it to the json node's output as shown below, configure it as follows, and click OK.

  • Name - Azure IoT Hub
  • Protocol - amqp
  • Connection String - Paste in the IoT Hub Device Connection String you just copied from myresources.txt. Again, make sure it's the connection string with that contains your DeviceId=<name>IntelIoTGateway device id we created earlier.

15. Next, double click on the Create Payload node. This function generates the actual JSON message that will be sent to the Azure IoT Hub. We will want to be able to retrieve the actual device id from that payload later, so we want to update it to use the device ID we created in our Azure IoT Hub Device Identity registry previously.

  • Replace the default IntelIoTGateway name with the <name>IntelIoTGateway*** you created (It should match the DeviceId=<name>IntelIoTGateway* device id value in the connection string you used above, then click OK.

16. Click the Deploy button to deploy the changes. At this point, the Node-RED flow on the In tel® NUCshould begin publishing Temperature data to your Azure IoT Hub as your <name>IntelIotGateway device.

17. Now the that device is publishing messages to the IoT Hub, we want to verify that by reading the messages back. From the command prompt or terminal window on your system, run the following command to monitor the messages being sent into your Azure IoT Hub the Node-RED flow running on the Intel® NUC:

  • You will need to copy the 'IoT Hub iothubowner SAS Policy Primary Connection String' from the myresources.txt file.
    Note: Again, you need to pay attention here. Make sure to copy the 'IoT Hub iothubowner SAS Policy Primary Connection String' value. It's the one that has SharedAccessKeyName=iothubowner in the connection string. This connection string allows you to connect to your IoT Hub with permissions to manage the hub. That includes of course the permission to read messages that devices send to the hub.
  • Use the device id you generated in place of the <name>IntelIoTGateway device id
iothub-explorer monitor-events <name>IntelIoTGateway --login "<IoT Hub 'iothubowner' SAS Policy Primary Connection String>"

For example:

iothub-explorer monitor-events mic16IntelIoTGateway --login "HostName=mic16iot.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=MuIeI2Bpp4lm6knbNiXX4J1V+UivTov/ebfIfykWD+g="

And you should see output similar to this:

Monitoring events from device mic16IntelIoTGateway
==== From: mic16IntelIoTGateway ====
{
"deviceID": "mic16IntelIoTGateway",
"timestamp": "2016-10-10T03:49:48.966Z",
"temperature": 36.959999999999994
}
====================
==== From: mic16IntelIoTGateway ====
{
"deviceID": "mic16IntelIoTGateway",
"timestamp": "2016-10-10T03:49:59.006Z",
"temperature": 37.62
}
====================
==== From: mic16IntelIoTGateway ====
{
"deviceID": "mic16IntelIoTGateway",
"timestamp": "2016-10-10T03:50:09.085Z",
"temperature": 36.959999999999994
}
====================

18. Remember that we had the Node-RED flow only getting temperature values once every 10 seconds (10000ms). It is recommended that you don't publish too much more frequently during this event. It just helps to reduce the amount of traffic on the network.

19. If you are feeling adventurous, trade iothubowner connection strings and device IDs with a neighbor in the lab and verify that you can monitor each other's devices. For example:

iothub-explorer monitor-events <your neighbors device id> --login "<Your neighbors 'iothubowner' SAS Policy Primary Connection String>"

20.  One last comment, we are using the iothubowner connection string to monitor the events. You could actually use a less privileged policy, like the service sas policy we copied the connection string for earlier. Go ahead and try monitoring events with the IoT Hub service SAS Policy Primary Connection String policy connection string (the one with SharedAccessKeyName=service in it) you pasted into the myresources.txt file. It should work just fine because that SAS policy has permissions to read messages from the IoT Hub and that is all the permissions that iothub-explorer monitor-events needs.

21.  To stop monitoring events, press Ctrl-C at the command prompt and confirm exiting the script.

 

IDE or Development Framework: 
Middleware: 
For more complete information about compiler optimizations, see our Optimization Notice.