Enthernet Code
DIIDevHeads IoT Integration Server
•Created by Enthernet Code on 7/11/2024 in #code-review
Why isn't my Arduino Nano 33 BLE Sense transmitting pulse and SpO2 data via BLE?
import time
from machine import I2C, Pin
from max30102 import MAX30102
from ubluetooth import BLE, UUID, FLAG_READ, FLAG_WRITE
# Initialize I2C
i2c = I2C(1, scl=Pin(19), sda=Pin(18))
# Initialize MAX30102
max30102 = MAX30102(i2c)
# Initialize BLE
ble = BLE()
ble.active(True)
# Define BLE Service and Characteristics
pulse_ox_service = UUID(0x1810)
pulse_char = (UUID(0x2A37), FLAG_READ | FLAG_WRITE)
spo2_char = (UUID(0x2A5F), FLAG_READ | FLAG_WRITE)
# Define BLE Services
pulse_ox_service = (pulse_ox_service, (pulse_char, spo2_char))
services = (pulse_ox_service, )
# Start advertising
ble.config(gap_name='HealthMonitor')
ble.gatts_register_services(services)
ble.gap_advertise(100, b'\x02\x01\x06\x03\x03\x10\x18')
def read_sensor():
red, ir = max30102.read_sequential()
pulse = calculate_pulse(ir)
spo2 = calculate_spo2(red, ir)
return pulse, spo2
def calculate_pulse(ir_data):
# Placeholder for pulse calculation algorithm
return sum(ir_data) / len(ir_data)
def calculate_spo2(red_data, ir_data):
# Placeholder for SpO2 calculation algorithm
return (sum(red_data) / sum(ir_data)) * 100
while True:
pulse, spo2 = read_sensor()
print("Pulse:", pulse, "SpO2:", spo2)
ble.gatts_notify(0, pulse_char[0], str(pulse))
ble.gatts_notify(0, spo2_char[0], str(spo2))
time.sleep(1)
import time
from machine import I2C, Pin
from max30102 import MAX30102
from ubluetooth import BLE, UUID, FLAG_READ, FLAG_WRITE
# Initialize I2C
i2c = I2C(1, scl=Pin(19), sda=Pin(18))
# Initialize MAX30102
max30102 = MAX30102(i2c)
# Initialize BLE
ble = BLE()
ble.active(True)
# Define BLE Service and Characteristics
pulse_ox_service = UUID(0x1810)
pulse_char = (UUID(0x2A37), FLAG_READ | FLAG_WRITE)
spo2_char = (UUID(0x2A5F), FLAG_READ | FLAG_WRITE)
# Define BLE Services
pulse_ox_service = (pulse_ox_service, (pulse_char, spo2_char))
services = (pulse_ox_service, )
# Start advertising
ble.config(gap_name='HealthMonitor')
ble.gatts_register_services(services)
ble.gap_advertise(100, b'\x02\x01\x06\x03\x03\x10\x18')
def read_sensor():
red, ir = max30102.read_sequential()
pulse = calculate_pulse(ir)
spo2 = calculate_spo2(red, ir)
return pulse, spo2
def calculate_pulse(ir_data):
# Placeholder for pulse calculation algorithm
return sum(ir_data) / len(ir_data)
def calculate_spo2(red_data, ir_data):
# Placeholder for SpO2 calculation algorithm
return (sum(red_data) / sum(ir_data)) * 100
while True:
pulse, spo2 = read_sensor()
print("Pulse:", pulse, "SpO2:", spo2)
ble.gatts_notify(0, pulse_char[0], str(pulse))
ble.gatts_notify(0, spo2_char[0], str(spo2))
time.sleep(1)
10 replies