- - Set the wifi.sta config with the data from the browser
- Connect
- On connect, callback
- Set a repeating timer that checks if an IP has been obtained every 500ms
- If so, send the IP and SSID back to the browser
- After sending, close, and move from station+AP to just station mode
It works 100% of the time, up to the repeating timer that checks if an IP has been obtained. Sending that data only works 5-10% of the time, and I can't figure out why. ("data" + the JSON data prints, "SENT" doesn't)
I already added the 5 second delay before switching over to station mode to prevent errors regarding the connection I was getting, and commented out the unregistering of the timer, which seemed to help initially, but still no luck.
Does anyone have a clue what could be preventing my socket from sending?
PS. Other instances of socket:send() work just fine, although those don't use as many callbacks.
setWiFi_sta(jsonData, true, function()
print("CONNECTED1")
local hold = false
local timer = tmr.create()
timer:alarm(500, tmr.ALARM_AUTO, function()
if (wifi.sta.status() == wifi.STA_GOTIP and not hold) then
hold = true
print("ip", wifi.sta.getip())
print("ssid", wifi.sta.getconfig(true).ssid)
local data = {}
data.ip = wifi.sta.getip()
data.ssid = wifi.sta.getconfig(true).ssid
print("data", sjson.encode(data))
localSocket:send(sjson.encode(data), function()
print("SENT")
localSocket:close()
tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
print("CLOSING SOCKET AND GOING TO STATION MODE")
wifi.setmode(wifi.STATION)
-- timer:unregister()
end)
end)
end
end)
end)
function setWiFi_sta(data, doConnectedCheck, connectedCallback)
print("setWiFi_sta")
if (data.ssid == "") then
setWiFi_ap()
return
end
wifi.sta.clearconfig()
print("config", wifi.sta.config({
ssid = data.ssid,
pwd = data.pass,
auto = false,
save = false
}))
if (data.ip ~= "") then
print("setip", wifi.sta.setip({
ip = data.ip,
netmask = data.netmask,
gateway = data.gateway
}))
end
if (doConnectedCheck) then
tmr.alarm(0, 60*1000, tmr.ALARM_SINGLE, function(timer)
-- If this code runs, the module hasn't connected to the network after 60 seconds,
-- so we should probably return to AP mode to fix this
setWiFi_ap()
end)
end
wifi.sta.connect(function()
print("CONNECTED2")
tmr.unregister(0)
if (connectedCallback ~= nil) then
connectedCallback()
end
end)
end