I want to be able to press the puttons on the web app to change the lights but I also want to be able to overide the web app with physical buttons to change the leds like the web app can. This is for a prototype for a wireless pool pump controller im making for my dad. Im using a pico w with micropython.
I tried putting the buttons code above all the network stuff with a while true loop and that did let me connect to the network, I tried just an or with the requests for example if request == '/low?' or lowButton.value() == 0:
but that would only read the button when I interacted with the web app and I want the button to work on its own. I tried making the “if button pressed do this” part a function and then adress the function in the “try” code and the very end and I got the same result. It just seems like nothing is working
import network
import socket
from time import sleep
from machine import Pin
import machine
import utime
ssid = 'ssid'
password = 'password'
lowTime = 5 #when the pump kicks on low speed
highTime = 22 #when the pump kicks on high speed
lowSpeed = Pin(0, Pin.OUT) #the pins for the contactors right now they are just leds
highSpeed = Pin(1, Pin.OUT)
lowButton = Pin(6, Pin.IN, Pin.PULL_UP) #the overide switches
highButton = Pin(7, Pin.IN, Pin.PULL_UP)
offButton = Pin(8, Pin.IN, Pin.PULL_UP)
timeButton = Pin(9, Pin.IN, Pin.PULL_UP) #the same as schedule
lowLed = Pin(10, Pin.OUT) #the status lights for each mode
highLed = Pin(11, Pin.OUT)
offLed = Pin(12, Pin.OUT)
timeLed = Pin(13, Pin.OUT)
#I got the networking code from a raspberry pi pico w networking tutorial
def connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def webpage():
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0">
</head>
<body style="background-color: black; text-align: center; vertical-align: middle; line-height: 100px;">
<h1 style="color:aliceblue; font-size: 300%;">
Father's Pool Pump Controll (PPC)
</h1>
<form action="./low">
<input type="submit" value="Low" style="height:350px; width:600px; border-radius: 40px; font-size:xx-large; background-color: rgb(0, 123, 254); color: aliceblue; font-weight: bold; border-color: aliceblue;"/>
</form>
<form action="./high">
<input type="submit" value="High" style="height:350px; width:600px; border-radius: 40px; font-size:xx-large; background-color: rgb(0, 123, 254); color: aliceblue; font-weight: bold; border-color: aliceblue;"/>
</form>
<form action="./off">
<input type="submit" value="OFF" style="height:350px; width:600px; border-radius: 40px; font-size:xx-large; background-color: rgb(0, 123, 254); color: aliceblue; font-weight: bold; border-color: aliceblue;"/>
</form>
<form action="./schedule">
<input type="submit" value="Schedule" style="height:350px; width:600px; border-radius: 40px; font-size:xx-large; background-color: rgb(0, 123, 254); color: aliceblue; font-weight: bold; border-color: aliceblue;"/>
</form>
</body>
</html>
"""
return str(html)
def serve(connection):
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
if request == '/low?':
lowSpeed.on()
highSpeed.off()
print("Low Selected!!!")
lowLed.value(1)
highLed.value(0)
offLed.value(0)
timeLed.value(0)
if request == '/high?':
lowSpeed.off()
highSpeed.on()
print("High Selected!!!")
lowLed.value(0)
highLed.value(1)
offLed.value(0)
timeLed.value(0)
if request == '/off?':
lowSpeed.off()
highSpeed.off()
print("Off Selected!!!")
lowLed.value(0)
highLed.value(0)
offLed.value(1)
timeLed.value(0)
if request == '/schedule?':
print("Schedule Seleceted!!!")
if utime.localtime()[3] >= highTime or utime.localtime()[3] < lowTime:
lowSpeed.off()
highSpeed.on()
print("Switching to High!!!")
if utime.localtime()[3] >= lowTime or utime.localtime()[3] < highTime:
lowSpeed.on()
highSpeed.off()
print("Switching to Low!!!")
lowLed.value(0)
highLed.value(0)
offLed.value(0)
timeLed.value(1)
html = webpage()
client.send(html)
client.close()
try:
ip = connect()
connection = open_socket(ip)
serve(connection)
# here is my code for the override buttons but im not sure where to put it to the code to read them
# it is basically the same as above just not in a function
# I tried putting the lowButton.value() == 0 part after the requests with an or statment but that didn't work
if lowButton.value() == 0:
lowSpeed.on()
highSpeed.off()
print("Low Selected!!!")
lowLed.value(1)
highLed.value(0)
offLed.value(0)
timeLed.value(0)
if highButton.value() == 0:
lowSpeed.off()
highSpeed.on()
print("High Selected!!!")
lowLed.value(0)
highLed.value(1)
offLed.value(0)
timeLed.value(0)
if offButton.value() == 0:
lowSpeed.off()
highSpeed.off()
print("Off Selected!!!")
lowLed.value(0)
highLed.value(0)
offLed.value(1)
timeLed.value(0)
if timeButton.value() == 0:
if utime.localtime()[3] >= highTime or utime.localtime()[3] < lowTime:
lowSpeed.off()
highSpeed.on()
print("Switching to High!!!")
if utime.localtime()[3] >= lowTime or utime.localtime()[3] < highTime:
lowSpeed.on()
highSpeed.off()
print("Switching to Low!!!")
lowLed.value(0)
highLed.value(0)
offLed.value(0)
timeLed.value(1)
# here is where the button code ends
except KeyboardInterrupt:
machine.reset()
Paxton M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.