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.
JavaScript
x
29
29
1
import machine
2
import urequests
3
import network
4
from network import WLAN
5
6
def wifi_con():
7
station = network.WLAN(network.STA_IF)
8
station.active(True)
9
wlan = WLAN()
10
nets = wlan.scan()
11
for i in range(len(nets)):
12
print(str(i) + 't' + str(nets[i][0])[2:-1])
13
print('Please enter the number corresponding to the SSID you wish to connect to:')
14
sel = -1
15
sel = int(input())
16
while sel not in range(len(nets)):
17
print('Please enter the number corresponding to the SSID you wish to connect to:')
18
print("please enter the wifi password: ")
19
connect_options = {
20
'ssid':str(nets[sel][0])[2:-1],
21
'password':input()
22
}
23
print(nets[sel][0], str(input()))
24
wlan.connect(str(nets[sel][0])[2:-1], input())
25
# test that we actually connected
26
print('getting the paste')
27
r = urequests.get('https://pastebin.com/raw/CZ6Mkdeg')
28
print(r.content)
29
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