import network import socket from machine import ADC, Pin import time # --- WLAN-Konfiguration --- WIFI_SSID = "ROUTER2" WIFI_PASS = "h347THuie894UIERIF83hrUJ4aspyq80734Y" # --- Statische IP --- STATIC_IP = "192.168.168.248" NETMASK = "255.255.255.0" GATEWAY = "192.168.168.1" DNS = "192.168.168.1" # --- Sensor --- sensor = ADC(Pin(2)) RAW_TROCKEN = 4095 RAW_NASS = 750 SPAN = RAW_TROCKEN - RAW_NASS def read_percent(): raw = sensor.read() raw = min(max(raw, RAW_NASS), RAW_TROCKEN) percent = (RAW_TROCKEN - raw) / SPAN * 100 return raw, percent # --- LEDs --- leds = { 1: Pin(7, Pin.OUT), 2: Pin(21, Pin.OUT), 3: Pin(4, Pin.OUT), 4: Pin(10, Pin.OUT), 5: Pin(9, Pin.OUT), 6: Pin(5, Pin.OUT), 7: Pin(20, Pin.OUT), 8: Pin(8, Pin.OUT), 9: Pin(6, Pin.OUT) } gnd_leds = {1, 2, 9} vcc_leds = {3, 4, 5, 6, 7, 8} def leds_all_off(): for num, pin in leds.items(): pin.value(0 if num in gnd_leds else 1) def led_on(num): leds[num].value(1 if num in gnd_leds else 0) # --- Button --- button = Pin(3, Pin.IN, Pin.PULL_UP) # --- Zentrale Messroutine --- def measure_and_update(): raw, percent = read_percent() leds_all_off() num_leds_on = int(percent // 10) + 1 num_leds_on = max(1, min(9, num_leds_on)) for i in range(1, num_leds_on + 1): led_on(i) time.sleep(1) leds_all_off() return raw, percent # --- WLAN verbinden --- wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.ifconfig((STATIC_IP, NETMASK, GATEWAY, DNS)) wlan.connect(WIFI_SSID, WIFI_PASS) print("Verbinde mit WLAN...") while not wlan.isconnected(): time.sleep(0.2) print("WLAN verbunden:", wlan.ifconfig()) # --- HTML-Seite --- def webpage(raw, percent): return f""" Feuchtigkeitsanzeige

T2000SXe

Feuchtigkeitsmessung

{percent:.1f}%

RAW-Wert: {raw}
""" # --- Webserver vorbereiten --- addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1] server = socket.socket() server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind(addr) server.listen(1) server.setblocking(False) print("Webserver läuft auf http://" + STATIC_IP) # --- Startzustand --- leds_all_off() # --- Hauptschleife --- while True: # --- Physischer Button --- if button.value() == 0: raw, percent = measure_and_update() print("RAW:", raw, " | Feuchtigkeit: %.1f%%" % percent) while button.value() == 0: time.sleep_ms(10) leds_all_off() # --- Webserver --- try: conn, addr = server.accept() request = conn.recv(1024) raw, percent = measure_and_update() html = webpage(raw, percent) conn.send("HTTP/1.1 200 OK\r\n") conn.send("Content-Type: text/html\r\n") conn.send("Connection: close\r\n\r\n") conn.sendall(html) conn.close() except OSError: pass time.sleep_ms(10)