Verunchikc / proyecto-Buikuan

0 stars 0 forks source link

Solo cardiaco, no se como funciona el de oxigeno #3

Open Verunchikc opened 9 months ago

Verunchikc commented 9 months ago

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

print("I2C device ID not corresponding to MAX30102 or MAX30105.")

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

    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 at 50/100

"""
return html

_thread.start_new_thread(get_max30102_values, ())

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5)

while True: try: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) request = str(request)
update = request.find('/update')

    if update == 6:
        print('update') 

    response = web_page()
    response = response.replace(" @@","")
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    conn.close()
except Exception as e:
    print(e)