main.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. -- ctiot库API
  2. -- ctiot.init()
  3. -- 初始化引擎,开机后只需要一次,没有参数输入输出
  4. -- ctiot.ep(new_ep)
  5. -- 设置读取用户自定义EP,如果不设置,则使用IMEI,输入新的EP,同时输出当前EP,
  6. -- 输入nil,则为读取,输入"",清除EP
  7. -- ctiot.param(server_ip, server_port, lifetime)
  8. -- 设置读取必要的参数,分别为服务器IP,端口和保活时间
  9. -- 输入nil,则为读取参数
  10. -- ctiot.connect()
  11. -- 启动连接服务器
  12. -- ctiot.disconnect()
  13. -- 断开服务器
  14. -- ctiot.write(data, mode, seq)
  15. -- 发送数据,输入参数为数据,发送模式,发送序号
  16. -- 发送模式为ctiot.CON, ctiot.NON, ctiot.NON_REL, ctiot.CON_REL
  17. -- 发送序号只是一个标识,不会影响发送的数据和模式,在发送结果回调时会返回
  18. -- ctiot.isReady()
  19. -- 检测当前ctiot连接状态是否可以发送数据
  20. -- 0可以,其他都不可以
  21. PROJECT = "ctiot"
  22. VERSION = "1.0.0"
  23. local TAG="ctiot"
  24. -- sys库是标配
  25. _G.sys = require("sys")
  26. local function cb_rx(data)
  27. log.info(TAG, "rx", data:toHex())
  28. end
  29. local function cb_dereg(error, error_code, param)
  30. log.info(TAG, "dereg", error, error_code, param)
  31. end
  32. local function cb_wakeup(error, error_code, param)
  33. log.info(TAG, "wakeup", error, error_code, param)
  34. end
  35. local function cb_fota(error, error_code, param)
  36. log.info(TAG, "fota", error, error_code, param)
  37. end
  38. local function cb_other(error, error_code, param)
  39. log.info(TAG, "other", error, error_code, param)
  40. end
  41. --sys.subscribe("CTIOT_TX", cb_tx)
  42. sys.subscribe("CTIOT_RX", cb_rx)
  43. --sys.subscribe("CTIOT_REG", cb_reg)
  44. --sys.subscribe("CTIOT_DEREG", cb_dereg)
  45. --sys.subscribe("CTIOT_WAKEUP", cb_wakeup)
  46. sys.subscribe("CTIOT_OTHER", cb_other)
  47. sys.subscribe("CTIOT_FOTA", cb_fota)
  48. local function send_test()
  49. log.info(TAG, "test tx")
  50. local result, tx_error, error_code, param
  51. --发送的数据请按照自己的profile修改
  52. --发送数据,并要求服务器应答
  53. result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 0)
  54. --发送数据,并要求服务器应答,序号为11
  55. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 11)
  56. --发送数据,不需要服务器应答
  57. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 0)
  58. --发送数据,不需要服务器应答,序号为23
  59. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 23)
  60. --发送数据,不需要服务器应答,发送完成后立刻释放RRC,加快进入休眠的速度
  61. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 0)
  62. --发送数据,不需要服务器应答,发送完成后立刻释放RRC,加快进入休眠的速度,序号为56
  63. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 56)
  64. --发送数据,并要求服务器应答,接收到应答后立刻释放RRC,加快进入休眠的速度
  65. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 0)
  66. --发送数据,并要求服务器应答,接收到应答后立刻释放RRC,加快进入休眠的速度,序号为255,序号最大255
  67. --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 255)
  68. log.info(TAG, result, tx_error)
  69. result, tx_error, error_code, param = sys.waitUntilExt("CTIOT_TX", 20000)
  70. if result then
  71. if not tx_error then
  72. log.info(TAG, "tx ok", param)
  73. else
  74. log.info(TAG, "tx fail", error_code, param)
  75. ctiot.disconnect()
  76. result, dereg_error, error_code, param = sys.waitUntilExt("CTIOT_DEREG", 30000)
  77. ctiot.connect()
  78. while true do
  79. result, error, error_code, param = sys.waitUntilExt("CTIOT_REG", 60000)
  80. if not result and error==nil then
  81. log.info(TAG, "reg wait timeout")
  82. break
  83. end
  84. log.info(TAG, result, error, error_code, param)
  85. if not error and param > 0 then
  86. log.info(TAG, "reg ok")
  87. isConnected = true
  88. break
  89. end
  90. if error then
  91. log.info(TAG, "reg fail")
  92. break
  93. end
  94. end
  95. end
  96. else
  97. log.info(TAG, "tx no work")
  98. end
  99. end
  100. local function task()
  101. local inSleep = false
  102. local isConnected = false
  103. local result, error, error_code, param
  104. if pm.lastReson() ~= 0 then
  105. --如果不是自己的定时器唤醒,继续睡
  106. if pm.dtimerCheck(0) then
  107. pm.request(pm.HIB)
  108. return
  109. end
  110. --唤醒的不需要处理
  111. pm.request(pm.IDLE)
  112. log.info(TAG, "after", ctiot.ep(), ctiot.param())
  113. --一定要等待一下,否则会重启!!!
  114. sys.wait(1000)
  115. else
  116. --设置自定义EP,如果不设置,则使用IMEI
  117. --local ep="867814046436271"
  118. --local ep="qweasdzxcrtyfgh"
  119. --if ctiot.ep() ~= ep then
  120. --ctiot.ep(ep)
  121. --end
  122. ctiot.ep(nil)
  123. --设置服务器IP,端口,保活时间
  124. --ctiot.param("180.101.147.115", 5683, 300)
  125. ctiot.param("221.229.214.202", 5683, 300)
  126. local ip, port, keeptime = ctiot.param()
  127. log.info(TAG, "set param", ip, port, keeptime)
  128. end
  129. while not socket.isReady() do
  130. log.info("net", "wait for network ready")
  131. sys.waitUntil("NET_READY", 1000)
  132. end
  133. --无论是重启还是从深度休眠唤醒,必须重新启动连接
  134. ctiot.connect()
  135. while true do
  136. result, error, error_code, param = sys.waitUntilExt("CTIOT_REG", 60000)
  137. if not result and error==nil then
  138. log.info(TAG, "reg wait timeout")
  139. break
  140. end
  141. log.info(TAG, result, error, error_code, param)
  142. if not error and param > 0 then
  143. log.info(TAG, "reg ok")
  144. isConnected = true
  145. break
  146. end
  147. if error then
  148. log.info(TAG, "reg fail")
  149. break
  150. end
  151. end
  152. if isConnected then
  153. send_test()
  154. --凡是不进入深度休眠的,221.229.214.202 这个IP必须在保活时间内update一下
  155. --ctiot.update()
  156. --result, error, error_code, param = sys.waitUntilExt("CTIOT_UPDATE", 60000)
  157. end
  158. -- 30秒后唤醒,发个测试数据
  159. if not inSleep then
  160. pm.dtimerStart(0, 30000)
  161. inSleep = true
  162. end
  163. -- 我要睡了!!! 事实上还会等几秒, 这时候服务器还能下发数据
  164. pm.request(pm.HIB)
  165. -- 退出CTIOT
  166. -- ctiot.dereg()
  167. -- result, dereg_error, error_code, param = sys.waitUntilExt("CTIOT_DEREG", 30000)
  168. end
  169. ctiot.init()
  170. sys.taskInit(task)
  171. sys.run()