汇思锐模块LED亮灭控制

汇思锐模块LED亮灭控制的详细介绍

创作者:zls121zls | 更新日期:2017-01-17 | 在线时长:43天
在贝壳网站上申请了一片汇思锐的8266开发底板,有源码哦

在贝壳网站上申请了一片汇思锐的8266开发底板

首先下载乐鑫的固件然后按照下图烧写固件

打开汇思锐调试工具,按照上面一步一步设置

点击airkiss 按钮进入airkiss 配网模式。

AT+RST
OK
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)
load 0x40100000, len 1856, room 16 
tail 0
chksum 0x63
load 0x3ffe8000, len 776, room 8 
tail 0
chksum 0x02
load 0x3ffe8310, len 552, room 8 
tail 0
chksum 0x79
csum 0x79
2nd boot version : 1.5
  SPI Speed      : 40MHz
  SPI Mode       : DOUT
  SPI Flash Size & Map: 16Mbit(512KB+512KB)
jump to run user1 @ 1000

rl??r?
ready

----------------------------

AT+CWMODE=1
OK
AT+CWSTARTSMART=2
OK

----------------上述测试说明模块已经焊接没问题----------------------------------------------------

一:准备工作

环境搭建:

1、PC端软件,需要安装java   ESPlorer

2、固件烧录工具:esp8266 flash升级烧写烧录工具v2.1绿色版

3、烧写教程:参考difiot

4、以上所有资料访问百度云盘

 

基于nodemuc编程API说明中文版本

https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn

NodeMCU所有资料和源码

https://github.com/nodemcu

基础编程手册

http://manual.luaer.cn/

二、硬件介绍

汇思锐开发板
开发板标识  汇思锐开发板 NODEMCU引脚 LED等亮
LED1 GPIO4 IO2 低有效
LED2 GPIO14 IO5 高有效
LED3 GPIO12 IO6 低有效
LED4 GPIO5 IO1 低有效

 

 

三、程序的编写

init.lua 文件

--init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config("JSZLZXBGS","jszlzx123")
--here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip()== nil then
        print("IP unavaiable, Waiting...")
    else
        tmr.stop(1)
        print("IP:"..wifi.sta.getip())
        dofile("runtime.lua")     
    end
end)

汇思锐开发板
开发板标识  汇思锐开发板 NODEMCU引脚 LED等亮
LED1 GPIO4 IO2 低有效
LED2 GPIO14 IO5 高有效
LED3 GPIO12 IO6 低有效
LED4 GPIO5 IO1 低有效

config.lua文件

config={host= host or "www.bigiot.net",
port = port or 8181,
DEVICEID = "1323",
UID="806",
LEDID="1319",
APIKEY="d32cb2f79",
LED1 = 2,
LED2 = 5,
LED3 = 6,
LED4 = 1,
}

runtime.lua文件

dofile("config.lua")

bLive=0
bOnLine=0

gpio.mode(config.LED1,gpio.OUTPUT)
gpio.mode(config.LED2,gpio.OUTPUT)
gpio.mode(config.LED3,gpio.OUTPUT)
gpio.mode(config.LED4,gpio.OUTPUT)

gpio.write(config.LED1, gpio.HIGH)
gpio.write(config.LED2, gpio.LOW)
gpio.write(config.LED3, gpio.HIGH)
gpio.write(config.LED4, gpio.HIGH)


cu = net.createConnection(net.TCP)
cu:connect(config.port, config.host)

cu:on("receive", function(cu, c) 
    print(c)
    r = cjson.decode(c)
    --如果存活标记为1,置为0
    if r.M=="isOL" then
        bLive=0 
    end
    tmr.alarm(1, 10000, 1, function()
        --checkin和心跳
        dofile("checkIn.lua")
        --在线后再读取数据,发送年,接收命令
    end)
    --执行命令
    dofile("sayCommand.lua")

end)



 

checkin.lua文件
 

dofile("config.lua")

bLive=0
bOnLine=0

gpio.mode(config.LED1,gpio.OUTPUT)
gpio.mode(config.LED2,gpio.OUTPUT)
gpio.mode(config.LED3,gpio.OUTPUT)
gpio.mode(config.LED4,gpio.OUTPUT)

gpio.write(config.LED1, gpio.HIGH)
gpio.write(config.LED2, gpio.LOW)
gpio.write(config.LED3, gpio.HIGH)
gpio.write(config.LED4, gpio.HIGH)


cu = net.createConnection(net.TCP)
cu:connect(config.port, config.host)

cu:on("receive", function(cu, c) 
    print(c)
    r = cjson.decode(c)
    --如果存活标记为1,置为0
    if r.M=="isOL" then
        bLive=0 
    end
    tmr.alarm(1, 10000, 1, function()
        --checkin和心跳
        dofile("checkIn.lua")
        --在线后再读取数据,发送年,接收命令
    end)
    --执行命令
    dofile("sayCommand.lua")

end)

sayCommand.lua

 --sayCommand.lua
gpio.mode(config.LED1,gpio.OUTPUT)
gpio.mode(config.LED2,gpio.OUTPUT)
gpio.mode(config.LED3,gpio.OUTPUT)
gpio.mode(config.LED4,gpio.OUTPUT)

if r.M == "say" then     
    local commander=r.C    
    if commander == "play" then  
        print("play:LED1 turn on!")    
        gpio.write(config.LED1,gpio.LOW) 
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="play:LED1 turn on!"})
        cu:send( played.."\n" )
    elseif commander == "stop" then 
        print("stop:LED1 turn off!")         
        gpio.write(config.LED1,gpio.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="stop:LED1 turn off!"})
        cu:send( stoped.."\n" )     
       --------1--------
   elseif commander == "up" then 
        print("up:LED2 turn on!") 
        gpio.write(config.LED2,gpio.HIGH)
        ok, uped = pcall(cjson.encode, {M="say",ID=r.ID,C="up:LED2 turn on!"})
        cu:send( uped.."\n" )  
   elseif commander == "plus" then  
        print("plus:LED2 turn off!")       
        gpio.write(config.LED2,gpio.LOW)
        ok, plused = pcall(cjson.encode, {M="say",ID=r.ID,C="plus:LED2 turn off!"})
        cu:send( plused.."\n" )  
        --------2--------
   elseif commander == "pause" then  
        print("pause:LED3 turn on!") 
        gpio.write(config.LED3,gpio.LOW)
        ok, paused = pcall(cjson.encode, {M="say",ID=r.ID,C="pause:LED3 turn on!"})
        cu:send( paused.."\n" )  
   elseif commander == "right" then 
        print("right:LED3 turn off!")        
        gpio.write(config.LED3,gpio.HIGH)
        ok, righted = pcall(cjson.encode, {M="say",ID=r.ID,C="right:LED3 turn off!"})
        cu:send( righted.."\n" )  
        --------3--------
   elseif commander == "down" then 
        print("right:LED4 turn on!")        
        gpio.write(config.LED4,gpio.LOW)
        ok, downed = pcall(cjson.encode, {M="say",ID=r.ID,C="down:LED4 turn on!"})
        cu:send( downed.."\n" ) 
   elseif commander == "forward" then 
        print("forward:LED4 turn off!")        
        gpio.write(config.LED4,gpio.HIGH)
        ok, forward = pcall(cjson.encode, {M="say",ID=r.ID,C="forward:LED4 turn off!"})
        cu:send( forward.."\n" )  
        --------4--------        
   elseif commander == "offOn" then 
        print("offOn:LED ALL off!")        
        gpio.write(config.LED1,gpio.HIGH)
        gpio.write(config.LED2,gpio.LOW)
        gpio.write(config.LED3,gpio.HIGH)
        gpio.write(config.LED4,gpio.HIGH)
        ok, offOned = pcall(cjson.encode, {M="say",ID=r.ID,C="offOn:LED ALL off!"})
        cu:send( offOned.."\n" )         
   elseif commander == "minus" then 
        print("minus:LED ALL off!")        
        gpio.write(config.LED1,gpio.LOW)
        gpio.write(config.LED2,gpio.HIGH)
        gpio.write(config.LED3,gpio.LOW)
        gpio.write(config.LED4,gpio.LOW)
        ok, minused = pcall(cjson.encode, {M="say",ID=r.ID,C="minus:LED ALL ON!"})
        cu:send( minused.."\n" ) 
        --------5--------
   elseif commander == "left" then 
        print("^~^left^~^")        
       
        ok, lefted = pcall(cjson.encode, {M="say",ID=r.ID,C="^~^left^~^"})
        cu:send( lefted.."\n" ) 
   elseif commander == "backward" then 
        print("^~^backward^~^")      
       
        ok, forwarded = pcall(cjson.encode, {M="say",ID=r.ID,C="^~^forward^~^"})
        cu:send( forwarded.."\n" )     
    end  
end