Welcome, Guest
You have to register before you can post on our site.

Username/Email:
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 1,089
» Latest member: Starlion2007
» Forum threads: 2,047
» Forum posts: 2,217

Full Statistics

Online Users
There are currently 13 online users.
» 0 Member(s) | 13 Guest(s)

Latest Threads
Disability Payday Loans C...
Forum: ESP32 Chip
Last Post: ShoshanaRn
11-14-2021, 10:15 AM
» Replies: 0
» Views: 257
Installation for S100
Forum: User Manual for Tank Chassis
Last Post: smartarduino
03-10-2017, 11:39 AM
» Replies: 0
» Views: 14,587
ESPduino Code for WiFi Ta...
Forum: ESPDuino=ESP8266+Arduino
Last Post: smartarduino
11-16-2016, 08:35 AM
» Replies: 0
» Views: 19,453
ESP32 Chip Documents
Forum: ESP32 Chip
Last Post: smartarduino
10-11-2016, 04:11 AM
» Replies: 0
» Views: 18,634
DataSheet for WiFi Grove ...
Forum: WiFi Grove kit based on ESP8266
Last Post: smartarduino
06-08-2016, 06:52 AM
» Replies: 0
» Views: 16,166
Some documents about Ai60...
Forum: Ai6060H
Last Post: smartarduino
05-26-2016, 01:03 AM
» Replies: 0
» Views: 16,832
Manual for 2-way motor & ...
Forum: 2-way motor & 16-way servo shield board for Arduino
Last Post: smartarduino
05-10-2016, 10:42 AM
» Replies: 0
» Views: 20,104
NodeMcu Firmware 1.4
Forum: NodeMcu
Last Post: smartarduino
05-10-2016, 06:24 AM
» Replies: 0
» Views: 23,409
User Manual for DoArm S7
Forum: DoArm
Last Post: smartarduino
04-25-2016, 10:07 AM
» Replies: 0
» Views: 16,233
Video for DoArm
Forum: DoArm
Last Post: smartarduino
04-14-2016, 04:01 AM
» Replies: 0
» Views: 6,547

 
Photo a Tutorial on CLOUD coding with NodeMCU
Posted by: smartarduino - 05-28-2015, 08:23 AM - Forum: NodeMcu Cloud Coding - No Replies

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

Print this item

Thumbs Up CLOUD coding protocal
Posted by: smartarduino - 05-28-2015, 08:20 AM - Forum: NodeMcu Cloud Coding - No Replies

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: [email protected]: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 [color=#ff9933]"__B__"[/color] to the node. the node open a file,and send [color=#ff9933]"cmd=next\r\n"[/color] 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 [color=#ff66ff]"cmd=next\r\n"[/color] to the server every package.
     2.3 Server send [color=#ff9933]"__E__"[/color] 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 [color=#ff9933]"cmd=keep\r\n"[/color] 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

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


Cool Angel Huh Idea Dodgy Tongue

Print this item