I am now using the following to achieve my task and so far it worked very well.
print("Set up wifi mode..")
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("TestAP","87654321")
ssid = 'TestAP'
found = nil
wifi.sta.getap(function(t)
--for k,v in pairs(t) do print(k.." : "..v) end
found = t[ssid] or false
end)
tmr.alarm(0, 500, 1, function()
if (found == nil) then
print('scanning...')
else
tmr.stop(0)
if (found == false) then
print("Network not Found! Creating..")
cfg={}
cfg.ssid="TestAP"
cfg.pwd="87654321"
wifi.ap.config(cfg)
print(wifi.ap.getip())
dofile("tcpserver.lua")
print("Opening File tcpserver.lua")
else
print('found', ssid, found)
print("Found Network TestAP")
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("tcpclient.lua")
print("Opening File tcpclient.lua")
end
end)
end
end
end)
Thanks for all your help!