作者:zls121zls | 更新时间:2016-12-08 | 浏览量:6199
环境搭建:
2、固件烧录工具:esp8266 flash升级烧写烧录工具v2.1绿色版
3、烧写教程:参考difiot
4、以上所有资料访问百度云盘
NodeMCU所有资料和源码
基础编程手册
二:硬件介绍
三:相关源码
TISON开发板 | ||||
NODEMCU引脚 | TISON开发板 | 输入输出 | 开发板标识 | 连接关系 |
IO6 | GPIO12 | 输入 | photoR | 连接Tison ESP8266光敏管 |
IO6 | GPIO12 | 输出 | REALY | 连接Tison ESP8266继电器 |
IO6 | GPIO12 | 输入输出 | DHT | 连接Tison ESP8266温湿度传感器 |
IO7 | GPIO13 | 输出 | LEDB | 连接Tison ESP8266开发板蓝色LED,蓝色灯亮起进入ESPTOUCH模式,配置完成关闭 |
IO5 | GPIO14 | 输出 | LEDR | 连接Tison ESP8267开发板红色LED,红色灯亮起表示故障,故障排除关闭 |
IO8 | GPIO15 | 输出 | LEDG | 连接Tison ESP8268开发板绿色LED,绿色灯亮起进入AIRKISS模式,配置完成关闭 |
--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)
dofile("config.lua")
dofile("Dht.lua")
dofile("SendData.lua")
bLive=0
bOnLine=0
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")
--在线后再读取数据,发送年,接收命令
if bOnLine==1 then
print("start send data")
--读取湿度
Humi,Temp=ReadDHT(config.DHTPin)
--发送数据
sendToBigiot(cu,Humi,Temp)
end
--执行命令
dofile("sayCommand.lua")
end)
end)
--modify DEVICEID1 INPUTID APIKEY DEVICEID2
config={
host = host or "www.bigiot.net",
port = port or 8181,
DEVICEID = "822",
UID=" ",
TempID=" ",
HumiID=" ",
APIKEY = " ",
DHTPin= 6,
LEDPin= 8
}
--收到连接正常,发送checkin
if r.M == "WELCOME TO BIGIOT" then
ok, s = pcall(cjson.encode, {M="checkin",ID=config.DEVICEID,K=config.APIKEY})
if ok then
print(s)
else
print("failed to encode!")
end
cu:send( s.."\n" )
bLive=0
--定时心跳,防止掉线
tmr.alarm(2, 40000, 1, function()
--如果标记为1,表示未收到上次的心跳返回,重启
if bLive==3 then
node.restart()
end
ok, ticket = pcall(cjson.encode, {M="isOL",ID="D"..config.DEVICEID})
print(ticket)
cu:send(ticket.."\n" )
--发送后将标记置为1
bLive=bLive+1
end)
end
if r.M=="checkinok" then
bOnLine=1
end
function ReadDHT(pin)
dhstatus, temp, humi, temp_dec, humi_dec = dht.read11(pin)
print(dhstatus)
if dhstatus == dht.OK then
print(string.format("DHT Temperature:%d.%01d;Humidity:%d.%01d\r\n",
math.floor(temp),
temp_dec,
math.floor(humi),
humi_dec
))
--转换实际温度
realTemp=math.floor(temp)
--转换实际湿度
realhumi=math.floor(humi)
return realhumi,realTemp
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
end
function sendToBigiot(cu,humi,temp)
print(humi)
print(temp)
if humi==nil then
humi=0
end
if temp==nil then
temp=0
end
--上报湿度
local v = {[config.TempID]=string.format("%d", math.floor(temp)),[config.HumiID]=string.format("%d",math.floor(humi))}
ok3, s3 = pcall(cjson.encode, {M="update",ID=config.DEVICEID,V=v})
print("send data:"..s3)
cu:send( s3.."\n")
end
--如果是say代表命令
LEDG = 5
LEDR = 7
LEDB = 8
gpio.mode(LEDG,gpio.OUTPUT)
gpio.mode(LEDR,gpio.OUTPUT)
gpio.mode(LEDB,gpio.OUTPUT)
gpio.write(LEDG, gpio.LOW)
gpio.write(LEDR, gpio.HIGH)
gpio.write(LEDB, gpio.HIGH)
if r.M == "say" then
local commander=r.C
if commander == "play" then
gpio.write(LEDG, gpio.LOW)
gpio.write(LEDR, gpio.LOW)
gpio.write(LEDB, gpio.LOW)
ok, played = pcall(cjson.encode, {M="say",ID=config.UID,C="LED turn on!"})
cu:send( played.."\n" )
elseif commander == "stop" then
gpio.write(LEDG, gpio.HIGH)
gpio.write(LEDR, gpio.HIGH)
gpio.write(LEDB, gpio.HIGH)
ok, stoped = pcall(cjson.encode, {M="say",ID=config.UID,C="LED turn off!"})
cu:send( stoped.."\n" )
end
end
四:相关资料参考
http://www.electrodragon.com/w/ESP8266_NodeMCU_Lua
http://www.nodemcu.com/index_cn.html
http://nodemcu.readthedocs.io/en/master/
http://www.electrodragon.com/smartconfig-nodemcu/
http://www.zhihu.com/question/36288709
http://blog.csdn.net/leytton/article/details/51723221
http://www.tinylab.org/nodemcu-kickstart/
http://my.oschina.net/u/2306127/blog/402931
http://www.esp8266.com/viewforum.php?f=17
http://nodemcu-dev.doit.am/index.html
https://github.com/SmartArduino/Doit_Cloud
http://git.oschina.net/HEKRCLOUD/hekr-esp8266-sdk-ra