ESP32 NodeMCU Module WiFi
Functions
- wifi.getchannel() Gets the current WiFi channel.
- wifi.getmode() Gets WiFi operation mode.
- wifi.mode() Configures the WiFi mode to use.
- wifi.start() Starts the WiFi interface(s).
- wifi.stop() Shuts down the WiFi interface(s).
- wifi.sta.config() Sets the WiFi station configuration.
- wifi.sta.connect() Connects to the configured AP in station mode.
- wifi.sta.disconnect() Disconnects from AP in station mode.
- wifi.sta.on() Registers callbacks for WiFi station status events.
- wifi.sta.scan() Scan for available networks.
- wifi.ap.config() Configures the AP.
- wifi.ap.on() Registers callbacks for WiFi AP events.
Scan for available all station networks
wifi.start()
-- Scan and print all found APs, including hidden ones
wifi.sta.scan({ hidden = 1 }, function(err,arr)
if err then
print ("Scan failed:", err)
else
print(string.format("%-26s","SSID"),"Channel BSSID RSSI Auth Bandwidth")
for i,ap in ipairs(arr) do
print(string.format("%-32s",ap.ssid),ap.channel,ap.bssid,ap.rssi,ap.auth,ap.bandwidth)
end
print("-- Total APs: ", #arr)
end
end)
Connect ESP32 to WiFi Router
print("wifi init")
wifi.start()
wifi.mode(wifi.STATIONAP)
--connect to Access Point (DO NOT save config to flash)
station_cfg={}
station_cfg.ssid="ArunEworld"
station_cfg.pwd="Arun"
wifi.sta.config(station_cfg)
wifi.sta.connect()
WiFi Event Monitoring for Station Mode
Check the device connected to WiFi router?
--register callback
wifi.sta.on("connected", function(ev, info)
print("Connected to Router")
print("NodeMCU IP Connected ssid", info.ssid, "bssid", info.bssid, "Channel", info.channel, "Auth", info.auth)
end)
--unregister callback
wifi.sta.on("connected", nil)
Check the device disconnected from WiFi router?
--register callback
wifi.sta.on("disconnected", function(ev, info)
print("disconnected to Router")
print("NodeMCU Disconnected ssid", info.ssid, "bssid", info.bssid, "reason", info.reason)
end)
--unregister callback
wifi.sta.on("disconnected", nil)
Check the device authentication mode changed in WiFi router?
--register callback
wifi.sta.on("authmode_changed", function(ev, info)
print("authmode_changed in Router")
print("NodeMCU authmode_changed old_mode", info.old_mode, "new_mode", info.new_mode)
end)
--unregister callback
wifi.sta.on("authmode_changed", nil)
Check the device got IP from WiFi router?
--register callback
wifi.sta.on("got_ip", function(ev, info)
print("NodeMCU IP config:", info.ip, "netmask", info.netmask, "gw", info.gw)
end)
--unregister callback
wifi.sta.on("got_ip", nil)
Set Access Point Mode in ESP32 NodeMUC WiFi
wifi.start()
cfg={}
cfg.ssid="ArunEworld"
cfg.pwd="Arun"
wifi.ap.config(cfg)
WiFi Event Monitoring for AP Mode
Check Which device are connected to ESP32 in AP Mode?
--register callback
wifi.ap.on("sta_connected", function(ev, info)
print("sta_connected to Router")
print("NodeMCU sta_connected mac", info.mac, "id", info.id)
end)
--unregister callback
wifi.ap.on("sta_connected", nil)
Check Which device is disconnected from ESP32 in AP Mode?
--register callback
wifi.ap.on("sta_disconnected", function(ev, info)
print("sta_disconnected from Router")
print("NodeMCU sta_disconnected mac", info.mac, "id", info.id)
end)
--unregister callback
wifi.ap.on("sta_disconnected", nil)
Check Which device have problem with ESP32 in AP Mode?
--register callback
wifi.ap.on("probe_req", function(ev, info)
print("probe_req from Router")
print("NodeMCU probe_req from", info.from, "rssi", info.rssi)
end)
--unregister callback
wifi.ap.on("probe_req", nil)