As the title says... Chat on...

User avatar
By picstart1
#3861
httpserver = function ()

srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on("receive", function(conn, payload) print(payload) end)

conn:send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\nServer: test123\r\n\n")
conn:send("</head><body><h1>DK v1 hello world Served from ESP8266</h1>")
--conn:send("hello world")
conn:send("<FORM action=\"\" method=\"POST\">")
conn:send("<button type=\"button\" value=\"Led On\">led on</button><br>")
conn:send("<INPUT type=\"submit\"")
conn:send("</html></body>")
conn:on("sent",function(conn) conn:close() end)
end --conn
)
end
---------------
print("Now scanning for valid IP")

tmr.alarm(1000, 1, function()
if wifi.sta.getip()=="0.0.0.0" then
print("Waiting for connection to AP")
else
print('Now connect to AP, IP: ',wifi.sta.getip())
httpserver()
tmr.stop()
end
end)


result is
POST / HTTP/1.1
Host: xx.xx.0.105
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://xx.xx.0.105/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0

xx.xx.0.105 is a real IP with xx replacing the real values so as to be secure
I was expecting the value Led On to be somewhere on the returned form
What do I need to learn about POST
The goal is to have the web page transfer a requested led state and have the lua code set GPIO0 to that state
User avatar
By alonewolfx2
#3863 your result just printing in uart. and we catch just first line. (post or get) and you can try processing get request.
you can find some code in this link
User avatar
By ThomasW
#3895
picstart1 wrote:
httpserver = function ()

srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on("receive", function(conn, payload) print(payload) end)

conn:send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\nServer: test123\r\n\n")
conn:send("</head><body><h1>DK v1 hello world Served from ESP8266</h1>")
--conn:send("hello world")
conn:send("<FORM action=\"\" method=\"POST\">")
conn:send("<button type=\"button\" value=\"Led On\">led on</button><br>")
conn:send("<INPUT type=\"submit\"")
conn:send("</html></body>")
conn:on("sent",function(conn) conn:close() end)
end --conn
)
end
---------------
print("Now scanning for valid IP")

tmr.alarm(1000, 1, function()
if wifi.sta.getip()=="0.0.0.0" then
print("Waiting for connection to AP")
else
print('Now connect to AP, IP: ',wifi.sta.getip())
httpserver()
tmr.stop()
end
end)


result is
POST / HTTP/1.1
Host: xx.xx.0.105
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://xx.xx.0.105/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0

xx.xx.0.105 is a real IP with xx replacing the real values so as to be secure
I was expecting the value Led On to be somewhere on the returned form
What do I need to learn about POST
The goal is to have the web page transfer a requested led state and have the lua code set GPIO0 to that state


Your current form does not send any data (see 'Content-Length: 0' in the answer) as you didn't assign 'name' attributes to the input fields.
I you want just two buttons with "Led on" and "Led off" the form should look something like that:

Code: Select all<html>
<form action="" method="POST">
<button name="led" type="submit" value="on">Switch on!</button><br>
<button name="led" type="submit" value="off">Switch off!</button><br>
</form>
</html>
</body>


Wich would result in:

Code: Select allPOST / HTTP/1.1
Host: 10.10.1.119:6666
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-at,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 15

led=on


With either "led=on" or "led=off".
Changing the request method to "GET" would result in
Code: Select allGET /?led=on HTTP/1.1

as the first line in the answer, probably easier to handle

Thomas
User avatar
By picstart1
#3919 Thank you Thomas.....OOps I left out the name. No excuse but with no error message I suspected it was going to be more complicated than my messing up the html.
Using GET and parsing for /?led=off in the returned form is an easier approach