| 12345678910111213141516171819202122232425262728293031 |
- --- SHT30 温湿度传感器驱动
- -- @module SHT30
- -- @copyright openLuat.com
- require "utils"
- module(..., package.seeall)
- --- 读取SHT30的数据 -->基于I2C_ModBus协议
- -- @number id, 端口号0-2
- -- @return string,string,第一个参数是温度,第二个是湿度
- -- @usage tmp, hum = read()
- function read(id)
- if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then
- log.error("I2C.init is: ", "fail")
- i2c.close(id)
- return
- end
- i2c.send(id, 0x44, {0x2C, 0x06}) -- 读取SHT30传感器数据
- sys.wait(2)
- local data = i2c.recv(id, 0x44, 6)
- i2c.close(id)
- if data == nil or data == 0 then return end
- log.warn("SHT30 HEX data::::::::::::::::", string.toHex(data," "))
- local _, t_H, t_L, h_crc, h_H, h_L, t_crc = pack.unpack(data,'b6') -- 温度2--CRC1--湿度2--CRC1
- if t_H and t_L and h_H and h_L then
- local tmp = ((1750 * ( t_H*256 + t_L) / 65535 - 450))
- local hum = ((1000 * ( h_H*256 + h_L) / 65535)) -- 由于底层没有采用float库,数据放大10倍
- log.warn("SHT30 data:", tmp, hum)
- return tmp, hum
- else return end
- end
|