LUA编程,ESP8266 NODEMCU串行通信

时间:2016-10-10 21:02:26

标签: lua arduino esp8266 serial-communication esplorer

我正在尝试通过ESP8266 12E NODE MCU开发套件的串口将数据发送到arduino串口。

我很难找到所用语法的示例,我尝试通过arduino使用serial.print()发送数据并且它可以正常工作,但我不确定如何实现这一点在Lua。

感谢任何帮助

我可以从arduino获取SSID和密码

INIT.lua

SSID = "XXXX"
Password = "XXXX"
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,Password) -- Replace with your AP Name and security key.
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() 
if wifi.sta.getip()== nil then 
print("Obtaining IP...") 
else 
tmr.stop(1)
print("Got IP. "..wifi.sta.getip())
dofile("LED_on_off.lua")

end

end)

LED_ON_OFF LUA

print(wifi.sta.getip())

led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, 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
        buf = buf.."<h1> ESP8266 Web Server</h1>";

        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON2")then

**********Here i would like to send data ot arduini that pin is swithced oN ************

              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

1 个答案:

答案 0 :(得分:2)

请阅读NodeMCU文档:

https://nodemcu.readthedocs.io/en/master/

尤其

https://nodemcu.readthedocs.io/en/master/en/modules/uart/

一切都在那里。您不必查找示例。您必须学习阅读和理解文档。没有阅读文档就没有适当的编程。

如果你只是想告诉arduino发生了一件事,你可能只是使用数字输出并将其连接到arduino输入。保持简单和愚蠢。