Azure Stream Analytics saving lives!

Azure Stream Analytics watches sensors, control device autonomously, send Tweets and alerts in case of a hazard. Controllable by smartphone.

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
SparkFun Graphic LCD 84×48 – Nokia 5110
× 1
Texas Instruments LM2596 DC-DC 4.5-40V Adjustable Step-down Power Supply Module
× 1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
× 1
ElecFreaks MQ-5 Gas Sensor
× 1
KEYES flame sensor module
× 1
NXP 74LS00 TTL Quad 2-input positive NAND gates
× 1
ST STM32L4, STM32F7 STB75NF75 N-channel Power MOSFET
× 3
Water/Gas Flow Solenoid Valve Normally Closed
× 1
2-Wire Ignition Coil
× 1
Resistor 10k ohm
Resistor 10k ohm
× 2
Resistor 4.75k ohm
Resistor 4.75k ohm
× 1
Breadboard (generic)
Breadboard (generic)
× 1
33 Ohm 10W Resistor Wire Wound
× 1
Jumper wires (generic)
Jumper wires (generic)
× 1
SparkFun DC Barrel Jack Adapter – Female
× 1
Heatsink Heat Sink for Voltage Regulator or MOSFET
× 3
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
× 1

Software apps and online services

Microsoft Azure
Microsoft Azure
Visual Studio 2015
Microsoft Visual Studio 2015
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)
Corded Drill


Inexpensive heaters like the one pictured here are common in southeast Asia. In my country gas pressure fluctuates throughout the day in the hours of peak demand. Every year there are many lives lost to unattended heaters with either Carbon Monoxide poisoning, or gas leak when a momentary decrease in pressure causes the flame to go out,

Inspiration for the project came one winter night when my daughter’s room was too cold but I could not allow the heater to stay ON while she slept.

When I started designing the project, I thought, If I put a WiFi enabled controller like MKR1000 in there then I can do lot of cool stuff apart from safety. That’s where smartphone connectivity, auto-ignition using motorcycle parts came into picture. There is some good learning along the way like solenoid current control, level conversion by a NAND chip and unbricking a MKR1000!

Block Diagram

Heater is sending telemetry data to Azure through IoT Hub. The Stream Analytics job is doing near real time analytics on the device data and will post alerts based on the SQL request it is configured with. The Worker role will pick up the alerts generated by Stream Analytics and will forward the alerts to the device through IoT Hub.


Only attempt this project if you have sufficient experience in working with gas and electronics.

To avoid fire hazard, be sensible when attaching cardboard and electronics to gas appliance.

Hardware Setup

Please see the video at the start where I explain the hardware choices, also check out attached schematic.

At the time of building this project MKR1000 was not released and documentation was not public. My concern was the MQ5 gas sensor uses heater and the current demand may not be met if connected to MKR1000 power rail. To be safe I configured the buck converter to provide the 5V rail. Then used MKR1000 on board regulator to provide 3.3V rail.

If you got 3V compliant MOSFETs like FQP30N06L from Sparkfun, then you don’t need the Quad NAND chip, but you do need to pull your MOSFETs down by a 10k resistor.

I highly recommend using a desk power supply that can measure current. This is useful when you made a mistake in software or wiring and accidentally let a MOSFET ON or in floating state. The desk supply current draw will alert you before you smell the burning silicon 🙂

Dht11 install

Temperature sensor installation



Flame sensor

Flame sensor with cover


Added gas sensor

Making the box

I just used an old shoebox and cut it to my spec. Laser cut Acrylic sheet was my first choice but did not get my hands on it.

caption (optional)


The latest edition of Arduino SAMD board definitions did not work with MRK1000. I had to install older release 1.6.3 in order to get it working.

The following libraries are required to build Arduino Sketch

  • Azure Iot  by Arduino
  • WiFi 101 by Arduino
  • Adafruit Unified Sensor Library (for DHT)
  • Adafruit Gfx Library
  • Adafruit-PCD8544-Nokia-5110-LCD-library

WiFi Firmware

Make sure to follow instructions: to get your device to talk to Azure HTTPS. You need to use hostname of your IoT hub.

Bug workarounds

There was a compiler error on Adafruit LCD library which only happen with the ARM toolchain. Here is my fix for it:

--- /home/asad/Arduino/libraries/Adafruit_PCD8544_Nokia_5110_LCD_library/Adafruit_PCD8544.cpp	2016-03-31 04:29:58.000000000 
+++ /media/asad/AC78E1AF78E1788A/Users/Asad/Documents/Arduino/libraries/Adafruit_PCD8544_Nokia_5110_LCD_library/Adafruit_PCD8544.cpp	1979-11-30 00:00:00.000000000 
@@ -181,15 +181,15 @@
   // Set software SPI specific pin outputs.
   pinMode(_din, OUTPUT);
   pinMode(_sclk, OUTPUT);
   // Set software SPI ports and masks.
-    clkport     = (PortReg*)portOutputRegister(digitalPinToPort(_sclk));
+    clkport     = portOutputRegister(digitalPinToPort(_sclk));
   clkpinmask  = digitalPinToBitMask(_sclk);
-    mosiport    = (PortReg*)portOutputRegister(digitalPinToPort(_din));
+    mosiport    = portOutputRegister(digitalPinToPort(_din));
   mosipinmask = digitalPinToBitMask(_din);
 // Set common pin outputs.
 pinMode(_dc, OUTPUT);
 if (_rst > 0)

Unbricking MKR1000

Two days before submission I was hit by an unpleasant surprize that my MKR1000 stopped appearing as USB serial port and I was no longer able to program it. It was truly an anticlimax. There is no documentation on MKR1000 that could help me figure out the issue.

I noticed the JTAG debug port. I looked around and found Adafruit excellent write-up for Arduino Zero. It did not work out of the box but still I just need to workaround some compiler issue. I was able to restore the bootloader and my board returned to normal.

Development Tips

One weekend I needed to travel out of town and I wanted to work on this project. In absence of hardware I wrote a Universal Windows Application to simulate heater behaviour with Azure cloud.

It was such useful testing tool that even after returning back, I kept using it to try different scenarios and parts of the cloud setup which are either difficult, or too slow to do with actual hardware. The code for simulator is present in GitHub. To get an idea of the difficulty, see below video where I test out Humidity sensor on actual hardware.

caption (optional)

Below is the video when I tried the gas sensor first after installation.

When editing queries on Stream Analytic job, I found it useful to use “test” feature in old Azure management portal. Here you can upload json data and see the results of your query instantly.


Universal Windows App talks to MKR1000 on heater via Azure to turn the Heater On and set temperature.

In below video, I take the gas pipe off in order to trigger the gas sensor. Please notice the sensor values goes up on serial console and my Azure Stream Analytics job, sends a Tweet, a user notification as well as turns the heater Off.

In next video, I turned the gas off in order to cause an ignition failure. See how application gets notified of the issue.

Custom parts and enclosures

Flame sensor mount

Flame sensor needs an adjustable rail for proper operation without heat up.

Flame Sensor Cover

Flame sensor needs cover to protect from ambient sunlight.

LCD Mount

LCD can use a simple mount


Circuit Diagram

Please bear with me the hand drawn diagram. was down on the day of submission.


Smart Heater Azure

The Universal Windows Apps (UWA) and Azure Worker Roles projects require Visual Studio Community 2015. Arduino sketch required Arduino IDE 1.6.7 or better. 3D print STL files can be used by Slic3r application

This project won “Best In Show” in Microsoft Azure: Building The Enterprise IoT Cloud! contest — Read More


Source : Azure Stream Analytics saving lives!

Leave a Comment

= 5 + 1

(Spamcheck Enabled)

Read previous post:
Carbon Introduces SpeedCell System & Bigger 3D Printers
Carbon Introduces SpeedCell System & Bigger 3D Printers

Since 2013, the additive manufacturing startup Carbon had altered the 3D printing industry. Carbon produced its industry-changing M1 3D printer...

Scroll to top