ultra_low_power.lua 2.5 KB

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