main.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "wifidemo"
  3. VERSION = "1.0.0"
  4. -- 引入必要的库文件(lua编写), 内部库不需要require
  5. sys = require("sys")
  6. require("sysplus")
  7. if wdt then
  8. --添加硬狗防止程序卡死,在支持的设备上启用这个功能
  9. wdt.init(9000)--初始化watchdog设置为9s
  10. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  11. end
  12. -- OTA任务
  13. function ota_task()
  14. sys.taskInit(function()
  15. local dst_path = "/update.bin"
  16. os.remove(dst_path)
  17. local url = "http://site0.cn/api/esp32/ota?mac=" .. wlan.getMac()
  18. local code = http.request("GET", url, nil, nil, {dst=dst_path}).wait()
  19. if code and code == 200 then
  20. log.info("ota", "OTA 下载完成, 3秒后重启")
  21. sys.wait(3000)
  22. rtos.reboot()
  23. end
  24. log.info("ota", "服务器返回非200,就是不需要升级", code)
  25. os.remove(dst_path)
  26. end)
  27. end
  28. sys.taskInit(function()
  29. sys.wait(100)
  30. wlan.init()
  31. sys.wait(100)
  32. wlan.connect("luatos1234", "123456890")
  33. log.info("wlan", "wait for IP_READY", wlan.getMac())
  34. sys.waitUntil("IP_READY", 30000)
  35. -- 联网后,先执行一次OTA
  36. ota_task()
  37. -- 然后每隔6小时执行一次OTA
  38. sys.timerLoopStart(ota_task, 6*3600*1000)
  39. end)
  40. -- 用户代码已结束---------------------------------------------
  41. -- 结尾总是这一句
  42. sys.run()
  43. -- sys.run()之后后面不要加任何语句!!!!!