Getting Started with Microsoft Azure® and Wireless Temperature Humidity Sensor
The Azure is an IoT platform that provides full-duplex communication between the IoT devices and solution backend. Azure provides Platform as a Service(PaaS) that is a complete deployment and development environment in the cloud.
Using Azure IoT hub we can manage, connect and analyze billions of IoT devices into our IoT architecture. It provides two-way communication between devices with enhanced communication.
In this post, We will be using ESP32 as an Azure IoT gateway. Here We will learn about:
- Creating an IoT hub and registering a gateway device
- Setting up ESP32 board
- IoT long-range Wireless Temperature and Humidity sensor
- Making connections between the Wireless temperature sensor and ESP32
- Flashing ESP 32 and Collecting Sensor data from Wireless Temperature and Humidity sensor
- Analyzing and visualizing the sensor data in charts and graphs using Power BI
IoT Long Range Wireless Temperature Humidity Sensor
These are Industrial grade long range temperature and humidity sensors with a sensor resolution of ±1.7%RH ±0.6° C. Powered by just 2 AA batteries (included) with an operational lifetime of 500,000 wireless transmissions, you can expect to achieve up to 10 years of battery life depending on environmental conditions and the transmission interval can be chosen by you. Optionally, this sensor may be externally powered. These sensors have 2 Mile Range with On-Board Antenna. The range can be boasted up to 28 miles using mesh networking architecture.
Hardware needed for this Setup
Setting up ESP32 as Azure® IoT Gateway
ESP 32 is a low power SoC with integrated BLE and WiFi. We have used ESP32 as Azure IoT gateway. Setting Up ESP32 boards in your Arduino environment is fairly easy
- Download latest version of Arduino IDE
- Go to File -> Preference
- In Preferences page add this https://dl.espressif.com/dl/package_esp32_index.jsonURL in Additional Boards Manager URLs
- Now go to Tools -> Board -> BoardManager and search for ESP32 board and Install.
Here we are going to illustrate, how to collect sensor data from Wireless Temperature and Humidity Sensors, How to connect ESP32 to Azure, using MQTT link. Sending data to Azure and receiving messages from Azure.
Collecting Sensor data
- Create variables for storing temperature, humidity, battery, and other sensor values
- set the Rx, tx pin, baud rate and parity bits for the hardware serial
- First, check that there is something to read using Serial1.available()
- Check for 0x7E which is start byte
- humidity, temperature and battery values will be containing 2 bytes of data
- get the sensor name, type, sensor version will be containing 1 byte of data and can be got from there respective address
Connecting to Azure
- Include AzureIotHub.h, Esp32MQTTClient.h header files to set up a connection with Azure IoT hub
- Enter your connection String
- Create char variable to store your JSON, in this case, we have created messageData to store JSON
- Send the message Data to azure IoT hub
ESP32 Azure firmware can be found here.
Setting Up Azure
- Login to your Azure Portal
- We can monitor and analyze our IoT devices using IoT hub, For that choose Create a resource from the navigation panel
- Select Internet of Things and Search for IoT Hub.
- Click on Create to enable it
After Subscription. Select Basics
- Give some name to your IoT hub
- Select your subscription
- Create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list.
- Select Region
After this click on Size and Scale tab
- Either, you can take the defaults and just click Review + create at the bottom.
- Pricing and Scale tier You can adjust tiers depending on your features and messages you want to send per day. In free tier, you will get the provision to connect 5000 devices and send 8,000 messages per day.
- IoT Hub units: number of messages allowed per unit per day
Review your Settings and click on Create
Registring devices to IoT Hub
Before proceeding further, Add IoT devices to IoT hub.
- Go to IoT hub navigation panel and search for IoT devices
- Click on Add to add IoT device
- Enter unique deviceId
- Enter Primary and Secondary keys
- Save the device
You can find access keys and connection String in iothubowner.
- Search for Shared access policies in IoT hub navigation panel
- Click on iothubowner
- You will find Shared access keys on the iothubbowner panel on the right, copy the Connection String to the clipboard and use it in your project
Creating Stream Analytics Job
Create another resource and search for stream analytics job. The stream analytic job helps to monitor the device stream.
- Enter a unique job name
- Select the default Subscription
- Select the existing Resource group
- Select Location
- Select streaming units (default 3)
- Click on Create
Monitor your Stream
First, we need to configure the inputs, we are feeding to the IoT hub as follows
- Select Inputs from Stream Jobs navigation panel
- Give a unique name to input
- Select your IoT hub
- Save your settings
Now, we need to configure the output. Select the suitable sink, We are selecting Power Bi. More on Power Bi can be read in the next topic. It can be done as follows.
- Select Outputs from Stream Jobs navigation panel
- Give a unique name to output
- Name your dataset
- name your Table
- Authorize to azure
- Save your settings
Next, we need to configure the query. It can be done as follows.
- Update your query
- add the input to FROM query
- add the output to INPUT query
- Save your settings
Start your Stream
Click on start button to start your stream. The stream will consist of all the data here we are sending Temperature and Humidity data with UTCEnqueueTime and UTCProcessTime in JSON format.
Intro to Power BI
We are using Power BI to visualize our data. It provides interactive ways to visualize our data. This data can be interpreted in the form of Line charts, Bar graphs, Pie charts etc. Start by creating an account in Power Bi and sign in into your account.
Setting up Power Bi
- Scroll down navigation panel on the left and we will see our dataset listed at the end.
- There are different graphs and charts listed in the Visualization panel, Create your first chart by selecting any of the graphs from the Visualization panel.
- From the fields panel you can select different values from your table, we are feeding the values of Temperature and humidity wrt time in the graph
- Hit Refresh to get new values from the stream
- Save the graph as a new report
Exporting data to Power Bi
- We can see our reports listed at the end of the navigation panel
- Click on those three dots at the top right corner of the graph to export data to excel
- Click on Export Data
- Select File format and Hit Export
- Now, we can even visualize our data on Excel
Here we are visualizing our sensor data at different dates and time. We can see the percentage change in Humidity, Temperature and Battery Usage during different days with there values respectively.