AHT10.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. --- 模块功能:AHT10功能测试.
  2. -- @module i2c
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2021.8.10
  7. local function i2c_open(id)
  8. if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then
  9. log.error("I2C.init is: ", "fail")
  10. end
  11. end
  12. function readAHT10()
  13. local id = 2
  14. i2c_open(id)
  15. --数值查询,发送指令0xAC, 0x22, 0x00,通过iic发送完毕之后,AHT10返回的数值是6个字节的数组
  16. i2c.send(id, 0x38, {0xAC, 0x22, 0x00})
  17. --等待75毫秒以上
  18. --rtos.sleep(80)
  19. --1[状态位],2[湿度第一段],3[湿度第二段],4前四位[湿度第三段],4后四位[温度第一段],5[温度第二段],6[温度第三段]
  20. local data = i2c.recv(id, 0x38, 6)
  21. log.info("i2cdata", #data, data:toHex())
  22. i2c.close(id)
  23. if #data == 6 then
  24. local _, _, data2, data3, data4, data5, data6 = pack.unpack(data, "b6")
  25. local hum = bit.bor(bit.bor(bit.lshift(data2, 12), bit.lshift(data3, 4)), bit.rshift(data4, 4))/ 1048576 * 10000
  26. log.info("hum", hum/100 )
  27. local tmp = bit.bor(bit.bor(bit.lshift(bit.band(data4, 0x0f), 16), bit.lshift(data5, 8)), data6) / 1048576 * 20000 - 5000
  28. log.info("tmp", tmp/100)
  29. --前面将数据放大了100倍,方便没有float的固件保留精度,在使用float固件时直接缩小100倍还原
  30. --return tmp, hum
  31. return tmp/100, hum/100
  32. else
  33. return 0, 0
  34. end
  35. end
  36. sys.timerLoopStart(readAHT10,2000)