From 4d90cd2f27669391297a935313e39670718ad4a1 Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Fri, 13 Dec 2024 08:54:05 +0100 Subject: [PATCH] Dateien zum Post. --- index.html | 16 +++++++ led.py | 20 ++++++++ pico2w-ledserver.py | 111 ++++++++++++++++++++++++++++++++++++++++++++ wlan.json | 6 +++ 4 files changed, 153 insertions(+) create mode 100644 index.html create mode 100644 led.py create mode 100644 pico2w-ledserver.py create mode 100644 wlan.json diff --git a/index.html b/index.html new file mode 100644 index 0000000..4c37609 --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ + + + + + Pico W Page + + + +

Raspberry Pi Pico Status

+ + + diff --git a/led.py b/led.py new file mode 100644 index 0000000..4b8f1f7 --- /dev/null +++ b/led.py @@ -0,0 +1,20 @@ +from machine import Pin +from utime import sleep + +led = Pin("LED", Pin.OUT) +state = False +running = True + +#Endlosschleife + +led.off() +while True: + state = not state # state hin- und herschalten + sleep(1.0) + if state: + print('LED on') + led.on() +# running = False + else: + print('LED off') + led.off() \ No newline at end of file diff --git a/pico2w-ledserver.py b/pico2w-ledserver.py new file mode 100644 index 0000000..2a7303e --- /dev/null +++ b/pico2w-ledserver.py @@ -0,0 +1,111 @@ +#file: pico2w-ledserver +#encoding: utf-8 +import machine +import network +import json + +import socket +import rp2 +from time import sleep +from machine import Pin + +f = open('wlan.json', 'r') +c = json.load(f) + +ssid = c['ssid'] +passwd = c['passwd'] +rp2.country(c['regio']) + +led = Pin("LED", Pin.OUT) + +ledstates = {'False':'ausgeschaltet','True':'eingeschaltet'} +def toogle_LED(state): + if state: + led.on() + else: + led.off() + +def load_html(): + try: + f = open('index.html','r') + print(f'f={f}') + page = f.read() + + + + except IOError as x: + print(f'I/O-Fehler: {x}') + finally: + if f != None: + f.close() + + return page + + +def connect(): + + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + wlan.connect(ssid,passwd) + while not wlan.isconnected(): + print('Waiting for connection...') + sleep(1) + print(f'connected={wlan.isconnected()}') + return wlan.ifconfig() + +# Bindet einen Socket an die Verbindung +def open_socket(ip): + # Socket öffnen + s = socket.socket() + address = socket.getaddrinfo('0.0.0.0',80)[0][-1] + print(f'address={address}') + s.bind(address) + s.listen(1) + return s + +def serve(s): + page = load_html() + print(f'page={page}') + running = True + ledstatus = False + req_count = 0 + toogle_LED(ledstatus) + #Start web server + while running: + client, address = s.accept() + request = client.recv(1024) + request = str(request) + req_count += 1 + print(f'req_count={req_count}') + try: + request = request.split()[1] + except IndexError: + pass + if request == '/on': + print('Kommando on') + ledstatus = True + elif request =='/off': + print('Kommando off') + ledstatus = False + elif request =='/stop': + print('stopping') + running = False + ledstatus = False + + toogle_LED(ledstatus) + client.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n'.encode('utf-8')) + + state = ledstates[str(ledstatus)] + print(f'status={state}') + + page = page.format(state) + client.send(page.encode('utf-8')) + client.close() + +ip = connect() +print(f'ip={ip}') +s = open_socket(ip) +serve(s) +s.close() +toogle_LED(False) + diff --git a/wlan.json b/wlan.json new file mode 100644 index 0000000..a231419 --- /dev/null +++ b/wlan.json @@ -0,0 +1,6 @@ + +{ + "ssid": "", + "passwd": "", + "regio": "DE" +}