yangping 发表于 2015-5-27 17:02:55

云编程demo-教你用云端编程点亮NodeMCU的LED

使用doit的yun端编程功能,可以实现浏览器编写lua代码,在线发送到nodemcu节点,在nodemcu上直接运行。想想是不是有点酷?{:3_48:}{:3_42:}
这是一个demo,演示了如何使用yun.doit.am编程。
yun端协议在另一个页面:http://bbs.doit.am/forum.php?mod=viewthread&tid=44&extra=page%3D1

基本原理


NodeMCU节点连接到互联网,在NodeMCU中运行一个lua程序,在这个程序里面建立tcp client。该client连接到doit的yun服务器,实现与服务器的通信。
在doit yun服务器上,对lua源代码进行编辑,点击"run",即可将源代码下载到NodeMCU中。
在NodeMCU中,接收到来自服务器的lua源文件,将其保存成文件。并compile成lc文件运行。

要实现这个功能,首先要在NodeMCU中下载三个文件。分别是init.lua,sta.lua,yun.lua。
注意:yun端服务器和NodeMCU之间通信是靠deviceID来识别。本demo的deviceID为doitCar。你可以随意取一个自己喜欢的名字,并在yun.lua中将doitCar替换。
init.lua文件:
print("\n")
print("ESP8266 Started")

local exefile="sta"
local luaFile = {exefile..".lua","yun.lua"}
for i, f in ipairs(luaFile) do
      if file.open(f) then
      file.close()
      print("Compile File:"..f)
      node.compile(f)
          print("Remove File:"..f)
      file.remove(f)
      end
end

if file.open(exefile..".lc") then
      dofile(exefile..".lc")
else
      print(exefile..".lc not exist")
end
exefile=nil;luaFile = nil
collectgarbage()sta.lua文件:
print("Ready to Set up wifi mode")
wifi.setmode(wifi.STATION)
local ssid = "MERCURY_1013"
local psw = "123456789"
print("Conneting to "..ssid)
wifi.sta.config(ssid,psw)--ssid and password
wifi.sta.connect()
local cnt = 0
gpio.mode(0,gpio.OUTPUT);
tmr.alarm(3, 1000, 1, function()
    if (wifi.sta.getip() == nil) and (cnt < 20) then
            print("->")
            cnt = cnt + 1
                if cnt % 2 ==1 then
                   gpio.write(0,gpio.HIGH);
                else
                   gpio.write(0,gpio.LOW);
                end
    else
            tmr.stop(3)
            if (cnt < 20) then
                        print("Config done, IP is "..wifi.sta.getip())
                        cnt = nil;ssid=nil;psw=nil;
                        collectgarbage();
                        if file.open("yun.lc") then
                              dofile("yun.lc")
                        else
                              print("yun.lc not exist")
                        end
            else
                        print("Wifi setup time more than 20s, Pls verify\r\nssid:"..ssid.." psw:"..psw.."\r\nThen re-download the file.")
                        cnt=cnt+1;
                        tmr.alarm(1, 300, 1, function()
                              if cnt % 2 ==1 then
                                 gpio.write(0,gpio.HIGH);
                              else
                                 gpio.write(0,gpio.LOW);
                              end
                        end)
            end
               
    end
         end)
yun.lua文件:
--yun coding demo
--Created @ 2015/05/27 by Doit Studio
--Modified: null
--http://www.doit.am/
--http://www.smartarduino.com/
--http://szdoit.taobao.com/
--bbs: bbs.doit.am

print("Start yun")

gpio.mode(0,gpio.OUTPUT);--LED Light on
gpio.write(0,gpio.LOW);

local deviceID = "doitCar"
local timeTickCnt = 0
local fileName = "yunRemote"
local conn;
local flagClientTcpConnected=false;
print("Start TCP Client");
tmr.alarm(3, 5000, 1, function()
      if flagClientTcpConnected==true then
                timeTickCnt = timeTickCnt + 1;
                if timeTickCnt>=60 then --every 300 seconds send "cmd=keep\r\n" to server
                        timeTickCnt = 0;
                        conn:send("cmd=keep\r\n");
                end               
      elseif flagClientTcpConnected==false then
      print("Try connect Server");
      conn=net.createConnection(net.TCP, false)
      conn:connect(7500,"182.92.178.210");
                conn:on("connection",function(c)
                        print("TCPClient:conneted to server");
                        conn:send("cmd=subscribe&topic="..deviceID.."\r\n");
                        flagClientTcpConnected = true;timeTickCnt = 0;
                        end) --connection
                conn:on("disconnection",function(c)
                        flagClientTcpConnected = false;
                        conn=nil;collectgarbage();
                        end) --disconnection
                conn:on("receive", function(conn, m)
                        if string.sub(m,1,5)=="__B__" then
                              file.remove(fileName..".lua")
                              file.open(fileName..".lua", "w" )                              
                              conn:send("cmd=next\r\n");--start fetching prog file
                        elseif string.sub(m,1,5)=="__E__" then --finish fetching
                              file.close()
                              collectgarbage();
                              node.compile(fileName..".lua");
                              file.remove(fileName..".lua");
                              dofile(fileName..".lc")
                        else --the file context
                              print("Recieve:"..m)
                              file.writeline(m);
                              conn:send("cmd=next\r\n");--continue fetching
                        end
                        collectgarbage();
                        end)--receive
      end
end)


在浏览器中打开http://coding.doit.am。输入设备名称:doitCar
进入后,在编辑区域输入:
print("start flash led")
cnt = 10
while cnt>0 do
gpio.write(0,gpio.LOW)
tmr.delay(1000*1000) --1second
gpio.write(0,gpio.HIGH)
tmr.delay(1000*1000)--1second
cnt = cnt - 1;
end
print("yun demo finish!")
点击”run“。如果一切顺利的话,可以看到NodeMCU的led一秒钟闪一次。

enjoy it!

程序运行的log如下:
NodeMCU 0.9.6 build 20150406powered by Lua 5.1.4


ESP8266 Started

Hard Restart 2015年5月27日17:15:26

Ready to Set up wifi mode
Conneting to MERCURY_1013
> ->
->
->
Config done, IP is 192.168.1.116
Start yun
Start TCP Client
Try connect Server
TCPClient:conneted to server
Recieve:print("start flash led")
cnt = 10
while cnt>0 do
gpio.write(0,gpio.LOW)
tmr.delay(1000*1000) --1second
gpio.write(0,gpio.HIGH)
tmr.delay(1000*1000)--1second
cnt = cnt - 1;
end
print("yun demo finish!")
start flash led
yun demo finish!

英文版本:http://bbs.smartarduino.com/showthread.php?tid=3Github可以下载示例代码:
https://github.com/SmartArduino/COULD-coding-with-NodeMCU



页: [1]
查看完整版本: 云编程demo-教你用云端编程点亮NodeMCU的LED