如何在 ESP8266 NodeMCU 上读取 stdio

How can I read stdio on ESP8266 NodeMCU

我正在尝试从 Lua 控制台读取用户输入,但似乎无法从 stdio

读取
print("Enter your name:")
if file.open("stdio") then
  line = file.read("*a")
  file.close()
  print("Hello "..line)
end

我的固件有模块file,gpio,net,node,ow,tmr,uart,wifi,tls

如果我尝试 io.read("*a") 我会得到错误 Lua error: stdin:1: attempt to index global 'io' (a nil value)

NodeMCU固件中没有io库。因此你不能调用 io.read

如果你想读取串行输入,你可以使用 uart 库。 https://nodemcu.readthedocs.io/en/release/modules/uart/

-- when 4 chars is received.
uart.on("data", 4,
  function(data)
    print("receive from uart:", data)
    if data=="quit" then
      uart.on("data") -- unregister callback function
    end
end, 0)
-- when '\r' is received.
uart.on("data", "\r",
  function(data)
    print("receive from uart:", data)
    if data=="quit\r" then
      uart.on("data") -- unregister callback function
    end
end, 0)