{"id":625,"date":"2022-08-10T03:33:00","date_gmt":"2022-08-10T03:33:00","guid":{"rendered":"http:\/\/draith.com\/?p=625"},"modified":"2023-01-04T03:33:37","modified_gmt":"2023-01-04T03:33:37","slug":"mqtt-data-to-azure-log-analytics","status":"publish","type":"post","link":"http:\/\/draith.azurewebsites.net\/?p=625","title":{"rendered":"MQTT Data to Azure Log Analytics"},"content":{"rendered":"\n<p>Setting up solar on our farm in North Texas has been a treasure trove of data to analyze &#8211; everything from Inverter load, to PV watts in, to state-of-charge for the batteries &#8211; It&#8217;s a massive amount of useful information.  <br><br>So naturally I want that data in Azure Log Analytics.  It&#8217;s perfect platform to handle this amount of data.  Small messages, lots of entries.  Just ready for reporting and analytics.<\/p>\n\n\n\n<p>To do this, we are going to use IOT Hub as a go-between.  Here are the high-level steps:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>To get MQTT data into Azure Log Analytics via IoT Hub, you will need to perform the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Set up an IoT hub in your Azure account. <\/li>\n\n\n\n<li>Set up a device in your IoT hub.<\/li>\n\n\n\n<li>Configure the device to send data to your IoT hub using the MQTT protocol. <\/li>\n\n\n\n<li>Set up a log analytics workspace in your Azure account. <\/li>\n\n\n\n<li>Connect your IoT hub to your log analytics workspace. <\/li>\n\n\n\n<li>Start sending MQTT data from your device to your IoT hub. <\/li>\n<\/ol>\n\n\n\n<p>And now some details:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Set up an IoT hub in your Azure account:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the Azure portal, click on &#8220;Create a resource&#8221; in the top left corner, then search for &#8220;IoT Hub&#8221; and select it.<\/li>\n\n\n\n<li>Follow the prompts to create a new IoT hub, including selecting the subscription, resource group, and region you want to use.<\/li>\n\n\n\n<li>Make note of the IoT hub&#8217;s name and connection string, as you will need these later.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Set up a device in your IoT hub:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the Azure portal, go to the IoT hub blade and select &#8220;IoT Devices&#8221; from the left menu.<\/li>\n\n\n\n<li>Click the &#8220;Add&#8221; button to create a new device.<\/li>\n\n\n\n<li>Follow the prompts to set up the device, including giving it a unique device ID and generating a device key or certificate.<\/li>\n\n\n\n<li>Make note of the device ID and key\/certificate, as you will need these later.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Configure the device to send data to your IoT hub using MQTT:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The specific steps for this will depend on the device you are using, but generally you will need to specify the MQTT endpoint and authentication information for your IoT hub.<\/li>\n\n\n\n<li>The MQTT endpoint will be in the format &#8220;YOUR-IOT-HUB-NAME.azure-devices.net&#8221;, and you will need to use the device ID and key\/certificate you obtained in step 2 to authenticate.<\/li>\n\n\n\n<li>Specify the MQTT topic you want to publish data to.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Set up a log analytics workspace:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the Azure portal, click on &#8220;Create a resource&#8221; in the top left corner, then search for &#8220;Log Analytics&#8221; and select it.<\/li>\n\n\n\n<li>Follow the prompts to create a new log analytics workspace, including selecting the subscription, resource group, and region you want to use.<\/li>\n\n\n\n<li>Make note of the workspace ID and primary key, as you will need these later.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>Connect your IoT hub to your log analytics workspace:<\/strong>\n<ul class=\"wp-block-list\"><\/ul>\n<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the Azure portal, go to the IoT hub blade and select &#8220;Diagnostic settings&#8221; from the left menu.<\/li>\n\n\n\n<li>Click the &#8220;Add diagnostic setting&#8221; button to create a new setting.<\/li>\n\n\n\n<li>Select the log analytics workspace you created in step 4 as the destination for the data.<\/li>\n\n\n\n<li>Select the data types you want to collect<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>Start sending MQTT data from your device to your IoT hub:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using the device ID, key\/certificate, MQTT endpoint, and topic you obtained in steps 2 and 3, publish data to your IoT hub using the MQTT protocol.<\/li>\n\n\n\n<li>The data should be automatically forwarded to your log analytics workspace and be available for analysis.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up solar on our farm in North Texas has been a treasure trove of data to analyze &#8211; everything from Inverter load, to PV watts in, to state-of-charge for the batteries &#8211; It&#8217;s a massive amount of useful information. So naturally I want that data in Azure Log Analytics. It&#8217;s perfect platform to handle &hellip; <a href=\"http:\/\/draith.azurewebsites.net\/?p=625\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MQTT Data to Azure Log Analytics&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure"],"_links":{"self":[{"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=625"}],"version-history":[{"count":2,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/625\/revisions"}],"predecessor-version":[{"id":627,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/posts\/625\/revisions\/627"}],"wp:attachment":[{"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/draith.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}