scd.lua 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. ---scd--@module testSocket--@author 稀饭放姜--@license MIT--@copyright hnyfkj.com--@release 2017.9.27
  2. require "socket"
  3. require "misc"
  4. require "led"
  5. require "mqtt"
  6. require "lbsLoc"
  7. require "pins"
  8. require "autoGPS" -- utoGPS功能模块只能配合Air820使用;如果不是这款模块,不要打开agps功能
  9. require "ril"
  10. require "Update"
  11. require "sim"
  12. ------------------------------
  13. require "config" --默认参数保存
  14. require "nvm"
  15. module(..., package.seeall)
  16. -- 本地存储文件
  17. local DEVICE_CONF = "/device.conf"
  18. local SERVER_CONF = "/server.conf"
  19. local DATTIM_CONF = "/dat_f.conf"
  20. local DEV_REBOOT = "/rebootmes.txt"
  21. local PRARM_CONF = "/prarm.conf"
  22. local CONTROL_CONF = "/control.txt"
  23. local THIRD_CONF = "/thirdconf.conf" --第三方服务器数据保存
  24. local SHT30_DATA = "/sht30data.conf" --温湿度参数保存
  25. local GPS_CONF = "/gps.conf" --保存GPS定位数据
  26. -- 上报json数据的基础表
  27. local message = {cmd = "", ext = {}}
  28. -- 上报遗言的json表
  29. local willmsg = {cmd = "offline", ext = {imei = ""}}
  30. -- 温湿度天气接口的值
  31. local temp, hum
  32. --------------------------MQTT定时数据上报----------------------------
  33. local mqtt_status = "true"
  34. local mqtt_status_num = 0
  35. local timeout = 1
  36. local updata_freq = 0
  37. ---------------------------------------------------------------------
  38. --------------------------物联网模块重启--------------------------
  39. -- 重启标志
  40. local rebootstate
  41. -- 重启次数
  42. local rebootcnt
  43. -- 重启信息
  44. local rebootinfo = 1 --1:正常 2:socket重连 3:MQTT重连
  45. ----------------------------------------------------------------
  46. -- 清虫时间间隔
  47. local dat_f = 10
  48. -- 上报数据时间间隔
  49. local clt_t = 30
  50. local wsflag = 0
  51. local worktim = 0
  52. -- 光控模式状态标志位
  53. local clps = 0
  54. --雨控即时上报标志位
  55. local rps_x = 0
  56. local rps_y = 0
  57. ----------------------主板远程升级交互参数----------
  58. --置位BootLoader交互下发标志位 --全局变量定义参数
  59. updateflag = 0 --BootLoader进入升级模式标志位
  60. BootLoader_flag = 0 --BootLoader交互下发标志位
  61. upgrade_again =0 --STM32固件升级再次升级标志位
  62. bootend = 0 --STM32固件升级完成标志位
  63. local uart_flag = 0 --串口通讯交互心跳标志位
  64. stm8_update_sw = 0 --主板升级一键测试防误触
  65. -------------------------------------------------
  66. ---------------------MQTT1----------------------
  67. -- 检测是否分配到ip次数
  68. local err_num = 0
  69. -- TCP重连次数
  70. local tcp_cont = 0
  71. -- TCP连接正常的情况下,MQTT重订阅次数
  72. local sub_cont = 0
  73. -- mqtt1协程守护变量
  74. local mqtt1_cont = 1 * 60 * 60 -- 超时时间1小时
  75. ----------------------MQTT2---------------------
  76. -- 检测是否分配到ip次数
  77. local err2_num = 0
  78. -- TCP重连次数
  79. local tcp2_cont = 0
  80. -- mqtt2协程守护变量
  81. local mqtt2_cont = 1 * 60 * 60 -- 超时时间1小时
  82. ----------------------------------------------
  83. -- 时控参数配置
  84. local timcontrl = {
  85. type = "",
  86. timctrl = 0, --时控开关 0-关 1开 默认:时控关闭
  87. st = 20, --时控开始时间 默认:时间控制从半夜20点开始
  88. et = 2 --时控结束时间 默认:时间控制到次日2点结束
  89. }
  90. -- 配置参数保存基础表
  91. local paramtable = {
  92. cmd = "paramconf",
  93. ext = {
  94. power = {ds = 1}, -- 配置开关机状态
  95. work_tim = {timctrl = 0, st = 20, et = 2}, -- 配置定时控制模式
  96. data_tim = {dat_f = 10}, -- 配置数据上报时间间隔
  97. clt_tim = {clt_t = 30}, -- 配置清虫时间间隔
  98. -- location = {lat = "", lng = "", type = 0} -- 配置手动定位
  99. }
  100. }
  101. -- -------------------------定位-------------------------
  102. -- LBS经纬度
  103. local lbs_loc_lat
  104. local lbs_loc_lng
  105. -- GPS经纬度
  106. local gps_loc_lat
  107. local gps_loc_lng
  108. -- 定位状态
  109. local gps_status = 0 -- 初始值0
  110. local locat_cnt = 0
  111. local gps,agps
  112. local uartId=3
  113. -- GPS定位次数(处理GPS模块返回数据次数)
  114. local gps_cont = 0
  115. -- 手动定位数据调度基础表 BD-09(百度坐标)-->不用坐标转换
  116. local manual_location = {lat = "0", lng = "0", type = "0"}
  117. -- ------------------------------------------------------
  118. -- MQTT1服务器配置表
  119. serverconf = {
  120. imei = "", -- 唯一ID
  121. ip = "8.136.98.49",
  122. port = 61883, -- MQTT服务端口
  123. sub = "/yfkj/xycb/s2c/", -- 服务器到客户端 -- 注意pub和sub后带" /" ,以便设备自动拼接imei
  124. pub = "/yfkj/xycb/c2s/", -- 客户端到服务器
  125. lastwill = "/yfkj/xycb/offline/", -- 离线消息主题(部分)-->设备异常中断时用于mqtt服务器发布该设备的遗愿消息,个人网站客户端需订阅
  126. sub1 = "yfkj/xycb/s2c/",
  127. pub1 = "yfkj/xycb/c2s/",
  128. lastwill1 = "yfkj/xycb/offline/",
  129. keepalive = 60, -- 心跳保持时间
  130. uid = "user", -- 用户名
  131. pwd = "f335bf402c655ee5fd2b5300905124e", -- 密码
  132. qos = 0
  133. }
  134. -- MQTT2服务配置表,用于设备对接
  135. thirdserverconf = {
  136. imei = "", -- 唯一ID
  137. ip = "",
  138. port = 1883,
  139. sub = "/yfkj/xycb/s2c/",
  140. pub = "/yfkj/xycb/c2s/",
  141. keepalive = 60,
  142. lastwill = "/yfkj/xycb/offline/",
  143. uid = "",
  144. pwd = "",
  145. qos = 0
  146. }
  147. -- 设备运行上报下发参数表
  148. local paramconf = {
  149. imei = "", -- 唯一ID
  150. tt = 0, -- 默认光控定时模式下定时时常0小时
  151. vsunrise = 6000, -- 日出电压
  152. vsunset = 1500, -- 日落电压
  153. lcm = 0, -- 光控类型 0太阳能板光控 1光敏二极管光控
  154. vstart = 12000, -- 首次启动电压
  155. uvon = 11500, -- 电池欠压锁定电压
  156. uvoff = 12500, -- 电池欠压解锁电压
  157. vrecharge = 12800, -- 再次启动充电动作的电压
  158. vfloat = 13600, -- 电池浮充电压
  159. vcutof = 14400, -- 电池充电截至电压
  160. overtemp = 700, -- 高温保护阀值
  161. lowtemp = 100, -- 低温保护阀值
  162. unovert = 500, -- 高温解锁阀值
  163. unlowt = 150, -- 低温解锁阀值
  164. stamp = "20180301211805" -- 时间戳
  165. }
  166. --主板参数配置
  167. scdParam = {
  168. cmd = "Set_scdparam",
  169. ext = {
  170. lcm = 0, -- 光控类型 0太阳能板光控 1光敏二极管光控
  171. vsunrise = 1500, --日落电压
  172. vsunset = 6000, --日出电压
  173. vcutof = 14400, --最高充电电压
  174. vfloat = 13600, --恒压触发电压(浮充/恒压)
  175. vrecharge = 12800, --充电回滞电压
  176. uvstart = 12000, --开机电压
  177. uvoff = 12500, --放电迟滞电压
  178. uvon = 11500, --过放保护电压
  179. overtemp = 700, --高温保护电压
  180. lowtemp = 100, --低温保护电压
  181. unovert = 500, --高温解除电压
  182. unlowt = 150 --低温解除电压
  183. }
  184. }
  185. result_crc = {
  186. ds = 1, --设备开启状态 1:开 0:关
  187. tt=0, -- 默认光控定时模式下定时时常0小时
  188. ws = 0, -- 工作状态 0 待机,1工作,2充电
  189. lps = 0, --光控状态 1:0
  190. rps = 0, --雨控保护状态 1:0
  191. tps = 0, --温度保护状态 1:0
  192. dps = 0, --倾倒保护状态 1:0
  193. vps = 0, --电压状态
  194. bt = 250, --主板温度
  195. hwcnt = 0, -- 红外计数
  196. kcnt = 0, --高压计数
  197. cv = 0, --充电电压 xx.x
  198. bv = 12000, --电池电压 xx.x
  199. ver = "", --主板版本号
  200. }
  201. result = {
  202. ds = 1, --设备开启状态 1:开 0:关
  203. tt=0, -- 默认光控定时模式下定时时常0小时
  204. ws = 0, -- 工作状态 0 待机,1工作,2充电
  205. lps = 0, --光控状态 1:0
  206. rps = 0, --雨控保护状态 1:0
  207. tps = 0, --温度保护状态 1:0
  208. dps = 0, --倾倒保护状态 1:0
  209. vps = 0, --电压状态
  210. bt = 250, --主板温度
  211. hwcnt = 0, -- 红外计数
  212. kcnt = 0, --高压计数
  213. cv = 0, --充电电压 xx.x
  214. bv = 12000, --电池电压 xx.x
  215. ver = "", --主板版本号
  216. }
  217. --- 上传预警信息
  218. -- t:type
  219. -- s:status
  220. function upwarn(t,s)
  221. log.warn("------------------------t,:",t)
  222. log.warn("------------------------s,:",s)
  223. local dat
  224. -- 设备上报数据表
  225. local data = {
  226. type = t,
  227. status = s,
  228. }
  229. message.cmd = "warn"
  230. message.ext = data
  231. dat = json.encode(message)
  232. log.info("upload warn:", dat)
  233. return dat
  234. end
  235. ------------------------------------------------------------------------------------------------------
  236. --- 打印数据信息
  237. function PrintData()
  238. sys.wait(1000)
  239. if result.ds == 4 then result.ds = 1 end --滤除设备开关机状态标志位异常
  240. log.warn("++++++++++++++++++++++++++++++++++++++++Parameter display start!!+++++++++++++++++++++++++++++++++++++++++++++++")
  241. print("Table:", result)
  242. print( "eeprom state:", result.iSave,
  243. "\nDevice Status:", result.ds,
  244. "\nLignt control times:", result.tt,
  245. "\nWork Status:", result.ws,
  246. "\nLignt control Status:", result.lps,
  247. "\nRain control Status:", result.rps,
  248. "\ntempture control Status:", result.tps,
  249. "\nDump Protect:", result.dps,
  250. "\nvoltage Status:", result.vps,
  251. "\nCharging voltage:", result.cv,
  252. "\nBattery voltage:", result.bv,
  253. "\nBoard temperature:", result.bt,
  254. "\nLight control Type:", paramconf.lcm,
  255. "\nSunup voltage:", paramconf.vsunrise,
  256. "\nSunset voltage:", paramconf.vsunset,
  257. "\nCharging up to voltage:", paramconf.vcutof,
  258. "\nfloating charge voltage:", paramconf.vfloat,
  259. "\nonce again startup charging action voltage:", paramconf.vrecharge,
  260. "\nfirst startup voltage:", paramconf.vstart,
  261. "\nundervoltage clear voltage:", paramconf.uvoff,
  262. "\nundervoltage lock voltage:", paramconf.uvon,
  263. "\nHigh temperature Protect:", paramconf.overtemp,
  264. "\nLow temperature Protect:", paramconf.lowtemp,
  265. "\nHigh temperature clear:", paramconf.unovert,
  266. "\nLow temperature clear:", paramconf.unlowt,
  267. "\nHwjs counts:", result.hwcnt,
  268. "\nVol counts:", result.kcnt,
  269. "\nboard version:", result.ver)
  270. log.warn("++++++++++++++++++++++++++++++++++++++++Parameter display end!!+++++++++++++++++++++++++++++++++++++++++++++++")
  271. end
  272. --+++++++++++++++++++++++++++++++++++++++++获取主板信息+++++++++++++++++++++++++++++++++++++++++
  273. -- 获取主板状态 -->STM8从机地址0x51
  274. local function getStatus()
  275. local data = string.char(0x55,0x03)
  276. uart.write(1,data)
  277. sys.wait(300)
  278. end
  279. --------------------------------------------------------------------------------------------------------------
  280. -- ====================================================上传===================================================
  281. ------------------------------------------------- 上传数据信息-四情平台 ----------------------------------------
  282. function upData_120(...)
  283. local data, c, dat = {}
  284. getStatus() -- 获取主板状态
  285. data.imei = misc.getImei() -- 唯一ID
  286. data.iccid = string.upper(sim.getIccid()) --获取SIM卡的iccid号 ++++ 注意:开机lua脚本运行之后,会发送at命令去查询iccid,所以需要一定时间才能获取到iccid。开机后立即调用此接口,基本上返回nil
  287. data.csq = net.getRssi() -- 信号强度
  288. data.dver = VERSION -- 版本号 (main.lua第二行)
  289. data.proj = PROJECT -- 设备名称 (main.lua第一行)
  290. data.stm8vs = string.format(result.ver) -- 主板版本号
  291. -------------------------------------------
  292. data.ds = result.ds or 1 -- 设备是否开机
  293. data.ws = result.ws or 1 -- 工作状态 0待机 1工作
  294. data.cs = result.lps or 0 -- 充电标志位 0 非充电,1 充电
  295. data.rps = result.rps or 0 -- 雨控
  296. data.tps = result.tps or 0 -- 温控
  297. data.dps = result.dps or 0 -- 倾倒
  298. data.bs = result.vps or 0 -- 正常0 欠压 1 过压 2
  299. --------------------------------------
  300. data.tcs = timcontrl.timctrl --时控开关
  301. data.st = timcontrl.st --时控开始时间
  302. data.et = timcontrl.et --时控结束时间
  303. --------------------------------------
  304. data.lcm = result.lcm or 0 -- 光控类型 0太阳能板光控 1光敏二极管光控
  305. data.cv = result.cv or 0 -- 充电电压 单位:mV
  306. data.bv = result.bv or 12000 -- 电池电压 单位:mV
  307. data.at = temp --环境温度
  308. data.ah = hum -- 环境湿度
  309. if result.hwcnt > 10 then --过滤掉开机之后的红外计数异常
  310. data.infr_ct = 0
  311. else
  312. data.infr_ct = result.hwcnt -- 红外计数
  313. end
  314. if result.kcnt > 10 then -- 过滤掉开机之后的电击计数异常
  315. data.volt_ct = 2
  316. else
  317. data.volt_ct = result.kcnt -- 高压计数值
  318. end
  319. data.wind_drec = 666
  320. data.wind_sped = 66.6
  321. -----------------------------------------
  322. rebootcnt = io.readFile(DEV_REBOOT)
  323. data.bt_ct = rebootcnt -- 设备重启次数
  324. log.warn("Device Reboot times is:", rebootcnt)
  325. data.gps = gps_status -- 定位标志位 0 手动定位,1 GPS定位,2 LBS定位
  326. if gps_status == 0 then
  327. data.lat = manual_location.lat
  328. data.lng = manual_location.lng
  329. elseif gps_status == 1 then
  330. data.lat = gps_loc_lat
  331. data.lng = gps_loc_lng
  332. elseif gps_status == 2 then
  333. data.lat = lbs_loc_lat
  334. data.lng = lbs_loc_lng
  335. end
  336. log.warn("upload LBS data message:", data.lat, data.lng)
  337. c = misc.getClock() -- 获取系统时间
  338. data.stamp = string.format("%04d%02d%02d%02d%02d%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  339. message.cmd = "data"
  340. message.ext = data
  341. dat = json.encode(message)
  342. log.info("upload data:", dat)
  343. --ClearKcnt() --上报数据清零红外
  344. return dat
  345. end
  346. ------------------------- 上传参数配置信息
  347. function upStatus()
  348. local status, dat = {}
  349. getStatus()-- 获取主板状态
  350. status.imei = misc.getImei() -- 唯一ID
  351. status.iccid = string.upper(sim.getIccid())--获取SIM卡的iccid号 ++++ 注意:开机lua脚本运行之后,会发送at命令去查询iccid,所以需要一定时间才能获取到iccid。开机后立即调用此接口,基本上返回nil
  352. status.csq = net.getRssi() -- 信号强度
  353. status.tt = result.tt or 0 -- 定时时长
  354. message.cmd = "status"
  355. message.ext = status
  356. dat = json.encode(message)
  357. log.info("upload status:", dat)
  358. return dat
  359. end
  360. -----------------------------------------------------------------------------------------------------------------
  361. --------------------------------------- 上传数据信息-------大数据平台----------------------------------------------
  362. function upData()
  363. local data, c, dat = {}
  364. getStatus() -- 获取主板状态
  365. data.imei = misc.getImei() -- 唯一ID
  366. data.iccid = string.format(sim.getIccid()) -- 获取SIM卡的iccid号
  367. data.csq = string.format(net.getRssi()) -- 信号强度
  368. data.dver = VERSION -- 版本号 (main.lua第二行)
  369. data.proj = PROJECT -- 设备名称 (main.lua第一行)
  370. data.stm8vs = string.format(result.ver) --
  371. data.dtype = "4" -- 设备类型 -性诱III
  372. data.btype = "0" -- 0 蓄电池,1锂电池
  373. data.ds = string.format(result.ds or 1) -- 设备是否开机
  374. data.ws = string.format(result.ws or 1) -- 工作状态0待机,1工作,2充电
  375. data.cs = string.format(result.lps or 0) -- 充电标志位 0 非充电,1 充电
  376. data.rps = string.format(result.rps or 0) -- 雨控
  377. data.tps = string.format(result.tps or 0) -- 温控
  378. data.dps = string.format(result.dps or 0) -- 倾倒
  379. data.bs = string.format(result.vps or 0) -- 正常0 欠压 1 过压 2
  380. data.lcm = string.format(result.lcm or 0) -- 光控类型 0太阳能板光控 1光敏二极管光控
  381. data.bt = string.format(result.bt / 10) -- 主板温度
  382. data.ts = string.format(timcontrl.timctrl) -- 时控开关
  383. data.st = string.format(timcontrl.st) -- 时控开始时间
  384. data.et = string.format(timcontrl.et) -- 时控结束时间
  385. data.cv = string.format((result.cv or 18000) / 1000) -- 充电电压
  386. data.bv = string.format((result.bv or 12800) / 1000) -- 电池电压
  387. data.at = string.format(temp) or "25.0" -- 环境温度
  388. data.ah = string.format(hum) or "30.0" -- 环境湿度
  389. if tonumber(result.kcnt) > 10 then --过滤掉开机之后的电击计数异常
  390. data.volt_ct = "2"
  391. else
  392. data.volt_ct = string.format(result.kcnt) -- 电击次数
  393. end
  394. if tonumber(result.hwcnt) > 10 then -- 过滤掉开机之后的光栅计数异常
  395. data.infr_ct = 0
  396. else
  397. data.infr_ct = string.format(result.hwcnt) -- 高压计数值
  398. end
  399. data.wind_drec = "666"
  400. data.wind_sped = "66.6"
  401. data.dat_f = string.format(dat_f) -- 数据上传时间间隔
  402. data.clt_t = string.format(clt_t) -- 清虫时间间隔
  403. rebootcnt = io.readFile(DEV_REBOOT)
  404. if rebootcnt == nil or rebootcnt == "" then
  405. log.info("first startup!!!!!!!!")
  406. rebootcnt = 0
  407. rebootcnt = tonumber(rebootcnt)
  408. end
  409. log.warn("Device Reboot times is:", rebootcnt)
  410. data.rcnt = string.format(rebootcnt) -- 重启次数
  411. data.gps = string.format(gps_status) -- 定位标志位 0 手动定位,1 GPS定位,2 LBS定位
  412. if gps_status == 0 then
  413. data.lat = manual_location.lat
  414. data.lng = manual_location.lng
  415. elseif gps_status == 1 then
  416. data.lat = gps_loc_lat
  417. data.lng = gps_loc_lng
  418. elseif gps_status == 2 then
  419. data.lat = lbs_loc_lat
  420. data.lng = lbs_loc_lng
  421. end
  422. c = misc.getClock()
  423. data.stamp = string.format("%04d%02d%02d%02d%02d%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  424. message.cmd = "data"
  425. message.ext = data
  426. dat = json.encode(message)
  427. log.info("upload data:", dat)
  428. --ClearKcnt(); --清除点击次数
  429. return dat
  430. end
  431. -----------------------------------------------------------------------------------------------------------
  432. ----------------------------------------- 物联网模块参数配置开始 --------------------------------------------
  433. ----------------------------------------- 上传主板参数配置信息-----------------------------------------------
  434. function upParamConf(...)
  435. local c, dat
  436. paramconf.imei = misc.getImei() -- 唯一ID
  437. c = misc.getClock()
  438. paramconf.stamp = string.format("%04d%02d%02d%02d%02d%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  439. message.cmd = "paramconf"
  440. message.ext = paramconf
  441. dat = json.encode(message)
  442. log.info("upload paramconf:", dat)
  443. return dat
  444. end
  445. ----------------------------------------- 上传主服务器配置信息--------------------------------------------------
  446. function upServerConf(...)
  447. local dat
  448. serverconf.imei = misc.getImei()-- 唯一ID
  449. message.cmd = "serverconf"
  450. message.ext = serverconf
  451. dat = json.encode(message)
  452. log.info("upload serverconf:", dat)
  453. return dat
  454. end
  455. ----------------------------------------- 上传第三方服务器配置信息---------------------------------------------
  456. function upThirdserverConf(...)
  457. local dat
  458. thirdserverconf.imei = misc.getImei()-- 唯一ID
  459. message.cmd = "thirdserverconf"
  460. message.ext = thirdserverconf
  461. dat = json.encode(message)
  462. log.info("upload thirdserverconf:", dat)
  463. return dat
  464. end
  465. -- 设备给客户端的反馈 -->远程控制设备更新和重启等
  466. function upres(c_type, c_status)
  467. local dat
  468. -- 设备上报数据表
  469. local data = {
  470. type = c_type,
  471. status = c_status
  472. }
  473. message.cmd = "warn"
  474. message.ext = data
  475. dat = json.encode(message)
  476. log.info("upload upres:", dat)
  477. return dat
  478. end
  479. ------------------------------------------------- 物联网模块参数配置结束 --------------------------------------------------------
  480. ------------------------------------------------------------------------------------------------------------------------------
  481. --++++++++++++++++++++++++++++++++++++++++++++++++ 功能函数集合 开始++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  482. ---------------------------------------------------主板参数下发配置 开始--------------------------------------------------------
  483. --- 设置主板开关机
  484. function setsw(pwb)
  485. local setsw_cnt = 0
  486. local set_onoff = string.char(0xAA,0x01,pwb)
  487. while result.ds ~= pwd do
  488. setsw_cnt = setsw_cnt + 1
  489. uart.write(1,set_onoff)
  490. if setsw_cnt == 2 then break end
  491. sys.wait(500)
  492. end
  493. log.info("设备开关机:", pwb)
  494. end
  495. --- 设置主板光控定时时长
  496. -- @param tt, number类型
  497. function setTimer(tt)
  498. if not tt then
  499. return
  500. end
  501. if (tt <= 0 or tt > 8) and tt ==nil and tt == "" then
  502. log.error("setTimer value error!!!!!!!")
  503. return
  504. end
  505. local send_cnt = 0
  506. local dat_freq = string.char(0xAA,0x02,tt)
  507. while tt~=result.tt do
  508. send_cnt = send_cnt + 1
  509. uart.write(1,dat_freq)
  510. if send_cnt == 3 then
  511. break end
  512. end
  513. log.info("定时模式为光控,时长:", tt)
  514. end
  515. ---清零红外和高压计数值
  516. function ClearKcnt()
  517. local kcnt = 0
  518. local clear = string.char(0xAA,0x06)
  519. while true do
  520. kcnt = kcnt +1
  521. if result.infr_ct ~=0 or result.volt_ct ~=0 then
  522. uart.write(1,clear)
  523. end
  524. if kcnt == 2 then break end
  525. sys.wait(500)
  526. end
  527. log.info("clear kcnt!!!!")
  528. end
  529. ---清虫
  530. local function Clearworm()
  531. local setsw_cnt = 0
  532. local clear_worm = string.char(0xAA,0x05)
  533. while true do
  534. setsw_cnt = setsw_cnt + 1
  535. uart.write(1,clear_worm)
  536. sys.wait(500)
  537. if setsw_cnt == 2 then break end --确保置位清虫标志位
  538. end
  539. log.info("clear worm!!!!")
  540. end
  541. --下发时控设置
  542. function SetTimControl(crtl,st1,et1)
  543. if crtl == nil or crtl == "" then return end
  544. if st1==nil or st1=="" or et1 ==nil or et1 == "" then return end
  545. st = tonumber(st1)
  546. et = tonumber(et1)
  547. if (st < 1 or st > 23) or (et < 1 or et > 23) then
  548. log.error("value error!!!!!!!!!!")
  549. return
  550. end
  551. if crtl == 1 or crtl == 0 then
  552. timcontrl.timctrl = crtl
  553. end
  554. if (st >= 0 and st <=23) and ( et >= 0 and et <= 23) then
  555. timcontrl.st = st
  556. timcontrl.et = et
  557. end
  558. log.info("时控参数:", "timctrl:", timcontrl.timctrl, "st:", timcontrl.st, "et:", timcontrl.et)
  559. local data = json.encode(timcontrl)
  560. io.writeFile(CONTROL_CONF, data)
  561. end
  562. --- 设置清虫时间间隔
  563. function SetCleartime(clt)
  564. if not clt then
  565. return
  566. end
  567. if clt < 1 or clt > 120 then
  568. log.error("clt value error******")
  569. return
  570. end
  571. if clt > 9 and clt < 121 then
  572. cleartim = clt
  573. end
  574. log.info("设置清虫时间间隔为:", cleartim, "分钟")
  575. io.writeFile(CLEART_CONF, cleartim)
  576. end
  577. --- 下发配置数据上传时间间隔:
  578. function setDattim(dat)
  579. if not dat then
  580. return
  581. end
  582. if dat < 1 or dat > 120 then
  583. log.error("Dattim value error!!!!!!!")
  584. return
  585. end
  586. if dat > 9 and dat < 121 then
  587. dattim = dat
  588. end
  589. log.info("设置数据上传时间间隔为:", dattim, "分钟")
  590. io.writeFile(DATTIM_CONF, dattim)
  591. end
  592. -- 一键测试函数
  593. function Test_key()
  594. if stm8_update_sw == 1 then return end
  595. local test = string.char(0XAA,0X0A)
  596. uart.write(1,test)
  597. end
  598. --下发主板升级命令
  599. function update_stm32()
  600. local update_cnt = 0
  601. local up_date_32 = string.char(0xAA,0xA0)
  602. while true do
  603. update_cnt = update_cnt + 1
  604. uart.write(1,up_date_32)
  605. sys.wait(500)
  606. if update_cnt == 2 then break end
  607. end
  608. log.info("update_stm32 begin!!!!")
  609. end
  610. --光控模式切换
  611. function Light_control_handoff()
  612. local Light_control1 = string.char(0xAA,0x03,0x01)
  613. local Light_control2 = string.char(0xAA,0x03,0x00)
  614. while true do
  615. if result.lcm == 0 then
  616. uart.write(1,Light_control1)
  617. elseif result.lcm == 1 then
  618. uart.write(1,Light_control2)
  619. end
  620. return
  621. end
  622. log.info("Light control handoff begin!!!!")
  623. end
  624. --下发BootLoader交互命令
  625. function BootLoader_stm32()
  626. local boot_count = 0
  627. local BootLoader_32 = string.char(0x31)
  628. while true do
  629. boot_count = boot_count + 1
  630. uart.write(1,BootLoader_32)
  631. sys.wait(200)
  632. if boot_count == 2 then break end
  633. end
  634. log.info("BootLoader begin!!!!")
  635. end
  636. -----------------------------------------------------------------------------------------------------------------------------
  637. --================================================ 主板参数下发配置 结束=======================================================
  638. -----------------------------------------------反向获取平台温湿度接口传回MQTT 开始----------------------------------------------------
  639. function Setsensor_value(dat_sensor)
  640. local cmds,result,err = json.decode(dat_sensor)
  641. if not result then
  642. return
  643. end
  644. if cmds.ext.new_hum~=nil and cmds.ext.new_hum~="" then
  645. if tonumber(cmds.ext.new_hum)~= nil and tonumber(cmds.ext.new_hum)>=0 and tonumber(cmds.ext.new_hum)<= 100 then
  646. hum = tonumber(cmds.ext.new_hum)
  647. if nvm.get("hum")~= hum then
  648. nvm.set("hum",hum)
  649. end
  650. end
  651. end
  652. if cmds.ext.new_tem~=nil and cmds.ext.new_tem~="" then
  653. if tonumber(cmds.ext.new_tem)~= nil and tonumber(cmds.ext.new_tem) < 100 then
  654. temp = tonumber(cmds.ext.new_tem)
  655. if nvm.get("temp")~= temp then
  656. nvm.set("temp",temp)
  657. end
  658. end
  659. end
  660. log.warn(">>>>>>>>>sensor data get is:>>>>>>>>>>",hum,temp)
  661. end
  662. -----------------------------------------------反向获取平台温湿度接口传回MQTT 结束----------------------------------------------------
  663. ---------------------------------------------------------------------------------------------------------------------------
  664. ------------------------------------------------- 物联网模块参数配置开始 ------------------------------------------------------
  665. ---------------------------------------------------下发用户配置的参数----------------------------------------------------------
  666. -- --- 下发配置数据上传时间间隔
  667. -- @param dat, json数据结构
  668. function Set_dattim(dat)
  669. if nvm.get("platform")== "120.27.222.26" then
  670. local cmds, result, err = json.decode(dat)
  671. if not result then log.info("Set_dattim set the parameter error!") end
  672. if type(cmds.ext.dat_f) == "number" and cmds.ext.dat_f >= 10 and cmds.ext.dat_f <= 120 then -- number类型 data
  673. dat_f = cmds.ext.dat_f
  674. else
  675. log.error("EEEE:::::::::::::Dattim parameter not number or not range:::::::::::::::EEEE")
  676. return
  677. end
  678. log.warn("Set_dattim set the parameter value is: ", dat_f, "minutes")
  679. io.writeFile(DATTIM_CONF, dat)
  680. else
  681. local cmds, result, err = json.decode(dat)
  682. if not result then log.info("Set_dattim set the parameter error!") end
  683. dat_f = tonumber(cmds.ext.data_tim.dat_f)
  684. log.warn("Set_dattim set the parameter value is: ", dat_f, "minutes")
  685. paramtable.ext.data_tim.dat_f = dat_f
  686. local dat = json.encode(paramtable) -- 参数配置存入文件
  687. io.writeFile(PRARM_CONF,dat)
  688. end
  689. end
  690. -- --- 下发配置清虫时间间隔
  691. -- @param dat, json数据结构
  692. function Set_clttim(dat)
  693. local cmds, result, err = json.decode(dat)
  694. if not result then log.info("Set_dattim set the parameter error!") end
  695. clt_t = tonumber(cmds.ext.clt_tim.clt_t)
  696. log.warn("Set_dattim set the parameter value is: ", clt_t, "minutes")
  697. paramtable.ext.clt_tim.clt_t = clt_t
  698. local dat = json.encode(paramtable) -- 参数配置存入文件
  699. io.writeFile(PRARM_CONF,dat)
  700. end
  701. --- 下发设置时控参数
  702. -- @param dat, json数据结构
  703. function Set_timcontrol(dat)
  704. --四情平台设置
  705. if nvm.get("platform")== "120.27.222.26" then
  706. local cmds, result, err = json.decode(dat)
  707. if not result then
  708. log.error("Set_timcontrol error!!!!!")
  709. end
  710. timcontrl = cmds.ext
  711. log.warn("Set_timcontrol set the parameter value is: ", timcontrl)
  712. io.writeFile(CONTROL_CONF, dat)
  713. else
  714. --大数据平台设置
  715. local cmds, result, err = json.decode(dat)
  716. if not result then log.error("Set_timcontrol error!!!!!") end
  717. if cmds.ext.work_tim.timctrl ~= nil and type(cmds.ext.work_tim.timctrl) == "string" and tonumber(cmds.ext.work_tim.timctrl) >= 0 and tonumber(cmds.ext.work_tim.timctrl) <= 1 then
  718. timcontrl.timctrl = tonumber(cmds.ext.work_tim.timctrl)
  719. end
  720. if cmds.ext.work_tim.st ~= nil and type(cmds.ext.work_tim.st) == "string" and tonumber(cmds.ext.work_tim.st) >= 0 and tonumber(cmds.ext.work_tim.st) <= 23 then
  721. timcontrl.st = tonumber(cmds.ext.work_tim.st)
  722. end
  723. if cmds.ext.work_tim.et ~= nil and type(cmds.ext.work_tim.et) == "string" and tonumber(cmds.ext.work_tim.et) >= 0 and tonumber(cmds.ext.work_tim.et) <= 23 then
  724. timcontrl.et = tonumber(cmds.ext.work_tim.et)
  725. end
  726. log.warn("Set_timcontrol set the parameter value is: ", timcontrl)
  727. paramtable.ext.work_tim = timcontrl
  728. local dat = json.encode(paramtable) -- 参数配置存入文件
  729. io.writeFile(PRARM_CONF,dat)
  730. end
  731. end
  732. --- 下发参数配置信息
  733. -- @param dat, json数据结构
  734. function setParamConf(dat)
  735. local cmds, result, err = json.decode(dat)
  736. if not result then log.info("setParamConf set the parameter error!") end
  737. paramconf = cmds.ext
  738. log.warn("setParamConf set the parameter value is: ", paramconf)
  739. io.writeFile(DEVICE_CONF, dat)
  740. end
  741. --- 下发服务器配置信息
  742. -- @param dat, json数据结构
  743. function setServerConf(dat)
  744. local cmds, result, err = json.decode(dat)
  745. if not result then log.info("setServerConf set the parameter error!") end
  746. serverconf = cmds.ext
  747. log.warn("setServerConf set the parameter value is: ", serverconf)
  748. io.writeFile(SERVER_CONF, dat)
  749. end
  750. --- 下发第三方服务器配置信息
  751. -- @param dat, json数据结构
  752. function setThirdserverConf(dat)
  753. local cmds, result, err = json.decode(dat)
  754. if not result then log.info("setThirdserverConf set the parameter error!") end
  755. thirdserverconf = cmds.ext
  756. log.warn("setThirdserverConf set the parameter value is: ", thirdserverconf)
  757. io.writeFile(THIRD_CONF, dat)
  758. end
  759. --- 下发设置定位数据(--百度坐标系--)
  760. -- @param dat, json数据结构
  761. function Set_location(dat)
  762. local cmds, result, err = json.decode(dat)
  763. if not result then
  764. log.warn("Set work location failed!")
  765. end
  766. manual_location = cmds.ext
  767. log.warn("set manual_location is:",manual_location.lat,manual_location.lng)
  768. --paramtable.ext.location = manual_location
  769. --local dat = json.encode(paramtable) -- 参数配置存入文件
  770. io.writeFile(GPS_CONF,dat)
  771. end
  772. -----------------------------------------------------------------------------------------------------------------------------
  773. ---------------------------------------------------读取用户配置的参数----------------------------------------------------------
  774. --- 开机获取用户配置的参数
  775. function getConfig()
  776. local param, server,datt, third_ser,loc,param_conf, cmds, result, err
  777. param = io.readFile(DEVICE_CONF) -- 获取参数配置参数
  778. cmds, result, err = json.decode(param)
  779. if result then
  780. paramconf = cmds.ext
  781. end
  782. datt = io.readFile(DATTIM_CONF) -- 获取数据上传时间间隔参数
  783. cmds, result, err = json.decode(datt)
  784. if result then dat_f = cmds.ext.dat_f end
  785. server = io.readFile(SERVER_CONF) -- 开机读取主服务器配置
  786. cmds, result, err = json.decode(server)
  787. if result then
  788. serverconf = cmds.ext
  789. end
  790. third_ser = io.readFile(THIRD_CONF) -- 获取第三方服务器配置参数
  791. cmds, result, err = json.decode(third_ser)
  792. if result then
  793. thirdserverconf = cmds.ext
  794. end
  795. loc = io.readFile(GPS_CONF) -- 开机读取手动定位值
  796. cmds,result,err = json.decode(loc)
  797. if result then manual_location = cmds.ext end
  798. ----------------------------设备参数读取失败重置默认参数------------------------------------
  799. param_conf = io.readFile(PRARM_CONF)
  800. cmds, result, err = json.decode(param_conf)
  801. if result then
  802. dat_f = cmds.ext.data_tim.dat_f -- 获取数据上传时间间隔参数
  803. clt_t = cmds.ext.clt_tim.clt_t -- 获取清虫间隔参数
  804. timcontrl = cmds.ext.work_tim -- 获取时控控制参数
  805. if timcontrl == nil then
  806. timcontrl.st = 20
  807. timcontrl.et = 0
  808. timcontrl.timctrl = 0
  809. end
  810. -- manual_location = cmds.ext.location -- 获取手动定位值
  811. end
  812. end
  813. -----------------------------------------------------------------------------------------------------------------------------
  814. ------------------------------------------------- 物联网模块参数配置结束 ------------------------------------------------------
  815. -- +++++++++++++++++++++++++++++++++++++++++++++++++ 协程功能进程开始 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  816. -------------------------------------------------------定时清虫任务------------------------------------------------------------
  817. --+++++性诱测报III型设备没有定时清虫功能----可以屏蔽该段代码
  818. sys.taskInit(function()
  819. while true do
  820. sys.wait(clt_t * 60 *1000) --30分钟清虫一次
  821. Clearworm()
  822. log.warn("Clear worm time is:", clt_t)
  823. end
  824. end)
  825. -----------------------------------------------定时工作任务----------------------------------------------------
  826. -- 定时模式启动判断
  827. sys.taskInit(
  828. function()
  829. worksta = 0
  830. local workdone = 0 --(光控定时模式下)工作完成标志位
  831. local timeover = 0 --定时模式结束时间戳
  832. sys.wait(30 * 1000)
  833. --等待开机启动对时
  834. log.warn("Read timctrl is..................", timcontrl.timctrl)
  835. local c = misc.getClock()
  836. while true do
  837. while true do
  838. if c.year < 2020 then --如果未对时,关闭时控
  839. timcontrl.timctrl = 0
  840. c = misc.getClock()
  841. end
  842. if timcontrl.timctrl == 0 then -- 若时控关闭
  843. log.warn("it's not time control")
  844. log.warn("开始时间:", timcontrl.st, "结束时间:", timcontrl.et)
  845. worksta = 0
  846. workdone = 0
  847. end
  848. if timcontrl.timctrl == 1 then -- 若时控开启
  849. log.warn("it's time control", "start time:", timcontrl.st, "end time:", timcontrl.et)
  850. local c_sta = misc.getClock()
  851. c_sta.hour = timcontrl.st
  852. c_sta.min = 0
  853. c_sta.sec = 0
  854. if timcontrl.st < timcontrl.et then -- 如果开始和结束时间是同一天
  855. log.warn("the same day !!!!")
  856. local c_end = misc.getClock() -- **获取当前时间**
  857. c_end.hour = timcontrl.et
  858. c_end.min = 0
  859. c_end.sec = 0
  860. -- 如果当前时间大于开始时间且小于结束时间,且没有开始工作
  861. if os.difftime(os.time(), os.time(c_sta)) > 0 and os.difftime(os.time(), os.time(c_end)) < 0 and workdone == 0 then
  862. log.warn("模式1当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算结束时间戳")
  863. timeover = os.time(c_end)
  864. timeover = tonumber(timeover)
  865. worksta = 1
  866. break
  867. elseif os.difftime(os.time(), os.time(c_sta)) < 0 then
  868. log.warn("模式1当前时间小于开始时间!!!")
  869. worksta = 0
  870. elseif os.difftime(os.time(), os.time(c_end)) > 0 then
  871. log.warn("模式1当前时间大于结束时间!!!")
  872. worksta = 0
  873. end
  874. -- 如果工作完成:
  875. if workdone == 1 then
  876. workdone = 0
  877. end
  878. elseif timcontrl.st > timcontrl.et then -- 如果结束时间是第二天
  879. log.warn("not the same day !!!!")
  880. local n = misc.getClock() -- **获取当前时间**
  881. -- 如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  882. if os.difftime(os.time(), os.time(c_sta)) > 0 and workdone == 0 and n.hour > timcontrl.et then
  883. log.warn("模式2当前时间大于开始时间,且没有开始工作,当前时间位于0点之前,结束时间戳加1天,计算结束时间戳")
  884. local c_end = misc.getClock()
  885. c_end.hour = timcontrl.et
  886. c_end.min = 0
  887. c_end.sec = 0
  888. timeover = os.time(c_end) + 3600 * 24
  889. timeover = tonumber(timeover)
  890. worksta = 1
  891. break
  892. elseif os.difftime(os.time(), os.time(c_sta)) > 0 and workdone == 0 and n.hour < timcontrl.et then
  893. log.warn("模式2当前时间大于开始时间,但是小于结束时间,当前时间位于0点之后,所以是重启引起的,时间戳不动,计算结束时间戳")
  894. local c_end = misc.getClock()
  895. c_end.hour = timcontrl.et
  896. c_end.min = 0
  897. c_end.sec = 0
  898. timeover = os.time(c_end)
  899. worksta = 1
  900. break
  901. elseif os.difftime(os.time(), os.time(c_sta)) < 0 then
  902. log.warn("模式2当前时间小于开始时间!!!")
  903. worksta = 0
  904. end
  905. -- 如果工作完成:
  906. if workdone == 1 then
  907. workdone = 0
  908. end
  909. end
  910. end
  911. sys.wait(1000)
  912. end
  913. -- 等待定时任务完成
  914. if timeover ~= nil or timeover ~= 0 then
  915. while (timeover > os.time()) do
  916. sys.wait(1000)
  917. log.warn("Working.....:", "over", timeover, "now:", os.time())
  918. end
  919. end
  920. log.info(" The workTask time is end!")
  921. ----------------------标志工作定时结束!!!++++
  922. worksta = 0
  923. workdone = 1
  924. ----------------------标志工作完成!!!++++
  925. sys.wait(1000)
  926. end
  927. end
  928. )
  929. -- 时控判断是否工作-------------------------------------------------
  930. sys.taskInit(
  931. function()
  932. sys.wait(20 * 1000)
  933. while true do
  934. if timcontrl.timctrl == 1 then --时控定时模式
  935. log.warn("Start Timng Mode....")
  936. if worksta == 1 then
  937. if result.ds == 0 then -- 如果设备关机
  938. setsw(1) --开机
  939. end
  940. else
  941. if result.ds == 1 then -- 如果设备开机
  942. setsw(0)
  943. end
  944. end
  945. end
  946. if timcontrl.timctrl == 0 then --光控
  947. log.warn("Light control mode...")
  948. end
  949. sys.wait(1000)
  950. end
  951. end
  952. )
  953. --时控工作修改光控工作状态消除时控工作状态下不能达到工作时长
  954. sys.taskInit(
  955. function ()
  956. local cnf, result, err = json.decode(dat)
  957. sys.wait(1000)
  958. while true do
  959. if timcontrl.timctrl == 1 then
  960. tt = 0
  961. setTimer(0)
  962. end
  963. sys.wait(1000)
  964. end
  965. end
  966. )
  967. -----------------------------------------------雨控即时上报----------------------------------------------------
  968. --雨控即时上报
  969. sys.taskInit(
  970. function()
  971. sys.wait(1000)
  972. while true do
  973. if rps_x == 1 then
  974. updata_freq = 0
  975. rps_x = 0
  976. rps_y = 1
  977. end
  978. sys.wait(1000)
  979. end
  980. end
  981. )
  982. ------------------------------------------------------------协程通讯进程开始----------------------------------------------------------
  983. -- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MQTT1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  984. sys.taskInit(function()
  985. getConfig() -- 开机获取用户配置的参数
  986. nvm.init("config.lua")
  987. sys.wait(5000)
  988. log.info("MQTT1 Task is start!")
  989. -- 是否获取分配到的ip
  990. while not socket.isReady() do
  991. log.info("socket is not ready,err_num:", err_num)
  992. err_num = err_num + 1
  993. sys.wait(1000)
  994. if err_num >= 600 then
  995. rebootinfo = 2 -- ***SOCKET断开上报***(注意只针对120.27.222.26服务器)
  996. io.writeFile(DEVICE_ERR, rebootinfo)
  997. sys.restart("socket connect overtime!")
  998. end
  999. end
  1000. serverconf.ip = nvm.get("platform")
  1001. if serverconf.ip == "8.136.98.49" then
  1002. serverconf.port = 61883
  1003. sub = "/yfkj/xycb/s2c/" -- 服务器到客户端
  1004. pub = "/yfkj/xycb/c2s/" -- 客户端到服务器
  1005. lastwill = "/yfkj/xycb/offline/" -- 离线消息主题(部分)-->设备异常中断时用于mqtt服务器发布该设备的遗愿消息,个人网站客户端需订阅
  1006. elseif serverconf.ip == "120.27.222.26" then
  1007. serverconf.port = 1883
  1008. sub1 = "yfkj/xycb/s2c/" -- 服务器到客户端
  1009. pub1 = "yfkj/xycb/c2s/" -- 客户端到服务器
  1010. lastwill1 = "yfkj/xycb/offline/" -- 离线消息主题(部分)-->设备异常中断时用于mqtt服务器发布该设备的遗愿消息,个人网站客户端需订阅
  1011. end
  1012. if nvm.get("lat_lbs") ~= "" and nvm.get("lat_lbs") ~= nil then
  1013. lbs_loc_lat = nvm.get("lat_lbs")
  1014. lbs_loc_lng = nvm.get("lng_lbs")
  1015. gps_status = 2
  1016. end
  1017. if nvm.get("lat_gps") ~= "" and nvm.get("lat_gps") ~= nil then
  1018. gps_loc_lat = nvm.get("lat_gps")
  1019. gps_loc_lng = nvm.get("lng_gps")
  1020. gps_status = 1
  1021. end
  1022. if nvm.get("hum") ~= "" and nvm.get("hum") ~= nil then
  1023. hum = nvm.get("hum")
  1024. end
  1025. if nvm.get("temp") ~= "" and nvm.get("temp") ~= nil then
  1026. temp = nvm.get("temp")
  1027. end
  1028. -- 离线消息
  1029. willmsg.ext.imei = misc.getImei()
  1030. if serverconf.ip == "8.136.98.49" then
  1031. serverconf.lastwill = "/yfkj/xycb/offline/"
  1032. serverconf.pub = "/yfkj/xycb/c2s/"
  1033. serverconf.sub = "/yfkj/xycb/s2c/"
  1034. elseif serverconf.ip == "120.27.222.26" then
  1035. serverconf.lastwill = "yfkj/xycb/offline/"
  1036. serverconf.pub = "yfkj/xycb/c2s/"
  1037. serverconf.sub = "yfkj/xycb/s2c/"
  1038. end
  1039. local will = {qos = 1, retain = 0, topic = string.format(serverconf.lastwill .. misc.getImei()), payload = json.encode(willmsg)} -- 创建mqtt客户端--默认配置了MQTT遗愿消息
  1040. local mqttc = mqtt.client(misc.getImei(), serverconf.keepalive, serverconf.uid, serverconf.pwd, 1,will)
  1041. local pub = string.format(serverconf.pub .. misc.getImei())
  1042. local sub = string.format(serverconf.sub .. misc.getImei())
  1043. local mqtt_sta = true
  1044. while true do
  1045. -- 连接mqtt服务器
  1046. mqtt_sta = mqttc:connect(serverconf.ip, serverconf.port)
  1047. log.info('mqtt connect status::', mqtt_sta)
  1048. if mqtt_sta == false then
  1049. log.info("mqtt1 connect fail,try to reconnect!", tcp_cont)
  1050. tcp_cont = tcp_cont + 1
  1051. if tcp_cont >= 600 then
  1052. rebootinfo = 3 -- ***MQTT断开上报***
  1053. io.writeFile(DEVICE_ERR, rebootinfo)
  1054. sys.restart("mqtt1 reconnect overtime:60!!!")
  1055. end
  1056. end
  1057. -- 订阅主题
  1058. if mqttc:subscribe(sub, serverconf.qos) then
  1059. local reason = rtos.poweron_reason()
  1060. log.info("reason:", reason)
  1061. mqttc:publish(pub,upwarn("device_820UG","turnon:" ..rtos.poweron_reason() ..";stm32:"..nvm.get("reboot_stm8")..";freefilesize:" ..rtos.get_fs_free_size() .. ";VERSION:" .. VERSION .. ";"..PROJECT.. "&core_version:" .. rtos.get_version() ..";stm8:"..result.ver),serverconf.qos)
  1062. if serverconf.ip == "8.136.98.49" then
  1063. if not mqttc:publish(pub, upData(), serverconf.qos) then break end
  1064. elseif serverconf.ip == "120.27.222.26" then
  1065. if not mqttc:publish(pub, upStatus(), serverconf.qos) then break end
  1066. if not mqttc:publish(pub, upData_120(), serverconf.qos) then break end
  1067. end
  1068. log.info("MQTT1 upload is done!")
  1069. while mqtt_sta==true do
  1070. mqtt_status = "true"
  1071. --local r, packet = mqttc:receive(dattim * 60 * 1000) -- 处理服务器下发指令
  1072. local r, packet, param = mqttc:receive(timeout * 1000 * 60, "NET_SENT_RDY_")
  1073. if r then -- 这里是有数据下发的处理
  1074. datalink = 2
  1075. local cnf, result, err = json.decode(packet.payload)
  1076. if result then
  1077. log.info("MQTT1 recv json is::::::::::::::::::::::::::::::::::::::::::::::", packet.payload)
  1078. if serverconf.ip == "8.136.98.49" then
  1079. -----------------------------------设备参数读取--------------------------------
  1080. if cnf.cmd == "read" then
  1081. if cnf.ext == 'data' then --参数读取
  1082. updata_freq = 0
  1083. elseif cnf.ext == 'paramconf' then --获取参数配置
  1084. if not mqttc:publish(pub, upParamConf(), serverconf.qos) then break end
  1085. elseif cnf.ext == 'serverconf' then --获取主服务器参数配置
  1086. if not mqttc:publish(pub, upServerConf(), serverconf.qos) then break end
  1087. updata_freq = 0
  1088. elseif cnf.ext == 'thirdserver_conf' then --获取第三方服务器配置
  1089. if not mqttc:publish(pub, upThirdserverConf(), serverconf.qos) then break end
  1090. updata_freq = 0
  1091. end
  1092. --------------------------------平台设备控制参数下发-----------------------------------
  1093. elseif cnf.cmd == 'paramconf' then
  1094. if cnf.ext.data_tim ~= nil then -- 大数据平台配置数据上传时间间隔
  1095. --if not mqttc:publish(pub, upres("xy-dat_f", "Remote set data_tim!!"), serverconf.qos) then break end
  1096. Set_dattim(packet.payload)
  1097. --updata_freq = 0
  1098. sys.wait(300)
  1099. end
  1100. if cnf.ext.clt_tim ~= nil then -- 大数据平台配置清虫时间间隔
  1101. --if not mqttc:publish(pub, upres("xy-clt_f", "Remote set clt_tim!!"), serverconf.qos) then break end
  1102. Set_clttim(packet.payload)
  1103. --updata_freq = 0
  1104. sys.wait(300)
  1105. end
  1106. if cnf.ext.work_tim ~= nil then -- 大数据平台配置定时控制参数
  1107. --if not mqttc:publish(pub, upres("xy-timcotrl", "Remote set work_tim!!"), serverconf.qos) then break end
  1108. Set_timcontrol(packet.payload)
  1109. --updata_freq = 0
  1110. sys.wait(300)
  1111. end
  1112. if cnf.ext.power ~= nil then -- 大数据平台开关机控制
  1113. --if not mqttc:publish(pub, upres("xy-switch", "Remote startup&shutdown action will run!!"), serverconf.qos) then break end
  1114. setsw(tonumber(cnf.ext.power.ds))
  1115. --updata_freq = 0
  1116. sys.wait(300)
  1117. end
  1118. if cnf.ext.location ~= nil then -- 大数据平台下发手动定位
  1119. Set_location(packet.payload)
  1120. --if not mqttc:publish(pub, upres("xy-manual_location", "Remote set manual_location!!"), serverconf.qos) then break end
  1121. --updata_freq = 0
  1122. sys.wait(300)
  1123. end
  1124. if not mqttc:publish(pub, upres("SCD-paramconf","data_tim is".." "..cnf.ext.data_tim.dat_f.." ".."clt_tim is".." "..cnf.ext.clt_tim.clt_t.." ".."TimControl".." "..cnf.ext.work_tim.timctrl.." ".."start_time".." "..cnf.ext.work_tim.st.." ".."end_time".." "..cnf.ext.work_tim.et.." ".."power is".." "..cnf.ext.power.ds), serverconf.qos) then break end
  1125. ---------------------------------------------------------------------------------------------------------
  1126. elseif cnf.cmd == "clearworm" then -- 大数据平台清虫
  1127. if not mqttc:publish(pub, upres("xy-clrworm", "Remote clearworm action will run!!"), serverconf.qos) then break end
  1128. sys.publish("clearworm")
  1129. Clearworm()
  1130. updata_freq = 0
  1131. sys.wait(300)
  1132. elseif cnf.cmd == "reboot" then -- 大数据平台重启
  1133. if not mqttc:publish(pub, upres("air820-reboot", "System will restart!!"), serverconf.qos) then break end
  1134. sys.restart("Device remote restart")
  1135. elseif cnf.cmd == "update" then -- 大数据平台物联网模块升级
  1136. if not mqttc:publish(pub, upres("air820-updata", "System will update!!"), serverconf.qos) then break end
  1137. Update.run()
  1138. elseif cnf.cmd == "update_stm32" then -- 大数据平台主板stm32升级
  1139. if not mqttc:publish(pub, upres("update_stm32", "stm32 update will start!!"), serverconf.qos) then break end
  1140. stm8_update_sw = 1
  1141. update_stm32() --STM32远程升级
  1142. ----------------------------参数配置下发------------------------------
  1143. elseif cnf.cmd == 'param_conf' then -- 大数据平台设备参数配置下发
  1144. setParamConf(packet.payload)
  1145. updata_freq = 0
  1146. elseif cnf.cmd == 'server_conf' then -- 大数据平台服务器配置下发
  1147. setServerConf(packet.payload)
  1148. updata_freq = 0
  1149. sys.restart("server_conf done then restart!!!")
  1150. elseif cnf.cmd == 'thirdserver_conf' then -- 大数据平台第三方服务器配置下发
  1151. setThirdserverConf(packet.payload)
  1152. sys.restart("thirdserver_conf done then restart!!!")
  1153. elseif result and cnf.cmd=="platform" then----平台切换参数下发
  1154. if cnf.ext.type == "120.27.222.26" then
  1155. nvm.set("platform","120.27.222.26")
  1156. sys.restart("120.27.222.26")
  1157. elseif cnf.ext.type == "8.136.98.49" then
  1158. nvm.set("platform","8.136.98.49")
  1159. sys.restart("8.136.98.49")
  1160. end
  1161. local c = misc.getClock()
  1162. local date = string.format("%00004d-%02d-%02d %02d:%02d:%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  1163. if not mqttc:publish(pub,upwarn("平台操作","切换平台至:"..cnf.ext.type..",操作时间:"..date), serverconf.qos) then
  1164. break
  1165. end
  1166. end
  1167. elseif serverconf.ip == "120.27.222.26" then
  1168. if cnf.cmd == "read" then
  1169. if cnf.ext == 'status' then --状态读取
  1170. updata_freq = 0
  1171. elseif cnf.ext == 'data' then --参数读取
  1172. updata_freq = 0
  1173. elseif cnf.ext == 'paramconf' then --获取参数配置
  1174. if not mqttc:publish(pub, upParamConf(), serverconf.qos) then break end
  1175. elseif cnf.ext == 'serverconf' then --获取主服务器配置
  1176. if not mqttc:publish(pub, upServerConf(), serverconf.qos) then break end
  1177. elseif cnf.ext == 'thirdserver_conf' then --第三方服务器配置读取
  1178. if not mqttc:publish(pub, upThirdserverConf(), serverconf.qos) then break end
  1179. updata_freq = 0
  1180. end
  1181. elseif cnf.cmd == 'set' then
  1182. if cnf.ext.type == "data_tim" then -- 四情平台配置数据上传时间间隔
  1183. Set_dattim(packet.payload)
  1184. updata_freq = 0
  1185. sys.wait(300)
  1186. elseif cnf.ext.type == "work_tim" then -- 四情平台配置定时控制参数
  1187. Set_timcontrol(packet.payload)
  1188. updata_freq = 0
  1189. sys.wait(300)
  1190. else
  1191. log.error("EEEE:::::::::::::not set match:::::::::::::::EEEE")
  1192. end
  1193. elseif cnf.cmd == "ctrl" then
  1194. if cnf.ext.type == "power" then -- 四情平台开关机
  1195. if not mqttc:publish(pub, upres("Remote startup&shutdown action will run!", "ok"), serverconf.qos) then break end
  1196. setsw(cnf.ext.ds)
  1197. updata_freq = 0
  1198. sys.wait(300)
  1199. elseif cnf.ext == "clearworm" then -- 四情平台清虫
  1200. if not mqttc:publish(pub, upres("Remote clearworm action will run!", "ok"), serverconf.qos) then break end
  1201. sys.publish("clearworm")
  1202. Clearworm()
  1203. updata_freq = 0
  1204. sys.wait(300)
  1205. end
  1206. elseif cnf.cmd == "timer" then --下发设置光控定时时长参数
  1207. setTimer(cnf.ext.tt)
  1208. sys.wait(300)
  1209. if not mqttc:publish(pub, upData_120(), serverconf.qos) then break end
  1210. elseif cnf.cmd == "location" then -- 四情平台下发手动定位
  1211. Set_location(packet.payload)
  1212. if not mqttc:publish(pub, upres("Set the location", "OK"), serverconf.qos) then break end
  1213. elseif cnf.cmd == "reboot" then -- 四情平台重启
  1214. log.warn("Device remote reboot!!!!!")
  1215. if not mqttc:publish(pub, upres("air820-reboot", "system will restart!!", "ok"), serverconf.qos) then break end
  1216. sys.restart("Device remote restart")
  1217. elseif cnf.cmd == "update" then -- 四情平台物联网模块升级
  1218. log.warn("Device remote update!!!!!")
  1219. if not mqttc:publish(pub, upres("air820-updata", "system update will start!!", "ok"), serverconf.qos) then break end
  1220. Update.run()
  1221. elseif cnf.cmd == "update_stm32" then -- 四情平台平台主板stm32升级
  1222. if not mqttc:publish(pub, upres("update_stm32", "stm32 update will start!!"), serverconf.qos) then break end
  1223. stm8_update_sw = 1
  1224. update_stm32() --STM32远程升级
  1225. -------------------------------
  1226. elseif cnf.cmd == 'paramconf' then --四情平台下发参数配置
  1227. setParamConf(packet.payload)
  1228. updata_freq = 0
  1229. sys.restart("paramconf done then restart!!!")
  1230. elseif cnf.cmd == 'server_conf' then -- 四情平台服务器配置下发
  1231. setServerConf(packet.payload)
  1232. updata_freq = 0
  1233. sys.restart("server_conf done then restart!!!")
  1234. elseif cnf.cmd == 'thirdserver_conf' then --四情平台下第三方发服务器参数配置
  1235. setThirdserverConf(packet.payload)
  1236. sys.restart("thirdserver_conf done then restart!!!")
  1237. elseif result and cnf.cmd=="platform" then --四情平台平台切换参数下发
  1238. if cnf.ext.type == "120.27.222.26" then
  1239. nvm.set("platform","120.27.222.26")
  1240. sys.restart("120.27.222.26")
  1241. elseif cnf.ext.type == "8.136.98.49" then
  1242. nvm.set("platform","8.136.98.49")
  1243. sys.restart("8.136.98.49")
  1244. end
  1245. local c = misc.getClock()
  1246. local date = string.format("%00004d-%02d-%02d %02d:%02d:%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  1247. if not mqttc:publish(pub,upwarn("平台操作","切换平台至:"..cnf.ext.type..",操作时间:"..date), serverconf.qos) then
  1248. break
  1249. end
  1250. end
  1251. elseif result and cnf.cmd=="sensor" then --温湿度
  1252. Setsensor_value(packet.payload)
  1253. if nvm.get("json_w_stm8boot") ~= "" and nvm.get("json_w_stm8boot") ~= nil then
  1254. if mqttc:publish(pub, upwarn(nvm.get("json_w_stm8boot"),"主板重启"), serverconf.qos) then
  1255. nvm.set("json_w_stm8boot","")
  1256. end
  1257. end
  1258. end
  1259. else
  1260. log.error("EEEE:::::::::::::not json download::::::::::::::EEEE")
  1261. end
  1262. end
  1263. if packet == "NET_SENT_RDY_" then
  1264. timeout = 60
  1265. log.info("updata is", param)
  1266. if not mqttc:publish(pub, param, serverconf.qos) then
  1267. break
  1268. end
  1269. end
  1270. end
  1271. end
  1272. mqttc:disconnect()
  1273. log.info("=============mqtt1 disconnect=============")
  1274. sys.wait(1000)
  1275. datalink = 0 -- 服务器断开链接数据指示灯慢闪
  1276. end
  1277. end)
  1278. -- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ MQTT2 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1279. sys.taskInit(function()
  1280. sys.wait(10*1000)
  1281. getConfig() -- 开机获取用户配置的参数
  1282. log.warn('MQTT2 Task is running!!!!!!!!!!')
  1283. if thirdserverconf.ip == "" or thirdserverconf.ip == nil then
  1284. log.info("MQTT2 Ip Not Set!")
  1285. return
  1286. end
  1287. log.info("MQTT2 sub!", thirdserverconf.sub )
  1288. log.info("MQTT2 pub!", thirdserverconf.pub )
  1289. log.info("MQTT2 Ip !", thirdserverconf.ip )
  1290. log.info("MQTT2 port!", thirdserverconf.port )
  1291. log.info("MQTT2 keepalive!", thirdserverconf.keepalive )
  1292. log.info("MQTT2 pwd!", thirdserverconf.pwd )
  1293. log.info("MQTT2 uid!", thirdserverconf.uid )
  1294. -- 是否获取分配到的ip
  1295. while not socket.isReady() do
  1296. log.info("socket2 is not ready,err2_num:", err2_num)
  1297. err2_num = err2_num + 1
  1298. sys.wait(1000)
  1299. if err2_num >= 600 then
  1300. log.warn("socket2 connect overtime!") --软件重启(重启原因:socket connect overtime!)注意:重启后的trace中会打印出此重启原因
  1301. end
  1302. end
  1303. -- 离线消息
  1304. willmsg.ext.imei = misc.getImei()
  1305. will2 = {qos = 1, retain = 0, topic = string.format(thirdserverconf.lastwill .. misc.getImei()), payload = json.encode(willmsg)}-- 创建mqtt客户端--默认配置了MQTT遗愿消息
  1306. local mqttc2 = mqtt.client(misc.getImei(), thirdserverconf.keepalive, thirdserverconf.uid, thirdserverconf.pwd, 1, will2)
  1307. -- 拼接主题
  1308. local pub2 = string.format(thirdserverconf.pub .. misc.getImei())
  1309. local sub2 = string.format(thirdserverconf.sub .. misc.getImei())
  1310. local mqtt2_sta = true
  1311. while true do
  1312. mqtt2_sta = mqttc2:connect(thirdserverconf.ip, thirdserverconf.port)
  1313. log.info('mqtt2 connect status::', mqtt2_sta)
  1314. if mqtt2_sta == false then
  1315. log.info("mqtt2 connect fail,try to reconnect!", tcp2_cont)
  1316. tcp2_cont = tcp2_cont + 1
  1317. if tcp2_cont >= 50 then
  1318. tcp2_cont = 0
  1319. sys.restart("mqtt2 reconnect overtime:30!!!")
  1320. end
  1321. end
  1322. -- 订阅主题
  1323. if mqttc2:subscribe(sub2, thirdserverconf.qos) then
  1324. log.warn("------------------------------------------*-*-*-*---*-------------------------------------------")
  1325. if not mqttc2:publish(pub2, upData(), thirdserverconf.qos) then break end
  1326. log.info("'mqtt2 First upload data is done!!!!!'")
  1327. log.warn("*********************************************-*-*-*---*-********************************************")
  1328. sys.wait(2000)
  1329. while mqtt2_sta==true do
  1330. local r, packet, param = mqttc2:receive(timeout * 1000 * 60, "NET_SENT_RDY_")
  1331. if r then -- 这里是有数据下发的处理
  1332. local cnf, result, err = json.decode(packet.payload) -- json解析
  1333. if result then
  1334. log.info("::::::::::::::::::::::::::::::::::::::::MQTT2 recv json is::::::::::::::::::::::::::::::::::::::::::", packet.payload)
  1335. if cnf.cmd == "read" then
  1336. if cnf.ext == "data" then
  1337. updata_freq = 0
  1338. end
  1339. -- ++++++下发设置/远程设置++++++
  1340. elseif cnf.cmd == "paramconf" then -- 参数配置
  1341. if cnf.ext.data_tim ~= nil then -- 平台配置数据上传时间间隔
  1342. if not mqttc2:publish(pub2, upres("xy-dat_f", "Remote set data_tim!!"), thirdserverconf.qos) then break end
  1343. Set_dattim(packet.payload)
  1344. --updata_freq = 0
  1345. sys.wait(300)
  1346. end
  1347. if cnf.ext.clt_tim ~= nil then -- 平台配置清虫时间间隔
  1348. if not mqttc2:publish(pub2, upres("xy-clt_f", "Remote set clt_tim!!"), thirdserverconf.qos) then break end
  1349. Set_clttim(packet.payload)
  1350. --updata_freq = 0
  1351. sys.wait(300)
  1352. end
  1353. if cnf.ext.work_tim ~= nil then -- 平台配置定时控制参数
  1354. if not mqttc2:publish(pub2, upres("xy-timcotrl", "Remote set work_tim!!"), thirdserverconf.qos) then break end
  1355. Set_timcontrol(packet.payload)
  1356. --updata_freq = 0
  1357. sys.wait(300)
  1358. end
  1359. if cnf.ext.power ~= nil then -- 平台开关机控制
  1360. if not mqttc2:publish(pub2, upres("xy-switch", "Remote startup&shutdown action will run!!"), thirdserverconf.qos) then break end
  1361. setsw(tonumber(cnf.ext.power.ds))
  1362. --updata_freq = 0
  1363. sys.wait(300)
  1364. end
  1365. if cnf.ext.location ~= nil then -- 平台下发手动定位
  1366. Set_location(packet.payload)
  1367. if not mqttc2:publish(pub2, upres("xy-manual_location", "Remote set manual_location!!"), thirdserverconf.qos) then break end
  1368. --updata_freq = 0
  1369. end
  1370. if not mqttc2:publish(pub2, upData(), thirdserverconf.qos) then break end
  1371. updata_freq = 0
  1372. elseif cnf.cmd == "clearworm" then -- 平台手动下发清虫
  1373. if not mqttc2:publish(pub2, upres("xy-clrworm", "Remote clearworm action will run!!"), thirdserverconf.qos) then break end
  1374. Clearworm()
  1375. updata_freq = 0
  1376. elseif cnf.cmd == "reboot" then -- 物联网模块重启
  1377. if not mqttc2:publish(pub2, upres("air820-reboot", "System will restart!!"), thirdserverconf.qos) then break end
  1378. sys.restart("Device remote restart")
  1379. elseif cnf.cmd == "update" then -- 物联网模块升级
  1380. if not mqttc2:publish(pub2, upres("air820-updata", "System will update!!"), thirdserverconf.qos) then break end
  1381. Update.run()
  1382. elseif cnf.cmd == "update_stm32" then -- 四情平台平台主板stm32升级
  1383. if not mqttc2:publish(pub2, upres("update_stm32", "stm32 update will start!!"), thirdserverconf.qos) then break end
  1384. stm8_update_sw = 1
  1385. update_stm32() --STM32远程升级
  1386. end
  1387. end
  1388. end
  1389. if packet == "NET_SENT_RDY_" then
  1390. timeout = 60
  1391. log.info("updata is", param)
  1392. if not mqttc2:publish(pub2, param, thirdserverconf.qos) then break end
  1393. end
  1394. end
  1395. end
  1396. mqttc2:disconnect()
  1397. sys.wait(1000)
  1398. end
  1399. sys.restart("mqtt2 first publish message is error cause mqtt task is break!")
  1400. end)
  1401. -- +++++++++++++++++++++++++++++++++++++++++++++ *网络状况监控任务* ++++++++++++++++++++++++++++++++++++++++++++++++++
  1402. -- 网络状况监控任务:
  1403. sys.taskInit(function()
  1404. local err = 0
  1405. -- 等待设备联网:
  1406. log.warn("***************socket monitor task is running!***************")
  1407. while true do
  1408. while not socket.isReady() do
  1409. log.warn("socket is not Ready,", "err:", err)
  1410. err = err + 1
  1411. if err >= 600 then
  1412. sys.restart("socket connect err overtime:30!!!")
  1413. end
  1414. sys.wait(1000)
  1415. end
  1416. sys.wait(1000)
  1417. end
  1418. end)
  1419. --+++++++++++++++++++++++++++++++++++++++++++++++ * LBS+GPS定位任务 开始* ++++++++++++++++++++++++++++++++++++++++++++++++++
  1420. --[[
  1421. 功能 :获取基站对应的经纬度后的回调函数
  1422. 参数 :
  1423. result:number类型,0表示成功,1表示网络环境尚未就绪,2表示连接服务器失败,3表示发送数据失败,4表示接收服务器应答超时,5表示服务器返回查询失败;为0时,后面的3个参数才有意义
  1424. lat:string类型,纬度,整数部分3位,小数部分7位,例如031.2425864
  1425. lng:string类型,经度,整数部分3位,小数部分7位,例如121.4736522
  1426. 返回值:无
  1427. ]]
  1428. -- LBS定位回调函数 GCJ02(火星坐标)-->需转换BD-09(百度坐标)
  1429. function getLocCb(result, lat, lng) --注意:进入此函数时LBS已经定位成功
  1430. if result == 0 then --如果result=0,表示定位成功
  1431. log.info("get LBS location success!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  1432. log.warn("testLbsLoc.getLocCb*****************", result, lat, lng)
  1433. lbs_loc_lat = lat
  1434. lbs_loc_lng = lng
  1435. log.warn("get LBS location message >>>>>>>>>>>>>>>>>>>>>>>>>>", lbs_loc_lat, lbs_loc_lng)
  1436. gps_status = 2
  1437. nvm.set("lat_lbs",lat)
  1438. nvm.set("lng_lbs",lng)
  1439. else
  1440. log.info("get LBS location failed!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  1441. end
  1442. end
  1443. -- GPS日志信息输出函数 WGS-84(国际标准GPS原始坐标)-->需转换BD-09(百度坐标)
  1444. local function printGps()
  1445. if gps.isOpen() then
  1446. local tLocation = gps.getLocation()
  1447. local speed = gps.getSpeed()
  1448. log.info("testGps.printGps",
  1449. gps.isOpen(),
  1450. gps.isFix(),
  1451. tLocation.lngType,
  1452. tLocation.lng,
  1453. tLocation.latType,
  1454. tLocation.lat, -- 度格式的经纬度信息
  1455. gps.getAltitude(), -- 海拔
  1456. speed, -- 速度
  1457. gps.getCourse(), -- 方向角
  1458. gps.getViewedSateCnt(), -- 可见卫星的个数
  1459. gps.getUsedSateCnt()) -- 定位使用的卫星个数
  1460. end
  1461. end
  1462. ---------------------- GPS 模式切换 ------------------------------
  1463. local function test1Cb(tag)
  1464. log.info("testGps.test1Cb",tag)
  1465. printGps()
  1466. end
  1467. local function test2Cb(tag)
  1468. log.info("testGps.test2Cb",tag)
  1469. printGps()
  1470. end
  1471. local function test3Cb(tag)
  1472. log.info("testGps.test3Cb",tag)
  1473. printGps()
  1474. end
  1475. --测试代码开关,取值1,2
  1476. local testIdx = 1
  1477. local function test(idx)
  1478. --第1种测试代码
  1479. if idx==1 then
  1480. --执行完下面三行代码后,GPS就会一直开启,永远不会关闭
  1481. --因为gps.open(gps.DEFAULT,{tag="TEST1",cb=test1Cb}),这个开启,没有调用gps.close关闭
  1482. gps.open(gps.DEFAULT,{tag="TEST1",cb=test1Cb})
  1483. --10秒内,如果gps定位成功,会立即调用test2Cb,然后自动关闭这个“GPS应用”
  1484. --10秒时间到,没有定位成功,会立即调用test2Cb,然后自动关闭这个“GPS应用”
  1485. gps.open(gps.TIMERORSUC,{tag="TEST2",val=10,cb=test2Cb})
  1486. --300秒时间到,会立即调用test3Cb,然后自动关闭这个“GPS应用”
  1487. gps.open(gps.TIMER,{tag="TEST3",val=300,cb=test3Cb})
  1488. --第2种测试代码
  1489. elseif idx==2 then
  1490. --执行完下面三行代码打开GPS后,5分钟之后GPS会关闭
  1491. gps.open(gps.DEFAULT,{tag="TEST1",cb=test1Cb})
  1492. sys.timerStart(gps.close,300000,gps.DEFAULT,{tag="TEST1"})
  1493. gps.open(gps.TIMERORSUC,{tag="TEST2",val=10,cb=test2Cb})
  1494. gps.open(gps.TIMER,{tag="TEST3",val=60,cb=test3Cb})
  1495. end
  1496. end
  1497. --------------------------------------------------------------------
  1498. local function nmeaCb(nmeaItem)
  1499. log.info("testGps.nmeaCb",nmeaItem)
  1500. end
  1501. -- GPS+LBS定位任务
  1502. sys.subscribe("AUTOGPS_READY",function(gpsLib,agpsLib,kind,baudrate)
  1503. gps=gpsLib
  1504. agps=agpsLib
  1505. log.info("testGps.AUTOGPS_READY",baudrate,kind)
  1506. gps.setUart(uartId,baudrate,8,uart.PAR_NONE,uart.STOP_1) -- 设置串口通信参数,波特率为自适应
  1507. --gps.setAerialMode(1,1,0,0) -- 设置GPS+BD定位(此接口目前仅针对Air530或者Air800有效)
  1508. gps.setNmeaMode(2,nmeaCb) -- gps.lua和外部程序都处理NEMA数据
  1509. test(testIdx)
  1510. sys.timerLoopStart(printGps,2000)
  1511. end)
  1512. sys.taskInit(
  1513. function()
  1514. while true do
  1515. gps_status = 0
  1516. locat_cnt = 0
  1517. sys.wait(8 * 1000) -- 等待socket连接完毕
  1518. while true do
  1519. if tonumber(manual_location.type) == 0 then -- 判断是否手动定位 若手动定位直接关闭GPS
  1520. if socket.isReady() and gps_status ~=1 and gps_status ~= 2 then
  1521. log.warn("================ LBS Location successful ===============")
  1522. lbsLoc.request(getLocCb)
  1523. sys.wait(1000)
  1524. end
  1525. if gps.isFix() == false then
  1526. log.info("================ GPS Location failed ===============")
  1527. printGps()
  1528. elseif gps.isFix() == true then
  1529. gps_cont = gps_cont + 1
  1530. local location = gps.getLocation()
  1531. gps_loc_lat = location.lat
  1532. gps_loc_lng = location.lng
  1533. printGps()
  1534. if gps_cont >= 3 then -- 获取3次gps定位数据的最后一次
  1535. nvm.set("lat_gps",gps_loc_lat)
  1536. nvm.set("lng_gps",gps_loc_lng)
  1537. gps.closeAll()
  1538. end
  1539. gps_status = 1
  1540. log.info("================ GPS Location successful ===============")
  1541. end
  1542. sys.wait(1000)
  1543. log.info("GPS open status is:", gps.isOpen())
  1544. log.info("GPS location status is:", gps.isFix())
  1545. else
  1546. gps_status = 0
  1547. gps.closeAll()
  1548. sys.wait(2000)
  1549. end
  1550. sys.wait(1000)
  1551. locat_cnt = locat_cnt + 1
  1552. if locat_cnt >= 1800 then -- 每隔30分钟判断一下定位,若失败,则break
  1553. if (gps_loc_lng == "0" and gps_loc_lat == "0") and (lbs_loc_lat == "0" and lbs_loc_lng == "0") then
  1554. break
  1555. else
  1556. locat_cnt = 0
  1557. log.warn("location normal !!!")
  1558. end
  1559. end
  1560. end
  1561. end
  1562. end
  1563. )
  1564. ------------------------------------------------------ * LBS+GPS定位任务 结束* ------------------------------------------------
  1565. --++++++++++++++++++++++++++++++++++++++++++++++++++++ * SIM卡自动切换线程 * +++++++++++++++++++++++++++++++++++++++++++++++++++
  1566. local function cbFnc(result)
  1567. if result then
  1568. log.info("SIM switch successful")
  1569. else
  1570. log.info("SIM switch failed")
  1571. end
  1572. end
  1573. -- 说明:目前版本不支持SIM自动切换功能需手动切换SIM卡
  1574. -- 注意:本命令关机保存,但是需重启生效
  1575. -- 初次刷入程序默认SIM ID = 0
  1576. sys.taskInit(
  1577. function()
  1578. sys.wait(5 * 1000) -- 注意:开机lua脚本运行之后,会发送at命令去查询id,所以需要一定时间才能获取到id。开机后立即调用此接口,基本上返回nil
  1579. local ID1 = sim.getId()
  1580. local ID2 = ID1
  1581. log.info("Now SIM is:::", ID1)
  1582. if ID1 == 0 then
  1583. ID2 = 1
  1584. elseif ID1 == 1 then
  1585. ID2 = 0
  1586. end
  1587. sys.wait(5 * 1000)
  1588. while true do
  1589. if sim.getStatus() == false or sim.getStatus() == nil then -- 切换SIM卡
  1590. sim.setId(ID2,cbFnc)
  1591. sys.wait(1000)
  1592. sim.setId(ID2,cbFnc)
  1593. sys.restart("switch sim card")
  1594. end
  1595. local id = sim.getId() -- 获取目前设置的双卡单待id
  1596. log.info("Now system SIM card is:::", id)
  1597. sys.wait(5 * 1000)
  1598. end
  1599. end
  1600. )
  1601. --+++++++++++++++++++++++++++++++++++++++++++++++ * MQTT守护线程 * ++++++++++++++++++++++++++++++++++++++++++++++++
  1602. -- Mqtt异常监测
  1603. sys.taskInit(function()
  1604. sys.wait(10*1000)
  1605. local dev_dta_on = 1
  1606. while true do
  1607. if mqtt_status == "" then
  1608. mqtt_status_num = mqtt_status_num + 1
  1609. elseif mqtt_status == "true" then
  1610. mqtt_status = ""
  1611. mqtt_status_num = 0
  1612. sys.wait(1000)
  1613. end
  1614. if mqtt_status_num > timeout * 61 then
  1615. sys.restart("MQTT ERROR!!!")
  1616. end
  1617. log.warn("====================mqtt_check=================:",mqtt_status_num,mqtt_status,timeout)
  1618. sys.wait(1000)
  1619. end
  1620. end)
  1621. ---------------------------------------------------------------
  1622. --定时上报任务
  1623. sys.taskInit(
  1624. function()
  1625. sys.wait(15*1000)
  1626. if dat_f ==0 or dat_f == nil or dat_f =="" then
  1627. dat_f = 30
  1628. end
  1629. while true do
  1630. updata_freq = dat_f * 60
  1631. while updata_freq ~= 0 do
  1632. updata_freq = updata_freq - 1
  1633. sys.wait(1000)
  1634. log.info("++++++++ 1 !!!!!!!!")
  1635. end
  1636. if serverconf.ip == "8.136.98.49" then
  1637. sys.publish("NET_SENT_RDY_", upData())
  1638. sys.wait(100)
  1639. ClearKcnt() -- 上报数据清除电机次数
  1640. log.info("++++++++ 2 !!!!!!!!")
  1641. elseif serverconf.ip == "120.27.222.26" then
  1642. sys.publish("NET_SENT_RDY_", upData_120())
  1643. sys.publish("NET_SENT_RDY_", upStatus())
  1644. ClearKcnt() -- 上报数据清除电机次数
  1645. log.info("++++++++ 3 !!!!!!!!")
  1646. end
  1647. sys.wait(100)
  1648. log.info("+++++++++ 4 !!!!!!!!")
  1649. end
  1650. end
  1651. )
  1652. -- 每隔5秒更新主板数据and打印
  1653. sys.taskInit(function()
  1654. sys.wait(5 * 1000)
  1655. while true do
  1656. log.warn("................................Mainboard-uart 通信中1....................................")
  1657. PrintData()
  1658. --getStatus()
  1659. local firmware_name = rtos.get_version() -- 获取底层固件版本
  1660. local space = rtos.get_fs_free_size() -- 获取文件系统剩余空间(单位Bytes)
  1661. local netmode = net.getNetMode() -- 4:4G LTE网络
  1662. local SIMS = sim.getStatus() -- 获取sim卡的状态
  1663. log.warn("Clear worm time is:", clt_t)
  1664. log.warn("Data time is:", dat_f)
  1665. log.warn("Now firmware version is:", firmware_name .. " PROJECT:" .. _G.PROJECT .. " VERSION:" .. _G.VERSION)
  1666. log.warn("File system free space:", space)
  1667. log.warn("Net mode is:", netmode)
  1668. log.warn("Sim status is:", SIMS)
  1669. sys.wait(5 * 1000)
  1670. end
  1671. end)
  1672. --++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1673. -- 重启预警函数
  1674. function getRebootState()
  1675. rebootstate = io.readFile(DEV_REBOOT) -- 开机获取设备启动次数
  1676. rebootinfo = io.readFile(DEVICE_ERR) -- 开机获取设备软件重启原因
  1677. if rebootstate == nil then
  1678. log.info("first startup!!!!!!!!")
  1679. rebootstate = 0
  1680. end
  1681. rebootstate = rebootstate + 1
  1682. io.writeFile(DEV_REBOOT, rebootstate)
  1683. end
  1684. ---+++++++++++++++++++++++++++++++++++++++++++++ * Mainboard-uart 通信中 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1685. --每隔10秒打印输出
  1686. sys.taskInit(
  1687. function()
  1688. getRebootState()
  1689. sys.wait(100)
  1690. while true do
  1691. log.warn("................................Mainboard-uart 通信中....................................")
  1692. PrintData()
  1693. sys.wait(5 * 1000)
  1694. end
  1695. end
  1696. )
  1697. --BootLoader交互信息下发
  1698. sys.taskInit(
  1699. function()
  1700. sys.wait(500)
  1701. while true do
  1702. if BootLoader_flag == 1 then
  1703. BootLoader_flag = 0
  1704. log.info("BootLoader_flag",BootLoader_flag)
  1705. BootLoader_stm32()
  1706. log.info("stm32 SerialDownload !!!")
  1707. end
  1708. if upgrade_again == 1 then
  1709. log.info("update_stm32 system will upgrade again !!!")
  1710. update32.run()
  1711. end
  1712. sys.wait(500)
  1713. end
  1714. end
  1715. )
  1716. --串口通讯心跳交互
  1717. sys.taskInit(
  1718. function()
  1719. sys.wait(100)
  1720. local uart_flag_count = 0
  1721. local uart_32 = string.char(0xCC,0XDD)
  1722. while true do
  1723. if uart_flag == 1 then
  1724. uart_flag = 0
  1725. uart_flag_count = uart_flag_count + 1
  1726. if uart_flag_count > 5 then
  1727. uart_flag_count = 0
  1728. uart.write(1,uart_32)
  1729. log.info("uart_flag back!!!!")
  1730. end
  1731. end
  1732. sys.wait(50)
  1733. end
  1734. end
  1735. )
  1736. -- 串口缓冲区最大值
  1737. local SENDSIZE = 128
  1738. --串口写空闲
  1739. local writeIdle = {true}
  1740. --数据发送的消息队列
  1741. recvBuff = {{}}
  1742. local writeBuff = {{}}
  1743. local uartReadTime = 50 -- 串口读超时
  1744. local flowCount = {0, 0}
  1745. local uid = 1
  1746. -- 写标志位
  1747. local function writeDone()
  1748. if #writeBuff[1] == 0 then
  1749. writeIdle[1] = true
  1750. sys.publish("UART_" .. 1 .. "_WRITE_DONE")
  1751. log.warn("UART_" .. uid .. "write done!")
  1752. else
  1753. writeIdle[1] = false
  1754. uart.write(1, table.remove(writeBuff[1], 1))
  1755. log.warn("UART_" .. 1 .. "writing ...")
  1756. end
  1757. end
  1758. -- uart 写
  1759. function write(str)
  1760. if not str or str == "" or str == nil then
  1761. return
  1762. end
  1763. if str ~= true then
  1764. for i = 1, #str, SENDSIZE do
  1765. table.insert(writeBuff[1], str:sub(i, i + SENDSIZE - 1))
  1766. end
  1767. log.warn("uart" .. 1 .. ".write data length:", writeIdle[1], #str)
  1768. end
  1769. if writeIdle[1] and writeBuff[1] then
  1770. local str = table.concat(writeBuff[1])
  1771. table.remove(writeBuff[1], 1)
  1772. while true do
  1773. uart.write(1, str)
  1774. break
  1775. end
  1776. log.warn("UART_" .. uid .. "writing ...")
  1777. end
  1778. end
  1779. local function read(s)
  1780. local s = table.concat(recvBuff[1])
  1781. local idx, tmp = 1
  1782. local conf = s
  1783. recvBuff[1] = {}
  1784. -- 串口流量统计
  1785. flowCount[1] = flowCount[1] + #s
  1786. log.info("UART_" .. 1 .. "read length:", #s)
  1787. log.info("串口流量统计值:", flowCount[1])
  1788. log.info("UART:",s,string.toHex(s))
  1789. if updateflag == 1 then
  1790. log.info(s)
  1791. sys.publish("YM_IS_OK", s)
  1792. return
  1793. end
  1794. if s:find("update") then --判断是否进入升级模式
  1795. updateflag = 1
  1796. if update32.updateok == 0 then
  1797. s = upwarn("device_stm32", "system will update")
  1798. sys.publish("NET_SENT_RDY_", s)
  1799. else
  1800. s = upwarn("device_stm32", "system is updating......")
  1801. sys.publish("NET_SENT_RDY_", s)
  1802. end
  1803. log.info("update_stm32 system will update !!!")
  1804. update32.run()
  1805. end
  1806. if s:find("bootflag") then --重复远程升级标志位
  1807. upgrade_again = 1
  1808. end
  1809. if flowCount[1] == 60 then --串口通讯心跳检测
  1810. uart_flag = 1
  1811. end
  1812. if s:find("App") then --判断高压主板是否复位
  1813. local c = misc.getClock()
  1814. local date = string.format("%00004d-%02d-%02d %02d:%02d:%02d", c.year, c.month, c.day, c.hour, c.min, c.sec)
  1815. log.info("检测到stm32复位!!!")
  1816. nvm.set("reboot_stm8",nvm.get("reboot_stm8")+1)
  1817. nvm.set("json_w_stm8boot","检测到杀虫灯主板复位,总复位次数:"..nvm.get("reboot_stm8").."复位时间:"..date)
  1818. end
  1819. if #conf == nil or #conf == 0 or conf:len() == 60 then
  1820. log.info("getParam len,value:", string.toHex(conf, " ")) -- 将返回的数据填充到result表
  1821. result.ver = string.sub(conf, 45,59) -- 软件版本-DC-SCD-U-V1.0
  1822. idx, result.iSave = pack.unpack(conf, "b", idx) -- eeprom标志位
  1823. if result.iSave ~= 0xAA then return end -- 判断chuankou上传数据格式是否正确
  1824. idx, result_crc.ds = pack.unpack(conf, ">b", idx) -- 设备状态
  1825. if(result_crc.ds==0 or result_crc.ds==1) then result.ds = result_crc.ds end
  1826. idx, result_crc.tt = pack.unpack(conf, "b", idx) -- 光控时长小时
  1827. if(result_crc.tt>=0 and result_crc.tt<8) then result.tt = result_crc.tt end
  1828. idx, result_crc.ws = pack.unpack(conf, "b", idx) -- 工作状态
  1829. if(result_crc.ws==0 or result_crc.ws==1 or result_crc.ws==2) then result.ws = result_crc.ws end
  1830. idx, result_crc.lps = pack.unpack(conf, "b", idx) -- 光控状态
  1831. if(result_crc.lps==0 or result_crc.lps==1 or result_crc.lps==2) then result.lps = result_crc.lps end
  1832. idx, result_crc.rps = pack.unpack(conf, "b", idx) -- 雨控状态
  1833. if(result_crc.rps==0 or result_crc.rps==1) then
  1834. result.rps = result_crc.rps
  1835. if(result_crc.rps==1 and rps_y==0) then
  1836. rps_x = 1
  1837. elseif(result_crc.rps==0) then
  1838. rps_y = 0
  1839. end
  1840. end
  1841. idx, result_crc.tps = pack.unpack(conf, "b", idx) -- 温控状态
  1842. if(result_crc.tps==0 or result_crc.tps==1 or result_crc.tps==2) then result.tps = result_crc.tps end
  1843. idx, result_crc.dps = pack.unpack(conf, "b", idx) -- 倾倒保护
  1844. if(result_crc.dps==0 or result_crc.dps==1) then result.dps = result_crc.dps end
  1845. idx, result_crc.vps = pack.unpack(conf, "b", idx) -- 电压状态
  1846. if(result_crc.vps==0 or result_crc.vps==1 or result_crc.vps==2) then result.vps = result_crc.vps end
  1847. idx, result.lcm = pack.unpack(conf, "b", idx) -- 光控类型类型
  1848. idx, result_crc.hwcnt = pack.unpack(conf, "<H", idx) -- 红外传感器
  1849. if(result_crc.hwcnt>=0 and result_crc.hwcnt<20) then result.hwcnt = result_crc.hwcnt end
  1850. idx, result_crc.kcnt = pack.unpack(conf, "<H", idx) -- 电击击杀检测
  1851. if(result_crc.kcnt>=0 and result_crc.kcnt<20) then result.kcnt = result_crc.kcnt end
  1852. idx, result_crc.cv = pack.unpack(conf, "<H", idx) -- 充电电压
  1853. if(result_crc.cv>=0 and result_crc.cv<30000) then
  1854. result.cv = result_crc.cv
  1855. if result.cv < 500 then
  1856. result.cv = 0
  1857. end
  1858. end
  1859. idx, result_crc.bv = pack.unpack(conf, "<H", idx) -- 电池电压
  1860. if(result_crc.bv>=0 and result_crc.bv<30000) then result.bv = result_crc.bv end
  1861. idx, result_crc.bt = pack.unpack(conf, "<h", idx) -- 主板温度
  1862. if(result_crc.bt>-310 and result_crc.bt<1010) then result.bt = result_crc.bt end
  1863. idx, paramconf.vsunrise = pack.unpack(conf, "<H", idx) -- 日出电压
  1864. idx, paramconf.vsunset = pack.unpack(conf, "<H", idx) -- 日落电压
  1865. idx, paramconf.vcutof = pack.unpack(conf, "<H", idx) -- 充电截至电压
  1866. idx, paramconf.vfloat = pack.unpack(conf, "<H", idx) -- 充电浮充电压
  1867. idx, paramconf.vrecharge = pack.unpack(conf, "<H", idx) -- 再次启动充电动作的电压
  1868. idx, paramconf.vstart = pack.unpack(conf, "<H", idx) -- 首次启动电压
  1869. idx, paramconf.uvoff = pack.unpack(conf, "<H", idx) -- 欠压解锁电压
  1870. idx, paramconf.uvon = pack.unpack(conf, "<H", idx) -- 欠压锁定电压
  1871. idx, paramconf.overtemp = pack.unpack(conf, "<H", idx) -- 高温保护
  1872. idx, paramconf.lowtemp = pack.unpack(conf, "<H", idx) -- 低温保护
  1873. idx, paramconf.unovert = pack.unpack(conf, "<H", idx) -- 高温解锁
  1874. idx, paramconf.unlowt = pack.unpack(conf, "<H", idx) -- 低温解锁
  1875. return result
  1876. end
  1877. end
  1878. -- 串口配置
  1879. function uart_INIT()
  1880. uart.setup(1, 9600, 8, uart.PAR_NONE, uart.STOP_1)
  1881. uart.on(1, "sent", writeDone)
  1882. uart.on(
  1883. 1,
  1884. "receive",
  1885. function()
  1886. table.insert(recvBuff[1], uart.read(1, 8192))
  1887. sys.timerStart(sys.publish, tonumber(uartReadTime) or 30, "UART_RECV_WAIT_")
  1888. end
  1889. )
  1890. -- 处理串口接收到的数据
  1891. sys.subscribe("UART_RECV_WAIT_", read)
  1892. sys.subscribe("UART_SENT_RDY_", write)
  1893. end
  1894. local timecnt = 1
  1895. -- 每隔1分钟重置串口计数
  1896. sys.timerLoopStart(
  1897. function()
  1898. flow = tonumber(flow)
  1899. if flow and flow ~= 0 then
  1900. if flowCount[1] > flow then
  1901. uart.on(1, "receive")
  1902. log.info("uart1.read length count:", flowCount[1])
  1903. end
  1904. end
  1905. if timecnt > 10 then
  1906. timecnt = 1
  1907. flowCount = {0, 0}
  1908. else
  1909. timecnt = timecnt + 1
  1910. end
  1911. end,
  1912. 1000
  1913. )
  1914. --*********************开机运行**************************
  1915. uart_INIT()