mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-17 05:13:23 +00:00
examples: lorawan atcmd able to join
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/lora/lorawan"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Use to test if connection to module is OK.
|
// Use to test if connection to module is OK.
|
||||||
@@ -23,13 +25,42 @@ func id(args string) error {
|
|||||||
param, val, hasComma := strings.Cut(args, ",")
|
param, val, hasComma := strings.Cut(args, ",")
|
||||||
if hasComma {
|
if hasComma {
|
||||||
// set
|
// set
|
||||||
|
data := strings.Trim(val, "\"'")
|
||||||
|
|
||||||
|
// convert data from hex formatted string
|
||||||
|
data = strings.ReplaceAll(data, " ", "")
|
||||||
|
hexdata, err := hex.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
switch param {
|
switch param {
|
||||||
case "DevAddr":
|
case "DevAddr":
|
||||||
writeCommandOutput(cmd, "DevAddr, "+val)
|
if err := session.SetDevAddr(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
case "DevEui":
|
case "DevEui":
|
||||||
writeCommandOutput(cmd, "DevEui, "+val)
|
if err := otaa.SetDevEUI(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
case "AppEui":
|
case "AppEui":
|
||||||
writeCommandOutput(cmd, "AppEui, "+val)
|
if err := otaa.SetAppEUI(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
default:
|
default:
|
||||||
return errInvalidCommand
|
return errInvalidCommand
|
||||||
}
|
}
|
||||||
@@ -40,15 +71,15 @@ func id(args string) error {
|
|||||||
// get
|
// get
|
||||||
switch param {
|
switch param {
|
||||||
case "DevAddr":
|
case "DevAddr":
|
||||||
writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
case "DevEui":
|
case "DevEui":
|
||||||
writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
case "AppEui":
|
case "AppEui":
|
||||||
writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
default:
|
default:
|
||||||
writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -208,9 +239,38 @@ func rxwin1(setting string) error {
|
|||||||
|
|
||||||
func key(setting string) error {
|
func key(setting string) error {
|
||||||
cmd := "KEY"
|
cmd := "KEY"
|
||||||
writeCommandOutput(cmd, setting)
|
|
||||||
|
|
||||||
return nil
|
// look for comma in args
|
||||||
|
param, val, hasComma := strings.Cut(setting, ",")
|
||||||
|
if hasComma {
|
||||||
|
// set
|
||||||
|
data := strings.Trim(val, "\"'")
|
||||||
|
|
||||||
|
// convert data from hex formatted string
|
||||||
|
data = strings.ReplaceAll(data, " ", "")
|
||||||
|
hexdata, err := hex.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch param {
|
||||||
|
case "APPKEY":
|
||||||
|
if err := otaa.SetAppKey(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "APPKEY, "+otaa.GetAppKey())
|
||||||
|
default:
|
||||||
|
return errInvalidCommand
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cannot get keys
|
||||||
|
return errInvalidCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
func fdefault(setting string) error {
|
func fdefault(setting string) error {
|
||||||
@@ -230,7 +290,14 @@ func mode(setting string) error {
|
|||||||
func join(setting string) error {
|
func join(setting string) error {
|
||||||
cmd := "JOIN"
|
cmd := "JOIN"
|
||||||
writeCommandOutput(cmd, "Starting")
|
writeCommandOutput(cmd, "Starting")
|
||||||
|
if err := lorawan.Join(otaa, session); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "Network joined")
|
||||||
|
// TODO: display NetID and DevAddr
|
||||||
writeCommandOutput(cmd, "Done")
|
writeCommandOutput(cmd, "Done")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ func main() {
|
|||||||
fail(err.Error())
|
fail(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session = &lorawan.Session{}
|
||||||
|
otaa = &lorawan.Otaa{}
|
||||||
|
lorawan.UseRadio(radio)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if uart.Buffered() > 0 {
|
if uart.Buffered() > 0 {
|
||||||
data, _ := uart.ReadByte()
|
data, _ := uart.ReadByte()
|
||||||
|
|||||||
@@ -55,14 +55,14 @@ func setupLora() (lora.Radio, error) {
|
|||||||
// Prepare for Lora Operation
|
// Prepare for Lora Operation
|
||||||
loraConf := lora.Config{
|
loraConf := lora.Config{
|
||||||
Freq: FREQ,
|
Freq: FREQ,
|
||||||
Bw: lora.Bandwidth_500_0,
|
Bw: lora.Bandwidth_125_0,
|
||||||
Sf: lora.SpreadingFactor9,
|
Sf: lora.SpreadingFactor9,
|
||||||
Cr: lora.CodingRate4_7,
|
Cr: lora.CodingRate4_7,
|
||||||
HeaderType: lora.HeaderExplicit,
|
HeaderType: lora.HeaderExplicit,
|
||||||
Preamble: 12,
|
Preamble: 12,
|
||||||
Iq: lora.IQStandard,
|
Iq: lora.IQStandard,
|
||||||
Crc: lora.CRCOn,
|
Crc: lora.CRCOn,
|
||||||
SyncWord: lora.SyncPrivate,
|
SyncWord: lora.SyncPublic,
|
||||||
LoraTxPowerDBm: 20,
|
LoraTxPowerDBm: 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user