Post your best Lua script examples here

User avatar
By Hermann9
#86757 Hello everybody.
I wrote this code, it writes a config file "config.lua".

The problem is that it doesn't transmit special characters.

For example:
@ is %40
# is %23

What can I do to ensure that it is transmitted correctly?
Can someone help me.
Thank you.


My Fim is.
NodeMCU custom build by frightanic.com
branch: 1.5.4.1-final
commit: b9436bdfa452c098d5cb42a352ca124c80b91b25
SSL: false
modules: adc,encoder,file,gpio,http,net,node,ow,tmr,uart,wifi
build created on 2020-04-19 11:01
powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)
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
   

if (_GET.ssid) then
ssid =  _GET.ssid
pass =  _GET.pass
name =  _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)