Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- 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()}))
- print("AP Started")
- 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)
- 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)
- wifi_connect(FI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement