gnss.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. sys.taskInit(function()
  2. log.info("GPS", "start")
  3. -- gnss的复位
  4. local gpsRst = gpio.setup(27, 1)
  5. local uartId = 2
  6. libgnss.clear() -- 清空数据,兼初始化
  7. uart.setup(uartId, 115200)
  8. sys.wait(200) -- GPNSS芯片启动需要时间
  9. -- 调试日志,可选
  10. --libgnss.debug(true)
  11. libgnss.bind(2)
  12. end)
  13. -- 订阅GNSS状态编码
  14. sys.subscribe("GNSS_STATE", function(event, ticks)
  15. -- event取值有
  16. -- FIXED 定位成功
  17. -- LOSE 定位丢失
  18. -- ticks是事件发生的时间,一般可以忽略
  19. log.info("gnss", "state", event, ticks)
  20. if event == "FIXED" then
  21. local locStr = libgnss.locStr()
  22. log.info("gnss", "locStr", locStr)
  23. elseif event == "LOSE" then
  24. log.info("gnss", "no fix")
  25. end
  26. end)
  27. sys.timerLoopStart(function ()
  28. local isFixed = libgnss.isFix()
  29. attributes.set("isFixed", isFixed and "已定位" or "获取中")
  30. if isFixed then
  31. local loc = libgnss.getRmc(2)
  32. attributes.set("lat", tostring(loc.lat))
  33. attributes.set("lng", tostring(loc.lng))
  34. attributes.set("location", {
  35. lat = loc.lat,
  36. lng = loc.lng,
  37. })
  38. else
  39. attributes.set("lat", "无数据")
  40. attributes.set("lng", "无数据")
  41. end
  42. end,3000)