Network
Example
Set the WiFi name and password, connect to WiFi, and print the connection result on the serial port
from m5stack import *
from m5stack_ui import *
from uiflow import *
import wifiCfg
wifiCfg.doConnect('M5-R&D', '**********')
print(wifiCfg.wlan_sta.isconnected())
API
wifiCfg.autoConnect(lcdShow=False)
- Attempts to automatically connect to previously configured Wi-Fi networks.
wifiCfg.autoConnect(lcdShow=True)
- Attempts to automatically connect to previously configured Wi-Fi networks and displays the connection status on the LCD.
wifiCfg.reconnect()
- Attempts to reconnect to previously configured and saved Wi-Fi networks. This method is usually used when the device loses connection and needs to retry.
wifiCfg.doConnect('', '')
- Sets the Wi-Fi name and password.
M5Label('label0', x=193, y=67, color=0x000, font=FONT_MONT_14, parent=None)
- Sends a P2P (peer-to-peer) message to the specified API Key.
str(getP2PData())
- Retrieves received P2P (peer-to-peer) message data.
network.WLAN(network.STA_IF)
- Creates a WLAN (Wireless Local Area Network) interface object. Select the Wi-Fi mode:
- Station mode: Connects to a local Wi-Fi network.
- Access Point mode: Creates a Wi-Fi hotspot.
wlan.config(essid='', password='', authmode=network.AUTH_OPEN)
- Configures WLAN interface parameters.
- essid: Sets the SSID (network name) of the access point.
- password: Sets the password of the access point.
- authmode: Sets the authentication mode of the access point.
wlan.active(True)
- Activates the interface: Enables the Wi-Fi functionality of the device, allowing it to start wireless communication.
str(wlan.scan())
- Scans for available Wi-Fi networks and returns a list of network information.
str(wlan.isconnected())
- Checks if the ESP32 is connected to a Wi-Fi network.
wlan.connect('your_SSID', 'your_PASSWORD')
- ssid: The name of the Wi-Fi network to connect to.
- password: The password of the Wi-Fi network.
str(wlan.config('mac'))
- str(wlan.config('mac')): Retrieves the MAC address of the device and converts it to a string format.
- str(wlan.config('essid')): Retrieves the currently configured SSID (network name) and converts it to a string format.
str(wlan.ifconfig())
- Retrieves the network configuration of the device.
str(wlan.status())
- Retrieves the current Wi-Fi connection status.
wlan.disconnect()
- Disconnects from the Wi-Fi network.
str(1001)
- STAT_IDLE: Wi-Fi is idle. The device is not attempting to connect to any Wi-Fi network.
- STAT_CONNECTING: Wi-Fi is connecting. The device is trying to connect to the specified Wi-Fi network.
- STAT_GOT_IP: Wi-Fi is connected and has obtained an IP address. The device has successfully connected to the Wi-Fi network and received an IP address from the DHCP server.
smartconfig.set_type(smartconfig.ESPTOUCH)
- ESPTOUCH: Configures using the ESPTOUCH protocol. Sends Wi-Fi configuration information (SSID and password) from a mobile app to the device, allowing it to connect to the specified Wi-Fi network.
- AIRKISS: Configures using the AIRKISS protocol. AIRKISS is a wireless configuration protocol developed by Tencent, used to send Wi-Fi configuration information to the device.
- ESPTOUCH_AIRKISS: Configures using both ESPTOUCH and AIRKISS protocols. The device listens for both protocols, and the mobile app can use either protocol to send Wi-Fi configuration information. The device configures itself based on the received information.
smartconfig.start()
- Starts the SmartConfig function, putting the device into SmartConfig configuration mode, waiting to receive Wi-Fi configuration information (SSID and password) from the mobile app.
<img class="blockly_svg" src="
https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/static/assets/img/u
iflow/blockly/hardwares/network/uiflow_block_network_smartconfig_stop.svg">
smartconfig.stop()
- Stops the SmartConfig function, exiting the SmartConfig configuration mode.
str(smartconfig.status())
- Retrieves the current status of SmartConfig.
str(smartconfig.get_ssid())
- Retrieves the SSID (network name) received during the SmartConfig process.
str(smartconfig.get_password())
- Retrieves the password received during the SmartConfig process.
str(smartconfig.get_phoneip())
- Retrieves the IP address of the phone during the SmartConfig process.
str(smartconfig.get_phoneip())
- Indicates the event when SmartConfig finishes scanning Wi-Fi networks.