uartSentFile.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --- 模块功能:对101进行远程升级
  2. module(..., package.seeall)
  3. require "utils"
  4. require "pm"
  5. require "http"
  6. local PRODUCT_KEY = "icqZ0TNzOnBrZGuCueVPJFRQ4cXNfvFJ"
  7. -- 本demo实现了一个简单http请求升级文件,将升级文件从串口发送对101进行升级,仅供参考
  8. -- 注意!!!724串口电平为1.8V,Air101串口电平为3.3V,需要电平转换
  9. --[[
  10. 硬件连接
  11. Air724UG Air101
  12. UART1_TX ------ U1_RX (PB_07)
  13. UART1_RX ------ U1_TX (PB_06)
  14. GND ------ GND
  15. ]]
  16. local function httpCbFnc(result, prompt, head, body)
  17. log.info("result", result)
  18. if result then
  19. log.info("文件GET成功")
  20. sys.publish("HTTP_GET_SUCCESS")
  21. else
  22. log.info("文件GET失败")
  23. end
  24. end
  25. local uartID = 1
  26. local checkSwitch
  27. sys.taskInit(function()
  28. uart.on(uartID, "sent", function() sys.publish("UART1_SENT101_OK") end)
  29. uart.on(uartID, "receive", function() sys.publish("UART1_DATA_INCOMING") end)
  30. uart.setup(uartID, 115200, 8, uart.PAR_NONE, uart.STOP_1, nil, 1)
  31. local _, ver, imei, bsp = sys.waitUntil("HTTP_GET_START")
  32. log.info("test", _, ver, imei, bsp)
  33. local url =
  34. "http://iot.openluat.com/api/site/firmware_upgrade?project_key=" ..
  35. PRODUCT_KEY .. "&imei=" .. imei .. "&firmware_name=" .. bsp ..
  36. "&version=" .. ver
  37. http.request("GET", url, nil, nil, nil, 30000, httpCbFnc, "/lua/update.bin")
  38. sys.waitUntil("HTTP_GET_SUCCESS")
  39. local fileHandle = io.open("/lua/update.bin", "rb")
  40. if not fileHandle then
  41. log.error("open updateFile error")
  42. return
  43. end
  44. local size = io.fileSize("/lua/update.bin")
  45. log.info("打印大小", size)
  46. pm.wake("UART1_SENT101")
  47. uart.write(uartID, "###" .. size .. "&&&")
  48. sys.waitUntil("UART1_SENT101_OK")
  49. while true do
  50. local data = fileHandle:read(1460)
  51. if not data then break end
  52. uart.write(uartID, data)
  53. sys.waitUntil("UART1_SENT101_OK")
  54. end
  55. uart.close(uartID)
  56. pm.sleep("UART1_SENT101")
  57. fileHandle:close()
  58. os.remove("/lua/update.bin")
  59. checkSwitch = false
  60. end)
  61. local rdbuf = ""
  62. local function clearRdbuf() rdbuf = "" end
  63. local function proc(s)
  64. rdbuf = rdbuf .. s
  65. if not rdbuf:find("###VER:.+BSP:.+IMEI:.+&&&") then
  66. sys.timerStart(clearRdbuf, 5000)
  67. else
  68. sys.timerStop(clearRdbuf)
  69. local ver, bsp, imei = string.match(rdbuf,
  70. "###VER:(.+)BSP:(.+)IMEI:(.+)&&&")
  71. rdbuf = ""
  72. if not checkSwitch then
  73. checkSwitch = true
  74. sys.publish("HTTP_GET_START", ver, imei, bsp)
  75. end
  76. end
  77. end
  78. sys.taskInit(function()
  79. while true do
  80. sys.waitUntil("UART1_DATA_INCOMING")
  81. while true do
  82. local s = uart.read(uartID, 8192)
  83. if s and #s > 0 then
  84. proc(s)
  85. else
  86. break
  87. end
  88. end
  89. end
  90. end)
  91. -- 通知101要升级了,把unique_id发过来
  92. sys.timerStart(uart.write, 5000, uartID, "CHECK_UNIQUE_ID\r\n")