i have tryed in on an TTGO LORA32 and esp32-wroom-32D
code:
Code: Select all
import socket
import network
import time
SSID=""
PASSWORD=""
wlan=None
s=None
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #Activate the network interface
wlan.disconnect() #Disconnect the last connected WiFi
wlan.connect(ssid,passwd) #connect wifi
while(wlan.ifconfig()[0]=='0.0.0.0'):
time.sleep(1)
return True
try:
if(connectWifi(SSID,PASSWORD) == True): #judge whether to connect WiFi
ip=wlan.ifconfig()[0] #get ip addr
while True:
time.sleep(1)
print(wlan.status())
except:
if (s):
s.close()
wlan.disconnect()
wlan.active(False)