DEV Community

Cover image for Building a Heart Rate Monitor with Azure IoT Hub
Chloe Condon πŸŽ€ for Microsoft Azure

Posted on

Building a Heart Rate Monitor with Azure IoT Hub

If you've ever built an IoT project from scratch, then you know how difficult it can be to start. From acquiring the right hardware, to choosing the right tools, to testing out compatible APIs- getting started with IoT can sometimes make you want to throw your computer across the room... πŸ˜–

snl computer smash gif

Lucky for you- Beau Hayes-Pollard (Brookline High School, Class of 2022 πŸŽ“) has gone through all that for you and has created this amazing tutorial for his HeartRate Monitor with Azure IoT Hub!

screenshot of beau demoing project

While working as a mentor with the most recent cohort of students in BitCamp, I had the pleasure of watching Beau go through the high-highs and low-lows of debugging + troubleshooting how to make his very first IoT hardware project. 🀩 This project uses Azure IoT Hub to trigger an Azure Function when a resting heart rate above 100 bpm is detected. The function uses the Twilio API to send a warning message to the user's inputted contacts, ensuring that appropriate actions can be taken quickly. Beau got a chance to learn about technology and build his own ingenious, cheap, and easy-to-use health monitoring tool through the BitCamp program.

Lucky for you- Beau made a tutorial, so you don't have to start from scratch to build this project on your own! So, get that heart rate up, and enjoy building and testing this fabulous IoT Heart Rate Monitor tutorial- happy coding y'all! πŸ’™:

Alt Text

IoT Heartrate Monitor using Azure Applications (with ESP8266 and HR Sensor)

For this project, I wanted to create an easy-to-use, efficient, and cheap solution to an expensive Hospital Quality HR Monitor. This device would be so unbelievably simple, even your grandma and grandpa could use it. However, the setup process is a bit more rigorous so make sure you have enough snacks. This tutorial will be split into a few different parts: #1 The hardware: connecting wires, plugging in cables, and debugging. #2 The software: setting the hardware up using the Arduino IDE and programming the ESP8266 (a Wi-Fi microchip) to connect to the Azure IoT Hub. Lastly, #3 Azure: connecting the Azure Applications to each other (i.e. IoT Hub β†’ Event Grid β†’ Event Subscription β†’ Logic App (basically an Azure Function) β†’ Twilio API).

Prerequisites

Before we get started, make sure you have the following: an azure subscription, you can make one for free. This will allow us to use server-less applications and run our program in the cloud. Secondly, make sure you have the following hardware: Adafruit Huzzah Feather (ESP8266), Breadboard Cables, and a DFRobot HR Sensor. Although you don't need to have the exact replicas, make sure your hardware is somewhat similar. Lastly, make sure you download the Arduino IDE and this ESP8266 Driver (your Huzzah/ESP8266 won't connect without it πŸ€—).

Connecting the Hardware

After you have unboxed all your packages and remembered to pop the bubble wrap, you will need to connect the DFRobot HR Sensor to the Huzzah. You will then have to connect the Huzzah to a power source (in this case, a computer so we can flash code onto it!) To do this, you will want to connect the three-prong cable (included with the HR Sensor) to 3 different breadboard cables. Then attach those breadboard cables to specific ports on the Huzzah. The three-prong cable is universal so it doesn't matter which breadboard cable you plug into which socket!

Alt Text

The names of the ports are the 3v3, GND, and A0/ADC. If you are having trouble finding them, I have attached a image of the Huzzah wiring here. If you are having issues, think of it as the 2nd, 4th, and 5th port on the reset button side of the Adafruit Huzzah Feather. Once your HR Sensor and Huzzah are connected, simply plug in your Huzzah to your computer with a micro USB cable. When everything is done, open up the Arduino IDE and make sure you install the driver (ESP8266 Driver) for compatibility between devices!

Coding the Software

You should already have the ESP8266 driver installed (link above) and Arduino IDE (also linked above) ready to go. The first thing you need to do is open up the Arduino IDE and open up the files linked in the GitHub. Once the files have been opened, you should see a file called config.h. In there, you should change the following: your WIFI SSID, WIFI PASS, and CONNECTION STRING. Your SSID and PASS are just the username and password for your home Wi-Fi. Make sure your internet doesn't have any extra steps (e.g. account login, redirect) To find your connection string, you must create an Azure IoT Hub. If you have trouble, check out this helpful article. Once you have created an IoT Hub, follow these steps to find your connection string. Go to the Azure Portal and find your IoT Hub. On the left side, you should see Shared Access Policies. Then click on Service and you will see four strings. The string titled Connection String -- Primary Key is the one you will want to copy and paste. Make sure you don't share your connection string, it is super important!

Alt Text

After you have finished configuring your config.h, you should have four different files named config.h, iothubClient.ino, main.ino, and message.ino opened. To flash your program to the ESP8266/Huzzah, verify your code and press the upload button in the top left corner. Before we do this, lets set up the rest of our Azure Applications! Below is an appropriate state of the system moving forward:

Finalizing Azure Applications

You're on the final stretch! There are only a few more things we need to do. We are gonna start off by creating a Logic App. Azure Logic Apps allow us to use triggers to call actions like reading an email, interacting with a database, and sending a text message. The first step is to create our own logic app. Follow this tutorial until you have to set a trigger. For our project, we will be using HTTP Requests. An HTTP request is just a format for requests and responses between a client and a server, passing data across components.

Alt Text

Once your logic app is setup, go to the logic app designer and create a new trigger. You should see the trigger When a HTTP request is received. After selecting it, you will see a box for Request Body JSON Schema. In the GitHub repo, you should see a folder called JSON. Open up the file called package.json and copy the code. Then click the blue text called use sample payload to generate schema. Paste the code and press done. If you get an error, try using the backup.json file. Next, create an action for your trigger. For this project, we need to use the Twilio API. Twilio is a messaging API that allows us to send SMS messages when our HTTP request is triggered. If you have any trouble making an account and linking your ID and TOKEN, follow this tutorial.

Alt Text

If everything went smoothly, you should now have a fully functional Azure Logic App that sends an SMS when an HTTP request is triggered! Lastly, we need to create an event subscription in our IoT Hub. This basically monitors incoming data and reacts to it. In this case, we want it to trigger the HTTP request using a webhook. Before we setup the event subscription, we need the HTTP Post URL. You can find this by opening your Logic App Designer and clicking on your trigger. Copy the link at the top and save it for later! Return to the Azure Portal and locate your IoT Hub. Once you're in your IoT Hub, go to Events and click New Subscription. Create a name, make sure the Event Schema is set to Event Grid Schema, and the filter is set to Device Telemetry. For the final step, set the Endpoint Type to Webhook and paste in the link you copied earlier. When you are finished, press Create.

Congratulations, you finished. Flash your Huzzah, do some jumping jacks, and give yourself a huge pat on the back! πŸ₯³

Top comments (1)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Wow this is really cool!
Wish there where more IOT posts