Permalink
Browse files

CLOUD coding with NodeMCU

Principle:
Lua source code edited on the cloud is transferred to the node and
runned on the node.
The node communicates with server via TCP protocal(Server IP:
198.199.94.16@port:7500)

Here is protocal:

Code:
1, Define your node's name  (such as "doitCar")
cmd=subscribe&topic=doitCar\r\n

Code:
2, When a lua file is edited and needed to be downloaded at the node:
2.1 Server sends "__B__" to the node. the node open a file,and send
"cmd=next\r\n" to server as responce string. Once the node recievs
"__B__", it should know that a new source code file is prepared to be
transferred.
2.2 Server will send the source code in sevaral package. The node
recieves and store the package one by one, and sends "cmd=next\r\n" to
the server every package.
2.3 Server send "__E__" when the transfering is finished. The node
recieves the finishing string, saves the source code, closes the opened
file. compiles the file,and dofile it.

Code:
3, Send  "cmd=keep\r\n" every 3 minitus to keep the tcp link alive.

The CLOUD coding website:http://cloud.doit.am

A tutorial was posted at
here:http://bbs.smartarduino.com/showthread.php?tid=3&pid=3#pid3
that's really simple and easy!
Have fun!

A chinese protocal can be found here:
http://bbs.doit.am/forum.php?mod=viewthread&tid=44&extra=page%3D1
  • Loading branch information...
1 parent 4ded73f commit ec1bf86a2ba3423a79b3e7205f8f88ebdbad3160 smartarduino committed May 29, 2015
Showing with 171 additions and 0 deletions.
  1. BIN edit.jpg
  2. +31 −0 init.lua
  3. +29 −0 prtocal.txt
  4. +51 −0 sta.lua
  5. BIN welcome.jpg
  6. +60 −0 yun.lua
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,31 @@
+--DoitCar Ctronl Demo
+--sta mode
+--Created @ 2015/05/14 by Doit Studio
+--Modified: null
+--http://www.doit.am/
+--http://www.smartarduino.com/
+--http://szdoit.taobao.com/
+--bbs: bbs.doit.am
+
+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()
View
@@ -0,0 +1,29 @@
+Principle:
+Lua source code edited on the cloud is transferred to the node and runned on the node.
+The node communicates with server via TCP protocal(Server IP: 198.199.94.16@port:7500��
+
+Here is protocal:
+
+Code:
+1, Define your node's name (such as "doitCar")
+cmd=subscribe&topic=doitCar\r\n
+
+Code:
+2, When a lua file is edited and needed to be downloaded at the node:
+ 2.1 Server sends "__B__" to the node. the node open a file,and send "cmd=next\r\n" to server as responce string. Once the node recievs "__B__", it should know that a new source code file is prepared to be transferred.
+ 2.2 Server will send the source code in sevaral package. The node recieves and store the package one by one, and sends "cmd=next\r\n" to the server every package.
+ 2.3 Server send "__E__" when the transfering is finished. The node recieves the finishing string, saves the source code, closes the opened file. compiles the file,and dofile it.
+
+
+Code:
+3, Send "cmd=keep\r\n" every 3 minitus to keep the tcp link alive.
+
+
+The CLOUD coding website:http://cloud.doit.am
+
+A tutorial was posted at here:http://bbs.smartarduino.com/showthread.php?tid=3&pid=3#pid3
+that's really simple and easy!
+Have fun!
+
+A chinese protocal can be found here:
+http://bbs.doit.am/forum.php?mod=viewthread&tid=44&extra=page%3D1
View
@@ -0,0 +1,51 @@
+--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("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)
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,60 @@
+--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,"198.199.94.16");--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)
+

0 comments on commit ec1bf86

Please sign in to comment.