四博智联产品售后

 找回密码
 立即注册
搜索
查看: 19885|回复: 0

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

[复制链接]

0

主题

0

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2015-5-27 17:02:55 | 显示全部楼层 |阅读模式
使用doit的yun端编程功能,可以实现浏览器编写lua代码,在线发送到nodemcu节点,在nodemcu上直接运行。想想是不是有点酷?
这是一个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文件:
  1. print("\n")
  2. print("ESP8266 Started")

  3. local exefile="sta"
  4. local luaFile = {exefile..".lua","yun.lua"}
  5. for i, f in ipairs(luaFile) do
  6.         if file.open(f) then
  7.       file.close()
  8.       print("Compile File:"..f)
  9.       node.compile(f)
  10.           print("Remove File:"..f)
  11.       file.remove(f)
  12.         end
  13. end

  14. if file.open(exefile..".lc") then
  15.         dofile(exefile..".lc")
  16. else
  17.         print(exefile..".lc not exist")
  18. end
  19. exefile=nil;luaFile = nil
  20. collectgarbage()
复制代码
sta.lua文件
  1. print("Ready to Set up wifi mode")
  2. wifi.setmode(wifi.STATION)
  3. local ssid = "MERCURY_1013"
  4. local psw = "123456789"
  5. print("Conneting to "..ssid)
  6. wifi.sta.config(ssid,psw)--ssid and password
  7. wifi.sta.connect()
  8. local cnt = 0
  9. gpio.mode(0,gpio.OUTPUT);
  10. tmr.alarm(3, 1000, 1, function()
  11.     if (wifi.sta.getip() == nil) and (cnt < 20) then
  12.             print("->")
  13.             cnt = cnt + 1
  14.                 if cnt % 2 ==1 then
  15.                    gpio.write(0,gpio.HIGH);
  16.                 else
  17.                    gpio.write(0,gpio.LOW);
  18.                 end
  19.     else
  20.             tmr.stop(3)
  21.             if (cnt < 20) then
  22.                         print("Config done, IP is "..wifi.sta.getip())
  23.                         cnt = nil;ssid=nil;psw=nil;
  24.                         collectgarbage();
  25.                         if file.open("yun.lc") then
  26.                                 dofile("yun.lc")
  27.                         else
  28.                                 print("yun.lc not exist")
  29.                         end
  30.             else
  31.                         print("Wifi setup time more than 20s, Pls verify\r\nssid:"..ssid.." psw:"..psw.."\r\nThen re-download the file.")
  32.                         cnt=cnt+1;
  33.                         tmr.alarm(1, 300, 1, function()
  34.                                 if cnt % 2 ==1 then
  35.                                    gpio.write(0,gpio.HIGH);
  36.                                 else
  37.                                    gpio.write(0,gpio.LOW);
  38.                                 end
  39.                         end)
  40.             end
  41.                
  42.     end
  43.          end)
复制代码

yun.lua文件
  1. --yun coding demo
  2. --Created @ 2015/05/27 by Doit Studio
  3. --Modified: null
  4. --http://www.doit.am/
  5. --http://www.smartarduino.com/
  6. --http://szdoit.taobao.com/
  7. --bbs: bbs.doit.am

  8. print("Start yun")

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

  11. local deviceID = "doitCar"
  12. local timeTickCnt = 0
  13. local fileName = "yunRemote"
  14. local conn;
  15. local flagClientTcpConnected=false;
  16. print("Start TCP Client");
  17. tmr.alarm(3, 5000, 1, function()
  18.         if flagClientTcpConnected==true then
  19.                 timeTickCnt = timeTickCnt + 1;
  20.                 if timeTickCnt>=60 then --every 300 seconds send "cmd=keep\r\n" to server
  21.                         timeTickCnt = 0;
  22.                         conn:send("cmd=keep\r\n");
  23.                 end               
  24.         elseif flagClientTcpConnected==false then
  25.         print("Try connect Server");
  26.         conn=net.createConnection(net.TCP, false)
  27.         conn:connect(7500,"182.92.178.210");
  28.                 conn:on("connection",function(c)
  29.                         print("TCPClient:conneted to server");
  30.                         conn:send("cmd=subscribe&topic="..deviceID.."\r\n");
  31.                         flagClientTcpConnected = true;timeTickCnt = 0;
  32.                         end) --connection
  33.                 conn:on("disconnection",function(c)
  34.                         flagClientTcpConnected = false;
  35.                         conn=nil;collectgarbage();
  36.                         end) --disconnection
  37.                 conn:on("receive", function(conn, m)
  38.                         if string.sub(m,1,5)=="__B__" then
  39.                                 file.remove(fileName..".lua")
  40.                                 file.open(fileName..".lua", "w" )                                
  41.                                 conn:send("cmd=next\r\n");--start fetching prog file
  42.                         elseif string.sub(m,1,5)=="__E__" then --finish fetching
  43.                                 file.close()
  44.                                 collectgarbage();
  45.                                 node.compile(fileName..".lua");
  46.                                 file.remove(fileName..".lua");
  47.                                 dofile(fileName..".lc")
  48.                         else --the file context
  49.                                 print("Recieve:"..m)
  50.                                 file.writeline(m);
  51.                                 conn:send("cmd=next\r\n");--continue fetching
  52.                         end
  53.                         collectgarbage();
  54.                         end)--receive
  55.         end
  56. end)
复制代码


在浏览器中打开http://coding.doit.am。输入设备名称:doitCar
进入后,在编辑区域输入:
  1. print("start flash led")
  2. cnt = 10
  3. while cnt>0 do
  4. gpio.write(0,gpio.LOW)
  5. tmr.delay(1000*1000) --1second
  6. gpio.write(0,gpio.HIGH)
  7. tmr.delay(1000*1000)--1second
  8. cnt = cnt - 1;
  9. end
  10. print("yun demo finish!")
复制代码

点击”run“。如果一切顺利的话,可以看到NodeMCU的led一秒钟闪一次。

enjoy it!

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


  2. ESP8266 Started

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

  4. Ready to Set up wifi mode
  5. Conneting to MERCURY_1013
  6. > ->
  7. ->
  8. ->
  9. Config done, IP is 192.168.1.116
  10. Start yun
  11. Start TCP Client
  12. Try connect Server
  13. TCPClient:conneted to server
  14. Recieve:print("start flash led")
  15. cnt = 10
  16. while cnt>0 do
  17. gpio.write(0,gpio.LOW)
  18. tmr.delay(1000*1000) --1second
  19. gpio.write(0,gpio.HIGH)
  20. tmr.delay(1000*1000)--1second
  21. cnt = cnt - 1;
  22. end
  23. print("yun demo finish!")
  24. start flash led
  25. yun demo finish!
复制代码

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



输入设备名称.jpg
编辑界面.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2024-3-28 21:16 , Processed in 0.061315 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表