main.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --- 模块功能:lorademo
  2. -- @module lora
  3. -- @author Dozingfiretruck
  4. -- @release 2021.06.17
  5. -- LuaTools需要PROJECT和VERSION这两个信息
  6. PROJECT = "lorademo"
  7. VERSION = "1.0.0"
  8. log.info("main", PROJECT, VERSION)
  9. -- sys库是标配
  10. _G.sys = require("sys")
  11. --添加硬狗防止程序卡死
  12. if wdt then
  13. wdt.init(9000)--初始化watchdog设置为9s
  14. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  15. end
  16. local rtos_bsp = rtos.bsp()
  17. -- spi_id,pin_cs,pin_reset,pin_busy,pin_dio1
  18. local function lora_pin()
  19. if rtos_bsp == "AIR101" then
  20. return 0,pin.PB04,pin.PB00,pin.PB01,pin.PB06
  21. elseif rtos_bsp == "AIR103" then
  22. return 0,pin.PB04,pin.PB00,pin.PB01,pin.PB06
  23. elseif rtos_bsp == "AIR105" then
  24. return 5,pin.PC14,pin.PE08,pin.PE09,pin.PE06
  25. elseif rtos_bsp == "ESP32C3" then
  26. return 2,7,6,11,5
  27. elseif rtos_bsp == "ESP32S3" then
  28. return 2,14,15,13,12
  29. elseif rtos_bsp == "EC618" then
  30. return 0,8,1,18,19
  31. elseif string.find(rtos_bsp,"EC718") then
  32. return 0,8,1,31,32
  33. else
  34. log.info("main", "bsp not support")
  35. return
  36. end
  37. end
  38. local spi_id,pin_cs,pin_reset,pin_busy,pin_dio1 = lora_pin()
  39. sys.taskInit(function()
  40. spi_lora = spi.deviceSetup(spi_id,pin_cs,0,0,8,10*1000*1000,spi.MSB,1,0)
  41. lora_device = lora2.init("llcc68",{res = pin_reset,busy = pin_busy,dio1 = pin_dio1},spi_lora)
  42. print("lora_device",lora_device)
  43. lora_device:set_channel(433000000)
  44. lora_device:set_txconfig({mode=1,power=22,fdev=0,bandwidth=0,datarate=9,coderate=4,preambleLen=8,
  45. fixLen=false,crcOn=true,freqHopOn=0,hopPeriod=0,iqInverted=false,timeout=3000}
  46. )
  47. lora_device:set_rxconfig({mode=1,bandwidth=0,datarate=9,coderate=4,bandwidthAfc=0,preambleLen=8,symbTimeout=0,fixLen=false,
  48. payloadLen=0,crcOn=true,freqHopOn=0,hopPeriod=0,iqInverted=false,rxContinuous=false}
  49. )
  50. lora_device:on(function(lora_device, event, data, size)
  51. log.info("lora", "event", event, lora_device, data, size)
  52. if event == "tx_done" then
  53. lora_device:recv(1000)
  54. elseif event == "rx_done" then
  55. lora_device:send("PING")
  56. elseif event == "tx_timeout" then
  57. elseif event == "rx_timeout" then
  58. lora_device:recv(1000)
  59. elseif event == "rx_error" then
  60. end
  61. end)
  62. lora_device:send("PING")
  63. while 1 do
  64. print("lora")
  65. sys.wait(1000)
  66. end
  67. end)
  68. -- 用户代码已结束---------------------------------------------
  69. -- 结尾总是这一句
  70. sys.run()
  71. -- sys.run()之后后面不要加任何语句!!!!!