测试没问题,注意烧写固件时选择float的双精度,这样就有2个小数点了。
--init.lua
-- Written by niwusong
if true then
--change to if true
ssid1="free"
password1="??"
password2="???"
ssid3="freewifi" password3="********"
g_mac=nil print("set up wifi mode")
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid3,password3)
wifi.sta.connect()
cnt = 0
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
if (wifi.sta.getip() == nil)
and (cnt < 20) then
print("IP1 unavaiable, Waiting...")
cnt = cnt + 1
else
tmr.stop(1) if (cnt < 60) then
print("IP:"..wifi.sta.getip())
MAC=wifi.sta.getmac()
mac=string.gsub(MAC,":","")
g_mac = mac print("MAC:"..mac)
dofile("runtime.lua")
else
end
end
end)
else
print("\n")
print("Please edit 'init.lua' first:")
print("Step 1: Modify wifi.sta.config() function in line 5 according settings of your wireless router.")
print("Step 2: Change the 'if false' statement in line 1 to 'if true'.")
node.restart()
end
--config.lua
-- Written by niwusong
--modify DEVICEID1 INPUTID APIKEY DEVICEID2
host = host or "www.bigiot.net"
port = port or 8181
DEVICEID = "1347"
APIKEY = "09dd54b61"
TEMPID1 = "1338"
PRESSID2 = "1339"
AIRID3 = "1381"
DS18B20ID4 = "1398"
GY30ID5 = "1391"
TEMP=1
PRESS=2
LEDR= 5
LEDG= 6
LEDB= 7
Trig= 8
DS18B20=10
GY30SDA = 3
GY30SCL = 4
-----runtime.lua---------------------------------------------------
-- Written by niwusong
dofile("config.lua")
--dofile("Dht.lua")
--dofile("SendDate.lua")
--dofile("sayCommand.lua")
--gpio.mode(config.LEDR,gpio.OUTPUT)
--gpio.mode(config.LEDG,gpio.OUTPUT)
--gpio.mode(config.LEDB,gpio.OUTPUT)
--gpio.write(config.LEDR,gpio.LOW)
--gpio.write(config.LEDG,gpio.LOW)
--gpio.write(config.LEDB,gpio.LOW)
bLive=0
bOnLine=0
cu = net.createConnection(net.TCP)
cu:connect(port,host)
cu:on("receive", function(cu, c)
print(c) r = cjson.decode(c)
if r.M =="isOL" then bLive=0 end
--如果存活标记为1,置为0 tmr.alarm(1, 15000, 1, function() dofile("checkin.lua")
--checkin和心跳
-- Written by niwusong
if bOnLine==1 then
----------checkin后在线后再读取数据--------------
----读取BM180湿度、气压 -------
bmp085.init(1, 2)
temp = bmp085.temperature() / 10
press = tonumber(bmp085.pressure()/ 100)
print("Pressure: ", press, "mbar")
print("Temperature: ", temp, "℃")
if adc.force_init_mode(adc.INIT_ADC)
----读取MQ135湿度、气压、空气质量------
then
node.restart()
return
-- don't bother continuing, the restart is scheduled
end
ppm=0
ppm = adc.read(0)
print("Airppm: ", ppm, "ppm")
SDA_PIN =GY30SDA
-- sda pin, GPIO12
----读取GY30光照强度------
SCL_PIN = GY30SCL
-- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read(OSS)
l = bh1750.getlux()
l=string.format("%.2f ", l / 100)
print("lux: ", l, "lx")
dofile("SendDate.lua")
--sendToBigiot(cu,temp,press,ppm) ---------发送数据到贝壳物联云端-----------
-- Written by niwusong
end
end)
--dofile("sayCommand.lua")
--执行命令
end)
--checkin.lua
if r.M == "WELCOME TO BIGIOT" then
--收到连接正常,发送
checkin ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=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"..DEVICEID})
print(ticket)
cu:send(ticket.."\n" )
--发送后将标记置为1
bLive=bLive+1
end)
end
if r.M=="checkinok" then
bOnLine=1
end
***************************************************************************
-- BH1750 (GY-30)module for ESP8266 with nodeMCU
-- BH1750 compatible tested 2017-1-22 --
-- Written by niwusong
--**************************************************************************
local moduleName = ...
local M = {}
_G[moduleName] = M
--I2C slave address of GY-30
local GY_30_address = 0x23
-- i2c interface ID
local id = 0
--LUX local l
--CMD
local CMD = 0x10
local init = false
--Make it more faster
local i2c = i2c function M.init(sda, scl)
i2c.setup(id, sda, scl, i2c.SLOW)
--print("i2c ok..")
init = true
end
local function read_data(ADDR, commands, length)
i2c.start(id)
i2c.address(id, ADDR, i2c.TRANSMITTER)
i2c.write(id, commands)
i2c.stop(id)
i2c.start(id)
i2c.address(id, ADDR,i2c.RECEIVER)
tmr.delay(200000)
c = i2c.read(id, length)
i2c.stop(id)
return c
end
local function read_lux()
dataT = read_data(GY_30_address, CMD, 2)
--Make it more faster
UT = dataT:byte(1) * 256 + dataT:byte(2)
l = (UT*1000/12)
return(l)
end
function M.read()
if (not init) then
print("init() must be called before read.")
else
read_lux()
end
end
function M.getlux()
return l
end
return M
--SendDate.lua
--function sendToBigiot(cu,temp,press,ppm)
if temp==nil
then
temp=0
end
if
press==nil
then
press=0
end
if
ppm==nil
then
ppm=0
end
if
l==nil
then
l=0
end
--------开始上传数据-------------
print("start send data")
--str="{\"M\":\"update\",\"ID\":\""..DEVICEID.."\",\"V\":{\""..TEMPID1.."\":"..temp..",\""..PRESSID2.."\":"..press..",\""..AIRID3.."\":"..ppm.."}}\n"
--print("send data:"..str)
-- cu:send( str.."\n")
local v = {[TEMPID1]=temp,[PRESSID2]=press,[AIRID3]=ppm,[GY30ID5]=l,[DS18B20ID4]=t}
ok3, s3 = pcall(cjson.encode, {M="update",ID=DEVICEID,V=v})
print("send data:"..s3)
cu:send( s3.."\n")
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
--end niwusong QQ:82528190 欢迎交流,共同进步