''' On affiche dans une page web la valeur d'un potentiomètre branché au Raspberry Pi Pico Plus d'informations sur le blog: https://electroniqueamateur.blogspot.com/2022/07/raspberry-pi-pico-w-afficher-les.html ''' # importation des bibliothèques pertinentes import machine import socket import network import time # écrivez le nom du réseau wifi et le mot de passe ssid = 'nom_du_reseau' password = 'mot_de_passe' # potentiomètre branché à la broche GP26 pot = machine.ADC(26) # Contenu de la page web, en html # les accolades indiquent la position des valeurs numériques variables html = """<!DOCTYPE html> <html> <head> <title>Raspberry Pi Pico W</title> <meta http-equiv=Refresh content=2></head> <body> <h1>Valeur du potentiomètre:</h1> <h2> {} / 65535 </h2> <h2> {} % </h2> <h2> {} V </h2> </body> </html> """ # connexion au réseau wifi wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) max_wait = 10 # si pas connecté en 10 secondes, on abandonne print('attente de la connection a ' + ssid, end='') while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: break max_wait -= 1 print('.', end='') time.sleep(1) print ('') if wlan.status() != 3: raise RuntimeError('echec de la connexion a ' + ssid) else: print('connexion reussie') status = wlan.ifconfig() print( 'adresse ip = ' + status[0] ) addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) while True: cl, addr = s.accept() cl_file = cl.makefile('rwb', 0) while True: line = cl_file.readline() if not line or line == b'\r\n': break # lecture du potentiomètre et conversion en pourcentage et en volts: valeur = pot.read_u16() pourcentage = valeur / 65535 * 100 volts = valeur / 65535 * 3.3 # construction de la page web (on insère les variables) reponse = html.format(str(valeur), str("%.1f" % pourcentage), str("%.2f" % volts)) cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(reponse) cl.close()