-->
Page 1 of 2

Transmit special characters

PostPosted: Sat Apr 25, 2020 7:18 am
by Hermann9
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)


Re: Tranmit special characters

PostPosted: Sun Apr 26, 2020 12:06 pm
by bra1n
You are using the HTTP protocol in which some characters have special meanings so have to be encoded if not used for them https://secure.n-able.com/webhelp/NC_9-1-0_SO_en/Content/SA_docs/API_Level_Integration/API_Integration_URLEncoding.html You either need to urldecode them at the receiving end or use a different protocol

Re: Tranmit special characters

PostPosted: Tue Apr 28, 2020 10:04 am
by Hermann9
Thanks for the answer.
The esp8266 with LUA does not work with HTTPS.

Can I write a translator in my Lua script?
He has to do the following:

if %40 then @
and
if %23 then #

But how do I write this in my Lua script, I've already tried everything.

Re: Transmit special characters

PostPosted: Wed Apr 29, 2020 11:14 am
by bra1n
You would have exactly the same issue with https anyway. I'm afraid I have no experience with LUA