from machine import Pin, ADC, PWM
import time
# Simulated Wi-Fi and ThingSpeak Credentials
SSID = "VM9360388"
PASSWORD = "wj7Hrhcnztvm"
THINGSPEAK_API_KEY = "80EUDUUBNDVJY0V0"
# Sensor Pins
heart_sensor = ADC(Pin(26)) # Simulated Heart Rate Sensor (Potentiometer)
temp_sensor = ADC(Pin(27)) # Simulated Temperature Sensor (Potentiometer)
motion_sensor = Pin(14, Pin.IN, Pin.PULL_DOWN) # Simulated Motion Detection Button
# LED Pins
red_led = Pin(18, Pin.OUT) # Red LED - High Heart Rate
green_led = Pin(19, Pin.OUT) # Green LED - High Temperature
blue_led = Pin(20, Pin.OUT) # Blue LED - Motion Detected
# Buzzer Pin (PWM)
buzzer = PWM(Pin(2)) # Use GP2 for the buzzer
# Simulated Wi-Fi Connection
def connect_wifi():
print("Simulating Wi-Fi Connection...")
time.sleep(1)
print("Simulated Wi-Fi connected successfully! IP Address: 192.168.1.100")
# Simulate Data Upload to ThingSpeak
def upload_to_thingspeak(heart_rate, temperature, motion):
print("Simulating data upload to ThingSpeak...")
print(f"Simulated Data -> Heart Rate: {heart_rate}, Temperature: {temperature}, Motion: {motion}")
print("Simulated upload successful!")
# Simulate Sensor Values
def simulate_values():
heart_rate = int(heart_sensor.read_u16() / 65535 * 50 + 70) # Simulated 70–120 BPM
temperature = round((temp_sensor.read_u16() / 65535 * 6 + 34), 1) # Simulated 34–40°C
motion = motion_sensor.value() # 1 = Motion Detected, 0 = No Motion
return heart_rate, temperature, motion
# Function to Control LEDs and Buzzer
def control_alerts(heart_rate, temperature, motion):
# Control LEDs
red_led.value(1 if heart_rate > 100 else 0) # Red LED: High Heart Rate
green_led.value(1 if temperature > 38 else 0) # Green LED: High Temperature
blue_led.value(1 if motion else 0) # Blue LED: Motion Detected
# Buzzer Alerts
if motion: # Motion Detected
print("Motion Detected - On the move!")
buzzer.freq(1500)
buzzer.duty_u16(20000)
time.sleep(0.5)
buzzer.duty_u16(0)
elif heart_rate > 100: # High Heart Rate
print("High Heart Rate Alert - Heart rate exceeded!")
buzzer.freq(1000)
buzzer.duty_u16(20000)
time.sleep(0.5)
buzzer.duty_u16(0)
elif temperature > 38: # High Temperature
print("High Temperature Alert - Cool down!")
buzzer.freq(800)
buzzer.duty_u16(20000)
time.sleep(0.5)
buzzer.duty_u16(0)
else:
buzzer.duty_u16(0) # Ensure buzzer is OFF
# Main Execution
def main():
connect_wifi() # Simulated Wi-Fi connection
last_upload_time = time.time()
while True:
# Read Simulated Sensor Values
heart_rate, temperature, motion = simulate_values()
# Motion Status
motion_status = "Yes Motion" if motion else "No Motion"
# Display Sensor Values
print(f"Heart Rate: {heart_rate} BPM, Temperature: {temperature}°C, Motion: {motion_status}")
# Control Alerts (LEDs and Buzzer)
control_alerts(heart_rate, temperature, motion)
# Simulated Upload to ThingSpeak every 10 seconds
if time.time() - last_upload_time >= 10:
upload_to_thingspeak(heart_rate, temperature, motion)
last_upload_time = time.time()
# Short delay for polling sensors
time.sleep(0.5)
# Run the Main Function
main()
Loading
pi-pico-w
pi-pico-w