Skip to content
Boris Lovosevic edited this page Sep 13, 2019 · 8 revisions

network module


Class wifi

wifi module enables accessing the network resources using the ESP8266/ESP8285.
Some K210 boards (like MAIX-M1, DanDock) already have ESP8266 attached to K210 UART.
For other boards, any ESP8266/ESP8285 board or module can be simply attached, only Tx, Rx and GND are needed.
ESP-01M is recommended, as it is small and easy to connect.

wifi uses the advanced ESP8266 AT-firmware and the used ESP8266 module must be flashed with the approprite firmware before the first use.
Instructions and the firmware files are available here.
The flashing has to be done only once. If the formware update is available it can be done using the OTA update directly from MicroPython.

To use this class it must first be imported.

from network import wifi

or

import network
wifi = network.wifi

wifi.debug(True|False) method is provided to enable or disable low level wifi module communication logging.
If enabled, detailed information about communication between K210 and ESP8266 will be printed.


Methods


wifi.debug(True|False)

Enable or disable low level wifi module communication logging.

wifi.start(tx, rx, baudrate=115200, ssid, password, wait=False)

Starts the wifi interface and connects to the given acces point.
rx and tx must be set to the K210 gpios to which the ESP8266 module is connected.
ssid and password must be set according to your acces point credentials.
Optional wait argument can be set to True to wait for wifi initialization to finish. If False, the method will return imediatelly.
If the ESP8266 baudrate is different than selected, it will be automatically detected and selected instead.

import _thread, network, time, machine, socket, gc, os

wifi = network.wifi

wifi.debug(True)
wifi.start(tx=7, rx=6, ssid="LoBoInternet", password="1234xyz", baudrate=921600, wait=True)
M (371344346) [WIFI]: Initialize WiFi UART
M (371348631) [WIFI]: UART #0: initialize uart hardware
M (371378501) [WIFI]: UART #0: initialized, tx=7, rx=6, bdr=921641
M (371403520) [WIFI]: WiFi TASK STARTED
M (371506598) [ATCMD]: Check and set device baudrate
W (372147053) [ATCMD]: Trying at 115200 bd
W (372836550) [ATCMD]: Trying at 230400 bd
W (373526040) [ATCMD]: Trying at 460800 bd
W (373591099) [ATCMD]: Device baudrate is 460800 bd
M (373595712) [WIFI]: WiFi device initialization start
M (374111514) [ATCMD]: AT COMMAND: [AT\r\n] Expecting: [\r\nOK\r\n]
M (374119519) [ATCMD]: AT RESPONSE: match condition 1 at position 4 (2 ms, 0 byte(s), buf_start=0)
M (374137527) [ATCMD]: AT COMMAND: [ATE0\r\n] Expecting: [\r\nOK\r\n]
M (374145534) [ATCMD]: AT RESPONSE: match condition 1 at position 6 (2 ms, 0 byte(s), buf_start=0)
M (374163540) [ATCMD]: AT COMMAND: [AT+TCPCLOSE?\r\n] Expecting: [\r\nOK\r\n]
M (374172548) [ATCMD]: AT RESPONSE: match condition 1 at position 16 (2 ms, 0 byte(s), buf_start=0)
M (374190555) [ATCMD]: AT COMMAND: [AT+CWMODE?\r\n] Expecting: [+CWMODE:1]
M (374198561) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (1 ms, 0 byte(s), buf_start=0)
M (374216568) [ATCMD]: AT COMMAND: [AT+CWJAP?\r\n] Expecting: [+CWJAP:]
M (374224573) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (1 ms, 0 byte(s), buf_start=0)
M (374242579) [ATCMD]: AT COMMAND: [AT+CIPMUX=1\r\n] Expecting: [\r\nOK\r\n]
M (374251585) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (2 ms, 0 byte(s), buf_start=0)
M (374269594) [ATCMD]: AT COMMAND: [AT+CIPDINFO=0\r\n] Expecting: [\r\nOK\r\n]
M (374278599) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (2 ms, 0 byte(s), buf_start=0)
M (374296609) [ATCMD]: AT COMMAND: [AT+CIPRECVMODE?\r\n] Expecting: [\r\nOK\r\n]
M (374307618) [ATCMD]: AT RESPONSE: match condition 1 at position 16 (3 ms, 0 byte(s), buf_start=0)
M (374325625) [ATCMD]: AT COMMAND: [AT+CIPRECVMODE=0\r\n] Expecting: [\r\nOK\r\n]
M (374334632) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (1 ms, 0 byte(s), buf_start=0)
M (374352638) [ATCMD]: AT COMMAND: [AT+SYSCPUFREQ=160\r\n] Expecting: [\r\nOK\r\n]
M (374361644) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (1 ms, 0 byte(s), buf_start=0)
M (374379655) [ATCMD]: AT COMMAND: [AT+CIPSNTPCFG?\r\n] Expecting: [+CIPSNTPCFG:1], [+CIPSNTPCFG:0]
M (374390661) [ATCMD]: AT RESPONSE: match condition 2 at position 0 (2 ms, 0 byte(s), buf_start=0)
M (374408670) [ATCMD]: AT COMMAND: [AT+CIPSNTPCFG=1,0,"pool.ntp.org"\r\n] Expecting: [\r\nOK\r\n], [\r\nERROR\r\n]
M (374422674) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (3 ms, 0 byte(s), buf_start=0)
M (374440684) [ATCMD]: AT COMMAND: [AT+CIPSNTPCFG?\r\n] Expecting: [+CIPSNTPCFG:1], [+CIPSNTPCFG:0]
M (374451689) [ATCMD]: AT RESPONSE: match condition 1 at position 0 (2 ms, 0 byte(s), buf_start=0)
M (374559766) [WIFI]: WiFi initialized and ready.
True

wifi.stop()

Stop and deinitialize the wifi interface. Free all resources used.

wifi.status()

Returns 2-items tuple containing the current wifi interface status (num_stat, txt_stat)

(89, 'Idle')
(98, 'Not started')

wifi.ntptime([set])

Get the current time from NTP server. If the optional argument set is True, system RTC time is updated.
Returns the unix time (number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970).

wifi.baudrate([bdr])

Set or get the current ESP8266 baudrate.
With no arguments returns ESP8266 baudrate.
If bdr is provided, sets ESP8266 baudrate. The changed baudrate takes effect immediately and is stored in ESP8266 flash and will be as default.
Accepted baudrates are: [115200, 230400, 460800, 921600, 1000000, 2000000].

wifi.sslbuffer([size])

Set or get current ESP8266 SSL buffer size.
With no arguments returns current ESP8266 SSL buffer size.
If size is provided, sets ESP8266 SSL buffer size. Allowed range is 2048 ~ 16384
Default value: 4096
SSL buffer size affects the SSL operations. Sometimes it may be necessary to increase the buffer size above default.

wifi.mode([mode])

Set or get current ESP8266 SSL mode.
0 - CA is disabled, so ESP8266 can verify SSL server
2 - CA is enabled, so ESP8266 can verify SSL server
With no arguments returns current ESP8266 SSL mode.

wifi.ifconfig()

Get current wifi interface configuration.
Returns 5-items tuple: (IP, gateway, mask, dns1, dns2)

>>> wifi.ifconfig()
('192.168.0.25', '192.168.0.1', '255.255.255.0', '83.139.103.3', '83.139.121.8')
>>> 

wifi.reset())

Reset ESP8266.
After the reset the wifi interface is started and initialized.

wifi.atcmd(cmd, timeout=500, response=('OK', 'ERROR'), delay=5, incresp=True)

Execute any AT Command.
This method provides flexible and feature rich way of executing AT Commands and parsing the result.
Single or multiple AT commands can be executed and single or multiple responses can be processed.

Single AT command processing

Argument Description
cmd string starting with AT
response string containing expected command response
or
tuple of strings containing the condition on which the waiting for the response will be terminated as successful
timeout timeout in ms for waiting for the response.
Default 500
incresp If set to False the terminating condition will not be included in the result.
Default: True
delay not used

Multiple AT command processing

Argument Description
cmd tuple containing the AT commands to be executed.
Each tuple item must itself be a tuple:
(command_data, is_data, timeout, response, repeat, incresp)
command_data - at command or data to be send
is_command set to True if command_data is data to be send, not the command
response not used, response from command tuple is used
timeout not used, timeout from command tuple is used
incresp not_used, incresp from command tuple is used
delay delay between commands in ms