tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/sx126x/lora_rxtx/lora_rxtx.go (about) 1 package main 2 3 // In this example, a Lora packet will be sent every 10s 4 // module will be in RX mode between two transmissions 5 6 import ( 7 "machine" 8 "time" 9 10 "tinygo.org/x/drivers/lora" 11 "tinygo.org/x/drivers/sx126x" 12 ) 13 14 const ( 15 LORA_DEFAULT_RXTIMEOUT_MS = 1000 16 LORA_DEFAULT_TXTIMEOUT_MS = 5000 17 ) 18 19 var ( 20 loraRadio *sx126x.Device 21 txmsg = []byte("Hello TinyGO") 22 ) 23 24 func main() { 25 time.Sleep(3 * time.Second) 26 27 println("\n# TinyGo Lora RX/TX test") 28 println("# ----------------------") 29 machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput}) 30 31 // Create the driver 32 loraRadio = sx126x.New(spi) 33 loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262) 34 35 // Create radio controller for target 36 loraRadio.SetRadioController(newRadioControl()) 37 38 // Detect the device 39 state := loraRadio.DetectDevice() 40 if !state { 41 panic("sx126x not detected.") 42 } 43 44 loraConf := lora.Config{ 45 Freq: lora.MHz_868_1, 46 Bw: lora.Bandwidth_125_0, 47 Sf: lora.SpreadingFactor9, 48 Cr: lora.CodingRate4_7, 49 HeaderType: lora.HeaderExplicit, 50 Preamble: 12, 51 Ldr: lora.LowDataRateOptimizeOff, 52 Iq: lora.IQStandard, 53 Crc: lora.CRCOn, 54 SyncWord: lora.SyncPrivate, 55 LoraTxPowerDBm: 20, 56 } 57 58 loraRadio.LoraConfig(loraConf) 59 60 var count uint 61 for { 62 start := time.Now() 63 64 println("main: Receiving Lora for 10 seconds") 65 for time.Since(start) < 10*time.Second { 66 buf, err := loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS) 67 if err != nil { 68 println("RX Error: ", err) 69 } else if buf != nil { 70 println("Packet Received: len=", len(buf), string(buf)) 71 } 72 } 73 println("main: End Lora RX") 74 println("LORA TX size=", len(txmsg), " -> ", string(txmsg)) 75 err := loraRadio.Tx(txmsg, LORA_DEFAULT_TXTIMEOUT_MS) 76 if err != nil { 77 println("TX Error:", err) 78 } 79 count++ 80 } 81 82 }