Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a Tutorial on CLOUD coding with NodeMCU
#1
Photo 
Hi, guys,

  how do you imagine coding on the CLOUD with NodeMCU? 
  It's realy cool!

This is a tutorial on how to code with CLOUD coding

The "CLOUD coding protocal" is posted at:
http://bbs.smartarduino.com/showthread.php?tid=2

Principle:
NodeMCU connects to internet as a node. A "yun.lua" runned in NodeMCU. a TCP client was established to connect to the the cload server:198.199.94.16 at port 7500.
The lua source code was edited in the browser at http://cloud.doit.am. Click "run" button, the source code would be transferred to the NodeMCU.
The "yun.lua" running in the NodeMCU recieving the souce code file, saved it as a lua file, compiled it and ran the lc file.

Three lua files are listed here to realize the CLOUD coding function. 

The steps:

1, these files should be downloaded to NodeMCU first: init.lua,sta.lua,yun.lua

Tips: the Cloud server and the NodeMCU are touched together with the deviceID. Such as "doitCar" here. You can name it as you wish.

init.lua:

Code:
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:

Code:
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:

Code:
--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");
               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)

2. Open the website:http://cloud.doit.am.

3. Type a device ID, such as "doitCar" here.
4. When you are in, typing the codes as follows:
Code:
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!")


5. Press "run".


If everything goes well, you will notice the led flashing in 1 sencond

Enjoy it.

Here is the programm log.

Code:
NodeMCU 0.9.6 build 20150406  powered 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!

A chinese tutorial can be found here:
http://bbs.doit.am/forum.php?mod=viewthread&tid=46&page=1&extra=#pid47


You can get the tutorial code at github:
https://github.com/SmartArduino/COULD-coding-with-NodeMCU

Confused Angel Undecided Sick My Cry Sleepy
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)