Use MicroPython development ESP32 (04): Use wireless networks and WebREPL

purpose

ESP32 a major feature is support for wireless networks, this article will introduce the use MicroPython setup / wireless network connection.

Wireless local area network (WLAN)

Function Description

MicroPython used mainly uses a wireless network networkmodule WLANtype, the specific function as follows:

  • class network.WLAN(interface_id)
    The constructor for declaring objects WLAN, optional parameters network.STA_IF(connected to an existing wireless network), network.AP_IF(as the network access point);
  • WLAN.active([is_active])
    Do not fill the parameters used to return the state of the wireless network function, fill in Truethe Wi-Fi enabled, fill Falsedeactivate the wireless network;
  • WLAN.connect(ssid=None, password=None, *, bssid=None)
    The STA mode is used to connect to wireless networks, parameters are ssidnetwork name, passwordpassword, bssidMAC address of the network device;
  • WLAN.disconnect()
    For disconnecting the current network connection;
  • WLAN.scan()
    Wireless networks available in the environment in a scanning STA mode, returning a list indicating the tuple consisting of wireless network information, wireless network information in the following format:
    (ssid, bssid, channel, RSSI, authmode, hidden)
    BSSID expressed in binary form, can be used ubinascii.hexlify () is converted to ASCII format;
    Channel is channel number, 2.4G network channel number from 1 to 13, based on the channel may cause contamination, often taking 1,6,11;
    the RSSI signal strength, the smaller the value the better the signal;
    the AuthMode encryption mode, is not of 0 – open, 1 – WEP, 2 – WPA-PSK, 3 – WPA2-PSK, 4 – WPA/WPA2-PSK;
    hidden indicate whether the wireless network False – 0 – visible – 可见的hidden , True – 1 – hidden –隐藏的;
  • WLAN.status([param])
    Return to the Network work state does not fill parameters:
    STAT_IDLE - Wi-Fi is not turned on;
    STAT_CONNECTING - Connecting;
    STAT_WRONG_PASSWORD - connection failed because of wrong password;
    STAT_NO_AP_FOUND - access point is not found;
    STAT_CONNECT_FAIL - connection failed for other reasons;
    STAT_GOT_IP - successful IP connection ;
    the STA mode may also be filled in 'rssi'as a parameter to get the current network signal strength;
  • WLAN.isconnected()
    If the STA mode has an IP network and returns True;
    AP mode if the access device returns True;
  • WLAN.ifconfig([(ip, subnet, gateway, dns)])
    Without parameters will return the current network device parameter, the parameter will be a tuple organization with four items, parameters are the IP address, subnet mask, gateway address, DNS server address;
    may be provided with parameters used when the device network parameters, for example in the following manner:
    nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
  • WLAN.config('param')
    WLAN.config(param=value, ...)
    Device used to query or modify network parameters, parameters required by the designated keyword manner, the available parameters are as follows:
    MAC - address of a physical device
    essid - ap network name mode
    channel - channel number
    hidden - is hidden
    authmode - encryption
    password - ap mode password
    dhcp_hostname - dhcp hostname

Demo

STA mode
Here Insert Picture Description
image above connection I called (SSID) AAA, the password is 12345678 wireless network, even after the network print IP, subnet mask, gateway, DNS server information.

AP mode
Here Insert Picture Description
image above-enabled wireless access point, the network name is lalala, encryption is WPA / WPA2-PSK, the password is 12345678;

WebREPL

All interact through the serial port and the development board before the presentation of the article, after the network-enabled, we can interact through a network, the following presentation:
Here Insert Picture Description
on the interactive map is made by a browser to access the IP address of the development board, when entering the password above, and Linux habits as the input will not be displayed. After a successful login press Ctrl + Bcan enter the Shell debugging. In this tool, you can also transfer files.

Need to make before using the tool development board settings:
Here Insert Picture Description
Enter the password and port number is set, the default port number can also be the default case of default port number is 8266. For webrepl settings are saved in a file, reboot the device also effective. You can use webrepl.stop () method to close the service. You can write a network of automatic initialization code is stored main.pyinto the module, so that after the next time you start to debug directly through the network.

WebREPL Download: https://github.com/micropython/webrepl
WebREPL online hosted version Address: http://micropython.org/webrepl

to sum up

Wireless networking is relatively simple to use, more content can refer to the following links:
http://docs.micropython.org/en/latest/library/network.WLAN.html

Published 68 original articles · won praise 176 · views 220 000 +

Guess you like

Origin blog.csdn.net/Naisu_kun/article/details/104049030