The problem I could not solve .
Here is my program. It is simpl relays control.
Code: Select allDEBUGPRINT=true -- true = with debugg prints; false = without debugg prints
_, BoRe= node.bootreason()
ssid="yourSSID" -- your router SSID
pass="YourPASS" -- your router password
code="1234"
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pass,1)
wifi.sta.connect()
rel1gpio=2 --GPIO4 for relay
rel2gpio=1 --GPIO5 for relay
rel1stat=0
rel2stat=0
on='<span style="color: red;">ON</span>'
off='<span style="color: blue;">OFF</span>'
rel1info = off
rel2info = off
code="1234"
gpio.mode(rel1gpio, gpio.OUTPUT)
gpio.mode(rel2gpio, gpio.OUTPUT)
gpio.write(rel1gpio,rel1stat)
gpio.write(rel2gpio,rel2stat)
function debugprint(...)
if DEBUGPRINT then
print(...)
end
end
local function cgidecode(str)
return (str:gsub('+', ' '):gsub("%%(%x%x)", function(xx) return string.char(tonumber(xx, 16)) end))
end
function parsecgi(str, keys, ignore_invalid)
local keyfound = {}
for pair in str:gmatch"[^&]+" do
local key, val = pair:match"([^=]*)=(.*)"
if not key then debugprint("1.IQS") end
local default = keys[key]
if default == nil then
if not ignore_invalid then debugprint("2.IQS") end
else
if type(default) == "table" then default[#default+1] = cgidecode(val)
elseif keyfound[key] then debugprint("3.IQS")
else
keyfound[key] = true
keys[key] = cgidecode(val)
end
end
end
return keys
end
function SendData(client)
debugprint("Serve as DATA",client:getpeer())
gpio.write(rel1gpio,rel1stat)
gpio.write(rel2gpio,rel2stat)
buff2='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'..
'<html><head>'..
'<meta content="text/html; charset=utf-8">'..
'<title>ESP8266</title></head>'..
'<body style="color: brown;background:#ffe4c4">'..
'<h1>ESP8266 - Relays Control</h1><br>'..
'<form action="/" method="post">'..
'<h2>Relay A '..rel1info..'<br><input type="submit" name="R1ZAP" value="ON">'..
'<input type="submit" name="R1VYP" value="OFF"><br><br>'..
'Relay B '..rel2info..'<br><input type="submit" name="R2ZAP" value="ON">'..
'<input type="submit" name="R2VYP" value="OFF"><br><br>'..
'<input type="submit" name="REFRESH" value="REFRESH"><br></h2><br>'..
'Reset ID : '..BoRe..'<br>Heap : '..node.heap()..'</form></body></html>'
lenght= #buff2
buff1 = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'..
'Content-Length: '.. lenght ..'\r\n'..
'Cache-Control: no-cache\r\n'..
'Pragma: no-cache\r\n'..
'Connection: keep-alive\r\n\r\n'..buff2
buff2=nil
collectgarbage()
client:send(buff1)
end
function SendCode(client)
debugprint("Serve as CODE",client:getpeer())
buff2='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'..
'<html><head>'..
'<meta content="text/html; charset=utf-8">'..
'<title>KEVA</title></head>'..
'<body style="color: brown;background:#ffe4c4">'..
'<h1>ESP8266 - Relays Login</h1><br>'..
'<form action="/" method="post">'..
'<h2>Code :<br>'..
'<input type="password" size="8" name="CODE" value=""><br><br>'..
'<input type="submit" name="SUBMIT" value="SUBMIT"><br></h2><br>'..
'</form></body></html>'
lenght= #buff2
buff1 = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'..
'Content-Length: '.. lenght ..'\r\n'..
'Cache-Control: no-cache\r\n'..
'Pragma: no-cache\r\n'..
'Connection: keep-alive\r\n\r\n'..buff2
buff2=nil
collectgarbage()
client:send(buff1)
end
function Send404(client)
debugprint("Serve as 404",client:getpeer())
buff2='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'..
'<html><head>'..
'<meta content="text/html; charset=utf-8">'..
'<title>404</title></head>'..
'<body >'..
'<h1>404 - Page not found</h1><br>'..
'</body></html>'
lenght= #buff2
buff1 = 'HTTP/1.1 404 NO\r\n'..
'Content-Type: text/html\r\n'..
'Content-Length: '.. lenght ..'\r\n'..
'Cache-Control: no-cache\r\n'..
'Pragma: no-cache\r\n'..
'Connection: keep-alive\r\n\r\n'..buff2
buff2=nil
collectgarbage()
client:send(buff1)
end
debugprint ("Reset ID :",BoRe)
debugprint("Controlling running!")
srv=net.createServer(net.TCP,60) srv:listen(80,function(conn)
conn:on("receive",function(client,payload)
debugprint(payload)
if string.find(payload,"POST / HTTP/1.1\r\n.+\r\n\r\n")~=nil
then
_,stend=string.find(payload,"POST / HTTP/1.1\r\n.+\r\n\r\n")
postda=string.sub(payload,stend+1)
cgivals = parsecgi(postda, {count = 7, start = 1, R1ZAP="", R2ZAP="", R1VYP="", R2VYP="", CODE="", REFRESH="",SUBMIT=""})
if cgivals.R1ZAP=="ON"
then
rel1stat=1
rel1info=on
end
if cgivals.R1VYP=="OFF"
then
rel1stat=0
rel1info=off
end
if cgivals.R2ZAP=="ON"
then
rel2stat=1
rel2info=on
end
if cgivals.R2VYP=="OFF"
then
rel2stat=0
rel2info=off
end
if cgivals.REFRESH=="REFRESH"
then
end
if (cgivals.SUBMIT=="SUBMIT" and cgivals.CODE==code)
then
passIP,passPort=client:getpeer()
debugprint("Authentic :",passIP,passPort)
end
actIP,actPort=client:getpeer()
if (passIP==actIP and passPort==actPort)
then
SendData(client)
end
actIP,actPort=client:getpeer()
if (passIP~=actIP or passPort~=actPort)
then
SendCode(client)
end
elseif string.find(payload,"GET / HTTP/1.1\r\n.+\r\n\r\n")~=nil
then
actIP,actPort=client:getpeer()
if (passIP==actIP and passPort==actPort)
then
SendData(client)
else
SendCode(client)
end
else
Send404(client)
end
end)
end)
Here is request and log for response from LAN. It is O.K.
Code: Select allPOST / HTTP/1.1
Host: 192.168.1.11
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: cs,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.1.11/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
CODE=1234&SUBMIT=SUBMIT
Authentic : 192.168.1.4 65050
Serve as DATA 192.168.1.4 65050
And here is fragmented the same request from WAN. Http fragmentation is legal. But fragmented requests the Lua not processed correctly. They are two requests for Lua IP stack!
Code: Select allPOST / HTTP/1.1
Host: 88.85.42.219:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: cs,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://88.85.42.219:8080/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
Serve as CODE 83.122.131.210 2080
CODE=1234&SUBMIT=SUBMIT
Serve as 404 83.122.131.210 2080
Here is not fragmented packet from LAN - WireShark
Code: Select all0000 5c cf 7f 01 f4 a3 b8 88 e3 35 a6 c1 08 00 45 00 \........5....E.
0010 01 cb 01 b0 40 00 80 06 00 00 c0 a8 01 04 c0 a8 ....@...........
0020 01 0b c3 53 00 50 48 8a 82 0c 00 00 4b 37 50 18 ...S.PH.....K7P.
0030 f8 cf 85 1d 00 00 50 4f 53 54 20 2f 20 48 54 54 ......POST / HTT
0040 50 2f 31 2e 31 0d 0a 48 6f 73 74 3a 20 31 39 32 P/1.1..Host: 192
0050 2e 31 36 38 2e 31 2e 31 31 0d 0a 55 73 65 72 2d .168.1.11..User-
0060 41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 35 Agent: Mozilla/5
0070 2e 30 20 28 57 69 6e 64 6f 77 73 20 4e 54 20 36 .0 (Windows NT 6
0080 2e 31 3b 20 57 4f 57 36 34 3b 20 72 76 3a 34 37 .1; WOW64; rv:47
0090 2e 30 29 20 47 65 63 6b 6f 2f 32 30 31 30 30 31 .0) Gecko/201001
00a0 30 31 20 46 69 72 65 66 6f 78 2f 34 37 2e 30 0d 01 Firefox/47.0.
00b0 0a 41 63 63 65 70 74 3a 20 74 65 78 74 2f 68 74 .Accept: text/ht
00c0 6d 6c 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 ml,application/x
00d0 68 74 6d 6c 2b 78 6d 6c 2c 61 70 70 6c 69 63 61 html+xml,applica
00e0 74 69 6f 6e 2f 78 6d 6c 3b 71 3d 30 2e 39 2c 2a tion/xml;q=0.9,*
00f0 2f 2a 3b 71 3d 30 2e 38 0d 0a 41 63 63 65 70 74 /*;q=0.8..Accept
0100 2d 4c 61 6e 67 75 61 67 65 3a 20 63 73 2c 65 6e -Language: cs,en
0110 2d 55 53 3b 71 3d 30 2e 37 2c 65 6e 3b 71 3d 30 -US;q=0.7,en;q=0
0120 2e 33 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 .3..Accept-Encod
0130 69 6e 67 3a 20 67 7a 69 70 2c 20 64 65 66 6c 61 ing: gzip, defla
0140 74 65 0d 0a 52 65 66 65 72 65 72 3a 20 68 74 74 te..Referer: htt
0150 70 3a 2f 2f 31 39 32 2e 31 36 38 2e 31 2e 31 31 p://192.168.1.11
0160 2f 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b /..Connection: k
0170 65 65 70 2d 61 6c 69 76 65 0d 0a 43 6f 6e 74 65 eep-alive..Conte
0180 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 61 nt-Type: applica
0190 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 6f 72 6d 2d tion/x-www-form-
01a0 75 72 6c 65 6e 63 6f 64 65 64 0d 0a 43 6f 6e 74 urlencoded..Cont
01b0 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 32 33 0d 0a ent-Length: 23..
01c0 0d 0a 43 4f 44 45 3d 31 32 33 34 26 53 55 42 4d ..CODE=1234&SUBM
01d0 49 54 3d 53 55 42 4d 49 54 IT=SUBMIT
Here is fragmented packet from WAN - WireShark
Code: Select all0040 63 63 65 70 74 3a 20 74 65 78 74 2f 68 74 6d 6c P/1.1..Host: 88.
0050 6c 6c 61 2f 35 2e 30 20 28 57 69 6e 64 6f 77 73 85.42.219:8080..
0060 55 73 65 72 2d 41 67 65 6e 74 3a 20 4d 6f 7a 69 User-Agent: Mozi
0070 6c 6c 61 2f 35 2e 30 20 28 57 69 6e 64 6f 77 73 lla/5.0 (Windows
0080 20 4e 54 20 35 2e 31 3b 20 72 76 3a 34 35 2e 30 NT 5.1; rv:45.0
0090 29 20 47 65 63 6b 6f 2f 32 30 31 30 30 31 30 31 ) Gecko/20100101
00a0 20 46 69 72 65 66 6f 78 2f 34 35 2e 30 0d 0a 41 Firefox/45.0..A
00b0 63 63 65 70 74 3a 20 74 65 78 74 2f 68 74 6d 6c ccept: text/html
00c0 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 68 74 ,application/xht
00d0 6d 6c 2b 78 6d 6c 2c 61 70 70 6c 69 63 61 74 69 ml+xml,applicati
00e0 6f 6e 2f 78 6d 6c 3b 71 3d 30 2e 39 2c 2a 2f 2a on/xml;q=0.9,*/*
00f0 3b 71 3d 30 2e 38 0d 0a 41 63 63 65 70 74 2d 4c ;q=0.8..Accept-L
0100 61 6e 67 75 61 67 65 3a 20 63 73 2c 65 6e 2d 55 anguage: cs,en-U
0110 53 3b 71 3d 30 2e 37 2c 65 6e 3b 71 3d 30 2e 33 S;q=0.7,en;q=0.3
0120 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e ..Accept-Encodin
0130 67 3a 20 67 7a 69 70 2c 20 64 65 66 6c 61 74 65 g: gzip, deflate
0140 0d 0a 52 65 66 65 72 65 72 3a 20 68 74 74 70 3a ..Referer: http:
0150 30 38 30 2f 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e //88.85.42.219:8
0160 30 38 30 2f 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 080/..Connection
0170 3a 20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a 43 6f : keep-alive..Co
0180 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c ntent-Type: appl
0190 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 6f ication/x-www-fo
01a0 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 0d 0a 43 rm-urlencoded..C
01b0 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 32 ontent-Length: 2
01c0 33 0d 0a 0d 0a 3....
0000 00 09 6b a5 53 1c 00 4f 4e 60 58 32 08 00 45 00 ..k.S..ON`X2..E.
0010 00 3f 0c fd 40 00 80 06 96 db ac 12 1d 8f 4e 50 .?..@.........NP
0020 3e ef 08 56 1f 90 e4 ee 5b 87 00 00 21 72 50 18 >..V....[...!rP.
0030 fb bd 95 53 00 00 43 4f 44 45 3d 31 32 33 34 26 ...S..CODE=1234&
0040 53 55 42 4d 49 54 3d 53 55 42 4d 49 54 SUBMIT=SUBMIT
Can any help me please?