LUA编程,ESP8266 NODEMCU串行通讯 [英] LUA Programming ,ESP8266 NODEMCU Serial communication

查看:165
本文介绍了LUA编程,ESP8266 NODEMCU串行通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过ESP8266 12E NODE MCU开发套件的串行端口向arduino串行端口发送数据.

我很难找到一个使用的语法示例,我尝试通过arduino使用serial.print()发送数据,并且它可以工作,但是我不确定如何在Lua中完成此操作. /p>

感谢您的帮助

我可以从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)

解决方案

请阅读NodeMCU文档:

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

尤其是

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

就在那里.您无需查找示例.您必须学习阅读和理解文档.没有阅读文档,没有正确的编程.

如果您只想告诉arduino发生了一件事情,尽管您最好使用数字输出并将其连接到arduino输入.保持简单和愚蠢.

I am trying to send data over the serial port of the ESP8266 12E NODE MCU dev kit to an arduino serial port.

I've having a hard time trying to find an example of the syntax used and I tried using serial.print() via the arduino to send data and it works but I'm unsure how to accomplish this in Lua.

Any help is appreciated

I can get the SSID and Password form arduino

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)

解决方案

Please read the NodeMCU documentation:

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

especially

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

It's all there. You don't have to find examples. You have to learn to read and understand documentations. There is no proper programming without reading documentations.

If you only want to tell the arduino that one single thing happened though you might as well just use a digital output and hook it up to an arduino input. Keep it simple and stupid.

这篇关于LUA编程,ESP8266 NODEMCU串行通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆