{"id":47,"date":"2021-01-08T23:31:17","date_gmt":"2021-01-09T04:31:17","guid":{"rendered":"https:\/\/pressbooks.bccampus.ca\/vahidnia\/?post_type=chapter&#038;p=47"},"modified":"2021-04-06T15:47:10","modified_gmt":"2021-04-06T19:47:10","slug":"chapter-6","status":"publish","type":"chapter","link":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/chapter\/chapter-6\/","title":{"raw":"Chapter 6: Microsoft Azure IoT Hub","rendered":"Chapter 6: Microsoft Azure IoT Hub"},"content":{"raw":"<h1>6.1 Introduction<\/h1>\r\nMicrosoft Azure IoT Hub is a cloud-based managed service acting as a central message hub for two-way secure communication between the IoT application and the IoT devices that it manages. IoT Hub supports several messaging patterns such as file upload from devices, device-to-cloud telemetry, and request-reply methods to control the IoT devices from the cloud. IoT Hub monitoring helps to maintain the health of the IoT solution by tracking events such as device failures, creation, and connections [8].\r\n<h1>6.2 Microsoft Azure IoT Hub<\/h1>\r\nIn this chapter, you will become familiar with Microsoft Azure IoT Hub and create an account. Then, you will register the IoT device with Microsoft Azure IoT Hub to exchange messages.\r\n<h2>A.\u00a0 Microsoft Azure account<\/h2>\r\n<ol>\r\n \t<li>Go to <a href=\"http:\/\/azure.microsoft.com\/\">azure.microsoft.com<\/a> and create a free Microsoft Azure account.<\/li>\r\n \t<li>Go to the Azure portal[footnote]https:\/\/portal.azure.com\/#home[\/footnote] and follow the instructions[footnote]https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal[\/footnote] on Microsoft website to create an IoT hub using the Azure portal.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_95\" align=\"aligncenter\" width=\"867\"]<img class=\"wp-image-95 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal.jpg\" alt=\"\" width=\"867\" height=\"353\" \/> Figure6.1. Microsoft Azure portal[\/caption]\r\n\r\nIn this example and through the procedure to create an IoT hub, we created a new resource group called \u201cIoTLab\u201d and entered \u201cMyIoTLab\u201d as the IoT hub name. In the \u201csize and scale\u201d tab, make sure you select the free pricing tier to avoid extra charges to your bill.\r\n\r\n[caption id=\"attachment_96\" align=\"aligncenter\" width=\"765\"]<img class=\"wp-image-96 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab.jpg\" alt=\"\" width=\"765\" height=\"417\" \/> Figure6.2. Create IoT hub - basic tab[\/caption]\r\n<ol start=\"3\">\r\n \t<li>Use the instructions[footnote]https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal[\/footnote] to register a new device in the IoT hub. Once the device is created, open the device from the list in the\u00a0<strong>IoT devices<\/strong>pane and take note of the\u00a0<strong>Primary Connection String<\/strong>\u00a0(it will be used later). This string gives you three important pieces of information for your newly registered device including HostName, DeviceID, and SharedAccessKey that will be used later when configuring the nodes in Node-RED. For example, as you see in the picture below, the primary connection string for the new device that we created is:<\/li>\r\n<\/ol>\r\n<pre><em>HostName=MyIoTLab.azure-devices.net;\r\nDeviceId=MyDeviceID;\r\nSharedAccessKey=W\/uBNVjPNq1ntkPpv8t5xCyMyVOdy7YjrO IAVCtFi8=<\/em><\/pre>\r\nTherefore, in this case:\r\n<ul>\r\n \t<li>HostName = MyIoTLab.azure-devices.net<\/li>\r\n \t<li>DeviceID = MyDeviceID<\/li>\r\n \t<li>Device SharedAccessKey = W\/uBNVjPNq1ntkPpv8t5xCyMyVOdy7YjrO IAVCtFi8=<\/li>\r\n<\/ul>\r\n[caption id=\"attachment_97\" align=\"aligncenter\" width=\"883\"]<img class=\"wp-image-97 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information.jpg\" alt=\"\" width=\"883\" height=\"429\" \/> Figure6.3. Device registry information[\/caption]\r\n<ol start=\"4\">\r\n \t<li>In the previous step, you registered and obtained the information for your registered device. In order to get the access information about your IoT hub, in the IoT portal go to \u201cAll resources\u201d and click on your IoT hub that you created. Then, go to Shared access policies -&gt; iothubowner and copy the <strong>Connection string \u2013 primary key<\/strong>. For example, our IoT hub connection string is:<\/li>\r\n<\/ol>\r\n<pre><em>HostName=MyIoTLab.azure-devices.net;\r\nSharedAccessKeyName=iothubowner;\r\nSharedAccessKey=8JUL6zD9joqYCQyeBIP9uyhQh6u 8BhjP\/mg3u ucRg=<\/em><\/pre>\r\nAs you could see, the HostName is the same as for the device in the previous step; but, we have:\r\n<ul>\r\n \t<li>SharedAccessKeyName = iothubowner<\/li>\r\n \t<li>IoT Hub SharedAccessKey = 8JUL6zD9joqYCQyeBIP9uyhQh6u 8BhjP\/mg3u ucRg=<\/li>\r\n<\/ul>\r\n[caption id=\"attachment_98\" align=\"aligncenter\" width=\"1061\"]<img class=\"wp-image-98 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key.jpg\" alt=\"\" width=\"1061\" height=\"372\" \/> Figure6.4. hared access policies to get the connection string \u2013 primary key[\/caption]\r\n<h2>B.\u00a0 Device Explorer<\/h2>\r\nThe Device Explorer tool is a Windows graphical tool to manage the devices in IoT Hub.\r\n<ol start=\"5\">\r\n \t<li>Click on\u00a0Azure IoT SDKs releases[footnote]https:\/\/github.com\/Azure\/azure-iot-sdks\/releases[\/footnote] and scroll down to the Downloads section to locate the download link for the\u00a0<strong>SetupDeviceExplorer.msi<\/strong>installer. Now download and run the installer.<\/li>\r\n \t<li>Open the Device Explorer tool. On the connection tab, paste the Shared Access Policies connection string (with HostName, SharedAccessKeyName, and SharedAccessKey) and click Update to connect the Device Explorer to your IoT hub.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_99\" align=\"aligncenter\" width=\"505\"]<img class=\"wp-image-99 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer.jpg\" alt=\"\" width=\"505\" height=\"399\" \/> Figure6.5. Device Explorer[\/caption]\r\n<ol start=\"7\">\r\n \t<li>Click on the Management tab. You can see the device you already created on Azure portal. You can also right click on the device to retrieve the connection string.<\/li>\r\n \t<li>Go to Data tab and select your event hub and device ID and click on Monitor to start receiving data on Azure IoT hub. Here is where you will see the data received from your IoT device.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_100\" align=\"aligncenter\" width=\"677\"]<img class=\"wp-image-100 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device.jpg\" alt=\"\" width=\"677\" height=\"384\" \/> Figure6.6. Device explorer is receiving data from the IoT device[\/caption]\r\n<h2>C.\u00a0 Node-RED flow<\/h2>\r\n<ol start=\"9\">\r\n \t<li>Download and install the recommended version of the JavaScript runtime Node.js[footnote]https:\/\/nodejs.org\/en\/#home-downloadhead[\/footnote]. Skip this step if you have Node.js installed on your computer.<\/li>\r\n \t<li>Open Command Prompt and type in the following code to install Node-RED as a global module with all its dependencies. Skip this step if you have Node-RED installed on your computer.<\/li>\r\n<\/ol>\r\n<p style=\"text-align: center;\"><strong><em>npm install -g --unsafe-perm node-red<\/em><\/strong><\/p>\r\n\r\n<ol start=\"11\">\r\n \t<li>In the Command Prompt type in the following command to start Node-RED. If you want to stop Node-RED, you can do so by pressing Ctrl-C or closing the terminal window.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<strong style=\"text-align: center;\"><em>Node-red<\/em><\/strong><\/li>\r\n<\/ol>\r\n<ol start=\"12\">\r\n \t<li>You can then access the Node-RED editor by pointing your browser at <a href=\"http:\/\/localhost:1880\">http:\/\/localhost:1880<\/a>. Make sure Port 1880 is open.<\/li>\r\n \t<li>Go to Settings -&gt; Palette -&gt; Install and search for \u201cnode-red-azure-iot-hub-mod\u201d to install Microsoft Azure IoT hub nodes.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_101\" align=\"aligncenter\" width=\"847\"]<img class=\"wp-image-101 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44..jpg\" alt=\"\" width=\"847\" height=\"320\" \/> Figure6.7. Installing Microsoft Azure IoT hub nodes[\/caption]\r\n<ol start=\"14\">\r\n \t<li>Same as previous lab, your IoT device will publish to an MQTT server. You will need a \u201cfunction\u201d node to prepare the message (i.e., add the device ID and primary key to the message payload) for the Azure IoT Hub.<\/li>\r\n \t<li>Drag and drop \u201cmqqt in\u201d, \u201cmosca in\u201d, \u201cdebug\u201d, \u201cfunction\u201d and \u201cAzure IoT Hub\u201d nodes into the canvas and connect them as shown below. You will need the information in the <strong>Primary Connection String <\/strong>to configure these nodes in the next step.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_102\" align=\"aligncenter\" width=\"915\"]<img class=\"wp-image-102 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection.jpg\" alt=\"\" width=\"915\" height=\"131\" \/> Figure6.8. Nodes interconnection[\/caption]\r\n<ol start=\"16\">\r\n \t<li>Similar to the previous lab, configure the mqqt nodes so that your IoT device can publish to the broker. Use the following JSON object and the \u201cDeviceID\u201d and the \u201cSharedAccessKey\u201d to set up the \u201cfunction\u201d node to prepare the message for the \u201cAzure IoT Hub\u201d. Use the \u201cHostName\u201d in the <strong>Primary Connection String<\/strong>to set up the \u201cAzure IoT Hub\u201d node and pick \u201cmqtt\u201d as the protocol. Before deploying the flow, make sure that you have access to Port 1880, 1883 and 8883 (Azure MQTT port)<\/li>\r\n<\/ol>\r\n<em>msg.payload={<\/em>\r\n\r\n<em>'deviceId':\"xxxxxxxxxx\",<\/em>\r\n\r\n<em>'key':\"xxxxxxxxxxxx\",<\/em>\r\n\r\n<em>'protocol':\"mqtt\",<\/em>\r\n\r\n<em>'data':msg.payload<\/em>\r\n\r\n<em>}<\/em>\r\n\r\n<em>return msg;<\/em>\r\n\r\n<em>For more information on JavaScript Object Notation (JSON), visit: <\/em><a href=\"https:\/\/www.w3schools.com\/js\/js_json_intro.asp\"><em>https:\/\/www.w3schools.com\/js\/js_json_intro.asp<\/em><\/a>\r\n\r\n&nbsp;\r\n\r\n[caption id=\"attachment_103\" align=\"aligncenter\" width=\"539\"]<img class=\"wp-image-103 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message.jpg\" alt=\"\" width=\"539\" height=\"333\" \/> Figure6.9. Prepare Azure message[\/caption]\r\n\r\n&nbsp;\r\n\r\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"532\"]<img class=\"wp-image-104 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings.jpg\" alt=\"\" width=\"532\" height=\"291\" \/> Figure6.10. Azure IoT Hub node settings[\/caption]\r\n<p style=\"text-align: center;\">Table6.1. Nodes settings<\/p>\r\n<img class=\"wp-image-114 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8.jpg\" alt=\"\" width=\"711\" height=\"241\" \/>\r\n<h2>D.\u00a0 Device to Cloud MQTT function<\/h2>\r\nThis part of the lab is similar to Lab 3.\r\n<ol start=\"17\">\r\n \t<li>Replace your own public IP address and enter the following AT Commands. You should be able to see the message \u201cTemp=25\u201d in the data tab of Device Explorer.<\/li>\r\n<\/ol>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>AT QMTOPEN=0,\" 172.218.32.19 \",1883\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Open a network for MQTT client. Use your own IP<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTOPEN: 0,0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/Opened the MQTT client network successfully<\/em>\r\n\r\n<strong>AT QMTCONN=0,\u201cMyClientID\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Connect a client to MQTT server. Use any client ID<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTCONN: 0,0,0\r\n\r\n<strong>AT QMTPUB=0,0,0,0,\u201csensor\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Publish a message to topic \u201csensor\u201d<\/em>\r\n\r\n<strong>&gt;Temp=25\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Press CTR Z to send<\/em>\r\n\r\nOK\r\n\r\n&nbsp;\r\n\r\nQMTPUB: 0,0,0\r\n\r\n&nbsp;<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nFigure6.11 shows how the received data will be represented in Device Explorer tool.\r\n\r\n[caption id=\"attachment_105\" align=\"aligncenter\" width=\"691\"]<img class=\"wp-image-105 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub.jpg\" alt=\"\" width=\"691\" height=\"366\" \/> Figure6.11. Received message on the IoT hub[\/caption]\r\n<h2>E.\u00a0 Cloud to Device MQTT function<\/h2>\r\n<ol start=\"18\">\r\n \t<li>Make appropriate changes to your Node-RED flow and enter the AT command (same as Lab 3) so that the IoT device can subscribe to the MQTT broker with topic \u201csensor\u201d.<\/li>\r\n \t<li>In the Device Explorer tool go to \u201cMessage to Device\u201d tab. Type and send a message to your device. You should see this message in your IoT device as well as the debug pannel in the Node-RED environment. You may need to use the \u201cfunction\u201d node to format the received message.<\/li>\r\n<\/ol>\r\n[caption id=\"attachment_106\" align=\"aligncenter\" width=\"639\"]<img class=\"wp-image-106 \" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging.jpg\" alt=\"\" width=\"639\" height=\"260\" \/> Figure6.12. Cloud to Device messaging[\/caption]\r\n\r\nBy conducting this lab you were able to communicate with Microsoft Azure IoT Hub. Now, if you are interested, you can make yourself familiar with other features of Microsoft Azure to create a dashboard and visualize the data, store the data on the cloud or send email alerts.","rendered":"<h1>6.1 Introduction<\/h1>\n<p>Microsoft Azure IoT Hub is a cloud-based managed service acting as a central message hub for two-way secure communication between the IoT application and the IoT devices that it manages. IoT Hub supports several messaging patterns such as file upload from devices, device-to-cloud telemetry, and request-reply methods to control the IoT devices from the cloud. IoT Hub monitoring helps to maintain the health of the IoT solution by tracking events such as device failures, creation, and connections [8].<\/p>\n<h1>6.2 Microsoft Azure IoT Hub<\/h1>\n<p>In this chapter, you will become familiar with Microsoft Azure IoT Hub and create an account. Then, you will register the IoT device with Microsoft Azure IoT Hub to exchange messages.<\/p>\n<h2>A.\u00a0 Microsoft Azure account<\/h2>\n<ol>\n<li>Go to <a href=\"http:\/\/azure.microsoft.com\/\">azure.microsoft.com<\/a> and create a free Microsoft Azure account.<\/li>\n<li>Go to the Azure portal<a class=\"footnote\" title=\"https:\/\/portal.azure.com\/#home\" id=\"return-footnote-47-1\" href=\"#footnote-47-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a> and follow the instructions<a class=\"footnote\" title=\"https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal\" id=\"return-footnote-47-2\" href=\"#footnote-47-2\" aria-label=\"Footnote 2\"><sup class=\"footnote\">[2]<\/sup><\/a> on Microsoft website to create an IoT hub using the Azure portal.<\/li>\n<\/ol>\n<figure id=\"attachment_95\" aria-describedby=\"caption-attachment-95\" style=\"width: 867px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-95\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal.jpg\" alt=\"\" width=\"867\" height=\"353\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal.jpg 2304w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-300x122.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-1024x417.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-768x313.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-1536x625.jpg 1536w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-2048x834.jpg 2048w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-65x26.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-225x92.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/38.-Microsoft-Azure-portal-350x142.jpg 350w\" sizes=\"auto, (max-width: 867px) 100vw, 867px\" \/><figcaption id=\"caption-attachment-95\" class=\"wp-caption-text\">Figure6.1. Microsoft Azure portal<\/figcaption><\/figure>\n<p>In this example and through the procedure to create an IoT hub, we created a new resource group called \u201cIoTLab\u201d and entered \u201cMyIoTLab\u201d as the IoT hub name. In the \u201csize and scale\u201d tab, make sure you select the free pricing tier to avoid extra charges to your bill.<\/p>\n<figure id=\"attachment_96\" aria-describedby=\"caption-attachment-96\" style=\"width: 765px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-96\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab.jpg\" alt=\"\" width=\"765\" height=\"417\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab.jpg 1127w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-300x163.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-1024x558.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-768x418.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-65x35.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-225x123.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/39.-Create-IoT-hub-basic-tab-350x191.jpg 350w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><figcaption id=\"caption-attachment-96\" class=\"wp-caption-text\">Figure6.2. Create IoT hub &#8211; basic tab<\/figcaption><\/figure>\n<ol start=\"3\">\n<li>Use the instructions<a class=\"footnote\" title=\"https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal\" id=\"return-footnote-47-3\" href=\"#footnote-47-3\" aria-label=\"Footnote 3\"><sup class=\"footnote\">[3]<\/sup><\/a> to register a new device in the IoT hub. Once the device is created, open the device from the list in the\u00a0<strong>IoT devices<\/strong>pane and take note of the\u00a0<strong>Primary Connection String<\/strong>\u00a0(it will be used later). This string gives you three important pieces of information for your newly registered device including HostName, DeviceID, and SharedAccessKey that will be used later when configuring the nodes in Node-RED. For example, as you see in the picture below, the primary connection string for the new device that we created is:<\/li>\n<\/ol>\n<pre><em>HostName=MyIoTLab.azure-devices.net;\r\nDeviceId=MyDeviceID;\r\nSharedAccessKey=W\/uBNVjPNq1ntkPpv8t5xCyMyVOdy7YjrO IAVCtFi8=<\/em><\/pre>\n<p>Therefore, in this case:<\/p>\n<ul>\n<li>HostName = MyIoTLab.azure-devices.net<\/li>\n<li>DeviceID = MyDeviceID<\/li>\n<li>Device SharedAccessKey = W\/uBNVjPNq1ntkPpv8t5xCyMyVOdy7YjrO IAVCtFi8=<\/li>\n<\/ul>\n<figure id=\"attachment_97\" aria-describedby=\"caption-attachment-97\" style=\"width: 883px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-97\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information.jpg\" alt=\"\" width=\"883\" height=\"429\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information.jpg 1279w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-300x146.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-1024x498.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-768x373.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-65x32.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-225x109.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/40.-Device-registry-information-350x170.jpg 350w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><figcaption id=\"caption-attachment-97\" class=\"wp-caption-text\">Figure6.3. Device registry information<\/figcaption><\/figure>\n<ol start=\"4\">\n<li>In the previous step, you registered and obtained the information for your registered device. In order to get the access information about your IoT hub, in the IoT portal go to \u201cAll resources\u201d and click on your IoT hub that you created. Then, go to Shared access policies -&gt; iothubowner and copy the <strong>Connection string \u2013 primary key<\/strong>. For example, our IoT hub connection string is:<\/li>\n<\/ol>\n<pre><em>HostName=MyIoTLab.azure-devices.net;\r\nSharedAccessKeyName=iothubowner;\r\nSharedAccessKey=8JUL6zD9joqYCQyeBIP9uyhQh6u 8BhjP\/mg3u ucRg=<\/em><\/pre>\n<p>As you could see, the HostName is the same as for the device in the previous step; but, we have:<\/p>\n<ul>\n<li>SharedAccessKeyName = iothubowner<\/li>\n<li>IoT Hub SharedAccessKey = 8JUL6zD9joqYCQyeBIP9uyhQh6u 8BhjP\/mg3u ucRg=<\/li>\n<\/ul>\n<figure id=\"attachment_98\" aria-describedby=\"caption-attachment-98\" style=\"width: 1061px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-98\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key.jpg\" alt=\"\" width=\"1061\" height=\"372\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key.jpg 2547w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-300x105.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-1024x359.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-768x270.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-1536x539.jpg 1536w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-2048x719.jpg 2048w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-65x23.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-225x79.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/41.-Shared-access-policies-to-get-the-connection-string-\u2013-primary-key-350x123.jpg 350w\" sizes=\"auto, (max-width: 1061px) 100vw, 1061px\" \/><figcaption id=\"caption-attachment-98\" class=\"wp-caption-text\">Figure6.4. hared access policies to get the connection string \u2013 primary key<\/figcaption><\/figure>\n<h2>B.\u00a0 Device Explorer<\/h2>\n<p>The Device Explorer tool is a Windows graphical tool to manage the devices in IoT Hub.<\/p>\n<ol start=\"5\">\n<li>Click on\u00a0Azure IoT SDKs releases<a class=\"footnote\" title=\"https:\/\/github.com\/Azure\/azure-iot-sdks\/releases\" id=\"return-footnote-47-4\" href=\"#footnote-47-4\" aria-label=\"Footnote 4\"><sup class=\"footnote\">[4]<\/sup><\/a> and scroll down to the Downloads section to locate the download link for the\u00a0<strong>SetupDeviceExplorer.msi<\/strong>installer. Now download and run the installer.<\/li>\n<li>Open the Device Explorer tool. On the connection tab, paste the Shared Access Policies connection string (with HostName, SharedAccessKeyName, and SharedAccessKey) and click Update to connect the Device Explorer to your IoT hub.<\/li>\n<\/ol>\n<figure id=\"attachment_99\" aria-describedby=\"caption-attachment-99\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-99\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer.jpg\" alt=\"\" width=\"505\" height=\"399\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer.jpg 1043w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-300x237.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-1024x808.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-768x606.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-65x51.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-225x178.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/42.-Device-Explorer-350x276.jpg 350w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><figcaption id=\"caption-attachment-99\" class=\"wp-caption-text\">Figure6.5. Device Explorer<\/figcaption><\/figure>\n<ol start=\"7\">\n<li>Click on the Management tab. You can see the device you already created on Azure portal. You can also right click on the device to retrieve the connection string.<\/li>\n<li>Go to Data tab and select your event hub and device ID and click on Monitor to start receiving data on Azure IoT hub. Here is where you will see the data received from your IoT device.<\/li>\n<\/ol>\n<figure id=\"attachment_100\" aria-describedby=\"caption-attachment-100\" style=\"width: 677px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-100\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device.jpg\" alt=\"\" width=\"677\" height=\"384\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device.jpg 1034w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-300x170.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-1024x580.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-768x435.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-65x37.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-225x128.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/43.-Device-explorer-is-receiving-data-from-the-IoT-device-350x198.jpg 350w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><figcaption id=\"caption-attachment-100\" class=\"wp-caption-text\">Figure6.6. Device explorer is receiving data from the IoT device<\/figcaption><\/figure>\n<h2>C.\u00a0 Node-RED flow<\/h2>\n<ol start=\"9\">\n<li>Download and install the recommended version of the JavaScript runtime Node.js<a class=\"footnote\" title=\"https:\/\/nodejs.org\/en\/#home-downloadhead\" id=\"return-footnote-47-5\" href=\"#footnote-47-5\" aria-label=\"Footnote 5\"><sup class=\"footnote\">[5]<\/sup><\/a>. Skip this step if you have Node.js installed on your computer.<\/li>\n<li>Open Command Prompt and type in the following code to install Node-RED as a global module with all its dependencies. Skip this step if you have Node-RED installed on your computer.<\/li>\n<\/ol>\n<p style=\"text-align: center;\"><strong><em>npm install -g &#8211;unsafe-perm node-red<\/em><\/strong><\/p>\n<ol start=\"11\">\n<li>In the Command Prompt type in the following command to start Node-RED. If you want to stop Node-RED, you can do so by pressing Ctrl-C or closing the terminal window.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<strong style=\"text-align: center;\"><em>Node-red<\/em><\/strong><\/li>\n<\/ol>\n<ol start=\"12\">\n<li>You can then access the Node-RED editor by pointing your browser at <a href=\"http:\/\/localhost:1880\">http:\/\/localhost:1880<\/a>. Make sure Port 1880 is open.<\/li>\n<li>Go to Settings -&gt; Palette -&gt; Install and search for \u201cnode-red-azure-iot-hub-mod\u201d to install Microsoft Azure IoT hub nodes.<\/li>\n<\/ol>\n<figure id=\"attachment_101\" aria-describedby=\"caption-attachment-101\" style=\"width: 847px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-101\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44..jpg\" alt=\"\" width=\"847\" height=\"320\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44..jpg 1483w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-300x113.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-1024x387.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-768x291.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-65x25.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-225x85.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/44.-350x132.jpg 350w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><figcaption id=\"caption-attachment-101\" class=\"wp-caption-text\">Figure6.7. Installing Microsoft Azure IoT hub nodes<\/figcaption><\/figure>\n<ol start=\"14\">\n<li>Same as previous lab, your IoT device will publish to an MQTT server. You will need a \u201cfunction\u201d node to prepare the message (i.e., add the device ID and primary key to the message payload) for the Azure IoT Hub.<\/li>\n<li>Drag and drop \u201cmqqt in\u201d, \u201cmosca in\u201d, \u201cdebug\u201d, \u201cfunction\u201d and \u201cAzure IoT Hub\u201d nodes into the canvas and connect them as shown below. You will need the information in the <strong>Primary Connection String <\/strong>to configure these nodes in the next step.<\/li>\n<\/ol>\n<figure id=\"attachment_102\" aria-describedby=\"caption-attachment-102\" style=\"width: 915px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-102\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection.jpg\" alt=\"\" width=\"915\" height=\"131\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-300x43.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-1024x148.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-768x111.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-65x9.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-225x33.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/45.-Nodes-interconnection-350x51.jpg 350w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><figcaption id=\"caption-attachment-102\" class=\"wp-caption-text\">Figure6.8. Nodes interconnection<\/figcaption><\/figure>\n<ol start=\"16\">\n<li>Similar to the previous lab, configure the mqqt nodes so that your IoT device can publish to the broker. Use the following JSON object and the \u201cDeviceID\u201d and the \u201cSharedAccessKey\u201d to set up the \u201cfunction\u201d node to prepare the message for the \u201cAzure IoT Hub\u201d. Use the \u201cHostName\u201d in the <strong>Primary Connection String<\/strong>to set up the \u201cAzure IoT Hub\u201d node and pick \u201cmqtt\u201d as the protocol. Before deploying the flow, make sure that you have access to Port 1880, 1883 and 8883 (Azure MQTT port)<\/li>\n<\/ol>\n<p><em>msg.payload={<\/em><\/p>\n<p><em>&#8216;deviceId&#8217;:&#8221;xxxxxxxxxx&#8221;,<\/em><\/p>\n<p><em>&#8216;key&#8217;:&#8221;xxxxxxxxxxxx&#8221;,<\/em><\/p>\n<p><em>&#8216;protocol&#8217;:&#8221;mqtt&#8221;,<\/em><\/p>\n<p><em>&#8216;data&#8217;:msg.payload<\/em><\/p>\n<p><em>}<\/em><\/p>\n<p><em>return msg;<\/em><\/p>\n<p><em>For more information on JavaScript Object Notation (JSON), visit: <\/em><a href=\"https:\/\/www.w3schools.com\/js\/js_json_intro.asp\"><em>https:\/\/www.w3schools.com\/js\/js_json_intro.asp<\/em><\/a><\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_103\" aria-describedby=\"caption-attachment-103\" style=\"width: 539px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-103\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message.jpg\" alt=\"\" width=\"539\" height=\"333\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message.jpg 1012w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message-300x185.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message-768x474.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message-65x40.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message-225x139.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/46.-Prepare-Azure-message-350x216.jpg 350w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><figcaption id=\"caption-attachment-103\" class=\"wp-caption-text\">Figure6.9. Prepare Azure message<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-104\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings.jpg\" alt=\"\" width=\"532\" height=\"291\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings.jpg 633w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings-300x164.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings-65x36.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings-225x123.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/47.-Azure-IoT-Hub-node-settings-350x191.jpg 350w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure6.10. Azure IoT Hub node settings<\/figcaption><\/figure>\n<p style=\"text-align: center;\">Table6.1. Nodes settings<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-114 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8.jpg\" alt=\"\" width=\"711\" height=\"241\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8.jpg 1438w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-300x102.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-1024x347.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-768x260.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-65x22.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-225x76.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/T8-350x119.jpg 350w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/p>\n<h2>D.\u00a0 Device to Cloud MQTT function<\/h2>\n<p>This part of the lab is similar to Lab 3.<\/p>\n<ol start=\"17\">\n<li>Replace your own public IP address and enter the following AT Commands. You should be able to see the message \u201cTemp=25\u201d in the data tab of Device Explorer.<\/li>\n<\/ol>\n<table>\n<tbody>\n<tr>\n<td><strong>AT QMTOPEN=0,&#8221; 172.218.32.19 &#8220;,1883\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Open a network for MQTT client. Use your own IP<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTOPEN: 0,0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <em>\/\/Opened the MQTT client network successfully<\/em><\/p>\n<p><strong>AT QMTCONN=0,\u201cMyClientID\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Connect a client to MQTT server. Use any client ID<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTCONN: 0,0,0<\/p>\n<p><strong>AT QMTPUB=0,0,0,0,\u201csensor\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Publish a message to topic \u201csensor\u201d<\/em><\/p>\n<p><strong>&gt;Temp=25\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><em>\/\/ Press CTR Z to send<\/em><\/p>\n<p>OK<\/p>\n<p>&nbsp;<\/p>\n<p>QMTPUB: 0,0,0<\/p>\n<p>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Figure6.11 shows how the received data will be represented in Device Explorer tool.<\/p>\n<figure id=\"attachment_105\" aria-describedby=\"caption-attachment-105\" style=\"width: 691px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-105\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub.jpg\" alt=\"\" width=\"691\" height=\"366\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub.jpg 1036w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-300x159.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-1024x543.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-768x407.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-65x34.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-225x119.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/48.-Received-message-on-the-IoT-hub-350x185.jpg 350w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><figcaption id=\"caption-attachment-105\" class=\"wp-caption-text\">Figure6.11. Received message on the IoT hub<\/figcaption><\/figure>\n<h2>E.\u00a0 Cloud to Device MQTT function<\/h2>\n<ol start=\"18\">\n<li>Make appropriate changes to your Node-RED flow and enter the AT command (same as Lab 3) so that the IoT device can subscribe to the MQTT broker with topic \u201csensor\u201d.<\/li>\n<li>In the Device Explorer tool go to \u201cMessage to Device\u201d tab. Type and send a message to your device. You should see this message in your IoT device as well as the debug pannel in the Node-RED environment. You may need to use the \u201cfunction\u201d node to format the received message.<\/li>\n<\/ol>\n<figure id=\"attachment_106\" aria-describedby=\"caption-attachment-106\" style=\"width: 639px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-106\" src=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging.jpg\" alt=\"\" width=\"639\" height=\"260\" srcset=\"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging.jpg 1037w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-300x122.jpg 300w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-1024x417.jpg 1024w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-768x313.jpg 768w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-65x26.jpg 65w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-225x92.jpg 225w, https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-content\/uploads\/sites\/1097\/2021\/01\/49.-Cloud-to-Device-messaging-350x142.jpg 350w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><figcaption id=\"caption-attachment-106\" class=\"wp-caption-text\">Figure6.12. Cloud to Device messaging<\/figcaption><\/figure>\n<p>By conducting this lab you were able to communicate with Microsoft Azure IoT Hub. Now, if you are interested, you can make yourself familiar with other features of Microsoft Azure to create a dashboard and visualize the data, store the data on the cloud or send email alerts.<\/p>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-47-1\">https:\/\/portal.azure.com\/#home <a href=\"#return-footnote-47-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><li id=\"footnote-47-2\">https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal <a href=\"#return-footnote-47-2\" class=\"return-footnote\" aria-label=\"Return to footnote 2\">&crarr;<\/a><\/li><li id=\"footnote-47-3\">https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-create-through-portal <a href=\"#return-footnote-47-3\" class=\"return-footnote\" aria-label=\"Return to footnote 3\">&crarr;<\/a><\/li><li id=\"footnote-47-4\">https:\/\/github.com\/Azure\/azure-iot-sdks\/releases <a href=\"#return-footnote-47-4\" class=\"return-footnote\" aria-label=\"Return to footnote 4\">&crarr;<\/a><\/li><li id=\"footnote-47-5\">https:\/\/nodejs.org\/en\/#home-downloadhead <a href=\"#return-footnote-47-5\" class=\"return-footnote\" aria-label=\"Return to footnote 5\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":197,"menu_order":6,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-47","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/users\/197"}],"version-history":[{"count":15,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/47\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/47\/revisions\/303"}],"part":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapters\/47\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/pressbooks\/v2\/chapter-type?post=47"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/contributor?post=47"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/cellulariot\/wp-json\/wp\/v2\/license?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}