ESP8266 restarts when timer is used in lua [on hold] Unicorn Meta Zoo #1: Why another...
Is it OK if I do not take the receipt in Germany?
Protagonist's race is hidden - should I reveal it?
How long after the last departure shall the airport stay open for an emergency return?
Is Electric Central Heating worth it if using Solar Panels?
Where did Arya get these scars?
Could Neutrino technically as side-effect, incentivize centralization of the bitcoin network?
Trumpet valves, lengths, and pitch
What's parked in Mil Moscow helicopter plant?
Raising a bilingual kid. When should we introduce the majority language?
What is the term for a person whose job is to place products on shelves in stores?
Has a Nobel Peace laureate ever been accused of war crimes?
How to not starve gigantic beasts
What *exactly* is electrical current, voltage, and resistance?
Why isn't everyone flabbergasted about Bran's "gift"?
Married in secret, can marital status in passport be changed at a later date?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Passing args from the bash script to the function in the script
Is there any hidden 'W' sound after 'comment' in : Comment est-elle?
"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"
Did the Roman Empire have penal colonies?
What is the least dense liquid under normal conditions?
Would reducing the reference voltage of an ADC have any effect on accuracy?
Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?
Reattaching fallen shelf to wall?
ESP8266 restarts when timer is used in lua [on hold]
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraLinking to lua libraries w/ codeblocks on linuxCan't install lua on Mac 10.6.4How to setup and use lcurses (curses for Lua)?Building and Using Lua for iMacHow to launch a lua server?Lua script can't find files on new Windows 10 deviceError when installing lua-xlsxwriter with luarockslua command won't work in lua.exeError while installing lua-nginx-moduleSend sensor data to esp8266 esp-01 via arduino uno
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My esp8266 module restarts when a timer function is called. esp module restarts when a push message from server is processed.but if a timer is not used in the process it works perfectly fine, else if tmr.alarm function used it restarts following a push message from server.can anyone please find me a better solution to this problem.
my code is posted below
SERVER = "xxxxxxxxxxxxxxxx"
deviceId = wifi.sta.getmac()
PINS = {
-- IN = 8, -- GPIO02
-- OS = 4, -- GPIO02
-- OP = 5, -- GPIO14
-- RS = 1 -- GPIO05
IN = 7, -- GPIO14
OS = 4, -- GPIO02
OP = 5, -- GPIO04
RS = 1 -- GPIO05
}
count=6
gpio.mode(PINS.IN, gpio.OUTPUT)
gpio.mode(PINS.OP, gpio.OUTPUT)
gpio.mode(PINS.OS, gpio.OUTPUT)
gpio.mode(PINS.RS, gpio.INT, gpio.PULLUP)
gpio.write(PINS.IN, gpio.HIGH)
gpio.write(PINS.OP, gpio.LOW)
if gpio.read(PINS.OP) == 1 then
gpio.write(PINS.OS, gpio.LOW)
else
gpio.write(PINS.OS, gpio.HIGH)
end
RS_F = false
gpio.trig(PINS.RS, "up", function(level)
if level == 1 then
if RS_F then
node.restart()
else
if wifi.getmode() ~= 2 then
RS_F = true
start_ap()
else
print("already on ap")
end
end
end
end)
--timer function
function timer_on(time)
local duration = time
sec = duration*1000*60
tmr.alarm(6, sec, 2, function ()
if count>=1 then
timer_on(1)
count=count-1
print("counter")
end
end)
if count==1 then
print("led on")
count=6
end
end
-- HANDLE PUSH MESSHAES FROM SERVER
function handle_push(ws, msg)
local json_ok, msg = pcall(sjson.decode, msg)
if json_ok then
gpio.write(PINS.OP, msg.state)
timer_on(1)
if msg.restart then
node.restart()
end
if msg.reset then
file.remove("config")
tmr.alarm(5, 100, 0, function ()
node.restart()
end)
end
end
ws:send(sjson.encode({state=gpio.read(PINS.OP), config=read_config()}))
end
function ap_config_start()
print("Starting AP..")
tmr.stop(1)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({
ssid = "ELAENOR_SMART",
pwd = "ELAENOR_SMART",
auth = wifi.WPA_PSK
})
wifi.ap.setip({
ip = "xxxxxxxxxxxx",
netmask = "xxxxxxxxxxxx",
gateway = "xxxxxxxxxxxxx"
})
tmr.alarm(2, 400, 1, function()
if gpio.read(PINS.IN) == 0 then
gpio.write(PINS.IN, 1)
else
gpio.write(PINS.IN, 0)
end
end)
print("AP Started")
end
-- START ACCESS POINT:
function start_ap()
ap_config_start()
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
srv = net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(resp, data)
local body = ""
data = data:gsub("GET /", "")
data = data:gsub("HTTP/1.1", "")
data = split(data, "n")[0]
data = unescape(data)
print("data recieved: " .. data)
local json_ok, jdata = pcall(sjson.decode, data)
if json_ok then
body = sjson.encode({
deviceId = deviceId,
success = true
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
tmr.alarm(3, 100, 0, function()
wifi_connect({ssid=jdata.ssid,pwd=jdata.pwd}, function(success)
if success then
file.open("config","w")
file.write(data)
file.close()
print("OK")
else
ap_config_start()
print("NOT_OK")
end
end)
end)
else
body = sjson.encode({
success = false,
message = "INVALID_INPUT"
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
end
end)
end)
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=0
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
-- WEBSOCKET
function websocket_start(WIFI_CNF)
print("Starting Web Socket..")
local ws = websocket.createClient()
local wsUrl = "ws://"..SERVER.."/?deviceId="..deviceId
ws:on("connection", function(wss)
print("Connected")
local signin = sjson.encode({
signin = true,
deviceId = deviceId,
config = read_config().config
})
print(signin)
ws:send(signin)
tmr.stop(4)
gpio.write(PINS.IN, 1)
end)
ws:on("receive", function(wss, msg)
print("Received: ", msg)
handle_push(ws, msg)
gpio.write(PINS.IN, 0)
tmr.alarm(4, 100, 0, function()
gpio.write(PINS.IN, 1)
end)
end)
ws:on("close", function(wss, status)
if RS_F == false then
gpio.write(PINS.IN, 0)
print("Closed! Retrying..")
if wifi.sta.status() == 2 or wifi.sta.status() == 3 or wifi.sta.status() == 4 then
wifi_connect(WIFI_CNF)
else
ws:connect(wsUrl)
end
end
end)
ws:connect(wsUrl)
end
-- connecting to wifi
function wifi_connect(WIFI_CNF, CALL)
tmr.stop(2)
gpio.write(PINS.IN, 0)
print("Trying to connect to SSID " .. WIFI_CNF.ssid .. " using Password " .. WIFI_CNF.pwd)
wifi.setmode(wifi.STATION)
wifi.sta.config(WIFI_CNF)
tmr.alarm(1, 1000, 1, function ()
if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
print("Connecting..")
elseif wifi.sta.status() == 5 then
tmr.stop(1)
print("Connected! IP: ", wifi.sta.getip())
websocket_start(WIFI_CNF)
if CALL == nil then
tmr.stop(4)
else
CALL(true)
end
else
print("Connection Failed! Status: ",wifi.sta.status())
if CALL ~= nil then
tmr.stop(1)
wifi.sta.disconnect()
CALL(false)
end
end
end)
end
function read_config()
if file.open("config","r") then
local cok, cdata = pcall(sjson.decode, file.read())
file.close()
return cdata
else
return false
end
end
print("Initializing..")
local FI = read_config()
if FI then
print("Previous config exists!")
wifi_connect(FI)
else
print("No config detected!")
start_ap()
end
arduino lua iot
New contributor
put on hold as off-topic by Mokubai♦ 16 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Mokubai
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
My esp8266 module restarts when a timer function is called. esp module restarts when a push message from server is processed.but if a timer is not used in the process it works perfectly fine, else if tmr.alarm function used it restarts following a push message from server.can anyone please find me a better solution to this problem.
my code is posted below
SERVER = "xxxxxxxxxxxxxxxx"
deviceId = wifi.sta.getmac()
PINS = {
-- IN = 8, -- GPIO02
-- OS = 4, -- GPIO02
-- OP = 5, -- GPIO14
-- RS = 1 -- GPIO05
IN = 7, -- GPIO14
OS = 4, -- GPIO02
OP = 5, -- GPIO04
RS = 1 -- GPIO05
}
count=6
gpio.mode(PINS.IN, gpio.OUTPUT)
gpio.mode(PINS.OP, gpio.OUTPUT)
gpio.mode(PINS.OS, gpio.OUTPUT)
gpio.mode(PINS.RS, gpio.INT, gpio.PULLUP)
gpio.write(PINS.IN, gpio.HIGH)
gpio.write(PINS.OP, gpio.LOW)
if gpio.read(PINS.OP) == 1 then
gpio.write(PINS.OS, gpio.LOW)
else
gpio.write(PINS.OS, gpio.HIGH)
end
RS_F = false
gpio.trig(PINS.RS, "up", function(level)
if level == 1 then
if RS_F then
node.restart()
else
if wifi.getmode() ~= 2 then
RS_F = true
start_ap()
else
print("already on ap")
end
end
end
end)
--timer function
function timer_on(time)
local duration = time
sec = duration*1000*60
tmr.alarm(6, sec, 2, function ()
if count>=1 then
timer_on(1)
count=count-1
print("counter")
end
end)
if count==1 then
print("led on")
count=6
end
end
-- HANDLE PUSH MESSHAES FROM SERVER
function handle_push(ws, msg)
local json_ok, msg = pcall(sjson.decode, msg)
if json_ok then
gpio.write(PINS.OP, msg.state)
timer_on(1)
if msg.restart then
node.restart()
end
if msg.reset then
file.remove("config")
tmr.alarm(5, 100, 0, function ()
node.restart()
end)
end
end
ws:send(sjson.encode({state=gpio.read(PINS.OP), config=read_config()}))
end
function ap_config_start()
print("Starting AP..")
tmr.stop(1)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({
ssid = "ELAENOR_SMART",
pwd = "ELAENOR_SMART",
auth = wifi.WPA_PSK
})
wifi.ap.setip({
ip = "xxxxxxxxxxxx",
netmask = "xxxxxxxxxxxx",
gateway = "xxxxxxxxxxxxx"
})
tmr.alarm(2, 400, 1, function()
if gpio.read(PINS.IN) == 0 then
gpio.write(PINS.IN, 1)
else
gpio.write(PINS.IN, 0)
end
end)
print("AP Started")
end
-- START ACCESS POINT:
function start_ap()
ap_config_start()
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
srv = net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(resp, data)
local body = ""
data = data:gsub("GET /", "")
data = data:gsub("HTTP/1.1", "")
data = split(data, "n")[0]
data = unescape(data)
print("data recieved: " .. data)
local json_ok, jdata = pcall(sjson.decode, data)
if json_ok then
body = sjson.encode({
deviceId = deviceId,
success = true
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
tmr.alarm(3, 100, 0, function()
wifi_connect({ssid=jdata.ssid,pwd=jdata.pwd}, function(success)
if success then
file.open("config","w")
file.write(data)
file.close()
print("OK")
else
ap_config_start()
print("NOT_OK")
end
end)
end)
else
body = sjson.encode({
success = false,
message = "INVALID_INPUT"
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
end
end)
end)
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=0
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
-- WEBSOCKET
function websocket_start(WIFI_CNF)
print("Starting Web Socket..")
local ws = websocket.createClient()
local wsUrl = "ws://"..SERVER.."/?deviceId="..deviceId
ws:on("connection", function(wss)
print("Connected")
local signin = sjson.encode({
signin = true,
deviceId = deviceId,
config = read_config().config
})
print(signin)
ws:send(signin)
tmr.stop(4)
gpio.write(PINS.IN, 1)
end)
ws:on("receive", function(wss, msg)
print("Received: ", msg)
handle_push(ws, msg)
gpio.write(PINS.IN, 0)
tmr.alarm(4, 100, 0, function()
gpio.write(PINS.IN, 1)
end)
end)
ws:on("close", function(wss, status)
if RS_F == false then
gpio.write(PINS.IN, 0)
print("Closed! Retrying..")
if wifi.sta.status() == 2 or wifi.sta.status() == 3 or wifi.sta.status() == 4 then
wifi_connect(WIFI_CNF)
else
ws:connect(wsUrl)
end
end
end)
ws:connect(wsUrl)
end
-- connecting to wifi
function wifi_connect(WIFI_CNF, CALL)
tmr.stop(2)
gpio.write(PINS.IN, 0)
print("Trying to connect to SSID " .. WIFI_CNF.ssid .. " using Password " .. WIFI_CNF.pwd)
wifi.setmode(wifi.STATION)
wifi.sta.config(WIFI_CNF)
tmr.alarm(1, 1000, 1, function ()
if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
print("Connecting..")
elseif wifi.sta.status() == 5 then
tmr.stop(1)
print("Connected! IP: ", wifi.sta.getip())
websocket_start(WIFI_CNF)
if CALL == nil then
tmr.stop(4)
else
CALL(true)
end
else
print("Connection Failed! Status: ",wifi.sta.status())
if CALL ~= nil then
tmr.stop(1)
wifi.sta.disconnect()
CALL(false)
end
end
end)
end
function read_config()
if file.open("config","r") then
local cok, cdata = pcall(sjson.decode, file.read())
file.close()
return cdata
else
return false
end
end
print("Initializing..")
local FI = read_config()
if FI then
print("Previous config exists!")
wifi_connect(FI)
else
print("No config detected!")
start_ap()
end
arduino lua iot
New contributor
put on hold as off-topic by Mokubai♦ 16 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Mokubai
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
My esp8266 module restarts when a timer function is called. esp module restarts when a push message from server is processed.but if a timer is not used in the process it works perfectly fine, else if tmr.alarm function used it restarts following a push message from server.can anyone please find me a better solution to this problem.
my code is posted below
SERVER = "xxxxxxxxxxxxxxxx"
deviceId = wifi.sta.getmac()
PINS = {
-- IN = 8, -- GPIO02
-- OS = 4, -- GPIO02
-- OP = 5, -- GPIO14
-- RS = 1 -- GPIO05
IN = 7, -- GPIO14
OS = 4, -- GPIO02
OP = 5, -- GPIO04
RS = 1 -- GPIO05
}
count=6
gpio.mode(PINS.IN, gpio.OUTPUT)
gpio.mode(PINS.OP, gpio.OUTPUT)
gpio.mode(PINS.OS, gpio.OUTPUT)
gpio.mode(PINS.RS, gpio.INT, gpio.PULLUP)
gpio.write(PINS.IN, gpio.HIGH)
gpio.write(PINS.OP, gpio.LOW)
if gpio.read(PINS.OP) == 1 then
gpio.write(PINS.OS, gpio.LOW)
else
gpio.write(PINS.OS, gpio.HIGH)
end
RS_F = false
gpio.trig(PINS.RS, "up", function(level)
if level == 1 then
if RS_F then
node.restart()
else
if wifi.getmode() ~= 2 then
RS_F = true
start_ap()
else
print("already on ap")
end
end
end
end)
--timer function
function timer_on(time)
local duration = time
sec = duration*1000*60
tmr.alarm(6, sec, 2, function ()
if count>=1 then
timer_on(1)
count=count-1
print("counter")
end
end)
if count==1 then
print("led on")
count=6
end
end
-- HANDLE PUSH MESSHAES FROM SERVER
function handle_push(ws, msg)
local json_ok, msg = pcall(sjson.decode, msg)
if json_ok then
gpio.write(PINS.OP, msg.state)
timer_on(1)
if msg.restart then
node.restart()
end
if msg.reset then
file.remove("config")
tmr.alarm(5, 100, 0, function ()
node.restart()
end)
end
end
ws:send(sjson.encode({state=gpio.read(PINS.OP), config=read_config()}))
end
function ap_config_start()
print("Starting AP..")
tmr.stop(1)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({
ssid = "ELAENOR_SMART",
pwd = "ELAENOR_SMART",
auth = wifi.WPA_PSK
})
wifi.ap.setip({
ip = "xxxxxxxxxxxx",
netmask = "xxxxxxxxxxxx",
gateway = "xxxxxxxxxxxxx"
})
tmr.alarm(2, 400, 1, function()
if gpio.read(PINS.IN) == 0 then
gpio.write(PINS.IN, 1)
else
gpio.write(PINS.IN, 0)
end
end)
print("AP Started")
end
-- START ACCESS POINT:
function start_ap()
ap_config_start()
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
srv = net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(resp, data)
local body = ""
data = data:gsub("GET /", "")
data = data:gsub("HTTP/1.1", "")
data = split(data, "n")[0]
data = unescape(data)
print("data recieved: " .. data)
local json_ok, jdata = pcall(sjson.decode, data)
if json_ok then
body = sjson.encode({
deviceId = deviceId,
success = true
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
tmr.alarm(3, 100, 0, function()
wifi_connect({ssid=jdata.ssid,pwd=jdata.pwd}, function(success)
if success then
file.open("config","w")
file.write(data)
file.close()
print("OK")
else
ap_config_start()
print("NOT_OK")
end
end)
end)
else
body = sjson.encode({
success = false,
message = "INVALID_INPUT"
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
end
end)
end)
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=0
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
-- WEBSOCKET
function websocket_start(WIFI_CNF)
print("Starting Web Socket..")
local ws = websocket.createClient()
local wsUrl = "ws://"..SERVER.."/?deviceId="..deviceId
ws:on("connection", function(wss)
print("Connected")
local signin = sjson.encode({
signin = true,
deviceId = deviceId,
config = read_config().config
})
print(signin)
ws:send(signin)
tmr.stop(4)
gpio.write(PINS.IN, 1)
end)
ws:on("receive", function(wss, msg)
print("Received: ", msg)
handle_push(ws, msg)
gpio.write(PINS.IN, 0)
tmr.alarm(4, 100, 0, function()
gpio.write(PINS.IN, 1)
end)
end)
ws:on("close", function(wss, status)
if RS_F == false then
gpio.write(PINS.IN, 0)
print("Closed! Retrying..")
if wifi.sta.status() == 2 or wifi.sta.status() == 3 or wifi.sta.status() == 4 then
wifi_connect(WIFI_CNF)
else
ws:connect(wsUrl)
end
end
end)
ws:connect(wsUrl)
end
-- connecting to wifi
function wifi_connect(WIFI_CNF, CALL)
tmr.stop(2)
gpio.write(PINS.IN, 0)
print("Trying to connect to SSID " .. WIFI_CNF.ssid .. " using Password " .. WIFI_CNF.pwd)
wifi.setmode(wifi.STATION)
wifi.sta.config(WIFI_CNF)
tmr.alarm(1, 1000, 1, function ()
if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
print("Connecting..")
elseif wifi.sta.status() == 5 then
tmr.stop(1)
print("Connected! IP: ", wifi.sta.getip())
websocket_start(WIFI_CNF)
if CALL == nil then
tmr.stop(4)
else
CALL(true)
end
else
print("Connection Failed! Status: ",wifi.sta.status())
if CALL ~= nil then
tmr.stop(1)
wifi.sta.disconnect()
CALL(false)
end
end
end)
end
function read_config()
if file.open("config","r") then
local cok, cdata = pcall(sjson.decode, file.read())
file.close()
return cdata
else
return false
end
end
print("Initializing..")
local FI = read_config()
if FI then
print("Previous config exists!")
wifi_connect(FI)
else
print("No config detected!")
start_ap()
end
arduino lua iot
New contributor
My esp8266 module restarts when a timer function is called. esp module restarts when a push message from server is processed.but if a timer is not used in the process it works perfectly fine, else if tmr.alarm function used it restarts following a push message from server.can anyone please find me a better solution to this problem.
my code is posted below
SERVER = "xxxxxxxxxxxxxxxx"
deviceId = wifi.sta.getmac()
PINS = {
-- IN = 8, -- GPIO02
-- OS = 4, -- GPIO02
-- OP = 5, -- GPIO14
-- RS = 1 -- GPIO05
IN = 7, -- GPIO14
OS = 4, -- GPIO02
OP = 5, -- GPIO04
RS = 1 -- GPIO05
}
count=6
gpio.mode(PINS.IN, gpio.OUTPUT)
gpio.mode(PINS.OP, gpio.OUTPUT)
gpio.mode(PINS.OS, gpio.OUTPUT)
gpio.mode(PINS.RS, gpio.INT, gpio.PULLUP)
gpio.write(PINS.IN, gpio.HIGH)
gpio.write(PINS.OP, gpio.LOW)
if gpio.read(PINS.OP) == 1 then
gpio.write(PINS.OS, gpio.LOW)
else
gpio.write(PINS.OS, gpio.HIGH)
end
RS_F = false
gpio.trig(PINS.RS, "up", function(level)
if level == 1 then
if RS_F then
node.restart()
else
if wifi.getmode() ~= 2 then
RS_F = true
start_ap()
else
print("already on ap")
end
end
end
end)
--timer function
function timer_on(time)
local duration = time
sec = duration*1000*60
tmr.alarm(6, sec, 2, function ()
if count>=1 then
timer_on(1)
count=count-1
print("counter")
end
end)
if count==1 then
print("led on")
count=6
end
end
-- HANDLE PUSH MESSHAES FROM SERVER
function handle_push(ws, msg)
local json_ok, msg = pcall(sjson.decode, msg)
if json_ok then
gpio.write(PINS.OP, msg.state)
timer_on(1)
if msg.restart then
node.restart()
end
if msg.reset then
file.remove("config")
tmr.alarm(5, 100, 0, function ()
node.restart()
end)
end
end
ws:send(sjson.encode({state=gpio.read(PINS.OP), config=read_config()}))
end
function ap_config_start()
print("Starting AP..")
tmr.stop(1)
wifi.setmode(wifi.SOFTAP)
wifi.ap.config({
ssid = "ELAENOR_SMART",
pwd = "ELAENOR_SMART",
auth = wifi.WPA_PSK
})
wifi.ap.setip({
ip = "xxxxxxxxxxxx",
netmask = "xxxxxxxxxxxx",
gateway = "xxxxxxxxxxxxx"
})
tmr.alarm(2, 400, 1, function()
if gpio.read(PINS.IN) == 0 then
gpio.write(PINS.IN, 1)
else
gpio.write(PINS.IN, 0)
end
end)
print("AP Started")
end
-- START ACCESS POINT:
function start_ap()
ap_config_start()
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
srv = net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(resp, data)
local body = ""
data = data:gsub("GET /", "")
data = data:gsub("HTTP/1.1", "")
data = split(data, "n")[0]
data = unescape(data)
print("data recieved: " .. data)
local json_ok, jdata = pcall(sjson.decode, data)
if json_ok then
body = sjson.encode({
deviceId = deviceId,
success = true
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
tmr.alarm(3, 100, 0, function()
wifi_connect({ssid=jdata.ssid,pwd=jdata.pwd}, function(success)
if success then
file.open("config","w")
file.write(data)
file.close()
print("OK")
else
ap_config_start()
print("NOT_OK")
end
end)
end)
else
body = sjson.encode({
success = false,
message = "INVALID_INPUT"
})
resp:send(table.concat (
{
"HTTP/1.1 200 OK",
"Content-Type: application/json",
"Content-length: " .. #body,
"",
body
}, "rn"))
end
end)
end)
end
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=0
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
-- WEBSOCKET
function websocket_start(WIFI_CNF)
print("Starting Web Socket..")
local ws = websocket.createClient()
local wsUrl = "ws://"..SERVER.."/?deviceId="..deviceId
ws:on("connection", function(wss)
print("Connected")
local signin = sjson.encode({
signin = true,
deviceId = deviceId,
config = read_config().config
})
print(signin)
ws:send(signin)
tmr.stop(4)
gpio.write(PINS.IN, 1)
end)
ws:on("receive", function(wss, msg)
print("Received: ", msg)
handle_push(ws, msg)
gpio.write(PINS.IN, 0)
tmr.alarm(4, 100, 0, function()
gpio.write(PINS.IN, 1)
end)
end)
ws:on("close", function(wss, status)
if RS_F == false then
gpio.write(PINS.IN, 0)
print("Closed! Retrying..")
if wifi.sta.status() == 2 or wifi.sta.status() == 3 or wifi.sta.status() == 4 then
wifi_connect(WIFI_CNF)
else
ws:connect(wsUrl)
end
end
end)
ws:connect(wsUrl)
end
-- connecting to wifi
function wifi_connect(WIFI_CNF, CALL)
tmr.stop(2)
gpio.write(PINS.IN, 0)
print("Trying to connect to SSID " .. WIFI_CNF.ssid .. " using Password " .. WIFI_CNF.pwd)
wifi.setmode(wifi.STATION)
wifi.sta.config(WIFI_CNF)
tmr.alarm(1, 1000, 1, function ()
if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
print("Connecting..")
elseif wifi.sta.status() == 5 then
tmr.stop(1)
print("Connected! IP: ", wifi.sta.getip())
websocket_start(WIFI_CNF)
if CALL == nil then
tmr.stop(4)
else
CALL(true)
end
else
print("Connection Failed! Status: ",wifi.sta.status())
if CALL ~= nil then
tmr.stop(1)
wifi.sta.disconnect()
CALL(false)
end
end
end)
end
function read_config()
if file.open("config","r") then
local cok, cdata = pcall(sjson.decode, file.read())
file.close()
return cdata
else
return false
end
end
print("Initializing..")
local FI = read_config()
if FI then
print("Previous config exists!")
wifi_connect(FI)
else
print("No config detected!")
start_ap()
end
arduino lua iot
arduino lua iot
New contributor
New contributor
New contributor
asked 16 hours ago
Pranav VenuPranav Venu
1
1
New contributor
New contributor
put on hold as off-topic by Mokubai♦ 16 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Mokubai
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Mokubai♦ 16 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Mokubai
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes