DIY Azure IoT Temperature Sensor with Power BI Visualization

In this post I want to show you how to build a temperature and humidity sensor and how to connect this sensor to Azure IoT Hub to visualize the data in Power BI.

As edge device I’m using a ESP8266 Wi-Fi microchip with a DHT22 temperature and humidity sensor, which sends the data via MQTT to Azure IoT Hub. Stream Analytics uses Azure IoT Hub as input source and streams the data to Power BI.

Shopping List

Putting it all together

Once everything has arrived, you just need to put it all together and you are ready to start!

Circuit Diagram
Assembled Wemos D1 Mini

Deploying Azure IoT Hub

Deploy a new Azure IoT Hub free tier resource to Azure.

Create a new consumer group

Once you have deployed the resource, create a new Consumer Group from the Built-in endpoints blade.

Azure IoT Hub shared access policies
Grab the connection string

Grab the Connection string—primary key of the iothubowner policy from Shared access policies blade of the Azure IoT Hub.

Now you need to create an Azure IoT device and a SAS token. Therefore you need to download the Azure IoT explorer.

Azure IoT explorer
Azure IoT explorer

Paste the connection string from the Azure portal into the appropriate field and click on Update.

Azure IoT explorer - Create new device
Create a new device

Switch to the Management tab and click on Create to create a new device.

Azure IoT explorer - Create new SAS token
Create a SAS token

Click on SAS Token… and create a SAS token for the newly created device. Keep the output for later.

Flashing the ESP8266 Microchip

For the ESP8266 microchip I decided to use Mongoose OS, an open source IoT firmware development framework. I’ve created a Mongoose OS app, which you can flash on to the ESP8266.

To flash the ESP8266, you need to download mos.exe.

Setup mos.exe

Select the COM port your microchip is connected to, as well as the platform (in our ESP8266) from the top dropdown menu.

Press CTRL+N to clone a Mongoose OS JS demo app from GitHub.

You will find a folder called app1 in the same directory as mos.exe is located in. I will recycle the file structure of the demo app, so just locate the file mos.yml and init.js and replace the content of those files with the following code.

Now execute the command mos build. Afterwards you can flash the app with the command mos flash.

Once the firmware is flashed you need to configure the Wi-Fi, as well as the MQTT settings.

Execute the following commands in mos.exe to configure the microchip:

# Connects to your Wi-Fi
mos wifi myWifiSsid myWifiPsk

# Sets the pin the sensor is connected to (Pin D1 on Wemos D1 Mini would be pin 5)
mos config-set

# Sets the MQTT topic the data is published to
mos config-set sensor.topic=devices/kitchen/messages/events/

# Sets the interval (in milliseconds) the data is sent to the MQTT broker
mos config-set sensor.interval=60000

# Sets the MQTT broker
mos config-set

# Sets the MQTT client ID
mos config-set mqtt.client_id=kitchen

# Sets the MQTT username
mos config-set

# Sets the MQTT password
mos config-set mqtt.pass='SharedAccessSignature'

# Sets the path to the well-known root CAs
mos config-set mqtt.ssl_ca_cert=ca.pem

# Enables MQTT
mos config-set mqtt.enable=true

The ESP8266 is now sending temperature and humidity data to Azure IoT Hub.

Deploying Stream Analytics

To stream the data from Azure IoT Hub to Power BI you need to deploy a Stream Analytics resource to Azure.

Create a Stream Analytics input

Create a new Stream Analytics input for Azure IoT Hub.

Create a Stream Analytics output

Create a new Stream Analytics output for Power BI and link it to your Power BI dashboard.

Create a Stream Analytics query

Create a new Stream Analytics query and select the columns that should be streamed to a Power BI dataset.

    EventEnqueuedUtcTime, temperature, humidity
Start the Stream Analytics job

Start the Stream Analytics job to stream the data to a Power BI dataset.

Creating Power BI Report

You’re almost done! You should now see a new dataset in your Power BI dashboard.

Create a new Power BI report

Click on the dataset and select Create report.

Customize your Power BI report

You can now create your own report and visualize your temperature and humidity data in Power BI.

Leave a Comment