1

Hey my problem: I have an Raspberry Pi pico w and it is used as an access point and web server where users can enter SSID and password of a WLAN they want the rasi to connect with. The data of the form gets send back and the raspi should then turn of the access point and connect the raspi with the given SSID and password to the Wi-Fi. But I just cant manage to do that here is my code so far everything works including sending the data back to the raspy just not the connection after to the given Wi-Fi :

import network
import socket
import re

# Webserver Konfiguration
server_address = ('', 8080)
html = """<!DOCTYPE html>
<html>
<head>
  <title>SSID und Passwort eingeben</title>
<style>
    body {
      text-align: center;
    }
    form {
      display: inline-block;
      margin-top: 50px;
      text-align: left;
    }
    label {
      display: block;
      margin-bottom: 10px;
    }
input[type="text"],
    input[type="password"] {
      width: 250px;
      height: 30px;
      font-size: 16px;
      margin-bottom: 20px;
    }
    input[type="submit"] {
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
      padding: 10px 20px;
    }
    input[type="submit"]:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>
  <h1>SSID und Passwort eingeben</h1>
  <form method="post" action="/">
    <label for="ssid">SSID:</label>
    <input type="text" id="ssid" name="ssid" required>
    <label for="password">Passwort:</label>
    <input type="password" id="password" name="password" required>
    <br>
    <input type="submit" value="Verbinden">
  </form>
</body>
</html>
"""

# Access Point Konfiguration
ap_ssid = "pico-wifi"
ap_password = "pico-wifi-password"

# Webserver starten
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(server_address)
server_socket.listen(1)
print('Webserver gestartet auf:', server_address)

while True:
    # Access Point starten
    ap = network.WLAN(network.AP_IF)
    ap.config(essid=ap_ssid, password=ap_password)
    ap.active(True)
    print(ap.ifconfig())

    # Auf Verbindungen warten
    print('Warten auf Verbindung...')
    client_socket, client_address = server_socket.accept()

    # HTTP Anfrage empfangen
    request = client_socket.recv(1024)
    request = str(request)

    # SSID und Passwort aus dem Formular auslesen
    ssid_match = re.search("ssid=([^&]+)", request)
    password_match = re.search("password=([^&]+)", request)
    ssid = ssid_match.group(1) if ssid_match else ""
    password = password_match.group(1) if password_match else ""

    # SSID und Passwort auf der Konsole ausgeben
    print('SSID:', ssid)
    print('Passwort:', password)
    
    # HTTP Antwort senden
    response = 'HTTP/1.1 200 OK\nContent-Type: text/html\n\n' + html
    client_socket.send(response.encode('utf-8'))

    # Verbindung zum Access Point trennen
    ap.active(False)
    
    # Versuche, mit dem WLAN zu verbinden
    if ssid and password:
        print('Versuche, mit WLAN zu verbinden...')
        sta_if = network.WLAN(network.STA_IF)
        if not sta_if.isconnected():
            sta_if.active(True)
            sta_if.connect
            
            
            # Versuche, mit dem WLAN zu verbinden
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect(ssid, password)

# Warten, bis die Verbindung hergestellt ist
while not sta.isconnected():
    pass

# Verbindungsinformationen ausgeben
print('Verbunden mit WLAN:', ssid)
print('IP-Adresse:', sta.ifconfig()[0])



I tried much but everything failed that i tried ^^ if i try to connect the pi pico byitself without ap and server running it works but not like this

Jonasus123
  • 13
  • 5

0 Answers0