Skip to content

Commit

Permalink
Update init.lua example to use new wifi.sta.config() implementation (n…
Browse files Browse the repository at this point in the history
…odemcu#1953)

* Update init.lua example in upload.md with new station config format
* Fixed typo in description of wifi.eventmon.register()
* Fixed typo and improved example init.lua in docs/en/upload.md
  • Loading branch information
dnc40085 authored and Konrad Eisele committed Jan 7, 2018
1 parent 460e51e commit 2ca3092
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/en/modules/wifi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ T: Table returned by event.
- `wifi.eventmon.STA_DISCONNECTED`: Station was disconnected from access point.
- `SSID`: SSID of access point.
- `BSSID`: BSSID of access point.
- `REASON`: See [wifi.eventmon.reason](#wifieventmonreason) below.
- `reason`: See [wifi.eventmon.reason](#wifieventmonreason) below.
- `wifi.eventmon.STA_AUTHMODE_CHANGE`: Access point has changed authorization mode.
- `old_auth_mode`: Old wifi authorization mode.
- `new_auth_mode`: New wifi authorization mode.
Expand Down
69 changes: 55 additions & 14 deletions docs/en/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,65 @@ function startup()
end
end

-- Define WiFi station event callbacks
wifi_connect_event = function(T)
print("Connection to AP("..T.SSID..") established!")
print("Waiting for IP address...")
if disconnect_ct ~= nil then disconnect_ct = nil end
end

wifi_got_ip_event = function(T)
-- Note: Having an ip address does not mean there is internet access!
-- Internet connectivity can be determined with net.dns.resolve().
print("Wifi connection is ready! IP address is: "..T.IP)
print("Startup will resume momentarily, you have 3 seconds to abort.")
print("Waiting...")
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end

wifi_disconnect_event = function(T)
if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then
--the station has disassociated from a previously connected AP
return
end
-- total_tries: how many times the station will attempt to connect to the AP.
local total_tries = 75
print("\nWiFi connection to AP("..T.SSID..") has failed!")

--There are many possible disconnect reasons, the following iterates through
--the list and returns the string corresponding to the disconnect reason.
for key,val in pairs(wifi.eventmon.reason) do
if val == T.reason then
print("Disconnect reason:"..val.."("..key..")")
break
end
end

if disconnect_ct == nil then
disconnect_ct = 1
else
disconnect_ct = disconnect_ct + 1
end
if disconnect_ct < total_tries then
print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")")
else
wifi.sta.disconnect()
print("Aborting connection to AP!")
disconnect_ct = nil
end
end

-- Register WiFi Station event callbacks
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
wifi.sta.config({ssid=SSID, pwd=PASSWORD, save=true})
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(cb_timer)
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
cb_timer:unregister()
print("WiFi connection established, IP address: " .. wifi.sta.getip())
print("You have 3 seconds to abort")
print("Waiting...")
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
end
end)
```

Inspired by [https://github.com/ckuehnel/NodeMCU-applications](https://github.com/ckuehnel/NodeMCU-applications)
```

# Compiling Lua on your PC for Uploading

Expand Down

0 comments on commit 2ca3092

Please sign in to comment.