The use of the ESP8266 in the world of IoT

User avatar
By bra1n
#86814 A quick Google search for 'LUA url decode' turned up a number of promising hits including https://www.rosettacode.org/wiki/URL_decoding#Lua which has
Code: Select allfunction decodeChar(hex)
   return string.char(tonumber(hex,16))
end
 
function decodeString(str)
   local output, t = string.gsub(str,"%%(%x%x)",decodeChar)
   return output
end
 
-- will print "http://foo bar/"
print(decodeString("http%3A%2F%2Ffoo%20bar%2F"))
User avatar
By Hermann9
#86838 Thanks for the idea.
The code works now perfectly.

Here is the finished code.
Code: Select allwifi.setmode(wifi.SOFTAP)
wifi.ap.config({ssid="Water", pwd="12345678", auth=wifi.OPEN})
wifi.ap.setip({ip="10.1.2.1", netmask="255.255.255.0", gateway="10.1.2.1"})

srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
  conn:on("receive", function(client, request)
    local buf = ""
    local _, _, vars = string.find(request, "[A-Z]+ .+?(.+) HTTP")
    if (method == nil) then
      _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
    end
    local _GET = {}
    if (vars ~= nil) then
      for k, v in string.gmatch(vars, "(%w+)=([%w+-@%%]+)&*") do
        _GET[k] = v
      end
    end
   
-----------
 function unescape (s)
      s = string.gsub(s, "+", " ")
      s = string.gsub(s, "%%(%x%x)", function (h)
            return string.char(tonumber(h, 16))
          end)
      return s
    end
-------------

if (_GET.ssid) then
ssid = (unescape(_GET.ssid))
pass = (unescape(_GET.pass))
name = (unescape(_GET.name))


file.open("config.lua","w+")
file.write ("\n")
file.write ("wifi.setmode(wifi.STATION)\n")

file.write ("wifi.sta.config({ssid='")
file.write (ssid)
file.write ("',pwd='")
file.write (pass)
file.write ("'})\n")
file.write ("wifi.sta.connect()\n")

file.write ("name = ")
file.write (name)

file.open("config.lua")
print(file.read())

buf = buf.."OK"

else

buf = buf.."<!DOCTYPE html><html><head><meta charset='utf-8'><title>Config</title><meta name='viewport' content='width=300'></head><center>"
buf = buf.."<form action='http://10.1.2.1/' method='GET'>"
buf = buf.."SSID<br><input name='ssid'><br><br>"
buf = buf.."Password <br><input name='pass'><br><br>"
buf = buf.."Name<br><input name='name'><br><br>"
buf = buf.."<input type='submit' value='Send'>"
buf = buf.."</form></center></body></html>"

end

client:send(buf)
client:close()
end)
end)