Quantcast
Viewing all articles
Browse latest Browse all 4325

General • Re: WiFi configuration without a display

This is what I have. Will be interesting to see if it works for others. It would need to present itself as an initial AP for a user to specify the SSID and password to connect to which this doesn't do.

Code:

import networkimport secretsimport socketimport time# ------------------------------------------------------------print("Making initial connection to router")wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.SSID, secrets.PASSWORD)while wlan.status() != 3:  print("  Waiting for initial connection ...")  time.sleep(1)  print("Initial connection successful")info = wlan.ifconfig()desc = ["IP Address", "Netmask", "Gateway", "DNS"]for n in range(4):  print("  {:<12} : {}".format(desc[n], info[n]))ip_tag = "Pico W on " + info[0]# ------------------------------------------------------------print("Creating an access point with '{}' name".format(ip_tag))ap = network.WLAN(network.AP_IF)ap.config(ssid=ip_tag, password="random")ap.active(True)while not ap.active():  print("  Waiting to create access point ...")print("Access point creation successful")# ------------------------------------------------------------print("Making second connection to router")wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.SSID, secrets.PASSWORD)while wlan.status() != 3:  print("  Waiting for second connection ...")  time.sleep(1)  print("Second connection successful")info = wlan.ifconfig()desc = ["IP Address", "Netmask", "Gateway", "DNS"]for n in range(4):  print("  {:<12} : {}".format(desc[n], info[n]))# ------------------------------------------------------------html = """  <!DOCTYPE html>  <html>    <head>      <title>Pico W - wifi_web_server_simplest</title>    </head>    <body>      <h1>Pico W</h1>      <p>wifi_web_server_simplest : {}</p>    </body>  </html>"""addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]print("URL for this Pico W : http://{}:{}".format(wlan.ifconfig()[0], addr[-1]))s = socket.socket()s.bind(addr)s.listen(1)while s:  try:    client, addr = s.accept()    request = str(client.recv(1024))    client.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")    client.send(html.strip().format(time.time()))    client.close()  except OSError as e:    client.close()  except KeyboardInterrupt:    s.close()    s = None

Statistics: Posted by hippy — Thu Feb 08, 2024 10:40 pm



Viewing all articles
Browse latest Browse all 4325

Trending Articles