While creating a temperature and humidity IoT project is the IoT “Hello World,” Marcel Meurer has put in a cool twist, connecting it to Azure and even PowerBI!
Build your own IoT device with ESP8266 system-on-a-chip, Azure Event Hub, Stream Analytics and Azure Storage
IoT is a big buzz word. I wanted to make it more tangible for me. So, I decided to build a small WiFi based sensor to monitor temperature and humidity, and to send these data to Azure. For my scenario at home I need less than 12 sensors, but the solution scales up to hundreds – even thousands – of sensors. That’s why I used Azure Event Hub and Stream Analytics.
The sensor ESP8266-01 – WiFi SoC
I want to build a very small sensor that can connect to my WiFi and execute a short program, as well. The System-on-a-Chip ESP8266-01 is ideal for this purpose. Its PCB Version 01 is flat and has a small form factor, less than 25mm x 15mm. The chip itself works with WiFi (including TCP/IP) and can be flashed with one’s own programs. It’s also brings two I/O ports (GPIO) to connect to – for example, a temperature/humidity sensor. In my case it’s a DHT22 – a digital temperature and humidity sensor. The ESP and the sensor together cost less than $7.
Prepare the Arduino IDE to program the chip
Now we can access the chip and send programs to it with the Arduino IDE. These programs are saved permanently and executed on start up. Open the Arduino IDE and include the board definition for ESP8266 chips: File -> Preferences -> Additional Boards Manager URLs and add http://arduino.esp8266.com/stable/package_esp8266com_index.json.
Building Event Hub and Stream Analytics services for IoT data
If it works as expected, we can now build our Azure services to collect data sent from our IoT device – after this we must extend our sketch to send data to the Event Hub.
To receive data from sensors/IoT devices I use an Event Hub and a Stream Analytics service in Azure. I don’t use an IoT Hub because I only want to have a one-way transmission to the cloud and don’t need to control my sensor(s).
Generate a long-lasting authorization code for the sensors
The sketch we built before must now be extended to send the data to Azure. But first we need an Authorization code to communicate with the REST API. We can create this code based on the saved connection string from the shared access policy “messages.”
Extend the sketch to send data to the Event Hub
To send data to the Event Hub we must extend our sketch (see below). For this, we need some variables to configure the Azure connection:
And after a few minutes in the running Stream Analytics job you will see:
If you connect it to PowerBI, you can have these charts: