tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lora/lorawan/basic-demo/main.go (about) 1 // Simple code for connecting to Lorawan network and uploading sample payload 2 package main 3 4 import ( 5 "errors" 6 "strconv" 7 "time" 8 9 "tinygo.org/x/drivers/examples/lora/lorawan/common" 10 "tinygo.org/x/drivers/lora" 11 "tinygo.org/x/drivers/lora/lorawan" 12 "tinygo.org/x/drivers/lora/lorawan/region" 13 ) 14 15 var ( 16 reg string 17 debug string 18 ) 19 20 const ( 21 LORAWAN_JOIN_TIMEOUT_SEC = 180 22 LORAWAN_RECONNECT_DELAY_SEC = 15 23 LORAWAN_UPLINK_DELAY_SEC = 60 24 ) 25 26 var ( 27 radio lora.Radio 28 session *lorawan.Session 29 otaa *lorawan.Otaa 30 ) 31 32 func loraConnect() error { 33 start := time.Now() 34 var err error 35 for time.Since(start) < LORAWAN_JOIN_TIMEOUT_SEC*time.Second { 36 println("Trying to join network") 37 err = lorawan.Join(otaa, session) 38 if err == nil { 39 println("Connected to network !") 40 return nil 41 } 42 println("Join error:", err, "retrying in", LORAWAN_RECONNECT_DELAY_SEC, "sec") 43 time.Sleep(time.Second * LORAWAN_RECONNECT_DELAY_SEC) 44 } 45 46 err = errors.New("Unable to join Lorawan network") 47 println(err.Error()) 48 return err 49 } 50 51 func failMessage(err error) { 52 println("FATAL:", err) 53 for { 54 } 55 } 56 57 func main() { 58 println("*** Lorawan basic join and uplink demo ***") 59 60 // Board specific Lorawan initialization 61 var err error 62 radio, err = common.SetupLora() 63 if err != nil { 64 failMessage(err) 65 } 66 67 // Required for LoraWan operations 68 session = &lorawan.Session{} 69 otaa = &lorawan.Otaa{} 70 71 // Connect the lorawan with the Lora Radio device. 72 lorawan.UseRadio(radio) 73 switch reg { 74 case "AU915": 75 lorawan.UseRegionSettings(region.AU915()) 76 case "EU868": 77 lorawan.UseRegionSettings(region.EU868()) 78 case "US915": 79 lorawan.UseRegionSettings(region.US915()) 80 default: 81 lorawan.UseRegionSettings(region.EU868()) 82 } 83 84 // Configure AppEUI, DevEUI, APPKey, and public/private Lorawan Network 85 setLorawanKeys() 86 87 if debug != "" { 88 println("main: Network joined") 89 println("main: DevEui, " + otaa.GetDevEUI()) 90 println("main: AppEui, " + otaa.GetAppEUI()) 91 println("main: DevAddr, " + otaa.GetAppKey()) 92 } 93 94 // Try to connect Lorawan network 95 if err := loraConnect(); err != nil { 96 failMessage(err) 97 } 98 99 if debug != "" { 100 println("main: NetID, " + otaa.GetNetID()) 101 println("main: NwkSKey, " + session.GetNwkSKey()) 102 println("main: AppSKey, " + session.GetAppSKey()) 103 println("main: Done") 104 } 105 // Try to periodicaly send an uplink sample message 106 upCount := 1 107 for { 108 payload := "Hello TinyGo #" + strconv.Itoa(upCount) 109 110 if err := lorawan.SendUplink([]byte(payload), session); err != nil { 111 println("Uplink error:", err) 112 } else { 113 println("Uplink success, msg=", payload) 114 } 115 116 println("Sleeping for", LORAWAN_UPLINK_DELAY_SEC, "sec") 117 time.Sleep(time.Second * LORAWAN_UPLINK_DELAY_SEC) 118 upCount++ 119 } 120 }