tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lora/lorawan/atcmd/main.go (about)

     1  // AT command set console running on the device UART to communicate with
     2  // an attached LoRa device.
     3  //
     4  // Computer <-> UART <-> MCU <-> SPI <-> SX126x/SX127x
     5  //
     6  // Connect using default baudrate for this hardware, 8-N-1 with your terminal program.
     7  // For details on the AT command set, see:
     8  // https://files.seeedstudio.com/products/317990687/res/LoRa-E5%20AT%20Command%20Specification_V1.0%20.pdf
     9  package main
    10  
    11  import (
    12  	"machine"
    13  	"time"
    14  
    15  	"tinygo.org/x/drivers/examples/lora/lorawan/common"
    16  	"tinygo.org/x/drivers/lora"
    17  	"tinygo.org/x/drivers/lora/lorawan"
    18  	"tinygo.org/x/drivers/lora/lorawan/region"
    19  )
    20  
    21  // change these to test a different UART or pins if available
    22  var (
    23  	uart  = machine.Serial
    24  	tx    = machine.UART_TX_PIN
    25  	rx    = machine.UART_RX_PIN
    26  	input = make([]byte, 0, 64)
    27  
    28  	radio   lora.Radio
    29  	session *lorawan.Session
    30  	otaa    *lorawan.Otaa
    31  
    32  	defaultTimeout uint32 = 1000
    33  )
    34  
    35  var reg string
    36  
    37  func main() {
    38  	uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
    39  
    40  	var err error
    41  	radio, err = common.SetupLora()
    42  	if err != nil {
    43  		fail(err.Error())
    44  	}
    45  
    46  	session = &lorawan.Session{}
    47  	otaa = &lorawan.Otaa{}
    48  	lorawan.UseRadio(radio)
    49  
    50  	switch reg {
    51  	case "AU915":
    52  		lorawan.UseRegionSettings(region.AU915())
    53  	case "EU868":
    54  		lorawan.UseRegionSettings(region.EU868())
    55  	case "US915":
    56  		lorawan.UseRegionSettings(region.US915())
    57  	default:
    58  		lorawan.UseRegionSettings(region.EU868())
    59  	}
    60  
    61  	for {
    62  		if uart.Buffered() > 0 {
    63  			data, _ := uart.ReadByte()
    64  
    65  			switch data {
    66  			case 13:
    67  				// return key
    68  				if err := parse(input); err != nil {
    69  					uart.Write([]byte("ERROR: "))
    70  					uart.Write([]byte(err.Error()))
    71  					crlf()
    72  				}
    73  				input = input[:0]
    74  			default:
    75  				// just capture the character
    76  				input = append(input, data)
    77  			}
    78  		}
    79  		time.Sleep(10 * time.Millisecond)
    80  	}
    81  }
    82  
    83  func fail(msg string) {
    84  	for {
    85  		uart.Write([]byte(msg))
    86  		crlf()
    87  
    88  		time.Sleep(time.Minute)
    89  	}
    90  }