psm_plus.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. local server_ip = "112.125.89.8" --如果用TCP服务器,目前需要在用极致功耗模式时先断开服务器
  2. local server_port = 48810 --换成自己的
  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) -- demo用TCP服务器,目前需要在用极致功耗模式时先断开服务器
  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.setup(23,nil)
  56. gpio.close(35) --这里pwrkey接地才需要,不接地通过按键控制的不需要
  57. gpio.setup(32, function() --配置wakeup中断,外部唤醒用
  58. log.info("gpio")
  59. end, gpio.PULLUP, gpio.FALLING)
  60. pm.dtimerStart(3, period) --启动深度休眠定时器
  61. mobile.flymode(0,true)
  62. pm.power(pm.WORK_MODE,3) --进入极致功耗模式
  63. log.info(rtos.meminfo("sys"))
  64. sys.wait(15000) --demo演示唤醒时间是一分钟,如果15s后模块重启,则说明进入极致功耗模式失败,
  65. log.info("进入极致功耗模式失败,尝试重启")
  66. rtos.reboot()
  67. end
  68. sysplus.taskInitEx(testTask, d1Name, netCB, server_ip, server_port)