wlan - wifi Operation#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view the demo example of wlan

Example

--[[
Reminder:
For modules that only support wifiscan, only the init/scan/scanResult functions are available

For example, Air780E/Air600E/Air780EG only support wifiscan
]]

Constant#

constant

type

explanation

wlan.ESPTOUCH

esptouch distribution network,

V1

wlan.AIRKISS

Airkiss Distribution network,

WeChat commonly used

wlan.ESPTOUCH_V2

esptouch Distribution network,

V2, not tested

wlan.init()#

Initialization

Parameters

None

Return Value

return value type

explanation

bool

Returns true on success, otherwise false

Examples

None


wlan.setMode(mode)#

Set wifi mode

Parameters

Incoming Value Type

Explanation

int

wifi Mode

Return Value

return value type

explanation

bool

Returns true on success, otherwise false

Examples

-- Set to AP mode, broadcast ssid, receive wifi client link
wlan.setMode(wlan.AP)

-- Set to STATION mode, which is also the default mode after initialization
wlan.setMode(wlan.STATION)

-- Mixed mode, do AP and do again STATION
wlan.setMode(wlan.APSTA)

wlan.ready()#

When acting as a STATION, whether the AP has been connected and the IP has been obtained successfully

Parameters

None

Return Value

return value type

explanation

bool

The connection has been successfully returned true, otherwise it returns false

Examples

None


wlan.connect(ssid, password, auto_reconnect, bssid)#

As a STATION, connect to the specified AP

Parameters

Incoming Value Type

Explanation

string

AP of ssid

string

AP password of, optional

int

0 Turn off automatic reconnect, 1 turn on automatic reconnect. Auto reconnect is currently forced on

string

AP bssid, optional, must be 6 bytes

Return Value

return value type

explanation

bool

Returns true if the connection is initiated, otherwise false. Note that the connection to the AP is not successful!!

Examples

-- Normal mode with password
wlan.connect("myap", "12345678")
-- Normal mode, without password
wlan.connect("myap")
-- Special mode, reuse the previous ssid and password, this direct connection
-- Note that the premise is that ssid and or password have been transmitted after this power-on, otherwise it will fail.
wlan.connect()

-- Special mode, use ssid and password, this connection specifies bssid, 2024.5.7 new
local bssid = string.fromHex("00182946365f")
wlan.connect("myap", "12345678", 1, bssid)

wlan.disconnect()#

When STATION, disconnect AP

Parameters

None

Return Value

None

Examples

None


wlan.scan()#

scan wifi band

Parameters

None

Return Value

None

Examples

-- Note that wlan.scan() is an asynchronous API and will return immediately after starting the scan.

-- wifi After the scan is successful, there will be a WLAN_SCAN_DONE message, read
sys.subscribe("WLAN_SCAN_DONE", function ()
    local results = wlan.scanResult()
    log.info("scan", "results", #results)
    for k,v in pairs(results) do
        log.info("scan", v["ssid"], v["rssi"], (v["bssid"]:toHex()))
    end
end)

-- The following demonstration is a regular scan after initializing wifi, which can be modified according to actual business requirements.
sys.taskInit(function()
    sys.wait(1000)
    wlan.init()
    while 1 do
        wlan.scan()
        sys.wait(15000)
    end
end)

wlan.scanResult()#

Get wifi scan results

Parameters

None

Return Value

return value type

explanation

table

Scan Results

Examples

-- Please refer to wlan.scan() function for usage

wlan.smartconfig(mode)#

distribution network

Parameters

Incoming Value Type

Explanation

int

Distribution network mode, which defaults to esptouch. If 0 is passed, the distribution network will be stopped actively.

Return Value

return value type

explanation

bool

Start success or stop success, returns true, otherwise returns false

Examples

wlan.smartconfig()
local ret, ssid, passwd = sys.waitUntil("SC_RESULT", 180*1000) -- Wait up to 3 minutes.
log.info("sc", ret, ssid, passwd)
-- Please see the detailed usage demo

wlan.getMac(tp, hexstr)#

Get mac

Parameters

Incoming Value Type

Explanation

int

What mac address to set, for ESP32 series, only STA address can be set, I .e. 0, and the default value is also 0

bool

Whether to convert the HEX character, the default is true, that is, the output hex string.

Return Value

return value type

explanation

string

MAC Address, hexadecimal string form “AABBCCDDEEFF” or raw data

Examples

None


wlan.setMac(tp, mac)#

Setup mac

Parameters

Incoming Value Type

Explanation

int

What mac address to set, for ESP32 series, only STA address can be set, namely 0

string

MAC address to be set, length 6 bytes

Return Value

return value type

explanation

bool

Returns true on success, otherwise false

Examples

-- Set MAC address, firmware compiled after 2023-03-01 is available
local mac = string.fromHex("F01122334455")
wlan.setMac(0, mac)

-- Some modules support restoring the default MAC, such as the esp32 series
-- Firmware compiled after 2023-11-01 is available
local mac = string.fromHex("000000000000")
wlan.setMac(0, mac)

wlan.getIP()#

obtain ip address, meaningful only in STATION or APSTA mode

Parameters

None

Return Value

return value type

explanation

string

ip Address. Currently, only ipv4 addresses are returned. For example “192.168.1.25”

Examples

None


wlan.createAP(ssid, passwd, gateway, netmask, channel, opts)#

Start AP

Parameters

Incoming Value Type

Explanation

string

AP SSID of, required

string

AP Password for, optional

string

AP The gateway address of the, default 192.168.4.1

string

AP Gateway mask of, default 255.255.255.0

int

AP established channel, default 6

table

AP Configuration options for, optional

Return Value

return value type

explanation

bool

Returns true for successful creation, otherwise false

Examples

-- Note that when calling this AP, if the wifi mode is STATION, it will automatically switch APSTA
wlan.createAP("luatos1234", "12341234")
-- Set gateway IP, mask, channel, 2023.7.13 new, BSP may not support
-- wlan.createAP("luatos1234", "12341234", "192.168.4.1", "255.255.255.0", 6)

-- opts More configuration items, 2024.3.5 new
--[[
{
    hidden = false, -- Whether to hide the SSID, default false, do not hide
    max_conn = 4 -- Maximum number of clients, default 4
}
]]

wlan.stopAP()#

Turn off the AP function

Parameters

None

Return Value

return value type

explanation

bool

Returns true for successful creation, otherwise false

Examples

wlan.stopAP()

wlan.getInfo()#

Obtain information, such as the bssid and signal strength of the AP, which can be obtained after the STA is connected to the Internet.

Parameters

None

Return Value

return value type

explanation

table

Details, key-value pair form

Examples

log.info("wlan", "info", json.encode(wlan.getInfo()))
--[[
Typical output
{
    "bssid" : "xxxxxx",
    "rssi"  : -89,
    "gw" : "192.168.1.1"
}
]]

wlan.powerSave(mode)#

Read or set power saving mode

Parameters

Incoming Value Type

Explanation

int

Power saving mode, optional, incoming is the setting, for example wlan.PS_NONE

Return Value

return value type

explanation

int

Current power saving mode/power saving mode after setting

Examples

-- Please refer to the Constant Table  PS_NONE/PS_MIN_MODEM/PS_MAX_MODEM
log.info("wlan", "PS", wlan.powerSave(wlan.PS_NONE))
-- This API was added on March 31, 2023.03.31

wlan.hostname(new_name)#

Read or set Hostname

Parameters

Incoming Value Type

Explanation

string

New hostname, optional, incoming is setting

Return Value

return value type

explanation

string

Current hostname or after setting hostname

Examples

-- This API was added on July 23, 2023.0.
-- This function should be set before wlan.init, at the latest before wlan.connect
-- hostname The default value is the MAC value of the "LUATOS_" device
-- For example: LUATOS_0022EECC2399

wlan.hostname("My wifi IoT device")

wlan.staIp(dhcp_enable, ip, netmask, gateway)#

Set IP acquisition mode in Station mode

Parameters

Incoming Value Type

Explanation

bool

Whether to enable DHCP, the default is true

string

Local IP address, such as 192.168.2.200, required when DHCP is disabled

string

Native IP mask, for example 255.255.255.0, required when DHCP is disabled

string

Native IP gateway, such as 192.168.2.1, required when DHCP is disabled

Return Value

return value type

explanation

bool

Returns true on success, otherwise false

Examples

-- This API was added on October 06, 2023.
-- This function needs to be called after wlan.init.

-- Enable DHCP, the default is also enabled DHCP, here is the demo API use
wlan.staIp(true)
-- Disable DHCP, set IP/Mask/Gateway by yourself
wlan.staIp(false, "192.168.2.200", "255.255.255.0", "192.168.2.1")