import machine
import dht
import time
import os
# DHT22 sensor setup
sensor_dht22 = dht.DHT22(machine.Pin(28))
# microSD card setup
spi = machine.SPI(0, sck=machine.Pin(18), mosi=machine.Pin(19), miso=machine.Pin(16))
cs = machine.Pin(17, machine.Pin.OUT)
vfs = os.VfsFat(machine.SDCard(spi, cs))
os.mount(vfs, '/sd')
# Heart rate sensor setup (example for MAX30100/MAX30102)
# Replace '21' and '22' with the correct SCL and SDA pin numbers for your setup
i2c = machine.I2C(1, scl=machine.Pin(21), sda=machine.Pin(22))
# Initialize the heart rate sensor here (library-specific initialization)
while True:
try:
# Read from DHT22
sensor_dht22.measure()
temp = sensor_dht22.temperature()
hum = sensor_dht22.humidity()
# Read from heart rate sensor (specific code depends on the sensor and library)
heart_rate = 0 # Replace this with actual heart rate reading code
# Prepare data string
data = "Temperature: {:.2f} C, Humidity: {:.2f} %, Heart Rate: {} bpm\n".format(temp, hum, heart_rate)
print(data)
# Write data to microSD card
with open('/sd/data.txt', 'a') as f:
f.write(data)
except Exception as e:
print("Error:", e)
time.sleep(2)