def get_max30102_values():
while True:
global history
global beats_history
global beat
global beats
global t_start
sensor.check()
# Check if the storage contains available samples
if sensor.available():
# Access the storage FIFO and gather the readings (integers)
red_reading = sensor.pop_red_from_storage()
ir_reading = sensor.pop_ir_from_storage()
value = red_reading
history.append(value)
# Get the tail, up to MAX_HISTORY length
history = history[-MAX_HISTORY:]
minima = 0
maxima = 0
threshold_on = 0
threshold_off = 0
minima, maxima = min(history), max(history)
threshold_on = (minima + maxima * 3) // 4 # 3/4
threshold_off = (minima + maxima) // 2 # 1/2
if value > 1000:
if not beat and value > threshold_on:
beat = True
led.on()
t_us = ticks_diff(ticks_us(), t_start)
t_s = t_us/1000000
f = 1/t_s
bpm = f * 60
if bpm < 500:
t_start = ticks_us()
beats_history.append(bpm)
beats_history = beats_history[-MAX_HISTORY:]
beats = round(sum(beats_history)/len(beats_history) ,2)
if beat and value< threshold_off:
beat = False
led.off()
else:
led.off()
print('Not finger')
beats = 0.00
def web_page():
html = """
ESP32 Web Server
ESP32 Web Server
Sensor MAX30102
"""+ str(beats) +"""
at 50/100
"""+ str(round(sensor.read_temperature(),2)) +""" °C
import socket from max30102 import MAX30102, MAX30105_PULSE_AMP_MEDIUM import _thread from machine import sleep, SoftI2C, Pin, Timer from utime import ticks_diff, ticks_us
led = Pin(2, Pin.OUT) MAX_HISTORY = 32 history = [] beats_history = [] beat = False beats = 0
i2c = SoftI2C(sda=Pin(21),scl=Pin(22),freq=400000) sensor = MAX30102(i2c=i2c) # An I2C instance is required
Scan I2C bus to ensure that the sensor is connected
if sensor.i2c_address not in i2c.scan(): print("Sensor not found.")
elif not (sensor.check_part_id()):
Check that the targeted sensor is compatible
else: print("Sensor connected and recognized.")
sensor.setup_sensor() sensor.set_sample_rate(400) sensor.set_fifo_average(8) sensor.set_active_leds_amplitude(MAX30105_PULSE_AMP_MEDIUM) sensor.set_led_mode(2) sleep(1)
t_start = ticks_us() # Starting time of the acquisition
def display_bpm(f): global beats print('BPM:', beats)
timer=Timer(1) timer.init(period=2000,mode=Timer.PERIODIC,callback=display_bpm)
def get_max30102_values(): while True: global history global beats_history global beat global beats global t_start
def web_page(): html = """
ESP32 Web Server
Sensor MAX30102