Skip to content

Instantly share code, notes, and snippets.

@pnck
Created January 12, 2024 09:15
Show Gist options
  • Save pnck/c4b4acc222840def009a13f2efcdaead to your computer and use it in GitHub Desktop.
Save pnck/c4b4acc222840def009a13f2efcdaead to your computer and use it in GitHub Desktop.
GP2015 NodeMCU Codes
gserverip = "192.168.1.253"
gnetwork = "GP2015 Remastered" -- originally "hukouliufang"
gnetkey = "12345678" -- originally "hbLVcyMyrs"
gdebugmode = 1 -- originally 0
function p(m)
print(m)
end
function lmsg(msg)
if (gdebugmode ~= 0) then
p(tmr.now() .. ": " .. msg .. "\n")
end
end
function createNewSocket()
tempsk = net.createConnection(net.TCP, 0)
tempsk:on(
"receive",
function(sck, c)
lmsg(c)
ghb = ghblimit
if (c:len() < 2) then
return
end
cmd = c:sub(1, 2)
lmsg("command is " .. cmd)
if (cmd == "00" or cmd == "01" or cmd == "02" or cmd == "03" or cmd == "04") then
if ((cmd == "02" or cmd == "03") and c:len() < 3) then
lmsg("command incorrect")
return
end
gcmd = cmd
elseif (cmd == "05") then
node.restart()
else
lmsg("command incorrect")
return
end
if (cmd == "01") then
goto01()
elseif (cmd == "02") then
gtempinfo = tonumber(c:sub(3, 4))
p("\n")
p("PIC(0,0," .. gtempinfo .. ");\n")
elseif (cmd == "03") then
gfsstate = 0
gtempinfo = c:sub(3, 3)
gtimein03 = 0
p("\n")
p("CLS(0);")
gstrcnt = 0
showWords(c:sub(4))
if (gtempinfo == "0") then
tmr.alarm(3, gfsitvl, 0, dofs)
end
elseif (cmd == "04") then
p(c:sub(3))
p("\n")
elseif (cmd == "00") then
p("\n")
p("CLS(0);\n")
end
end
)
tempsk:on(
"connection",
function(sck)
lmsg("connected server!")
sck:send("helloSuccess")
gfailcount = 0
end
)
tempsk:on(
"disconnection",
function(sck)
lmsg("disconnected!")
goto01()
tmr.alarm(2, greconnectitvl, 0, doReconnect)
end
)
return tempsk
end
gcheckitvl = 1000
gserverport = 8080
ghblimit = 90
grollitvl = 5
greconnectitvl = 5000
gkeep03limit = 20
gfsitvl = 300
gsk = createNewSocket()
gwaitcount = 0
gwfst = 0
gcmd = "01"
gtempinfo = 1
ghb = ghblimit
grollcount = grollitvl
gtimein03 = 0
gfsstate = 0
gstr1 = ""
gstr2 = ""
gstr3 = ""
gstrcnt = 0
function showWords(words)
if (gstrcnt == 0) then
gstr1 = ""
gstr2 = ""
gstr3 = ""
idx = 1
tempst = ""
stIdx = 0
while (idx <= words:len()) do
if (words:sub(idx, idx) == "#") then
stIdx = stIdx + 1
if (stIdx == 1) then
gstr1 = tempst
elseif (stIdx == 2) then
gstr2 = tempst
elseif (stIdx == 3) then
gstr3 = tempst
end
tempst = ""
else
tempst = tempst .. words:sub(idx, idx)
end
if (stIdx == 3) then
break
end
idx = idx + 1
end
gstrcnt = stIdx
end
if (gstrcnt == 0) then
lmsg("st is empty")
return
elseif (gstrcnt == 1) then
p("DS32(20,70,'" .. gstr1 .. "',5);\n")
elseif (gstrcnt == 2) then
p("DS32(20,28,'" .. gstr1 .. "',5);")
p("DS32(20,108,'" .. gstr2 .. "',5);\n")
elseif (gstrcnt >= 3) then
p("DS32(20,11,'" .. gstr1 .. "',5);")
p("DS32(20,69,'" .. gstr2 .. "',5);")
p("DS32(20,128,'" .. gstr3 .. "',5);\n")
end
end
function dofs()
if (gcmd ~= "03" or gtempinfo ~= "0") then
return
end
if (gfsstate == 0) then
gfsstate = 1
p("\n")
p("CLS(0);\n")
else
gfsstate = 0
p("\n")
showWords("")
end
tmr.alarm(3, gfsitvl, 0, dofs)
end
function doReconnect()
lmsg("reconnect")
status = wifi.sta.status()
if (status == 5) then
gsk:close()
gsk = createNewSocket()
lmsg("connecting server now")
gsk:connect(gserverport, gserverip)
else
lmsg("wifi fail,wait more time")
tmr.alarm(2, greconnectitvl, 0, doReconnect)
end
end
function goto01()
if (gcmd ~= "01") then
gcmd = "01"
gtempinfo = 1
grollcount = grollitvl
end
end
function tmrf()
ghb = ghb - 1
if (ghb <= 0) then
ghb = ghblimit
lmsg("restart program due to no hb")
node.restart()
end
if (gwfst == 0) then
lmsg("connect wifi now")
wifi.sta.connect()
gwfst = 1
elseif (gwfst == 1) then
status = wifi.sta.status()
if (status == 5) then
lmsg(wifi.sta.status() .. " ")
lmsg(wifi.sta.getip())
lmsg("connectted wifi")
gwfst = 2
else
gwaitcount = gwaitcount + 1
if (gwaitcount > 20) then
gwfst = 0
gwaitcount = 0
lmsg("connect time out,will reconnect")
end
end
elseif (gwifitatus == 2) then
status = wifi.sta.status()
if (status ~= 5) then
gwaitcount = 0
gwfst = 0
end
end
if (gcmd == "01") then
if (gtempinfo ~= 1 and gtempinfo ~= 2 and gtempinfo ~= 3) then
gtempinfo = 1
end
if (grollcount >= grollitvl) then
grollcount = 0
p("\n")
p("PIC(0,0," .. gtempinfo .. ");\n")
gtempinfo = gtempinfo + 1
if (gtempinfo == 4) then
gtempinfo = 1
end
else
grollcount = grollcount + 1
end
elseif (gcmd == "03") then
gtimein03 = gtimein03 + 1
if (gtimein03 >= gkeep03limit) then
gtimein03 = 0
goto01()
end
end
end
wifi.setmode(wifi.STATION)
wifi.sta.config(gnetwork, gnetkey)
wifi.sta.autoconnect(0)
tmr.alarm(1, gcheckitvl, 1, tmrf)
tmr.alarm(2, greconnectitvl, 0, doReconnect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment