sht30_app.lua 931 B

1234567891011121314151617181920212223242526272829303132333435
  1. --加载AirSHT30_1000驱动文件
  2. local air_sht30 = require "AirSHT30_1000"
  3. --每隔1秒读取一次温湿度数据
  4. local function read_sht30_task_func()
  5. --打开sht30硬件
  6. air_sht30.open(1)
  7. while true do
  8. --读取温湿度数据
  9. local temprature, humidity = air_sht30.read()
  10. --读取成功
  11. if temprature then
  12. -- 打印输出结果(保留2位小数)
  13. log.info("read_sht30_task_func", "temprature", string.format("%.2f ℃", temprature))
  14. log.info("read_sht30_task_func", "humidity", string.format("%.2f %%RH", humidity))
  15. --读取失败
  16. else
  17. log.error("read_sht30_task_func", "read error")
  18. end
  19. --等待1秒
  20. sys.wait(1000)
  21. end
  22. --关闭sht30硬件
  23. air_sht30.close()
  24. end
  25. --创建一个task,并且运行task的主函数read_sht30_task_func
  26. sys.taskInit(read_sht30_task_func)