import machine
import time
import urequests
import network
# Wi-Fi configuration
SSID = "Wokwi-Guest"
PASSWORD = "-"
# ThingSpeak configuration
thingspeak_api_key = "I8MRZLLJYU8OELQL"
thingspeak_channel_id = "2389682"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
# Wait for the Wi-Fi connection to be established
while not wifi.isconnected():
pass
print("Connected to Wi-Fi")
potentiometer_1_pin = machine.ADC(26) # Analog pin for potentiometer 1 (heart rate)
potentiometer_2_pin = machine.ADC(27) # Analog pin for potentiometer 2 (respiratory rate)
temperature_sensor_pin = machine.ADC(28) # Analog pin for the NTC temperature sensor
def read_potentiometers():
potentiometer_1_value = potentiometer_1_pin.read_u16()
potentiometer_2_value = potentiometer_2_pin.read_u16()
temperature_sensor_value = temperature_sensor_pin.read_u16()
return potentiometer_1_value, potentiometer_2_value, temperature_sensor_value
def map_value(value, in_min, in_max, out_min, out_max):
return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def convert_to_temperature(adc_value):
# You'll need to replace this with the appropriate conversion formula or lookup table for your specific NTC temperature sensor
# This is just a placeholder example
return map_value(adc_value, 0, 65535, 20, 40) # Assuming a linear mapping for demonstration purposes
while True:
potentiometer_1_value, potentiometer_2_value, temperature_sensor_value = read_potentiometers()
heart_rate = map_value(potentiometer_1_value, 0, 65535, 60, 120) # Scale potentiometer 1 value to heart rate range (60-120 bpm)
respiratory_rate = map_value(potentiometer_2_value, 0, 65535, 10, 20) # Scale potentiometer 2 value to respiratory rate range (10-20 breaths per minute)
temperature = convert_to_temperature(temperature_sensor_value) # Convert NTC temperature sensor value to temperature
# Check for abnormal conditions
if heart_rate > 110:
print("Patient abnormal: High heart rate!")
print("SEND HELP, EMERGENCY!")
if respiratory_rate < 12:
print("Patient abnormal: Low respiratory rate!")
print("SEND HELP, EMERGENCY!")
if respiratory_rate > 18:
print("Patient abnormal: High respiratory rate!")
print("SEND HELP, EMERGENCY!")
# Send data to ThingSpeak
url = "https://api.thingspeak.com/update?api_key={}&field1={}&field2={}&field3={}".format(thingspeak_api_key, heart_rate, respiratory_rate, temperature)
response = urequests.post(url)
print("ThingSpeak Response:", response.text)
response.close()
# Print readings to serial monitor
print("Heart Rate:", heart_rate, "bpm")
print("Respiratory Rate:", respiratory_rate, "breaths per minute")
print("Temperature:", temperature, "degrees Celsius")
time.sleep(0) # Send readings to ThingSpeak every 45 seconds