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
- ESP8266 microchip / Wemos D1 Mini (1,67 EUR)
- DHT22 temperature and humidity sensor (2,20 EUR)
- 10k resistor (0,47 EUR/100 pcs)
- Jumper wires and breadboard (3,25 EUR)
Putting it all together
Once everything has arrived, you just need to put it all together and you are ready to start!


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

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

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.

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

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

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.

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.
- MQTT Broker: Azure IoT hostname
envsensors-iot.azure-devices.net:8883
- MQTT Client ID: Azure IoT device name
kitchen
- MQTT Username: Combination of Azure IoT hostname and device name
envsensors-iot.azure-devices.net/kitchen/?api-version=2018-06-30
- MQTT Password: “SharedAccessSignature” part from Azure IoT device SAS token
SharedAccessSignature sr=envsensors-iot.azure-devices.net%2Fdevices%2Fkitchen&sig=sy9l3eK0aGNWAyOB8dM0AVKBDx9o3cXWIw9Vu2djQGA%3D&se=1621193273
- MQTT Topic:
devices/kitchen/messages/events/
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 sensor.pin=5
# 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 mqtt.server=envsensors-iot.azure-devices.net:8883
# Sets the MQTT client ID
mos config-set mqtt.client_id=kitchen
# Sets the MQTT username
mos config-set mqtt.user=envsensors-iot.azure-devices.net/bedroom/?api-version=2018-06-30
# Sets the MQTT password
mos config-set mqtt.pass='SharedAccessSignature sr=envsensors-iot.azure-devices.net%2Fdevices%2Fkitchen&sig=sy9l3eK0aGNWAyOB8dM0AVKBDx9o3cXWIw9Vu2djQGA%3D&se=1621193273'
# 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 new Stream Analytics input for Azure IoT Hub.

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

Create a new Stream Analytics query and select the columns that should be streamed to a Power BI dataset.
SELECT
EventEnqueuedUtcTime, temperature, humidity
INTO
powerbi
FROM
envsensorsiot

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.

Click on the dataset and select Create report.

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