main.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "HZ201P"
  3. VERSION = "1.0.4"
  4. log.info("main", PROJECT, VERSION)
  5. -- 引入必要的库文件(lua编写), 内部库不需要require
  6. sys = require "sys"
  7. sysplus = require("sysplus")
  8. --mcu.hardfault(0)
  9. --运营商给的dns经常抽风,手动指定
  10. socket.setDNS(nil, 1, "223.5.5.5")
  11. socket.setDNS(nil, 2, "119.29.29.29")
  12. pm.ioVol(pm.IOVOL_ALL_GPIO, 1800)
  13. -- gnss的备电和gsensor的供电
  14. local vbackup = gpio.setup(24, 1)
  15. -- gnss的供电
  16. local gpsPower = gpio.setup(26, 1)
  17. -- 使用合宙iot平台时需要这个参数
  18. PRODUCT_KEY = "YXdzIDo5QawWCIRywShMAKjmJsInXtsb" -- 到 iot.openluat.com 创建项目,获取正确的项目id
  19. libfota = require "libfota"
  20. function fota_cb(ret)
  21. log.info("fota", ret)
  22. if ret == 0 then
  23. rtos.reboot()
  24. end
  25. end
  26. -- 使用合宙iot平台进行升级
  27. sys.subscribe("IP_READY",function()
  28. libfota.request(fota_cb)
  29. sys.timerLoopStart(libfota.request, 3600000, fota_cb)
  30. end)
  31. --云平台逻辑
  32. require "cloud"
  33. --获取所有参数
  34. attributes = require "attributes"
  35. attributes.initial()--初始化
  36. --gnss
  37. require "gnss"
  38. -- Gsensor
  39. require "da267"
  40. sys.subscribe("STEP_COUNTER", function(step)
  41. log.info("STEP_COUNTER", step)
  42. if step > 0 then
  43. attributes.set("step", step)
  44. end
  45. end)
  46. -- LED
  47. --全局状态变量
  48. _G_CONNECTED = false
  49. local blueLed = gpio.setup(1, 0)
  50. local redLed = gpio.setup(16, 0, nil, nil, 4)
  51. sys.taskInit(function()
  52. while true do
  53. if attributes.get("ledControl") then
  54. blueLed(attributes.get("blueLed") and 1 or 0)
  55. redLed(attributes.get("redLed") and 1 or 0)
  56. sys.wait(500)
  57. else
  58. redLed(attributes.get("isCharging") and 1 or 0)
  59. blueLed(1)
  60. sys.wait(_G_CONNECTED and 100 or 1000)
  61. blueLed(0)
  62. sys.wait(_G_CONNECTED and 100 or 1000)
  63. end
  64. end
  65. end)
  66. --关机键
  67. local powerTimer
  68. local powerKey = gpio.setup(46, function()
  69. log.info("powerKey", gpio.get(46))
  70. if gpio.get(46) == 0 then
  71. sys.publish("POWERKEY_PRESSED")
  72. powerTimer = sys.timerStart(function()
  73. log.info("powerKey", "long press")
  74. --把灯关都掉,让用户以为已经关机了
  75. blueLed(0) redLed(0)
  76. blueLed = function() end
  77. redLed = blueLed
  78. --两秒后真正关机
  79. sys.timerStart(pm.shutdown, 2000)
  80. end, 3000)
  81. else
  82. if powerTimer then
  83. sys.timerStop(powerTimer)
  84. log.info("powerKey", "stop press")
  85. end
  86. end
  87. end,gpio.PULLUP)
  88. --电量检测与上报
  89. require "battery"
  90. --信号值检测与上报
  91. sys.taskInit(function ()
  92. while true do
  93. attributes.set("rsrp", mobile.rsrp())
  94. attributes.set("rsrq", mobile.rsrq())
  95. sys.wait(60000)
  96. end
  97. end)
  98. --拨打电话与音频
  99. require "ccVolte"
  100. -- SIM DETECT
  101. local simCheck = gpio.setup(41, function()
  102. log.info("sim status", gpio.get(41))
  103. end)
  104. -- 用户代码已结束---------------------------------------------
  105. -- 结尾总是这一句
  106. sys.run()