import machine
import utime
import network
import socket
# ------------Variables-------------
sensor_pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
# LEDs representing overhead lights
led = machine.PWM(machine.Pin(15))
led2 = machine.PWM(machine.Pin(16))
led.freq(1000)
led2.freq(1000)
# TEMP: heating indicator LED
led_heat = machine.Pin(17, machine.Pin.OUT)
# Time block allowed in seconds
amount = 5
# Potentiometer variables
potentiometer = machine.ADC(26)
conversion_factor = 3.3 / 65535 # not used directly, but fine to keep
# Schedule storage (8:00–19:00)
schedule = {
8: False,
9: False,
10: False,
11: False,
12: False,
13: False,
14: False,
15: False,
16: False,
17: False,
18: False,
19: False
}
motion_detected = False
def pir_handler(pin):
global motion_detected
motion_detected = True
sensor_pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)
def room_activation():
led_heat.toggle()
for i in range(amount):
value = potentiometer.read_u16()
led.duty_u16(value)
led2.duty_u16(value)
utime.sleep(1)
# turn off lights after activation period
led.duty_u16(0)
led2.duty_u16(0)
led_heat.toggle()
# ------------Webpage Section-------------
def web_page():
buttons = ""
for hour in schedule:
if schedule[hour]:
color = "buttonRed"
status = "BOOKED"
else:
color = "buttonGreen"
status = "OPEN"
next_hour = hour + 1
buttons += f"""
<button class="{color}" name="Block{hour}" value="toggle" type="submit">
{hour}:00 - {next_hour}:00 {status}
</button><br>
"""
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {{ font-family: Helvetica; text-align: center; }}
.buttonGreen {{
background-color: #4CAF50;
border: 2px solid black;
color: white;
padding: 12px;
font-size: 16px;
margin: 4px;
cursor: pointer;
}}
.buttonRed {{
background-color: #D11D53;
border: 2px solid black;
color: white;
padding: 12px;
font-size: 16px;
margin: 4px;
cursor: pointer;
}}
</style>
</head>
<body>
<h2>Room Schedule</h2>
<p>Green = OPEN | Red = BOOKED</p>
<form>
{buttons}
</form>
</body>
</html>
"""
return html
# WIFI ACCESS POINT
ssid = "Automation - Team 9"
password = "3141592653589"
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while not ap.active():
pass
print("Connection successful")
print(ap.ifconfig())
# WEB SERVER
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 80))
s.listen(5)
while True:
conn, addr = s.accept()
print("Connection from", addr)
request = conn.recv(1024)
request = str(request)
print(request)
# check each time block for button presses
for hour in schedule:
if request.find(f"Block{hour}=toggle") != -1:
schedule[hour] = not schedule[hour]
if schedule[hour]:
print(hour, "BOOKED")
else:
print(hour, "OPEN")
# build and send response
response = web_page()
conn.send("HTTP/1.1 200 OK\n")
conn.send("Content-Type: text/html\n")
conn.send("Connection: close\n\n")
conn.sendall(response)
conn.close()
# motion-based activation
if motion_detected:
motion_detected = False
print("Motion detected -> room activation")
room_activation()
# schedule-based activation
current_hour = utime.localtime()[3]
if current_hour in schedule and schedule[current_hour]:
print("Scheduled hour active -> room activation")
room_activation()Loading
pi-pico-w
pi-pico-w