
Jak w tytule załączam fotkę ekranu serwera oraz, kod który powinien realizować funkcję przesyłania znaków ASCII z klawiatury do UART ale jest błąd którego nie umiem określić.
Nie działa mianowicie funkcja przekazywania znaków wpisywanych z klawiatury do zmiennej 'mycode'. Pozostałe funkcje programu włącznie z samą komunikacją UART (przycisk 'SENDtoUART') czyli nadawaniem i odbiorem znaków działają bez zarzutu.
Linie kodu gdzie wg mnie przypuszczalnie tkwi błąd:
conn:send('</form>\n')
conn:send('<form action="" method="GET">\n')
conn:send('<scripttype="text/javascript"\n')
conn:send('function inptext(){\n')
conn:send('var mycode = document.getElementById("field").value \n')
conn:send('document.getElementById("mycode").innerHTML = mycode}\n')
conn:send('</script>\n')
conn:send('<input type="text" id="field" />\n')
conn:send('<input type="submit" value="<-INPUT" onclick="inptext()" />\n')
conn:send('<div id="mycode"></div>\n')
Być może powinna być zastosowana inna metoda?
Jak widać na fotce program generuje przycisk onclick i okienko i można do niego wpisywać znaki. Jednak po kliknięciu przycisku INPUT wpisane znaki znikają z okienka nic sie nie dzieje, czyli zmienna 'mycode' pozostaje pusta, ma wartość 'nil'.
Do obsługi programowania płytki używałem programu Flasher oraz ESPlorera wg opisu:
https://nodemcu.readthedocs.io/en/master/en/flash/#sdk-init-data
Jest pytanie i prośba do programistów, którzy ogarniają ESP8266 i programowanie w LUA by przyjrzeć sie temu.
całość kodu:
--uart
uart.setup(0,9600,8,0,1,1)
dwa = " "
datax = " "
--mycode= "oz"
gpio.mode(2, gpio.OUTPUT)
gpio.write(2, gpio.LOW)
---- Set the WEB AP server, constant IP address of the router needed
SSID = "TTTTTTTT" --input your router login
PW = "SSSSSSSSS"--input your router password
IPADR = "192.168.1.175"--Requested router static IP address for the ESP
IPROUTER = "192.168.1.1"--IP address your router (LAN)
PORT = "78"--set the inner port
PORTZEWN = "31"--set the external port
wifi.setmode(wifi.STATION)
wifi.sta.setip({ip=IPADR,netmask="255.255.255.0",gateway=IPROUTER,port=PORT})
wifi.sta.config(SSID,PW)
srv=net.createServer(net.TCP) srv:listen(PORTZEWN,function(conn)
conn:on("receive",function(conn,payload)
--print(payload)
--print(wifi.sta.getmac())
--print(wifi.sta.getip())
function ctrlpower()
klik=string.sub(payload,kdesi[2]+1,#payload)
if klik=="ON2" then gpio.write(2, gpio.HIGH) dwa = "* " return end
if klik=="OFF2" then gpio.write(2, gpio.LOW) dwa = " " return end
if klik=="SENDtoUART" and mycode~=nil then uart.write(0,mycode.."$") return end--send to UART string
end
kdesi={string.find(payload,"pwmi=")}
if kdesi[2]~=nil then ctrlpower()end
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>ESP8266</title></head>\n')
conn:send('<body>\n')
conn:send('<h4>NodeMcu Esp8266</h4>\n')
--------
conn:send('<form action="" method="POST">\n')
conn:send(dwa)
conn:send('<input type="submit" name="pwmi" value="ON2">\n')
conn:send('<input type="submit" name="pwmi" value="OFF2"><br><br>\n')
-------
conn:send('<input type="submit" name="pwmi" value="SENDtoUART"><br><br>\n')
-- conn:send(mycode)
conn:send(datax) --wyświetl odpowiedź odebraną z UART
conn:send('</form>\n')
----------------------------------------------------
conn:send('<form action="http://192.168.1.175:31" onsubmit="event.preventDefault(); return false;">\n')
--conn:send('<form onsubmit="event.preventDefault(); return false;">\n')
conn:send('<script type="text/javascript">\n')
conn:send('function wprowadz(){\n')
conn:send('var mycode = document.getElementById("pole").value\n')
conn:send('document.getElementById("mycode").innerHTML = mycode\n')
conn:send('console.log(mycode)}\n')
--conn:send('}\n')
conn:send('</script>\n')
conn:send('<input type="text" id="pole"/>\n')
conn:send('<input type="submit" value="<-INPUT" onclick="wprowadz()"/>\n')
conn:send('<div id="mycode"></div>\n')
--------------------------------------------------------
conn:send('</form></body></html>\n')
conn:on("sent",function(conn) conn:close() end)
end)
end)
--the dot ends the string of UART string receiving
uart.on("data", "\.", function(data)
datax = data
if data=="quit\." then
uart.on("data") -- unregister callback function
end
end, 0)
--------------
--blinking led at output 4, its only for conroll program
znacznik = 1
pin=4
gpio.mode(pin, gpio.OUTPUT)
tmr.alarm(0, 100, 1,--2 sek
function ()
if znacznik == 0 then
gpio.write(pin, gpio.LOW)
znacznik = 1
else
gpio.write(pin, gpio.HIGH)
znacznik = 0
end
end)
Płytka ESP8266
