import time
import network # Import network module

# Enter correct router name and password
ssidRouter     = 'REPLACE_WITH_YOUR_SSID' # Enter the router name
passwordRouter = 'REPLACE_WITH_YOUR_PASSWORD' # Enter the router password

def STA_Setup(ssidRouter,passwordRouter):
    print("Setup start")
    sta_if = network.WLAN(network.STA_IF) # Set ESP32 in Station mode
    if not sta_if.isconnected():
        print('connecting to',ssidRouter)
# Activate ESP32’s Station mode, initiate a connection request to the router
# and enter the password to connect.
        sta_if.active(True)
        sta_if.connect(ssidRouter,passwordRouter)
# Wait for ESP32 to connect to router until they connect to each other successfully.
        while not sta_if.isconnected():
            pass
# Print the IP address assigned to ESP32 in “Shell”.
    print('Connected, IP address:', sta_if.ifconfig())
    print("Setup End")

try:
    STA_Setup(ssidRouter,passwordRouter)
except:
    sta_if.disconnect()