Process ECG Signals Using MQTT on STM32 Processor Boards
This example shows how to use the STM32 Nucleo F767ZI board using Embedded Coder(R) Support Package for STMicroelectronics(R) STM32 Processors to process an ECG signal input from an ECG sensor, extract the heart rate in beats per minutes (BPM), and send the ECG signal and heart rate to the ThingSpeak internet of things (IoT) analytics platform service.
Required Hardware
This example requires the following hardware:
STM32 F767ZI board
SHIELD-EKG-EMG, data acquisition board
SHIELD-EKG-EMG-PRO, passive electrode for shield
MIKROE-2456, ECG sensor electrodes pack
USB A TO USB MICRO B cables
RJ45 Ethernet cable.
Prerequisites
For more information on how to use the Embedded Coder Support Package for STMicroelectronics STM32 Processors to run a Simulink(R) model on the STM32 Nucleo F767ZI board, see Getting Started with STMicroelectronics STM32 Processor Based Boards.
For more information on MQTT protocol, see Introduction to MQTT.
For more information on ThingSpeak, see Create a ThingSpeak MQTT Device (ThingSpeak).
Required Hardware Connections
This example uses the Shield-EKG-EMG from Olimex as the extension board. This board is designed, with specific amplifiers and filters, to measure biosignals such as electrocardiogram (ECG) and electromyogram (EMG).
Connect the Olimex extension board to the STMF767ZI board via the Arduino Uno connectors. The sensors are three passive electrodes, which you can connect to the Shield-EKG-EMG extension board via a jack connector. The electrode marked L is for the left arm, R is for the right arm, and D is for the driven right leg (DRL) ground. To measure a correct input signal, the electrodes must be placed on the patient as shown in Olimex Sensor Placement on Patient. Make all the connections as shown in Fully Connected System.
STMF767ZI Board
Olimex Shield-EKG-EMG Extension Board
Olimex SHIELD-EKG-EMG-PRO Sensors
Olimex Sensor Placement on Patient
Fully Connected System
Understanding Signal Processing Chain
From a signal processing point of view, an ECG wave is very interesting to study and process, because it is made of both low-frequency and high-frequency components. In this example, the goal of the application is to extract the heart rate or in other words to determine the distance between two consecutive high-frequency R peaks (normal sinus rhythm impulse).
Normal Sinus Rhythm Impulse
To get the number of beats per minutes (BPM) out of an ECG captured on a patient, a one minute measurement can be done and the high frequency peaks can be manually counted. To speed it up and still have an accurate result, a 30 second measurement can be realized. The sum of the counted peaks is then multiplied by two to get the final BPM value. The goal of the application software is to automate the process measuring the BPM. It first filters the ECG signal to separate and extract the high-frequency peaks and then counts the number of peaks and computes the time difference between every pair of consecutive peaks. The signal processing chain represented in the block diagram is based on these steps.
Signal processing chain to extract the heart rate of an ECG signal
From the diagram, you can identify two distinct sections: the first section that goes from the input sensors to the output of the Lowpass Digital Filter block, which represents the pre-processing part, and the second section that goes from the input of the Peaks Extraction block to the output of the processing chain, which represents the post-processing part. The first stage focuses more on filtering the ECG signal and the second one more on computing the needed statistics. The aim of the pre-processing stage is to almost eliminate the low-frequency components to keep only the high-frequency peaks, which is why you use a Bandpass Digital Filter subsystem. You must tune the coefficients of this filter precisely for the first stage to work correctly. The Rectifier subsystem is there to get the absolute value of the signal and the Lowpass Digital Filter subsystem integrates over the processed signal. The Pre-emphasis Analog Filter located on the Olimex extension board (see Olimex Shield-EKG-EMG Extension Board) amplifies the frequency components of interest in the acquired ECG signal to reduce the effect of potential higher ones. The post-processing stage must then distinguish between global maxima representing the high frequency peaks and local maxima that are some remains of the filtering process. Maxima extraction is performed by the Maxima Extraction subsystem that has a threshold parameter that must be tuned precisely so that the global maxima are identified correctly. Once these maxima have been extracted, the Desired Time Info subsystem computes the time difference in seconds between each pair of consecutive peaks and provides results in the desired BPM format.
Sampling Frequency
The heart rate values depend on the age and gender of human beings. In general, with normal sinus rhythm, the maximum possible value for an athlete is 220 BPM. In theory, the maximum possible heart rate for a human being is of 300 BPM. A value above this theoretical value means that the heart is not following a normal sinus rhythm and that the patient is in severe danger.
For this example, assume that the application can measure a heart rate within the range of 0 to 255 BPM. An advantage of this range is that BPM values can be encoded as unsigned integer of 8 bits. The maximum value of 255 BPM highlights that the BPM is abnormal, and that the patient needs urgent assistance. Following the Nyquist frequency principle for a digital system, the sampling frequency (FS) of the digital part of the signal processing chain is twice twice the maximum value, as defined in the expression.
FS = 2·255 = 510 Hz,
where the measurement range in Hz is [0, 255].
Create ThingSpeak Device
MQTT access to your channel, including credentials, is handled by a ThingSpeak MQTT broker. Your STMF767ZI board is configured with the credentials necessary for your MQTT client to communicate with ThingSpeak, and for authorizing specific channels. For more information on how to create a MQTT device, see Create a ThingSpeak MQTT Device (ThingSpeak). Make a note of the list of credentials that ThingSpeak generates.
For more information on how to create multiple fields on a channel, see Collect Data in a New Channel (ThingSpeak).
Configure MQTT Properties for Simulink Models
In the STM32, double-click the MQTT Publish and MQTT Subscribe Simulink models, go to the Configuration Parameters > Target hardware resources > MQTT properties and configure these properties.
Set Broker service to
ThingSpeak
.To know the Broker service IP, run the
nslookup mqtt3.thingspeak.com
command in the windows command prompt.After the ThingSpeak generates a list of credentials (username, client ID, and password) for the STMF767ZI board, populate these credentials in the Username, Password, and Client ID parameters of the MQTT properties.
For more information on MQTT properties, see MQTT.
Configure IOC for Simulink Model
Press Ctrl+E to open the Configuration Parameters dialog box. Then, select the required hardware board by navigating to Hardware Implementation > Hardware board.
Navigate to Target hardware resources > Build options and verify that the
F767ZI.ioc
file is selected.The
F767ZI.ioc
file is a preconfigured file. For more information, see Configure Ethernet Options in STM32CubeMX for STM32 Processor Based Boards.
Deploy Model on STM Board
Open model
stm_MQTT_ECGProcessing.slx
.Connect the board to the computer using the USB cable.
Configure the network properties for the model as described in the: Configure MQTT Properties for Simulink Models section.
Open MQTT Publish block and add the channelID in the Topic section. The topic must use the format:
channels/channelID/publish
. You can obtain the channel ID from the ThingSpeak channel.In the model, press Ctrl+B or on the Hardware tab, click Build, Deploy & Start > Build Stand-Alone to build, load, and run the model on the target.
Model starts running on the board.
Open your ThingSpeak channel that you have configured in the model, to view the BPM values.
ThingSpeak Channel BMP Charts
View of ThingSpeak Channel
Close Model and Script Files
Once you finish simulating the model, close all the files.