Getting data from the webserver
This webpage...
...is created using this code:
-- Program Little Website
-- Ungaretti 01/2018
-- Operates a little webpage while blinking a led
-------------------------
-- Blink
-------------------------
-- Pin definition
pin = 2
status = gpio.LOW
duration = 500 -- 0.5 second duration for timer
-- Initialising pin
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, status)
-- Create an interval
tmr.alarm(1, duration, 1, function () -- Alarm 1, not the same as WiFi's
if status == gpio.LOW then
status = gpio.HIGH
else
status = gpio.LOW
end
gpio.write(pin, status)
end)
---------------------------------
-- Setup and WiFi Connection
---------------------------------
wifi.setmode(wifi.STATION) -- Connects as station, not AP
station_cfg={} -- Creates config table - MUST!
station_cfg.ssid="triwifi" -- WiFi router name
station_cfg.pwd="18doforte546" -- WiFi router password
wifi.sta.config(station_cfg)
tmr.alarm(0, 1000, 1, function() -- Checks if is connected every second
if wifi.sta.getip() == nil then -- If you can't get IP, is not connected
print("Connecting to AP...\n")
else -- Connected!
ip, nm, gw=wifi.sta.getip()
print("IP Info: \nIP Address: ",ip)
print("Netmask: ",nm)
print("Gateway Addr: ",gw,'\n')
tmr.stop(0)
end
end)
----------------
-- Web Server --
----------------
print("Starting Web Server...")
-- Create a server object with 30 second timeout
srv = net.createServer(net.TCP, 30)
-- server listen on 80,
-- if data received, print data to console,
-- then serve a little website
srv:listen(80,function(conn)
conn:on("receive", function(conn, payload)
print(payload) -- Print data from browser to serial terminal
---------------------------------------------------------------
--
-- PARSING CODE GOES HERE
--
---------------------------------------------------------------
-- CREATE WEBSITE --
-- HTML Header Stuff
conn:send('HTTP/1.1 200 OK\n\n')
conn:send('<!DOCTYPE HTML>\n')
conn:send('<html>\n')
conn:send('<head><meta content="text/html; charset=utf-8">\n')
conn:send('<title>Simple Server</title></head>\n')
conn:send("<h4> This is my simple webpage </h4>")
-- Create HTML form
conn:send('<form action="" method="POST">\n')
-- Textfields
conn:send('<input type="text" name="myfield1"><br><br>')
conn:send('<input type="text" name="myfield2"><br><br>')
conn:send('<input type="text" name="myfield3"><br><br>')
--- Radio buttons
conn:send('<input type="radio" name="gender" value="male" checked> Male <br>')
conn:send('<input type="radio" name="gender" value="female"> Female <br>')
conn:send('<input type="radio" name="gender" value="other"> Other <br>')
-- Buttons
conn:send('<input type="submit" name="mybutton" value="Right_button">\n')
conn:send('<input type="submit" name="mybutton" value="Middle_button">\n')
conn:send('<input type="submit" name="mybutton" value="Left_button">\n')
-- Closing
conn:send('</body></html>\n')
conn:on("sent", function(conn) conn:close() end)
end)
end)
This program not only sets a webpage, but also blinks a led.
In the webpage, If I set the fields and radiobuttons as the next picture shows and click on the middle button...
... I get this output at the console:
POST / HTTP/1.1
Host: 192.168.0.21
Connection: keep-alive
Content-Length: 84
Cache-Control: max-age=0
Origin: http://192.168.0.21
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.21/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
myfield1=Mary&myfield2=Somewhere&myfield3=57665&gender=female&mybutton=Middle_button
GET /favicon.ico HTTP/1.1
Host: 192.168.0.21
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.0.21/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Clearly, our information is here, at the 15th line of the "payload" variable:
One possible code to parse the variables sent by the website is this:
print("=========================")
start= string.find(payload,"myfield1")
print("start= ",start)
finish= string.len(payload)
print("finish= ", finish)
if start ~= nil then
mainline= string.sub(payload,start,finish)
print("mainline= ", mainline)
variables={}
for i=1, 4 do -- we have 5 variables
variables[i]= string.sub(mainline, string.find(mainline,"=")+1, string.find(mainline,"&")-1)
print(variables[i])
mainline= string.sub(mainline, string.find(mainline,"&")+1,string.len(mainline))
print(mainline)
end
variables[5]= string.sub(mainline, string.find(mainline,"=")+1) -- the fifth variable
print(variables[5])
end
Another possible way is described here (very good overview, by the way)