psm_plus.lua 2.7 KB

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