Introduction: IOT Heart Rate Monitor (ESP8266 and Android App)
As part of my final year project I wanted to design a device that would monitor your heart rate, store your data on a server and notify you via notification when your heart rate was abnormal. The idea behind this project came when I tried to build a fit-bit app that notifies a user when they're having a heart problem but I couldn't figure out a way of using real-time information.
The project has four main parts including the physical circuit for measuring the heartbeat, an ESP8266 Wi-Fi module with signal processing code, the server for storing the code and an Android app for displaying the heart rate.
A video detailing the physical circuit can be seen above. All the code for the project can be found on my Github.
Step 1: The Circuit
There are two main methods to measuring a heartbeat but for this project I decided to use photoplethysmography (PPG) which uses an infrared or red light source which is refracted through the first few layers of skin. A photo sensor is used to measure the change in light intensity (when blood is flowing through a vessel).
PPG signals are incredibly noisy so I used a band pass filter to filter out the specific frequencies required. A human heart beats between 1 and 1.6 Hz frequency.
The op-amp I used was the lm324 which had the best voltage-offset of all the op-amps that were available to me. If you are recreating this project then a precision op-amp would be a much better choice.
A gain of only two was used because the maximum voltage tolerance on the ESP8266 is 3.3v and I didn't want to damage my board!
Follow the circuit above and try to get it working on a bread board. If you don't have an oscilloscope at home you can plug the output into an Arduino and plot it but make sure that the voltage isn't higher than the arduino or microcontroller's tolerance.
The circuit was tested on a bread board and a change in the output was observed when a finger was placed across the LED and photo transistor. I then decided to solder the board together which wasn't shown in the video.
Step 2: The Signal Processing Code and Server Communications
I decided to use the Arduino IDE on the ESP8266 because it's so easy to use. When the signal was plotted it was still very noisy so I decided to clean it up with an FIR moving-average filter with a sample number of ten. I modified an example Arduino program called "smoothing" to do this.
I experimented a little in order to find a way of measuring the frequency of the signal. The pulses were of varying length and amplitude due to the heart having four different types of pulses and the characteristics of PPG signals. I chose a known middle value that the signal always crossed as a point of reference for each pulse. I used a ring buffer to determine when the slope of the signal was positive or negative. The combination of these two allowed my to calculate the period between the pulses when the signal was positive and was equal to a specific value.
The software produced a fairly inaccurate BPM which couldn't actually be used. With additional iterations a better program could be designed but due to time constraints this wasn't an option. The code can be found in the link below.
Step 3: The Server and Data Communications
I decided to use Firebase to store the data as it is a free service and is very easy to use with mobile apps. There is no official API for Firebase with the ESP8266 but I found the the Arduino library worked very well.
There is an example program that can be found on the ESP8266WiFi.h library which allows you to connect to a router with the SSID and the Password. This was used to connect the board to the internet so data could be sent.
Although storing data was easily done there is still a number of issues with sending the push notifications via a HTTP POST request. I found a comment on the Github which used a legacy method of doing this via Google cloud messaging and the HTTP library for ESP8266. This method can be seen in the code on my Github.
On Firebase I created a project and used the API and registration keys in the software. The firebase cloud messaging was used with the app in order to send push notifications to the user. When the communications were tested data could be seen in the database while the ESP8266 was running.
Step 4: The Android App
A very basic Android app was designed with two activities. The first activity signed the user in or registered them using the Firebase API. I researched the datasheet and found various tutorials on how to use Firebase with a mobile app.
The main activity which displayed the user's data user a real time event listener so there was no noticeable delay in changes to the user's BPM. The push notifications were done using Firebase cloud messaging that was mentioned previously. There is a lot of useful information on the Firebase datasheet on how to implement this and the app can be tested be sending notifications from the dashboard on the Firebase website.
All the code for the activities and the methods for the cloud messaging can be found in my Github Repository.
Step 5: Conclusion
There were some major issues with measuring the BPM of the user. The values varied greatly and were not usable to determine a user's health. This boiled down to the signal processing code that was implemented on the ESP8266. After additional research I found out that a heart has four different pulses with varying period so it was no wonder that the software was inaccurate. A way of combating this would be to take an average of the four pulses in an array and calculate the period of the heart over those four pulses.
The rest of the system was functional but this is a very experimental device that I wanted to build to see if the object was possible. The legacy code that was used to send push notifications will soon be unusable so if you are reading this in late 2018 or late a different method would bee required. This issue only occurs with the ESP though so if you wanted to implement this on a WiFi capable Arduino it would be no problem.
If you have any questions or issues please feel free to message me on Instructables.