13.1.2 Exemple 2
Le programme Python sera basé sur l'exemple de ce
Ce programme permettra d'allumer, d'éteindre et d'inverser la LED interne via des commandes sur un
navigateur. Il permet également de lire l'état d'un éventuel bouton câblé sur le GPIO 15 (Voir le
chapitre sur la gestion des entrées/sorties pour le câblage)
Créer d'abord un fichier secrets.py avec les informations de connexion de votre box.
ssid = 'Livebox-xxxx'
password = 'xxxxxxxxxxxxxxxxxxxx'
Puis sauvegardez ce programme en le nommant main.py afin qu'il démarre automatiquement à
l'allumage du PICO W
import
usocket
as
socket
import
network
from
machine
import
import
secrets
wlan
= network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.ssid, secrets.password)
light
= machine.Pin("LED", machine.Pin.OUT)
Led
=
"OFF"
light.off()
button
= machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP)
htmlresponse
=
"""HTTP/1.0 200 OK
Content-Type: text/html
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<title>Serveur RaspiPicoW</title>
</head>
<body>
<p>Etat de la LED : {} </p>
<p> <a href = "/?etat=on" >Led ON </a>
<p> <a href = "/?etat=off" >Led OFF </a> </p>
<p> <a href = "/?etat=toggle" >TOGGLE </a> </p>
<p> <a href = "/?etat=button" >Boutton </a> </p>
</body>
</html>
"""
def
connexion_wifi(ssid,password):
wlan
= network.WLAN(network.STA_IF)
wlan.active(True)
if not
wlan.isconnected():
print("connexion",ssid)
wlan.connect(secrets.SSID, secrets.PASSWORD)
while not
wlan.isconnected():
pass
print("Adresse IP
print("Masque réseau :
print("Gateway
:", wlan.ifconfig()[2])
print("Serveur DNS
return
wlan.ifconfig()[0]
AdresseIP
= connexion_wifi(secrets.ssid, secrets.password)
s
= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((AdresseIP,8080))
s.listen(1)
while
True
:
print("Attente connexion
Pin
:", wlan.ifconfig()[0])
", wlan.ifconfig()[1])
:", wlan.ifconfig()[3])
...")
site
:
</p>