letzte Änderungen für Blog-Post
This commit is contained in:
16
index.html
Normal file
16
index.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!--Diese Datei muss für den led-server auf dem PicoW gespeichert werden. -->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Pico W Page</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h2> Raspberry Pi Pico Status</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li> LED {0} </li>
|
||||||
|
<li> Button {1} </li>
|
||||||
|
</ul>
|
||||||
|
</head
|
||||||
|
</html>
|
26
led-button.py
Normal file
26
led-button.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from machine import Pin
|
||||||
|
from utime import sleep
|
||||||
|
|
||||||
|
led = Pin(15, Pin.OUT)
|
||||||
|
button = Pin(16, Pin.IN)
|
||||||
|
waittime = 0.2
|
||||||
|
state = False
|
||||||
|
running = True
|
||||||
|
# Setzt den Zustand der LED und wartet dann <wait> Sekunden.
|
||||||
|
def toogle_LED(state,wait):
|
||||||
|
if state:
|
||||||
|
led.on()
|
||||||
|
else:
|
||||||
|
led.off()
|
||||||
|
sleep(wait)
|
||||||
|
|
||||||
|
#Endlosschleife
|
||||||
|
while running:
|
||||||
|
state = not state # state hin- und herschalten
|
||||||
|
toogle_LED(state,waittime) # LED umschalten
|
||||||
|
pressed = button.value()
|
||||||
|
if pressed == 0:
|
||||||
|
led.off()
|
||||||
|
running = False
|
||||||
|
print('Button gedrückt, Ende')
|
||||||
|
|
22
led.py
22
led.py
@@ -1,21 +1,17 @@
|
|||||||
from machine import Pin
|
from machine import Pin
|
||||||
from utime import sleep
|
from utime import sleep
|
||||||
|
|
||||||
led = Pin(35, Pin.OUT)
|
led = Pin('LED', Pin.OUT)
|
||||||
state = False
|
state = False
|
||||||
LED_PIN =
|
|
||||||
|
|
||||||
# Setzt den Zustand der LED und wartet dann <wait> Sekunden.
|
def toggle_LED(state):
|
||||||
def toggle_LED(state,wait):
|
if state:
|
||||||
if state:
|
led.on()
|
||||||
led.on()
|
else:
|
||||||
else:
|
led.off()
|
||||||
led.off()
|
|
||||||
sleep(wait)
|
|
||||||
|
|
||||||
#Endlosschleife
|
|
||||||
while True:
|
while True:
|
||||||
state = not state # state hin- und herschalten
|
state = not state
|
||||||
|
|
||||||
toggle_LED(state,0.5) # LED umschalten
|
|
||||||
|
|
||||||
|
toggle_LED(state)
|
||||||
|
sleep(0.5)
|
103
picow-ledserver.py
Normal file
103
picow-ledserver.py
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#encoding: utf-8
|
||||||
|
import machine
|
||||||
|
import network
|
||||||
|
import socket
|
||||||
|
import rp2
|
||||||
|
from time import sleep
|
||||||
|
from machine import Pin
|
||||||
|
|
||||||
|
ssid = 'Echo Base'
|
||||||
|
passwd = 'EbsazdZ,d1GvdKAa,daWgw.DmsaaJaGmsvWM,dws.'
|
||||||
|
rp2.country('DE')
|
||||||
|
led = Pin(15, Pin.OUT)
|
||||||
|
button = Pin(16, Pin.IN)
|
||||||
|
|
||||||
|
ledstates = {False:'ausgeschaltet',True:'eingeschaltet'}
|
||||||
|
buttonstates = {False:'nicht gedrückt', True:'gedrückt'}
|
||||||
|
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
|
||||||
|
toogle_LED(ledstatus)
|
||||||
|
#Start web server
|
||||||
|
while running:
|
||||||
|
client, address = s.accept()
|
||||||
|
request = client.recv(1024)
|
||||||
|
print(f'1request={request}')
|
||||||
|
request = str(request)
|
||||||
|
print(f'2request={request}')
|
||||||
|
try:
|
||||||
|
request = request.split()[1]
|
||||||
|
print(f'3request={request}')
|
||||||
|
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'))
|
||||||
|
|
||||||
|
page = page.format(str(ledstatus),button.value())
|
||||||
|
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)
|
||||||
|
|
75
wlan.py
Normal file
75
wlan.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#encoding: utf-8
|
||||||
|
import machine
|
||||||
|
import network
|
||||||
|
import socket
|
||||||
|
import rp2
|
||||||
|
from time import sleep
|
||||||
|
from machine import Pin
|
||||||
|
|
||||||
|
led = Pin(15, Pin.OUT)
|
||||||
|
|
||||||
|
ssid = '<ssid>' # WLAN Name
|
||||||
|
passwd = '<passwd>' # Zugangspasswort des WLAN
|
||||||
|
rp2.country('DE') # WLAN im deutschen Frequenzspektrum
|
||||||
|
html = """<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Willkommen</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2> Willkommen auf dem Raspberry Pi Pico W</h2>
|
||||||
|
</head>
|
||||||
|
</html>"""
|
||||||
|
# Verbindung aufbauen
|
||||||
|
def connect():
|
||||||
|
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(True)
|
||||||
|
wlan.connect(ssid,passwd)
|
||||||
|
while not wlan.isconnected(): # Wiederhlen, bis Verbindung besteht
|
||||||
|
print('Waiting for connection...')
|
||||||
|
sleep(1)
|
||||||
|
print(f'connected={wlan.isconnected()}')
|
||||||
|
led.on() # Wenn die Verbindung zum WLAN steht, Status LED einschalten.
|
||||||
|
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(connection):
|
||||||
|
running = True
|
||||||
|
#Start web server
|
||||||
|
while running:
|
||||||
|
client, adress = connection.accept()
|
||||||
|
request = client.recv(1024)
|
||||||
|
print(f'1request={request}')
|
||||||
|
request = str(request)
|
||||||
|
print(f'2request={request}')
|
||||||
|
try:
|
||||||
|
request = request.split()[1]
|
||||||
|
print(f'3request={request}')
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
if request =='/stop':
|
||||||
|
print('stopping')
|
||||||
|
running = False
|
||||||
|
|
||||||
|
client.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n'.encode('utf-8'))
|
||||||
|
client.send(html.encode('utf-8'))
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
ip = connect()
|
||||||
|
print(f'ip={ip}')
|
||||||
|
s = open_socket(ip)
|
||||||
|
serve(s)
|
||||||
|
s.close()
|
||||||
|
led.off()
|
||||||
|
print(f'socket={s}')
|
||||||
|
#Hier ist jetzt die Verbindung aufgebaut und der Socket einsatzbereit.
|
Reference in New Issue
Block a user