gpsTask.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. local gps = require"gpsZkw"
  2. local gpsEnable = nvm.get("gpsEnable") or 1
  3. local gpsTimerId
  4. local function setGpsInfo()
  5. local tLocation = gps.getLocation()--获取度格式的经纬度信息
  6. local speed = gps.getSpeed()
  7. log.info("testGps.printGps",
  8. gps.isOpen(),--获取GPS模块是否处于开启状态,gps.isFix()--获取GPS模块是否定位成功,
  9. tLocation.lngType,tLocation.lng,tLocation.latType,tLocation.lat,--打印经纬度
  10. gps.getAltitude(),--获取海拔
  11. speed,--获取速度
  12. gps.getCourse(),--获取方向角
  13. gps.getViewedSateCnt(),--获取可见卫星的个数
  14. gps.getUsedSateCnt())--获取定位使用的卫星个数
  15. if gps.isFix() then
  16. log.info("gpsTask.printGps","location success,close gps ... ")
  17. gpsLocation = tLocation
  18. --断电GPS
  19. pins.setup(pio.P0_18, 1)
  20. pins.setup(pio.P0_3, 0)
  21. --保存定位数据到flash
  22. if gpsEnable == 1 then
  23. local gpsLng = nvm.get("gpsLng") or "0"
  24. if gpsLng ~= gpsLocation.lng then
  25. if not nvm.set("gpsLng",gpsLocation.lng) then
  26. log.error("gpsTask.printGps","nvm.set gpsLng fail! gpsLng:", gpsLocation.lng)
  27. return
  28. end
  29. end
  30. local lngType = nvm.get("lngType") or "0"
  31. if lngType ~= gpsLocation.lngType then
  32. if not nvm.set("lngType",gpsLocation.lngType) then
  33. log.error("gpsTask.printGps","nvm.set lngType fail! lngType:", gpsLocation.lngType)
  34. return
  35. end
  36. end
  37. local gpsLat = nvm.get("gpsLat") or "0"
  38. if gpsLat ~= gpsLocation.lat then
  39. if not nvm.set("gpsLat",gpsLocation.lat) then
  40. log.error("gpsTask.printGps","nvm.set gpsLat fail! gpsLat:", gpsLocation.lat)
  41. return
  42. end
  43. end
  44. local latType = nvm.get("latType") or "0"
  45. if latType ~= gpsLocation.latType then
  46. if not nvm.set("latType",gpsLocation.latType) then
  47. log.error("gpsTask.printGps","nvm.set latType fail! latType:", gpsLocation.latType)
  48. return
  49. end
  50. end
  51. local gpsTime = nvm.get("gpsTime") or 0
  52. if gpsTime ~= os.time() then
  53. if not nvm.set("gpsTime",os.time()) then
  54. log.error("gpsTask.printGps","nvm.set gpsTime fail! gpsTime:", os.time())
  55. return
  56. end
  57. end
  58. sys.publish("pub_gps_location_success_msg",tLocation)
  59. end
  60. end
  61. end
  62. function printGps()
  63. -- body
  64. local tLocation = gps.getLocation()--获取度格式的经纬度信息
  65. local speed = gps.getSpeed()
  66. log.info("testGps.printGps",
  67. gps.isOpen(),--获取GPS模块是否处于开启状态,gps.isFix()--获取GPS模块是否定位成功,
  68. tLocation.lngType,tLocation.lng,tLocation.latType,tLocation.lat,--打印经纬度
  69. gps.getAltitude(),--获取海拔
  70. speed,--获取速度
  71. gps.getCourse(),--获取方向角
  72. gps.getViewedSateCnt(),--获取可见卫星的个数
  73. gps.getUsedSateCnt())--获取定位使用的卫星个数
  74. end
  75. function gpsLocateCb( )
  76. -- body
  77. sys.timerStop(gpsTimerId)
  78. setGpsInfo()
  79. pins.setup(pio.P0_3, 0) --GPS复位
  80. pins.setup(pio.P0_18, 1) --关闭GPS供电
  81. end
  82. gps.setUart(3,9600,8,uart.PAR_NONE,uart.STOP_1)
  83. function openGpsSwitch( )
  84. -- body
  85. if gpsEnable == 1 then
  86. -- body
  87. --中科微GPS
  88. --打开GPS供电
  89. pins.setup(pio.P0_18, 0)
  90. --拉高GPS工作,拉低GPS复位
  91. pins.setup(pio.P0_3, 1)
  92. if not gps.isOpen() then
  93. -- body
  94. gpsTimerId = sys.timerLoopStart(printGps,4000)
  95. gps.open(gps.TIMERORSUC,{tag="GPS_TAG",val=120,cb=gpsLocateCb})
  96. end
  97. end
  98. end
  99. local gpsInterval = nvm.get("gpsInterval") or 1800
  100. sys.timerLoopStart(openGpsSwitch, gpsInterval*1000)
  101. sys.taskInit(function()
  102. sys.wait(5000)
  103. openGpsSwitch()
  104. while true do
  105. if socket.isReady() then --连上网再开始运行
  106. local result = sys.waitUntil("pub_gps_location_success_msg")
  107. if socket.isReady() and result == true then
  108. -- body
  109. local heartBeatData =modbusTT.getHeartbeatContent()
  110. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/Heart",["data"]=heartBeatData}))
  111. end
  112. else --没连上网别忘了延时!不然会陷入while true死循环,导致模块无法运行其他代码
  113. -- log.info("socket not ready")
  114. sys.wait(5000) --等待5秒
  115. end
  116. end
  117. end)