ultra_low_power.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. local server_ip = "112.125.89.8"
  2. local server_port = 47523 -- 换成自己的
  3. local period = 3 * 60 * 60 * 1000 -- 3小时唤醒一次
  4. local reason, slp_state = pm.lastReson() -- 获取唤醒原因
  5. log.info("wakeup state", pm.lastReson())
  6. local libnet = require "libnet"
  7. local d1Name = "D1_TASK"
  8. local function netCB(msg)
  9. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  10. end
  11. local function testTask(ip, port)
  12. local txData
  13. if reason == 0 then
  14. txData = "normal wakeup"
  15. elseif reason == 1 then
  16. txData = "timer wakeup"
  17. elseif reason == 2 then
  18. txData = "pad wakeup"
  19. elseif reason == 3 then
  20. txData = "uart1 wakeup"
  21. end
  22. if slp_state > 0 then
  23. mobile.flymode(0, false) -- 退出飞行模式,进入psm+前进入飞行模式,唤醒后需要主动退出
  24. end
  25. --gpio.close(32)
  26. local netc, needBreak
  27. local result, param, is_err
  28. netc = socket.create(nil, d1Name)
  29. socket.debug(netc, false)
  30. socket.config(netc)
  31. local retry = 0
  32. while retry < 3 do
  33. log.info(rtos.meminfo("sys"))
  34. result = libnet.waitLink(d1Name, 0, netc)
  35. result = libnet.connect(d1Name, 5000, netc, ip, port)
  36. if result then
  37. log.info("服务器连上了")
  38. result, param = libnet.tx(d1Name, 15000, netc, txData)
  39. if not result then
  40. log.info("服务器断开了", result, param)
  41. break
  42. else
  43. needBreak = true
  44. end
  45. else
  46. log.info("服务器连接失败")
  47. end
  48. libnet.close(d1Name, 5000, netc)
  49. retry = retry + 1
  50. if needBreak then
  51. break
  52. end
  53. end
  54. uart.setup(1, 9600) -- 配置uart1,外部唤醒用
  55. -- 配置GPIO以达到最低功耗的目的
  56. gpio.close(23)
  57. gpio.close(45)
  58. gpio.close(46) --这里pwrkey接地才需要,不接地通过按键控制的不需要
  59. pm.dtimerStart(3, period) -- 启动深度休眠定时器
  60. mobile.flymode(0, true) -- 启动飞行模式,规避可能会出现的网络问题
  61. pm.power(pm.WORK_MODE, 3) -- 进入极致功耗模式
  62. sys.wait(15000) -- demo演示唤醒时间是三十分钟,如果15s后模块重启,则说明进入极致功耗模式失败,
  63. log.info("进入极致功耗模式失败,尝试重启")
  64. rtos.reboot()
  65. end
  66. sysplus.taskInitEx(testTask, d1Name, netCB, server_ip, server_port)