In this blog series, I’m going to write about how I Internet of Things (IoT) enabled my gate, garage door and garden irrigation system using Azure IoT Hub, ESP32, IFTTT, .Net Core and much more.
Custom IoT solutions are relatively new to me. Of course, IoT is everywhere these days, but I’ve never worked with my own micro-controllers and utilised services and protocols like Azure IoT Hub, and MQTT. This blog series will not be about best practices in how to do this, but more about my experience and learnings.
Introduction to the project
When COVID-19 lockdowns started in Australia around mid-March 2020, I like many others, ended up having a bunch of free time. With this free time I embarked on some big DIY projects, including putting up a driveway gate and wiring up a motor to it, so it opens like a garage door with a remote. The gate was fantastic, but now, I was all of a sudden stuck with two remotes, one for the gate that used a unique frequency and one for the garage that used a rolling code. At this point, I was wondering what I could do to fix this as there is no universal remote out there to do it all! Trust me, I looked!
A few ideas came to mind, like wiring up the normally open (NO) loops of both gate and garage together. This, however, created the issue if I just wanted to open one, I couldn’t. At this point, I knew the option I had to go down was something entirely custom, so here we are.
Let's get started
First, we need some hardware to run this custom solution. Something like an Arduino, Rasberry Pi, ESP8266 or an ESP32. I wanted something cheap and did all the things, so here is where I made my first mistake and brought a custom ESP32 PCB from eBay. Within 5 minutes of having the PCB, I had killed the ESP32 chip!
My recommendation if you’re starting with IoT, buy a hardware option from a known manufacturer with lots of documentation. Custom PCB’s, while sounding like a great idea (“Hey, this does everything, I can’t go wrong”), are very lacking in the detail. My mistake by sending 5 volts somewhere I shouldn’t have on the PCB was ultimately because the PCB schematic was wrong. Turns out I got a different iteration of the PCB from the documentation they sent me.
What I ended up doing is buying from a local store my second ESP32 development board that had integrated USB and a separate 4-ch relay controller. Not only did I have documentation of the pin layouts, but the people at my local store were super helpful about how I should tackle the project.
So with the hardware, I started putting it all together. Thankfully, following the tip from the guys at my local store and some great videos on YouTube, I was able to get started really quickly. I did spend some time however getting my IDE set up correctly, so I’d encourage you, learning from my second mistake, to start very small with an example sketch in say Arduino IDE or VSCode and build on from there.
With everything right to push code from my local PC, I had wired up the first relay to the appropriate GPIO. I had compiled the C++ sketch/code to make use of all four relays with a simple web server front-end. Sure, I only have the gate and garage to control, but why not think big!
I was pretty excited at this point. I could control my gate with just going to an IP address on the network and presto, no more need for two remotes!
The real-world test the very next day
Having this all set up, I learnt something quickly, when not on the network, a web server is no good to me! A bit more back story here, the gate I’ve installed blocks any access to the front of the house and subsequently, the meter boxes. This design decision became a sticking point when the meter reader rang my Ring doorbell on the gate, and I couldn’t let him in because the ESP32 web server was inaccessible. At that point, I knew I had to connect my ESP32 to something like IFTTT so I could control it remotely.
Now, this is where Azure IoT Hub and the power of MQTT come in. Essentially, I needed my ESP32 to be subscribing (listening) to Azure IoT Hub for it to do a thing when I triggered a change.
The way I describe MQTT subscribing is that the ESP32, the nagging child, is continually nagging mum, the Azure IoT Hub for the chocolate bar! When Azure IoT Hub finally says yes after persistently saying no, as in changes from 0 to 1, little child ESP32 gets their chocolate (opens the gate).
Setting up the subscribing and publishing of values was relatively straight forward. In my C++ sketch, all I needed to do was to connect to the Azure IoT Hub and set up a function in the loop() that checked if the value had changed, do the function to open the gate and then, have another function then publish back a 0 again.
To interface with Azure IoT Hub itself and change the value of 0 to 1, I had a few options like a Logic App or Function App in Azure. However, to keep it simple, I decided to stick with what I know and write a PowerShell script hosted in an Automation Account Runbook that I could call with a WebHook. That way I could do an HTTP Post with a JSON body that could say open the gate or garage or even both at the same time.
Now that I have the WebHook, I could theoretically call it from anywhere in the world, but I wanted to have a nice way of doing it besides using Postman. Here is where I leveraged IFTTT applets, one for my phone (push notification with IFTTT app when Ring Doorbell rung), and another for Google Assistant.
The result is the ability to control my gate and garage from anywhere in the world, securely and quickly.
Presto, open sesame!
In the next part of this series, I’ll dive into how I integrated my ESP32 with my Bluetooth enabled garden irrigation system so I can now trigger garden watering from anywhere and using data from the Bureau of Meteorology! Stay tuned