Skip to content
Advertisement

Connecting to Wifi in micro python hangs

I’m trying to setup a function that interactively gets takes user input and connects to WiFi. I can scan the nearby networks and get a list of SSIDs but once I go to actually connect the program always hangs. I can’t seem to figure out why.

import machine
import urequests
import network
from network import WLAN

def wifi_con():
    station = network.WLAN(network.STA_IF)
    station.active(True)
    wlan = WLAN()
    nets = wlan.scan()
    for i in range(len(nets)):
            print(str(i) + 't' + str(nets[i][0])[2:-1])
    print('Please enter the number corresponding to the SSID you wish to connect to:')
    sel = -1
    sel = int(input())
    while sel not in range(len(nets)):
            print('Please enter the number corresponding to the SSID you wish to connect to:')
    print("please enter the wifi password: ")
    connect_options = {
            'ssid':str(nets[sel][0])[2:-1],
            'password':input()
    }
    print(nets[sel][0], str(input()))
    wlan.connect(str(nets[sel][0])[2:-1], input())
    # test that we actually connected
    print('getting the paste')
    r = urequests.get('https://pastebin.com/raw/CZ6Mkdeg')
    print(r.content)

hardware: LOLIN D32 (esp32 based board)

this is the only code on the board so I do not think anything else is interfering with it.

Advertisement

Answer

So in case anyone comes looking for this issue later I have found the issue. Apparently the board was maintaining a connection from earlier even though I was resetting the board. I disconnected from the wifi using this function and then my function worked after I realized I was calling input() more than once like an idiot

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement