节流计划旨在降低物联网产品的硬件成本,本专栏将在2021年持续更新。
节流计划不采用STM32/STC/PIC等传统MCU,而是充分挖掘AIR724UG芯片除4G通信外的其他功能,包含但不限于GPIO、TTS、LCD、OTA、FLASH、ADC,KEY,UART,I2C、SPI等。
版权所有,谢绝转载,侵权必究。承接相关物联网项目合作,邮箱:realiot@163.com。
官方例程
? ? ? ? 1 AHT10数据读取
sys.taskInit(
function()
local i2cid = 2
local i2cslaveaddr = 0x38
if i2c.setup(i2cid, i2c.SLOW) ~= i2c.SLOW then
log.error("AHT10", "i2c.setup fail")
return
end
while true do
if i2c.send(i2cid, i2cslaveaddr, {0xAC, 0x33, 0x00}) ~= 3 then
log.error("AHT10", "i2c.send fail")
return
end
sys.wait(100)
qryResult = i2c.recv(i2cid, i2cslaveaddr, 6)
if (nil == qryResult) or (6 ~= #qryResult) then
log.error("ATH10", "i2c.recv fail.")
return
end
log.info("ATH10", "recv", qryResult:toHex())
sys.wait(2000)
end
end
)
? ? ? ? 2 BH1750数据读取
local i2cid = 2 --i2cid
local BH1750_ADDRESS_AD0_LOW = 0x23 -- address pin low (GND), default for InvenSense evaluation board
local BH1750_ADDRESS_AD0_HIGH = 0x24 -- address pin high (VCC)
local i2cslaveaddr = BH1750_ADDRESS_AD0_LOW
-- bh1750 registers define
local BH1750_POWER_DOWN = 0x00 -- power down
local BH1750_POWER_ON = 0x01 -- power on
local BH1750_RESET = 0x07 -- reset
local BH1750_CON_H_RES_MODE = 0x10 -- Continuously H-Resolution Mode
local BH1750_CON_H_RES_MODE2 = 0x11 -- Continuously H-Resolution Mode2
local BH1750_CON_L_RES_MODE = 0x13 -- Continuously L-Resolution Mode
local BH1750_ONE_H_RES_MODE = 0x20 -- One Time H-Resolution Mode
local BH1750_ONE_H_RES_MODE2 = 0x21 -- One Time H-Resolution Mode2
local BH1750_ONE_L_RES_MODE = 0x23 -- One Time L-Resolution Mode
local function i2c_send(data)
i2c.send(i2cid, i2cslaveaddr, data)
end
local function i2c_recv(num)
local revData = i2c.recv(i2cid, i2cslaveaddr, num)
return revData
end
local function bh1750_power_on()
i2c_send(BH1750_POWER_ON)
end
local function bh1750_power_down()
i2c_send(BH1750_POWER_DOWN)
end
local function bh1750_set_measure_mode(mode, time)
i2c_send(BH1750_RESET)
i2c_send(mode)
sys.wait(time)
end
local function bh1750_read_light()
bh1750_set_measure_mode(BH1750_CON_H_RES_MODE2, 180)
local _, light = pack.unpack(i2c_recv(2), ">h")
light = light / 1.2
return light
end
local function bh1750_test()
sys.wait(4000)
if i2c.setup(i2cid, i2c.SLOW) ~= i2c.SLOW then
log.error("I2c.init", "fail")
return
end
bh1750_power_on()
sys.wait(180)
while true do
log.info("bh1750_read_light", bh1750_read_light() * 10)
sys.wait(1000)
end
end
sys.taskInit(bh1750_test)
实测效果
? ? ? ? 1、官方代码可以使用
? ? ? ? 2、如果AIR724UG上电时传感器未接入,则任务会return退出,退出任务后即使再接上传感器,读取任务也无法继续运行了。
改进方案
? ? ? ?1、 将ATH10和BH1750的读写操作放到一个文件里完成。分时操作不同的传感器,避免两个任务同时运行时在某个时间段发送冲突。如此即使没有检测到传感器,整个程序能够继续运行,不会报错或重启。待传感器接入恢复正常后,仍然能够正常读取传感器的数据。
? ? ? ? 2、删除出现错误后退出任务的return语句。
? ? ? ? 3、I2C的初始化只能运行一次,二次初始化会造成运行错误。
sys.taskInit(
function()
sys.wait(5000)
if I2C_OPENED == 0 then
if i2c.setup(i2cid, i2c.SLOW) ~= i2c.SLOW then
log.error("I2C_INIT", "i2c.setup fail")
end
I2C_OPENED = 1
log.info("I2C", "I2C_INIT")
end
while true do
if i2c_slave_sta < 2 then
i2c_slave_sta = i2c_slave_sta + 1
else
i2c_slave_sta = 0
end
---[[
------------------------------------AHT10
local aht10_addr = 0x38
if i2c_slave_sta == 1 then
if i2c.send(i2cid, aht10_addr, {0xAC, 0x33, 0x00}) ~= 3 then
log.error("AHT10", "i2c.send fail")
end
sys.wait(200)
aht10_read = i2c.recv(i2cid, aht10_addr, 6)
if (nil == aht10_read) or (6 ~= #aht10_read) then
log.error("ATH10", "i2c.recv fail.")
else --正确获取返回值
--log.info("ATH10", "recv", aht10_read:toHex())
local humi = (aht10_read:byte(2) * 65536 + aht10_read:byte(3) * 256 + aht10_read:byte(4))
AHT10_HUMI = humi * 100 / 16 / 1024 / 1024
local temp =
(bit_and(aht10_read:byte(4), 0x0F) * 65536 + aht10_read:byte(5) * 256 + aht10_read:byte(6))
AHT10_TEMP = temp * 200 / 1024 / 1024 - 50
log.info("ATH10", "HUMI:" .. myfloor(AHT10_HUMI, 2) .. " TEMP:" .. myfloor(AHT10_TEMP, 2))
end
end
------------------------------------AHT10
--]]
---[[
-----------------------------------BH1750
if i2c_slave_sta == 2 then
local bh1750_addr_low = 0x23 -- address pin low (GND), default for InvenSense evaluation board
local bh1750_addr_high = 0x24 -- address pin high (VCC)
local bh1750_addr = bh1750_addr_high
-- bh1750 registers define
local BH1750_POWER_DOWN = 0x00 -- power down
local BH1750_POWER_ON = 0x01 -- power on
local BH1750_RESET = 0x07 -- reset
local BH1750_CON_H_RES_MODE = 0x10 -- Continuously H-Resolution Mode
local BH1750_CON_H_RES_MODE2 = 0x11 -- Continuously H-Resolution Mode2
local BH1750_CON_L_RES_MODE = 0x13 -- Continuously L-Resolution Mode
local BH1750_ONE_H_RES_MODE = 0x20 -- One Time H-Resolution Mode
local BH1750_ONE_H_RES_MODE2 = 0x21 -- One Time H-Resolution Mode2
local BH1750_ONE_L_RES_MODE = 0x23 -- One Time L-Resolution Mode
i2c.send(i2cid, bh1750_addr, BH1750_POWER_ON)
i2c.send(i2cid, bh1750_addr, BH1750_RESET)
i2c.send(i2cid, bh1750_addr, BH1750_CON_H_RES_MODE2)
sys.wait(200)
local bh1750_read = i2c.recv(i2cid, bh1750_addr, 2)
if (nil == bh1750_read) or (2 ~= #bh1750_read) then
log.error("BH1750", "i2c.recv fail.")
else --正确获取返回值
--log.info("BH1750", "recv", bh1750_read:toHex())
local _, light = pack.unpack(bh1750_read, ">h")
BH1750_LIGHT = light / 1.2
end
end
------------------------------BH1750
--]]
sys.wait(5000)
end
end
)
--]]
|