ESP32 In MicroPython: WiFi |
Written by Mike James & Harry Fairhead | ||||||||||||||||||||||||||||||||||||||
Tuesday, 01 August 2023 | ||||||||||||||||||||||||||||||||||||||
Page 2 of 3
A Practical ConnectIn practice, you need to check that the attempted connection has worked. The connect method returns at once and usually before the connection has been made. Ideally you would select a connection method that informed you of progress and reported any errors. You can find out the status of the current connection using the status method which returns an integer status code:
The problem is how to communicate this state to the user? The simplest solution is to flash an LED. For example: import network from machine import Pin, Timer from time import sleep_ms def setup(country, ssid, key): network.country(country) wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.disconnect() LED = Pin(2, Pin.OUT) LED.on() timeout = 20000 wifi.connect(ssid,key) timer = Timer(1) timer.init(period=200, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value())) s = network.STAT_IDLE while timeout>0: print("connecting",s) s = wifi.status() if s != network.STAT_CONNECTING: break sleep_ms(100) timeout = timeout-100 if(s==network.STAT_GOT_IP): timer.init(period=1000, mode=Timer.PERIODIC, This function accepts the country code, SSID (Service Set Identifier), i.e. your network's name and key for the connection. It then creates a WLAN object and activates the hardware. The ESP32 tends to keep its connections even after the current program has ended so a call to disconnect ensures that the connection is a fresh one. If you don’t want to do this you can sometimes speed up the function if the connection is already made. After the attempt to connect, a While loop checks the status every 100ms. While the connection is being made, s=1, the LED is flashed every 200ms. If the loop ends with a connection, the LED is switched on, but if there is an error it is reset to flash every second. You can expand the LED feedback to indicate to the user different error conditions using different flashing patterns. You can get the signal strength of the current connection using: wifi.status('rssi') You can test for a connection using: wifi.isconnected() You can also find out about the current configuration of the connection using: wifi.ifconfig() which returns a tuple of: (ip address, subnet mask, gateway ip, dns ip) You can also set these parameters by calling ifconfig with an appropriate tuple. For example: wifi.ifconfig(("192.168.253.120","255.255.255.0", "192.168.253.1","8.8.8.8")) You can also read and set some general configuration parameters using wifi.config(“parameter”) which returns the value of the named parameter. In theory you can request any of the following:
You can also use the parameter names as named parameters in a call to config to set the corresponding parameter. For example, to set the most important, hostname: wifi.config(hostname = "myESP") |
||||||||||||||||||||||||||||||||||||||
Last Updated ( Tuesday, 01 August 2023 ) |