lorawan cleanup and optimizations:

- Remove all default loraconf default initialisation in lorawan examples (Region Settings takes care of this now)
- Remove retries while receiving JoinAccept and increase LoraRX to 10s (no need to wait more than 10s)
- Add constants for RegionSetting default frequencies
- Standardize all lora default configurations in lora examples
- Add new AT+LW=NET,(ON|OFF) command in atcmd example
This commit is contained in:
Olivier Fauchon
2023-01-30 16:50:24 +01:00
committed by deadprogram
parent 4e687b29c3
commit fa0cf6ec7f
16 changed files with 89 additions and 102 deletions
+11
View File
@@ -39,3 +39,14 @@ Builds/flashes atcmd console application on Lora-E5 using onboard SX126x.
tinygo flash -target lorae5 ./examples/lora/lorawan/atcmd/
```
## Joining a Public Lorawan Network
```
AT+ID=DevEui,0101010101010101
AT+ID=AppEui,0123012301230213
AT+KEY=APPKEY,AEAEAEAEAEAEAEAAEAEAEAEAEAEAAEAE
AT+LW=NET,ON
AT+JOIN
```
AT+LW=NET,(ON|OFF) command changes Lora Sync Word to connect on public network(ON) or private networks(OFF)
+13
View File
@@ -337,6 +337,19 @@ func delay(setting string) error {
func lw(setting string) error {
cmd := "LW"
param, val, hasComma := strings.Cut(setting, ",")
if hasComma {
if param == "NET" {
if val == "ON" {
lorawan.SetPublicNetwork(true)
println("SET PUBLIC NET")
} else {
lorawan.SetPublicNetwork(false)
println("SET PRIVATE NET")
}
}
}
writeCommandOutput(cmd, setting)
return nil
@@ -2,10 +2,16 @@
package main
import (
"tinygo.org/x/drivers/lora/lorawan"
)
// These are sample keys, so the example builds
// Either change here, or create a new go file and use customkeys build tag
func setLorawanKeys() {
otaa.SetAppEUI([]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
otaa.SetDevEUI([]uint8{0xB3, 0xD5, 0x41, 0x00, 0x0A, 0xF1, 0xA4, 0x45})
otaa.SetAppKey([]uint8{0x12, 0x22, 0xA3, 0xFF, 0x0C, 0x7B, 0x76, 0x7B, 0x8F, 0xD3, 0x12, 0x4F, 0xCE, 0x7A, 0x32, 0x16})
lorawan.SetPublicNetwork(true)
}
+1 -17
View File
@@ -65,28 +65,12 @@ func main() {
session = &lorawan.Session{}
otaa = &lorawan.Otaa{}
// Initial Lora modulation configuration
loraConf := lora.Config{
Freq: 868100000,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPublic,
LoraTxPowerDBm: 20,
}
radio.LoraConfig(loraConf)
// Connect the lorawan with the Lora Radio device.
lorawan.UseRadio(radio)
lorawan.UseRegionSettings(region.EU868())
// Configure AppEUI, DevEUI, APPKey
// Configure AppEUI, DevEUI, APPKey, and public/private Lorawan Network
setLorawanKeys()
if debug != "" {
-16
View File
@@ -37,22 +37,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
-16
View File
@@ -41,22 +41,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
-16
View File
@@ -34,22 +34,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
// Prepare for Lora Operation
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPublic,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
+2 -4
View File
@@ -11,8 +11,6 @@ import (
"tinygo.org/x/drivers/sx126x"
)
const FREQ = 868100000
const (
LORA_DEFAULT_RXTIMEOUT_MS = 1000
LORA_DEFAULT_TXTIMEOUT_MS = 5000
@@ -44,8 +42,8 @@ func main() {
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Freq: lora.MHz_868_1,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
+2 -4
View File
@@ -11,8 +11,6 @@ import (
"tinygo.org/x/drivers/sx127x"
)
const FREQ = 868100000
const (
LORA_DEFAULT_RXTIMEOUT_MS = 1000
LORA_DEFAULT_TXTIMEOUT_MS = 5000
@@ -57,8 +55,8 @@ func main() {
// Prepare for Lora Operation
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Freq: lora.MHz_868_1,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
+7
View File
@@ -76,3 +76,10 @@ const (
SyncPublic = iota
SyncPrivate
)
const (
MHz_868_1 = 868100000
MHz_868_5 = 868500000
MHz_916_8 = 916800000
MHz_923_3 = 923300000
)
+22 -16
View File
@@ -24,7 +24,7 @@ var (
const (
LORA_TX_TIMEOUT = 2000
LORA_RX_TIMEOUT = 5000
LORA_RX_TIMEOUT = 10000
)
var (
@@ -33,10 +33,12 @@ var (
regionSettings region.RegionSettings
)
// UseRegionSettings sets current Lorawan Regional parameters
func UseRegionSettings(rs region.RegionSettings) {
regionSettings = rs
}
// UseRadio attaches Lora radio driver to Lorawan
func UseRadio(r lora.Radio) {
if ActiveRadio != nil {
panic("lorawan.ActiveRadio is already set")
@@ -44,15 +46,25 @@ func UseRadio(r lora.Radio) {
ActiveRadio = r
}
func ApplyChannelConfig(ch *region.Channel) {
// SetPublicNetwork defines Lora Sync Word according to network type (public/private)
func SetPublicNetwork(enabled bool) {
ActiveRadio.SetPublicNetwork(enabled)
}
// ApplyChannelConfig sets current Lora modulation according to current regional settings
func applyChannelConfig(ch *region.Channel) {
ActiveRadio.SetFrequency(ch.Frequency)
ActiveRadio.SetBandwidth(ch.Bandwidth)
ActiveRadio.SetCodingRate(ch.CodingRate)
ActiveRadio.SetSpreadingFactor(ch.SpreadingFactor)
ActiveRadio.SetPreambleLength(ch.PreambleLength)
ActiveRadio.SetTxPower(ch.TxPowerDBm)
// Lorawan defaults to explicit headers
ActiveRadio.SetHeaderType(lora.HeaderExplicit)
ActiveRadio.SetCrc(true)
}
// Join tries to connect Lorawan Gateway
func Join(otaa *Otaa, session *Session) error {
var resp []uint8
@@ -73,8 +85,7 @@ func Join(otaa *Otaa, session *Session) error {
}
// Prepare radio for Join Tx
ApplyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
@@ -82,18 +93,13 @@ func Join(otaa *Otaa, session *Session) error {
}
// Wait for JoinAccept
ApplyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetIqMode(lora.IQInverted)
for i := 0; i < Retries; i++ {
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}
if resp != nil {
break
}
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}
if resp == nil {
return ErrNoJoinAcceptReceived
}
@@ -106,6 +112,7 @@ func Join(otaa *Otaa, session *Session) error {
return nil
}
// SendUplink sends Lorawan Uplink message
func SendUplink(data []uint8, session *Session) error {
if regionSettings == nil {
@@ -117,8 +124,7 @@ func SendUplink(data []uint8, session *Session) error {
return err
}
ApplyChannelConfig(regionSettings.UplinkChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.UplinkChannel())
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
+3 -3
View File
@@ -15,19 +15,19 @@ type RegionSettingsAU915 struct {
func AU915() *RegionSettingsAU915 {
return &RegionSettingsAU915{
joinRequestChannel: &Channel{916800000,
joinRequestChannel: &Channel{lora.MHz_916_8,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{923300000,
joinAcceptChannel: &Channel{lora.MHz_923_3,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868500000,
uplinkChannel: &Channel{lora.MHz_868_5,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
+3 -3
View File
@@ -15,19 +15,19 @@ type RegionSettingsEU868 struct {
func EU868() *RegionSettingsEU868 {
return &RegionSettingsEU868{
joinRequestChannel: &Channel{868100000,
joinRequestChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{868100000,
joinAcceptChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868100000,
uplinkChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
+3
View File
@@ -12,5 +12,8 @@ type Radio interface {
SetSpreadingFactor(sf uint8)
SetPreambleLength(plen uint16)
SetTxPower(txpow int8)
SetSyncWord(syncWord uint16)
SetPublicNetwork(enable bool)
SetHeaderType(headerType uint8)
LoraConfig(cnf Config)
}
+6
View File
@@ -576,6 +576,12 @@ func (d *Device) SetTxPower(txpow int8) {
d.loraConf.LoraTxPowerDBm = txpow
}
// SetHeaderType sets implicit or explicit header mode
// NB: Change will be applied at next RX / TX
func (d *Device) SetHeaderType(headerType uint8) {
d.loraConf.HeaderType = headerType
}
//
// Lora functions
//
+10 -7
View File
@@ -276,8 +276,8 @@ func (d *Device) SetCodingRate(cr uint8) {
d.WriteRegister(SX127X_REG_MODEM_CONFIG_1, (d.ReadRegister(SX127X_REG_MODEM_CONFIG_1)&0xf1)|(cr<<1))
}
// SetImplicitHeaderModeOn Enables implicit header mode ***
func (d *Device) SetHeaderMode(headerType uint8) {
// SetHeaderType set implicit or explicit mode
func (d *Device) SetHeaderType(headerType uint8) {
d.loraConf.HeaderType = headerType
if headerType == lora.HeaderImplicit {
d.WriteRegister(SX127X_REG_MODEM_CONFIG_1, d.ReadRegister(SX127X_REG_MODEM_CONFIG_1)|0x01)
@@ -346,6 +346,14 @@ func (d *Device) SetIqMode(val uint8) {
d.WriteRegister(SX127X_REG_INVERTIQ2, 0x19)
}
}
// SetPublicNetwork changes Sync Word to match network type
func (d *Device) SetPublicNetwork(enabled bool) {
if enabled {
d.SetSyncWord(SX127X_LORA_MAC_PUBLIC_SYNCWORD)
} else {
d.SetSyncWord(SX127X_LORA_MAC_PRIVATE_SYNCWORD)
}
// Tx sends a lora packet, (with timeout)
func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error {
@@ -538,9 +546,4 @@ func bandwidth(bw uint8) uint8 {
}
}
func syncword(sw int) uint16 {
if sw == lora.SyncPublic {
return SX127X_LORA_MAC_PUBLIC_SYNCWORD
}
return SX127X_LORA_MAC_PRIVATE_SYNCWORD
}