Sorry, you need to enable JavaScript to visit this website.

You are here

Home IoT Project using Azure

BY Michelle Telstra 09 November 2020

In this blog series, I’m going to write about how Internet of Things (IoT) enabled my gate, garage door and garden irrigation system using Azure IoT Hub, ESP32, IFTTT, .Net Core and much more. 

 

This blog was originally posted here in a series of DIY IoT blogs by Trent Steenholdt, Managing Consultant at Telstra Purple on 03 Nov 2020. 

 

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.

 
  void publishAzuredata(char* event, unsigned int value){
  client.publish(event, value);
  }
  void subscibeAzuredata(char* event){
  client.subscribe(iothub_subscribe_endpoint);
  }
   
  void reconnect() {
  while (!client.connected()) {
  Serial.print("Attempting MQTT connection...");
  if (client.connect(iothub_deviceid, iothub_user, iothub_sas_token)) {
  Serial.println("connected");
   
  } else {
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println("try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
  }
  }
  }
   
  void callback(char* topic, byte* payload, unsigned int length) {
  if (String(topic) == "device/deviceID/gate") {
  if (value == "1") {
  digitalWrite(gatePin, HIGH);
  delay(2000);
  client.publish(topic, 0);
  digitalWrite(gatePin, LOW);
  }
  else if (value == "0") {
  //do nothing really
  //digitalWrite(gatePin, LOW);
  }
  }
  }
   
  void setup(){
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
  subscibeAzuredata("device/deviceID/gate");
  subscibeAzuredata("device/deviceID/garage");
  connect_mqtt();
  }
  void loop(){
  if (!client.connected()) {
  reconnect();
  }
  client.loop();
  }
view raw gistfile1.txt hosted with ❤ by GitHub

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

 

Related Blogs

's picture

By Michelle.idv

10/4/20

  • arduino
  • iot

Hands On: Long Range IoT Arduino MKR NB 1500 over the...

We put the Arduino MKR NB 1500 to the test using the Telstra network and show you how to
's picture

By Michelle.idv

10/4/20

  • hackathon
  • iot

Data Driven IoT Insights for sustainable communities:...

In the second TelstraDev challenge for GovHack 2020 (part 1 on Communication APIs here), we asked:
's picture

By Michelle.idv

10/4/20

  • iot
  • captis

Capture more insights with our new Captis Data Loggers!

Calling all IoT makers and innovators, there’s a new device on our IoT Marketplace! It
's picture

By Michelle.idv

10/4/20

  • iot
  • nbiot

What's the Difference Between Cat-M1 and NB-IoT?

Here’s what our previous CFO said which is good: https://exchange.telstra.com.au/telstra-
's picture

By Michelle.idv

10/4/20

  • iot
  • platform

Telstra's IoT Platform

The Internet of Things (IoT) is a network of physical objects which collects data from the
's picture

By Michelle.idv

10/4/20

  • iot
  • developer

Wireless Application Developers Guidelines for IoT

The Internet of Things (IoT) is an all-encompassing suite of technologies, with dozens of options
's picture

By praveen.kumar

8/3/18

  • arduino
  • mkr

Introducing the Arduino MKR NB1500

Last year we were proud to announce our partnership with Arduino to help create an (read more about