I'm completely new and just ditched the AT Firmware and flashed NodeMCU on my ESPs, so please bare with me
Im using NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
My plan is to let the ESP scan for available networks when powering up, then check if a specific network already exists.
-If it does, it should join this network
-If it didn't find it, create it itself
Here is the (not working) code I am using:
print("Set up wifi mode..")
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("MyTest","87654321")
--Scan for available networks
print("Scanning for networks..")
function listap(t)
for k,v in pairs(t) do
print(k.." : "..v)
end
end
wifi.sta.getap(listap)
tmr.delay(10000)
--Connect if network available, create if not found
if listap == "MyTest" then
print("Found Network MyTest")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
--dofile("yourfile.lua")
end
end)
else print("Network not Found! Creating..")
cfg={}
cfg.ssid="MyTest"
cfg.pwd="87654321"
wifi.ap.config(cfg)
print(wifi.ap.getip())
end
I see multiple issues, but since I'm completely new to lua i don't know how to fix them and so far didn't have any luck with google.
1. Because the wifi.sta.getap command needs a couple of seconds, I added a delay. But I'm not sure if this is the right way and works as it should? I simply wanted the following code to execute when the scanning for networks is finished.
2. I am then trying to see if the desired network is in this list (with: if listap == "MyTest" then). But this is surely the wrong way. How do I search the created table the right way so that if the network MyTest is there, it connects to this, rather than creating a new network with the same name?
This is what I get from the ESPlorer:
> dofile("init.lua");
Set up wifi mode..
Scanning for networks..
Network not Found! Creating..
192.168.4.1 255.255.255.0 192.168.4.1
> randomAP: 3,-64,00:26:f2:f7:cf:1c,3
>randomAP2:3,-64,00:26:f2:f7:cf:1c,3
Any Help would be greatly appreciated! Thank you for any tips!