Tutorial
Adrian
2023:11
\\
\\
\\
====== Polar H10: Real-time Heart Rate Monitoring ======
==== Introduction ====
Polar H10 is a precise heart rate sensor that uses electrodes attached to your chest to produce an electrocardiogram (ECG). The main advantage of performing an ECG is that it measures the heart activity much more accurately and reliably compared to other technologies, such as Optical Heart Rate sensors. When it comes to reading heart rate in real-time for your project or application, you have multiple options on how to achieve this. Polar provides its own SDK to develop Android and iOS applications for the Polar H10. Also, Since Polar H10 uses wireless Bluetooth Low Energy technology (BLE), you can even develop your script to read the heart rate readings directly from the sensor. In this guide, I will explain some of the existing options for reading the heart rate in real time. The described solutions use the MQTT protocol to publish sensor readings which can be read with an MQTT client. The idea is that MQTT provides a universal interface to read the heart rate for your project:
- Publish HR readings with the "Polar Sensor Logger" Android app
- Publish HR readings with a Python script
==== Requirements ====
* **Hardware**:
* Polar H10 sensor and strap
* **Software**:
* Mosquitto
To publish HR readings from your phone using MQTT:
* **Hardware**:
* Android Phone (with USB cable)
* **Software**:
* Polar Sensor Logger
* Android Debug Bridge (ADB)
To publish HR readings with a Python script:
* **Hardware**:
* Computer with Bluetooth Low Energy (BLE) capability
* **Software**:
* Python 3.4+ (should come with PIP)
==== Instructions ====
Firstly, you have to install and run the Mosquitto MQTT broker. After, you can publish the heart rate to the local Mosquitto broker through either an MQTT client with the Polar Sensor Logger App (on your Android phone) or through a Python script on your PC.
** Note:** Technically, you can use a 3rd party MQTT broker to publish the heart rate readings to (instead of using Mosquitto locally). If you choose to do so, you can skip the Mosquitto Setup Part
=== Setup Mosquitto MQTT Broker ===
== Installation ==
1. Install [[https://mosquitto.org/download/ | Mosquitto MQTT broker]] \\ \\
2. Add a new Firewall rule to allow TCP port 1883 for new MQTT Client connections:
- Open PowerShell in administrator mode
- Run in PowerShell: New-NetFirewallRule -DisplayName "Allow TCP Port 1883" -Direction inbound -Profile Any -Action Allow -LocalPort 1883 -Protocol TCP
**Note**: In practice, Windows should ask you to allow network communication for mosquitto.exe via a Windows Security Alert Prompt. If you give Mosquitto permission through this prompt, step 2 can be skipped \\ \\
3. Edit the Mosquitto config file to allow external connections:
- Go to Mosquitto's installation directory
- Edit mosquitto.conf file with a text editor -> insert: allow_anonymous true
listener 1883
== Usage ==
4. Run Mosquitto using the config file:
- Open cmd in Mosquitto's installation directory
- Run in cmd: mosquitto -c mosquitto.conf -v
//If everything goes well you should be seeing the logs in cmd confirming that the Mosquitto broker is running on port 1883:// \\
{{:images:polar_h10_realtime:mosquitto_cmd.png?direct&400|}}
=== Polar Sensor Logger (Android App) ===
Mount the Polar H10 sensor on the chest strap and wear it. Follow these steps on your Android device:
- Install the [[https://play.google.com/store/apps/details?id=com.j_ware.polarsensorlogger&pcampaignid=web_share|"Polar Sensor Logger" App]]
- Activate Bluetooth and Location Service
- Launch the "Polar Sensor Logger" App, in the main tab configure:
- "SDK data select:" check ECG only
- "Settings:" check MQTT only
- In the pop-up "MQTT-settings" configure:
- MQTT-broker address -> Insert the IPv4 of the PC on which the Mosquitto broker is running. To find it, run in cmd: ''ipconfig'', or go to [[https://whatismyipaddress.com/]]
- Port: ''1883''
- Topic: ''polar_h10''
- Client ID: ''polar_h10_realtime''
- Tap ''OK''
- Tap SEEK SENSOR
- Select listed sensor Polar H10
- Tap ''OK''
With Polar Sensor Logger main tab entry "SDK data select" option ECG activated, PSL publishes two topics:
* Topic psl/ecg (cp. listing 2.6.): delivers a JSON-Object as payload containing, among others
* a JSON-Field named "ecg": [ ... ], a JSON-Array of ECG values in microvolts [uV]
* Topic psl/hr (cp. listing 2.7.): delivers a JSON-Object as payload containing, among others
* a JSON-Field "hr": 64 containing a JSON-Integer which corresponds to the heart rate in beats per minute (bpm)
* a JSON-Field "rr": [ 938 ] containing a JSON-Array of RR intervals in milliseconds [ms]
=== Python Script ===
== Installation ==
- Download the project files from our [[https://github.com/utwente-interaction-lab/Polar-H10-mqtt|GitHub repository]]
- Go to the project's location
- Run in cmd:
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
== Usage ==
Run: python polar_h10_mqtt.py
==== Screenshots ====
{{:images:polar_h10_realtime:psl_main.jpg?nolink&200|}}
{{:images:polar_h10_realtime:psl_mqtt_config.jpg?nolink&200|}}
{{:images:polar_h10_realtime:psl_select_polar.jpg?nolink&200|}}
{{:images:polar_h10_realtime:psl_connected.jpg?nolink&200|}} \\ //Polar Sensor Logger \\ \\
{{:images:polar_h10_realtime:venv.png?nolink&500|}} \\ //Create and activate venv \\ \\
{{:images:polar_h10_realtime:pip_installed.png?nolink&700|}} \\ //All libraries installed \\ \\
{{:images:polar_h10_realtime:script_log.png?nolink&500|}} \\ //Log of Python script \\ \\
==== Code ====
https://github.com/utwente-interaction-lab/Polar-H10-mqtt
==== Attribution ====
* [[https://github.com/kbre93/dont-hold-your-breath|Don't Hold Your Breath – Breathing analysis with the Polar H10 Heart Rate Monitor]]: Project to automatically connect and log data from Polar H10.
* [[https://github.com/brugr9/Heartbeat51|Unreal Engine Project "Heartbeat"]]: An Unreal Engine project as proof-of-concept for receiving physiological data from Polar H10 heart rate monitor.
---
[[https://www.utwente.nl/en/eemcs/facilities/interaction-lab/|Interaction Lab]]