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
Software apps and online services
Hand tools and fabrication machines
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, http://goo.gl/JvEHK7 or gas leak when a momentary decrease in pressure causes the flame to go out, http://goo.gl/Y4JxfJ
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!
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.
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
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.
After fitting I realized that my solenoid was leaking gas. I open it up and discovered hole in its disk. A little super glue fixed it.
I needed a mount for flame sensor so that I can adjust it for best view of flame without getting lot of heat. 3D printing solved that issue. In addition the flame sensor needs to be protected from ambient sunlight. I 3D printed a cover for that purpose.
LCD mount was needed to raise it close to cover for easier readout.
LCD is running screensaver. The code is part of my Sketch just not enabled in the final version.
Azure Cloud Setup
In order to run the applications you will need to do the following:
- Create an IoT hub that will receive data from devices and send commands back to it
- Create pair of Event hub and Stream Analytics job that will read data from the IoT hub and post alerts into the Event hub
- Create another pair of Stream Analytics job and Event hub for bridging controllers and devices.
- Create a Storage account that will be used by the worker roles.
- Deploy worker roles that will read alerts from the Event hubs and forward alerts to devices through the IoT hub and send Tweets.
Create an IoT Hub
- Log on to the Azure Portal.
- In the jumpbar, click New, then click Internet of Things, and then click IoT Hub.
- In the New IoT Hub blade, specify the desired configuration for the IoT Hub.
- In the Name box, enter a name to identify your IoT hub. When the Name is validated, a green check mark appears in the Name box.
- Change the Pricing and scale tier as desired. This application required a paid tier that allows multiple devices.
- In the Resource group box, create a new resource group, or select and existing one. For more information, see Using resource groups to manage your Azure resources.
- Use Location to specify the geographic location in which to host your IoT hub.
- Once the new IoT hub options are configured, click Create. It can take a few minutes for the IoT hub to be created. To check the status, you can monitor the progress on the Startboard. Or, you can monitor your progress from the Notifications section.
- After the IoT hub has been created successfully, open the blade of the new IoT hub, take note of the Hostname, and select the Key icon on the top.
- Select the Shared access policy called iothubowner, then copy and take note of the connection string on the right blade. Also take note of the Primary key
- Your IoT hub is now created, and you have the Hostname and connection string you need to update in software.
- For the creation of the Stream Analytics job Input, you will need to retreive some informations from the IoT Hub:
- From the Messaging blade (found in the settings blade), write down the Event Hub-compatible name
- Look at the Event-hub-compatible Endpoint, and write down this part: sb://thispart.servicebus.windows.net/ lets call this one the IoTHub EventHub-compatible namespace
- For the key, you will need the Primary Key read in step #6
Create two Event Hubs
- Log on to the Azure Management Portal.
- In the lower left corner of the page, click on the + NEW button.
- Select App Services, Service Bus, Event Hub, Quick Create
- Enter the following settings for the Event Hub (use a name of your choice for the event hub and the namespace):
- Event Hub Name: “myeventhubname”
- Region: your choice
- Subscription: your choice
- Namespace Name: “mynamespacename-ns”
- Click on Create Event Hub
- Select the mynamespacename-ns and go in the Event Hub tab
- Select the myeventhubname event hub and go in the Configure tab
- in the Shared Access Policies section, add a new policy:
- Name = “readwrite”
- Permissions = Send, Listen
- Click Save, then go to the evnet hub Dashboard tab and click on Connection Information at the bottom
- Write down the connection string for the readwrite policy name.
Create two Stream Analytics job
- Log on to the Azure Portal.
- In the jumpbar, click New, then click Internet of Things, and then click Stream Analytics Jobs.
- Enter a name for the job, choose your subscription, select the same resource group you created when creating the IoT Hub, Select the location, and click on Create.
- Once the job is created, click on the Inputs tile in the job topology section. In the Inputs blade, click on Add
- Enter the following settings:
- Input Alias = “gassense”
- Type = “Data Stream”
- Source = “IoT Hub”
- IoT Hub = “myiothubname” (use the name for the IoT Hub you create before)
- Shared Access Policy Name = “iothubowner”
- Shared Access Policy Key = “iothubowner Primary Key” (That’s the key you wrote down when creating the IoT Hub)
- IoT Hub Consumer Group = “” (leave it to the default empty value)
- Event serialization format = “JSON”
- Encoding = “UTF-8”
- Back to the Stream Analytics Job blade, click on the Query tile.
- The Query of gas sensor check and tweet job will be:
gassense.gassense > 1500
- Back to the Stream Analytics Job blade, click on the Outputs tile and in the Outputs blade, click on Add
- Enter the following settings then click on create:
- Output Alias = “gassense4phone”
- Source = “Event Hub”
- Service Bus Namespace = “mynamespacename-ns
- Event Hub Name = “myeventhubname”
- Event Hub Policy Name = “readwrite”
- Event Hub Policy Key = “Primary Key for readwrite Policy name” (That’s the one you wrote down after creating the event hub)
- Partition Key Column = “4”
- Event Serialization format = “JSON”
- Encoding = “UTF-8”
- Format = “Line separated”
- Back in the Stream Analytics blade, start the job by clicking on the Start button at the top
- The query for the bridge job will simply be:
I also tried a Stream Analytics job to montor flame sensor with followin query:
gassense.flamesense < 500
But end up not using it.
Please note that flame sensor value go down when it sees flame, while a gas sensor values go up when it smells gas.
Create a storage account
- Log on to the Azure Portal.
- In the jumpbar, click New and select Data + Storage then Storage Account
- Choose Classic for the deployment model and click on create
- Enter the name of your choice (i.e. “mystorageaccountname” for the account name and select your resource group, subscription,… then click on “Create”
- Once the account is created, find it in the resources blade and write down the primary connection string for it to configure the worker role
Deploy the worker role
Azure Worker roles are to trigger alerts through IoT Hub. To build and deploy the workers role here are the steps:
- Open the solution events_to_device_service.sln in Visual Studio 2015
- open the file app.config and replace the fields below with the connection strings from the event hub, the storage account and the Iot Hub
<add key="Microsoft.ServiceBus.ConnectionString" value="[EventHub Connection String]" />
<add key="Microsoft.ServiceBus.EventHubName" value="[Event Hub Name]" />
<add key="AzureStorage.AccountName" value="[Storage Account Name]" />
<add key="AzureStorage.Key" value="[Storage Account Key]" />
<add key="AzureIoTHub.ConnectionString" value="[IoT Hub Connection String]" />
Compile the project and publish to Azure
Create a new device identity in the IoT Hub
To connect your device to the IoT Hub instance, you need to generate a unique identity and connection string. IoT Hub does that for you. To create a new device identity, you can use Device Explorer tool (runs only on Windows for now)
You need to install the following features in order to build and run the application and Azure web role projects.
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
Make sure to follow instructions:
https://github.com/arduino-libraries/WiFi101-FirmwareUpdater to get your device to talk to Azure HTTPS. You need to use hostname of your IoT hub.
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.
// 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.
if (_rst > 0)
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.
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.
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.
I like to thank Microsoft for extending me free trial of Azure so that I can play around with this commercial grade technology as a hobby project.