123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- local gps = require"gpsZkw"
- local gpsEnable = nvm.get("gpsEnable") or 1
- local gpsTimerId
- local function setGpsInfo()
- local tLocation = gps.getLocation()--获取度格式的经纬度信息
- local speed = gps.getSpeed()
- log.info("testGps.printGps",
- gps.isOpen(),--获取GPS模块是否处于开启状态,gps.isFix()--获取GPS模块是否定位成功,
- tLocation.lngType,tLocation.lng,tLocation.latType,tLocation.lat,--打印经纬度
- gps.getAltitude(),--获取海拔
- speed,--获取速度
- gps.getCourse(),--获取方向角
- gps.getViewedSateCnt(),--获取可见卫星的个数
- gps.getUsedSateCnt())--获取定位使用的卫星个数
- if gps.isFix() then
- log.info("gpsTask.printGps","location success,close gps ... ")
- gpsLocation = tLocation
- --断电GPS
- pins.setup(pio.P0_18, 1)
- pins.setup(pio.P0_3, 0)
- --保存定位数据到flash
- if gpsEnable == 1 then
- local gpsLng = nvm.get("gpsLng") or "0"
- if gpsLng ~= gpsLocation.lng then
- if not nvm.set("gpsLng",gpsLocation.lng) then
- log.error("gpsTask.printGps","nvm.set gpsLng fail! gpsLng:", gpsLocation.lng)
- return
- end
- end
- local lngType = nvm.get("lngType") or "0"
- if lngType ~= gpsLocation.lngType then
- if not nvm.set("lngType",gpsLocation.lngType) then
- log.error("gpsTask.printGps","nvm.set lngType fail! lngType:", gpsLocation.lngType)
- return
- end
- end
- local gpsLat = nvm.get("gpsLat") or "0"
- if gpsLat ~= gpsLocation.lat then
- if not nvm.set("gpsLat",gpsLocation.lat) then
- log.error("gpsTask.printGps","nvm.set gpsLat fail! gpsLat:", gpsLocation.lat)
- return
- end
- end
- local latType = nvm.get("latType") or "0"
- if latType ~= gpsLocation.latType then
- if not nvm.set("latType",gpsLocation.latType) then
- log.error("gpsTask.printGps","nvm.set latType fail! latType:", gpsLocation.latType)
- return
- end
- end
- local gpsTime = nvm.get("gpsTime") or 0
- if gpsTime ~= os.time() then
- if not nvm.set("gpsTime",os.time()) then
- log.error("gpsTask.printGps","nvm.set gpsTime fail! gpsTime:", os.time())
- return
- end
- end
- sys.publish("pub_gps_location_success_msg",tLocation)
- end
- end
- end
- function printGps()
- -- body
- local tLocation = gps.getLocation()--获取度格式的经纬度信息
- local speed = gps.getSpeed()
- log.info("testGps.printGps",
- gps.isOpen(),--获取GPS模块是否处于开启状态,gps.isFix()--获取GPS模块是否定位成功,
- tLocation.lngType,tLocation.lng,tLocation.latType,tLocation.lat,--打印经纬度
- gps.getAltitude(),--获取海拔
- speed,--获取速度
- gps.getCourse(),--获取方向角
- gps.getViewedSateCnt(),--获取可见卫星的个数
- gps.getUsedSateCnt())--获取定位使用的卫星个数
- end
- function gpsLocateCb( )
- -- body
- sys.timerStop(gpsTimerId)
- setGpsInfo()
- pins.setup(pio.P0_3, 0) --GPS复位
- pins.setup(pio.P0_18, 1) --关闭GPS供电
- end
- gps.setUart(3,9600,8,uart.PAR_NONE,uart.STOP_1)
- function openGpsSwitch( )
- -- body
- if gpsEnable == 1 then
- -- body
- --中科微GPS
- --打开GPS供电
- pins.setup(pio.P0_18, 0)
- --拉高GPS工作,拉低GPS复位
- pins.setup(pio.P0_3, 1)
- if not gps.isOpen() then
- -- body
- gpsTimerId = sys.timerLoopStart(printGps,4000)
- gps.open(gps.TIMERORSUC,{tag="GPS_TAG",val=120,cb=gpsLocateCb})
- end
- end
- end
- local gpsInterval = nvm.get("gpsInterval") or 1800
- sys.timerLoopStart(openGpsSwitch, gpsInterval*1000)
- sys.taskInit(function()
- sys.wait(5000)
- openGpsSwitch()
- while true do
- if socket.isReady() then --连上网再开始运行
- local result = sys.waitUntil("pub_gps_location_success_msg")
- if socket.isReady() and result == true then
- -- body
- local heartBeatData =modbusTT.getHeartbeatContent()
- sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/Heart",["data"]=heartBeatData}))
- end
- else --没连上网别忘了延时!不然会陷入while true死循环,导致模块无法运行其他代码
- -- log.info("socket not ready")
- sys.wait(5000) --等待5秒
- end
- end
- end)
|