Β
In our last post, we showed you how to create a DIY heart rate monitor using anΒ Arduino and a Pulse Sensor Module.
Β
Along with the components required for the heart rate monitor, that is, the Arduino, jumpers and a Pulse sensor module, you will also need aΒ Raspberry PiΒ and a working internet connection.
Β
The method weβll be employing to stream the BPM over internet is byΒ programming the Raspberry Pi to read the data from Arduinoβs serial monitor and publish it to an online free service like ThingSpeak.Β This service will allow the user to sign in to ThingSpeak from anywhere around the world and read the sensor data.
Β
WithoutΒ further ado, let us get started.
Β
Ensure that the Arduino is fully set up and is actively running the "Getting started to BPM" example code.
Β
Browse the Best Selection of Raspberry Pi Camera.
1.Β Make a ThingSpeak account and a channel toΒ stream data
- Sign up for ThingSpeak (you can follow thisΒ linkΒ to their website)
- Select the βChannelsβ page on the top ribbon and click on βNew Channelβ
- Fill in a suitable name and description andΒ click on βSave Channelβ
- Open your newly created channel and click on βAPI Keysβ
- Copy down theΒ key, as this will be required for the Raspberry Pi to connect to your channel
2. Setup the Raspberry Pi
- Make sure your Raspberry Pi is set up in a headless mode andΒ connected to Wi-Fi, so you can connect to your Raspberry Pi remotely via your PC. Weβll be using a software called PuTTY to SSH into your R-Pi. Open PuTTY, type in the IP address your R-Pi is connected and login when prompted for the user ID and password.
- If your Raspberry Pi isnβt set up in headless mode, you can connect you Raspberry Pi to an external display, connect to WiFi, and execute the following instructions via itβs built-in terminal.
- Connect your Arduino to Raspberry Pi using the Arduinoβs USB cable
- Navigate to documents by typing the following command
Β Β Β Β Β cd Documents
- To create a new python script, execute
- nano heartbeat_streaming.py
- In the nano editor,Β paste the following code. Make sure that your indentations are correct as the python compiler might later throw an error when you try to run the script.
Β
import serialΒ Β
importΒ http.client as httpΒ Β
importΒ urllibΒ Β
ser =Β serial.Serial('/dev/ttyACM0',9600)Β Β
key = "<ENTER YOUR THINGSPEAK KEY HERE>"Β Β
defΒ upload_to_ts(val):Β Β Β Β Β Β
Β Β Β Β params =Β urllib.parse.urlencode({'field1':Β val, 'key':key })Β Β Β
Β Β Β Β headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}Β Β
Β Β Β Β conn =Β http.HTTPConnection("api.thingspeak.com:80")Β Β
Β Β Β Β try:Β Β
Β Β Β Β Β Β Β Β conn.request("POST", "/update",Β params, headers)Β Β
Β Β Β Β Β Β Β Β response =Β conn.getresponse()Β Β
Β Β Β Β Β Β Β Β data =Β response.read()Β Β
Β Β Β Β Β Β Β Β conn.close()Β Β
Β Β Β Β except Exception:Β Β
Β Β Β Β Β Β Β Β print ("Connection failed")Β Β
Β Β Β Β exceptΒ KeyboardInterrupt:Β Β
Β Β Β Β Β Β Β Β print ("\nExiting.....")Β Β
Β Β Β Β Β Β Β Β exit()Β Β Β
while True:Β Β
Β Β Β Β try:Β Β
Β Β Β Β Β Β Β Β read_serial=ser.readline()Β Β
Β Β Β Β Β Β Β Β bpm =Β read_serial.decode('utf-8').strip()Β Β
Β Β Β Β Β Β Β Β print (bpm)Β Β
Β Β Β Β Β Β Β Β upload_to_ts(bpm)Β Β
Β Β Β Β exceptΒ KeyboardInterrupt:Β Β
Β Β Β Β Β Β Β Β print ("\nExiting.....")Β Β
Β Β Β Β Β Β Β Β breakΒ Β
Β
- Enter in your ThingSpeak channel key against the Key variable written on the 5th line of the code
- PressΒ Ctrl+X to exit the editor. Press βYβ and βEnterβ on the keyboard to successfully save and close your Python nano editor
- Type in the following command to run the code and start publishing data onto your ThingSpeak channel
- python3 heartbeat_streaming.py
- You should be able to see your BPM printed on the Raspberry Pi terminal, and subsequently, your Raspberry Pi will also be sending data to ThingSpeak.com
Β
Note: The free tier inΒ Thingspeak will only allow you to record one data every 15 seconds. In case you need a higher polling rate, youβll have to opt for a paidΒ tier that will allow you to do so.
The applications of such a project are endless, as it would enable you to monitor health remotely (or realistically anything) from anywhere around the world. So get creative and start building your own IoT projects!
Β