SI7021.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module(..., package.seeall)
  2. --- 模块功能:SI7021温湿度传感器
  3. -- @author openLuat
  4. -- @module SI7021
  5. -- @license MIT
  6. -- @copyright openLuat
  7. -- @release 2021.6.2
  8. sys.taskInit(
  9. function()
  10. sys.wait(6000)
  11. log.info("test start")
  12. i2c.setup(2, 100000, 0x40)
  13. while true do
  14. local temp, humi
  15. i2c.send(2, 0x40, 0xF3)
  16. sys.wait(200)
  17. local rawTemp = i2c.recv(2, 0x40, 3)
  18. local temp_h, temp_l = string.byte(rawTemp ,1), string.byte(rawTemp ,2)
  19. if temp_h and temp_l then
  20. temp = temp_h * 256 + temp_l
  21. temp = ((175.72 * temp) / 65536) - 46.85
  22. else
  23. log.info("没温度")
  24. end
  25. sys.wait(200)
  26. i2c.send(2, 0x40, 0xF5)
  27. sys.wait(200)
  28. local rawHumi = i2c.recv(2, 0x40, 3)
  29. local humi_h, humi_l = rawHumi:byte(1), rawHumi:byte(2)
  30. if humi_h and humi_l then
  31. humi = humi_h * 256 + humi_l
  32. humi = ((125 * humi) / 65536) - 6
  33. else
  34. log.info("没湿度")
  35. end
  36. if temp and humi then
  37. log.info("测量数据","温度:", temp - temp % 0.01 .. "°C", "湿度:", humi - humi % 0.01 .. "%RH")
  38. else
  39. log.info("没测量数据")
  40. end
  41. sys.wait(200)
  42. --i2c.close(2)
  43. end
  44. end
  45. )