Internetting all the Things with ESP8266 and Azure Event Hubs


The ESP8266 is an amazingly cheap and super powerful chip made by Espressif Systems that can be used to internet-enable virtually anything. For around $3 you get an wifi enabled chip that can be used to create sensor networks, create home automation solutions, etc. For a few bucks more you can get an ESP8266-based development board with more GPIOs, Analog-Digital conversion, PWM, a voltage regulator and much more with the Adafruit ESP8266 HUZZAH or the NodeMCU Development Board. Both are awesome, for this blog post I’m using the Adafruit HUZZAH ESP8266 Breakout board, but I’ve also got this working with the NodeMCU board.

Adafruit HUZZAH ESP8266, ESP-01, NodeMCU Development Board

For this project, I wanted to create a temp, humidity and light logger that would stream data to both Thingspeak, so I can easily visualize the data, and to Azure Event Hubs so I can perform additional analytics.  To read temp & humidity, I’m using the DHT11 sensor. For measuring light, I’m using a simple photocell.  I’m using the Adafruit ESP8266 Huzzah as the development board, and am programming it using the Arduino IDE. This post over on Adafruit will give you everything you need to know about setting up the development environment: Since the components are so cheap, I can create a fleet of these things and deploy them throughout my house if I wanted to get crazy.



I’ve wired the photocell to the single analog pin (A) on the board. I have a voltage divider on the photocell with a 10K resistor and a 440 Ohm resistor. It took some experimenting to get this right, but with these values the input values on the A pin max out at 1024 right when all of the lights in my basement are turned on, so I’m happy with it. The DHT11’s data pin is wired to digital input pin #2 with a 10K resistor. 

The Code:

Sending data to Thingspeak is easy, all it takes is an HTTP GET with the different data values (temp, humidity, light) passed as querystring parameters. I’m sending the data every 10 seconds and get nice charts that look like this:


Azure Event Hubs supports massive amounts of data ingestion. So, if I needed highly scalable solution, Event Hubs would be able to handle millions of events per second. It also allows me plug in adapters so I can react to trends in the data (e.g. temp in the basement is steadily increasing over time, do X). At this time, the ESP8266 doesn't support HTTPS (that I can tell, let me know in the comments if I’m wrong here), which is a requirement if we're going to post a message to an Azure Event Hub via their REST API. As an easy workaround I created a simple node.js gateway that receives messages from the ESP8266 and then sends them to my Azure Event Hub. I can easily deploy this gateway to a microcontroller such as a Raspberry PI on the same secure WIFI network as my sensors, which would then relay the messages to Azure Event Hubs over HTTPS.

Arduino Code:

The node.js gateway

This code is using the event-hub-client npm package, which seems to work well.

Stream Analytics

Once the messages are sent to Event Hubs, we can use Azure Stream Analytics to filter, analyze, aggregate and react to the events that are being sent. For instance, I can create query that shows me the average temp, humidity and light in a rolling 30 second window:


There’s much, much more that can be done with Stream Analytics, take a look at the docs to go a bit deeper:


In conclusion, we were able to construct an IoT sensor network using ESP8266, send actual temp, humidity and light info to both ThingSpeak and Azure Event Hubs, and then we concluded by performing a bit of analysis on the data sent to the event hubs.