nodeMCU don't wait for serial data
Posted: Fri Apr 07, 2017 11:45 am
Hello,
I'm here first time. I hope to find help for my project. I've created an arduino-project for my mobile home. It wors fine. I can interactive via a nextion touch-screen. Now I'd like to display some data on my mobile. I created a html-page, put it on the nodeMCU. Receiving is working fine, too.
What now i try to add: If I open the IP on my mobile, the nodeMCU shell send a command to the arduino. Then the arduino Shell send on textline with the data to the nodeMCU. The values changes some variables and the html-page will send to the mobil.
Problem is, that the nodeMCU don't wai for receiving the complete textline.
Can someone give me some ideas? Would be fantastic.
Here is my code:
I'm here first time. I hope to find help for my project. I've created an arduino-project for my mobile home. It wors fine. I can interactive via a nextion touch-screen. Now I'd like to display some data on my mobile. I created a html-page, put it on the nodeMCU. Receiving is working fine, too.
What now i try to add: If I open the IP on my mobile, the nodeMCU shell send a command to the arduino. Then the arduino Shell send on textline with the data to the nodeMCU. The values changes some variables and the html-page will send to the mobil.
Problem is, that the nodeMCU don't wai for receiving the complete textline.
Can someone give me some ideas? Would be fantastic.
Here is my code:
Code: Select all
-- toggle LED for testing
function LED_Toggle()
if lighton==0 then
lighton=1
gpio.write(pin,gpio.HIGH) -- Assign GPIO On
else
lighton=0
gpio.write(pin,gpio.LOW) -- Assign GPIO off
end
end
-- ##################################################################################
-- Hardware init
lighton=0
pin=4
gpio.mode(pin,gpio.OUTPUT) -- Assign GPIO to Output
Durchlauf=0
-- setup UART for receiving data
-- parameters uart.setup( UART-id, baud, databits, parity, stopbits, echo )
uart.setup(0,9600,8,0,1)
--5s delay before starting with the program, sufficient break execution if needed
tics=1000000
tmr.delay(5*tics)
-- Configure Wireless Internet
srv=net.createServer(net.TCP)
wifi.setmode(wifi.STATION)
wifi.sta.config("******","***********")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.10.67",netmask="255.255.255.0",gateway="192.168.10.1"})
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is " .. wifi.sta.getip())
print("Chip ID: " .. node.chipid())
print("Heap Size: " .. node.heap())
-- ##################################################################################
-- server listen on 80,
-- if data received, print data to console,
-- then serve up website
srv:listen(80,
function(conn)
local responseBytes = 0
local method=""
local url=""
local vars=""
conn:on("receive",
function(conn,payload)
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
responseBytes = -1
return
end
url="Anzeige.html"
responseBytes = 0
conn:send("HTTP/1.1 200 OK\r\n\r\n")
end
)
conn:on("sent",
function(conn)
if responseBytes>=0 and method=="GET" then
-- #########################################
if Durchlauf==0 then
-- "Befehl" senden
uart.write(0, "Action\n")
-- 1 Zeile empfangen
uart.on("data", "\r",
function(data)
print("Received from Arduino: " , data)
LED_Toggle()
end, 1
)
Durchlauf=1
end
-- #########################################
if file.open(url, "r") then
file.seek("set", responseBytes)
local line=file.read(512)
file.close()
if line then
conn:send(line)
responseBytes = responseBytes + string.len(line)
return
end
end
end
conn:close()
end
)
if Durchlauf==2 then
Durchlauf=0
elseif Durchlauf==1 then
Durchlauf=2
end
end
)