ultra_low_power.lua 2.5 KB

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