import network
import socket
import time
from machine import Pin, PWM
# Wi-Fi Setup
ssid = 'your_wifi_ssid' # Replace with your Wi-Fi SSID
password = 'your_wifi_password' # Replace with your Wi-Fi Password
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Check if already connected
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
print('Network connected:', wlan.ifconfig())
print('IP address:', wlan.ifconfig()[0]) # Print the IP address
# Connect to Wi-Fi
connect_wifi()
# Servo Motor Control Setup (GPIO 16 for PWM)
servo_pin = PWM(Pin(16))
servo_pin.freq(50) # Servo PWM frequency (50Hz)
# LED for door status (open/closed) (GPIO 15)
led_pin = Pin(15, Pin.OUT)
# Ultrasonic Sensor Setup (GPIO 14 for Trigger, GPIO 13 for Echo)
trig_pin = Pin(14, Pin.OUT) # Trigger pin of HC-SR04
echo_pin = Pin(13, Pin.IN) # Echo pin of HC-SR04
# Function to get distance from the ultrasonic sensor
def get_distance():
trig_pin.value(0)
time.sleep_us(2)
trig_pin.value(1)
time.sleep_us(10)
trig_pin.value(0)
pulse_time = time.pulse_width(echo_pin)
# Calculate distance in cm
distance = (pulse_time * 0.0343) / 2
return distance
# Function to open the door (servo to 90 degrees)
def open_door():
servo_pin.duty_u16(2500) # Servo open position (approx. 90 degrees)
led_pin.value(1) # LED ON (Door Open)
# Function to close the door (servo to 0 degrees)
def close_door():
servo_pin.duty_u16(500) # Servo close position (approx. 0 degrees)
led_pin.value(0) # LED OFF (Door Closed)
# Web page content to control the door
def web_page(door_status):
return f"""
<html>
<head>
<title>Automatic Door Control</title>
</head>
<body>
<h1>Automatic Door</h1>
<p>Status: {door_status}</p>
<a href="/open"><button>Open Door</button></a>
<a href="/close"><button>Close Door</button></a>
</body>
</html>
"""
# Web server setup (listening on port 80)
s = socket.socket()
s.bind(('0.0.0.0', 80)) # Listen on all interfaces on port 80
s.listen(1)
print("Web server running. Access the door at IP:", network.WLAN(network.STA_IF).ifconfig()[0])
# Main loop
while True:
# Get distance from ultrasonic sensor
distance = get_distance()
print(f"Distance: {distance} cm")
# If an object is detected within 20 cm, open the door
if distance < 20:
open_door()
print("Door Opened")
else:
close_door()
print("Door Closed")
# Handle incoming web requests (manual control via web page)
conn, addr = s.accept()
print('Connection from', addr)
request = conn.recv(1024)
request_str = str(request)
# Open Door: Set servo to 90 degrees
if '/open' in request_str:
open_door()
response = web_page("Door Opened")
# Close Door: Set servo to 0 degrees
elif '/close' in request_str:
close_door()
response = web_page("Door Closed")
else:
response = web_page("Idle")
# Send the HTTP response
conn.send('HTTP/1.1 200 OK\r\n')
conn.send('Content-Type: text/html\r\n')
conn.send('\r\n')
conn.send(response)
conn.close()
time.sleep(1) # Delay to prevent rapid loop execution
Loading
pi-pico-w
pi-pico-w