from machine import Pin, WDT
import neopixel
import network
import socket
import time
import os
# --- Setup watchdog ---
wdt = WDT(timeout=8000)
# --- Setup LED (neopixel op pin 47) ---
np = neopixel.NeoPixel(Pin(47), 1)
def int_led(status):
if status == 1:
np[0] = (5, 5, 5) # aan: dimwit licht
else:
np[0] = (0, 0, 0) # uit
np.write()
# --- Setup relais (pomp) op pin 36 ---
pomp_pin = Pin(36, Pin.OUT)
pomp_status = False
pomp_pin.value(0)
# --- Externe LED op pin 16 ---
ext_led = Pin(16, Pin.OUT)
ext_led.value(0)
# --- Knoppen ---
button_pin = Pin(0, Pin.IN, Pin.PULL_UP)
timer_button = Pin(35, Pin.IN, Pin.PULL_UP)
# --- WiFi ---
ssid = "Boris"
password = "0494636009"
station = network.WLAN(network.STA_IF)
station.active(True)
station.config(dhcp_hostname="Pomp")
station.ifconfig((
'192.168.0.100',
'255.255.255.0',
'192.168.0.1',
'8.8.8.8'
))
station.connect(ssid, password)
while not station.isconnected():
time.sleep_ms(100)
print("WiFi connected:", station.ifconfig()[0])
# --- Pomp en LED aansturen ---
def set_pomp(status):
if status:
int_led(1)
pomp_pin.value(1)
ext_led.value(1)
else:
int_led(0)
pomp_pin.value(0)
ext_led.value(0)
def toggle_pomp():
set_pomp(not pomp_status)
# --- Timer voor automatische uitschakeling ---
timer_end_time = None
status_tekst = "Uit"
# --- Knop logica ---
last_button_state = 1
last_timer_button = 1
def check_buttons():
global last_button_state, last_timer_button, timer_end_time, status_tekst
reading = button_pin.value()
if reading == 0 and last_button_state == 1:
toggle_pomp()
status_tekst = "Altijd aan" if pomp_status else "Uit"
timer_end_time = None
while button_pin.value() == 0:
time.sleep_ms(10)
last_button_state = reading
reading2 = timer_button.value()
if reading2 == 0 and last_timer_button == 1:
set_pomp(True)
timer_end_time = time.time() + 60
status_tekst = "Grote gieter vullen"
while timer_button.value() == 0:
time.sleep_ms(10)
last_timer_button = reading2
# --- Webserver setup ---
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
s.settimeout(0.1)
print("Server gestart op:", station.ifconfig()[0])
# --- Main loop ---
while True:
wdt.feed()
check_buttons()
if timer_end_time and time.time() >= timer_end_time:
set_pomp(False)
timer_end_time = None
status_tekst = "Uit"
try:
cl, addr = s.accept()
except OSError:
cl = None
if cl:
try:
cl_file = cl.makefile('rwb', 0)
request_line = cl_file.readline().decode()
method, path, _ = request_line.split()
headers = {}
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
parts = line.decode().split(':', 1)
if len(parts) == 2:
headers[parts[0].strip().lower()] = parts[1].strip()
if method == 'POST':
length = int(headers.get("content-length", 0))
body = cl_file.read(length)
boundary = headers.get("content-type", "").split("boundary=")[-1]
if boundary:
parts = body.split(b"--" + boundary.encode())
for part in parts:
if b'filename=' in part:
start = part.find(b'\r\n\r\n') + 4
end = part.rfind(b'\r\n')
content = part[start:end]
with open("new.py", "wb") as f:
f.write(content)
if "main.py" in os.listdir():
os.remove("main.py")
os.rename("new.py", "main.py")
print("main.py geüpdatet via upload.")
cl.send(b'HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(b"<html><body><h2>Upload gelukt. Herstart...</h2></body></html>")
cl.close()
# Reset uitvoeren na upload
import machine
machine.reset()
elif method == 'GET':
if '?pomp=on' in path:
set_pomp(True)
timer_end_time = None
status_tekst = "Altijd aan"
cl.send(b'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n')
elif '?pomp=1min' in path:
set_pomp(True)
timer_end_time = time.time() + 60
status_tekst = "Grote gieter vullen"
cl.send(b'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n')
elif '?pomp=30s' in path:
set_pomp(True)
timer_end_time = time.time() + 30
status_tekst = "Kleine gieter vullen"
cl.send(b'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n')
elif '?pomp=off' in path:
set_pomp(False)
timer_end_time = None
status_tekst = "Uit"
cl.send(b'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n')
else:
response = web_page()
cl.send(b'HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response.encode('utf-8'))
except Exception as e:
print("Fout in client handler:", e)
finally:
cl.close()
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1