SHT30.lua 1.1 KB

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