sht30_app.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. --[[
  2. @module sht30_app
  3. @summary sht30_app应用功能模块
  4. @version 1.0
  5. @date 2025.10.22
  6. @author 沈园园
  7. @usage
  8. 本文件为sht30_app应用功能模块,核心业务逻辑为:
  9. 1、每隔1秒读取一次温湿度数据;
  10. 本文件没有对外接口,直接在main.lua中require "sht30_app"就可以加载运行;
  11. ]]
  12. --加载AirSHT30_1000驱动文件
  13. local air_sht30 = require "AirSHT30_1000"
  14. --每隔1秒读取一次温湿度数据
  15. local function read_sht30_task_func()
  16. --打开sht30硬件
  17. air_sht30.open()
  18. while true do
  19. --读取温湿度数据
  20. local temprature, humidity = air_sht30.read()
  21. --读取成功
  22. if temprature then
  23. -- 打印输出结果(保留2位小数)
  24. log.info("read_sht30_task_func", "temprature", string.format("%.2f ℃", temprature))
  25. log.info("read_sht30_task_func", "humidity", string.format("%.2f %%RH", humidity))
  26. --读取失败
  27. else
  28. log.error("read_sht30_task_func", "read error")
  29. end
  30. --等待1秒
  31. sys.wait(1000)
  32. end
  33. --关闭sht30硬件
  34. air_sht30.close()
  35. end
  36. --创建一个task,并且运行task的主函数read_sht30_task_func
  37. sys.taskInit(read_sht30_task_func)