main.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "irtu_gnss"
  3. VERSION = "1.0.1"
  4. --[[
  5. 这个demo名字含irtu,实际上没有使用iRTU.
  6. 网站是: https://gps.nutz.cn/ 连上之后,输入IMEI并回车,就能看到当前坐标
  7. 小程序的名字: iRTU寻物 打开后扫码模块上的二维码,就能显示当前位置和历史轨迹
  8. 服务器源码: https://gitee.com/wendal/irtu-gps 里面的硬件和配置描述是Air800的,可以无视.
  9. 本demo需要Air780EG及V1003固件, 2023-1-11之后编译的版本
  10. 所需要的库文件,在 script/libs里面, 全部加入就可以了
  11. ]]
  12. -- sys库是标配
  13. local sys = require("sys")
  14. local sysplus = require("sysplus")
  15. local libnet = require("libnet")
  16. local gps_uart_id = 2
  17. -- libgnss库初始化
  18. libgnss.clear() -- 清空数据,兼初始化
  19. -- GNSS串口初始化
  20. uart.setup(gps_uart_id, 115200)
  21. function exec_agnss()
  22. local url = "http://download.openluat.com/9501-xingli/HXXT_GPS_BDS_AGNSS_DATA.dat"
  23. if http then
  24. -- AGNSS 已调通
  25. while 1 do
  26. local code, headers, body = http.request("GET", url).wait()
  27. log.info("gnss", "AGNSS", code, body and #body or 0)
  28. if code == 200 and body and #body > 1024 then
  29. for offset = 1, #body, 512 do
  30. log.info("gnss", "AGNSS", "write >>>", #body:sub(offset, offset + 511))
  31. uart.write(gps_uart_id, body:sub(offset, offset + 511))
  32. sys.wait(100) -- 等100ms反而更成功
  33. end
  34. -- sys.waitUntil("UART2_SEND", 1000)
  35. io.writeFile("/6228.bin", body)
  36. break
  37. end
  38. sys.wait(60 * 1000)
  39. end
  40. end
  41. sys.wait(20)
  42. -- "$AIDTIME,year,month,day,hour,minute,second,millisecond"
  43. local date = os.date("!*t")
  44. if date.year > 2022 then
  45. local str = string.format("$AIDTIME,%d,%d,%d,%d,%d,%d,000", date["year"], date["month"], date["day"],
  46. date["hour"], date["min"], date["sec"])
  47. log.info("gnss", str)
  48. uart.write(gps_uart_id, str .. "\r\n")
  49. sys.wait(20)
  50. end
  51. -- 读取之前的位置信息
  52. local gnssloc = io.readFile("/gnssloc")
  53. if gnssloc then
  54. str = "$AIDPOS," .. gnssloc
  55. log.info("POS", str)
  56. uart.write(gps_uart_id, str .. "\r\n")
  57. str = nil
  58. gnssloc = nil
  59. else
  60. uart.write(gps_uart_id, "$AIDPOS,3432.70,N,10885.25,E,1.0\r\n")
  61. end
  62. end
  63. sys.taskInit(function()
  64. -- Air780EG默认波特率是115200
  65. log.info("GPS", "start")
  66. pm.power(pm.GPS, true)
  67. -- 调试日志,可选
  68. libgnss.debug(true)
  69. sys.wait(200) -- GPNSS芯片启动需要时间,大概150ms
  70. -- 增加显示的语句,可选
  71. uart.write(gps_uart_id, "$CFGMSG,0,1,1\r\n") -- GLL
  72. sys.wait(20)
  73. uart.write(gps_uart_id, "$CFGMSG,0,5,1\r\n") -- VTG
  74. sys.wait(20)
  75. uart.write(gps_uart_id, "$CFGMSG,0,6,1\r\n") -- ZDA
  76. sys.wait(20)
  77. -- 定位成功后,使用GNSS时间设置RTC
  78. libgnss.rtcAuto(true)
  79. -- 绑定uart,底层自动处理GNSS数据
  80. libgnss.bind(gps_uart_id)
  81. exec_agnss()
  82. end)
  83. -- 单纯定时打印一下定位信息
  84. sys.taskInit(function()
  85. while 1 do
  86. sys.wait(5000)
  87. log.info("RMC", json.encode(libgnss.getRmc(4) or {}))
  88. end
  89. end)
  90. -- 订阅GNSS状态编码
  91. sys.subscribe("GNSS_STATE", function(event, ticks)
  92. -- event取值有
  93. -- FIXED 定位成功
  94. -- LOSE 定位丢失
  95. -- ticks是事件发生的时间,一般可以忽略
  96. log.info("gnss", "state", event, ticks)
  97. if event == "FIXED" then
  98. local locStr = libgnss.locStr()
  99. log.info("gnss", "locStr", locStr)
  100. if locStr then
  101. -- 存入文件,方便下次AGNSS快速定位
  102. io.writeFile("/gnssloc", locStr)
  103. end
  104. end
  105. end)
  106. -- 对接服务器
  107. local taskName = "gnsstask"
  108. function gnsstask()
  109. local tx_buff = zbuff.create(1024)
  110. local rx_buff = zbuff.create(1024)
  111. local netc
  112. local result, param, succ
  113. local host, port = "gps.nutz.cn", 19002
  114. local netc = socket.create(nil, taskName)
  115. -- socket.debug(netc, true)
  116. socket.config(netc, nil, nil, nil)
  117. while 1 do
  118. result = libnet.waitLink(taskName, 0, netc)
  119. result = libnet.connect(taskName, 15000, netc, host, port)
  120. local jdata = {
  121. imei = mobile.imei(),
  122. iccid = mobile.iccid()
  123. }
  124. if result then
  125. local data = json.encode(jdata)
  126. log.info("服务器连上了", "上报注册注册信息", data)
  127. libnet.tx(taskName, 0, netc, data)
  128. end
  129. while result do
  130. succ, param = socket.rx(netc, rx_buff)
  131. if not succ then
  132. log.info("服务器断开了", succ, param, host, port)
  133. break
  134. end
  135. if rx_buff:used() > 0 then
  136. log.info("收到服务器数据,长度", rx_buff:used())
  137. rx_buff:del()
  138. end
  139. if rx_buff:len() > 1024 then
  140. rx_buff:resize(1024)
  141. end
  142. log.info("sys", rtos.meminfo("sys"))
  143. result, param = libnet.wait(taskName, 1000, netc)
  144. if not result then
  145. log.info("服务器断开了", result, param)
  146. break
  147. end
  148. -- 发送数据包
  149. local msg = {libgnss.isFix(), os.time()}
  150. local rmc = libgnss.getRmc(1)
  151. local gsa = libgnss.getGsa()
  152. local vtg = libgnss.getVtg()
  153. table.insert(msg, rmc.lng)
  154. table.insert(msg, rmc.lat)
  155. local gll = libgnss.getGll()
  156. table.insert(msg, 0) -- altitude
  157. table.insert(msg, 0) -- azimuth
  158. table.insert(msg, (vtg and vtg.speed_kph) and vtg.speed_kph or 0) -- speed
  159. table.insert(msg, 0) -- sateCno
  160. table.insert(msg, 0) -- sateCnt
  161. --jdata.msg = msg
  162. local data = json.encode({msg=msg})
  163. log.info("report", data)
  164. if data then
  165. libnet.tx(taskName, 0, netc, data)
  166. end
  167. end
  168. d1Online = false
  169. libnet.close(taskName, 5000, netc)
  170. log.info("sys", rtos.meminfo("sys"))
  171. sys.wait(1000)
  172. end
  173. end
  174. local function netCB(msg)
  175. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  176. end
  177. sysplus.taskInitEx(gnsstask, "gnsstask", netCB)
  178. sys.taskInit(function()
  179. while 1 do
  180. sys.wait(3600 * 1000) -- 一小时检查一次
  181. local fixed, time_fixed = libgnss.isFix()
  182. if not fixed then
  183. exec_agnss()
  184. end
  185. end
  186. end)
  187. -- 用户代码已结束---------------------------------------------
  188. -- 结尾总是这一句
  189. sys.run()
  190. -- sys.run()之后后面不要加任何语句!!!!!