examples: further work on at-lora

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-01-06 14:13:08 +01:00
parent 125d8b6530
commit 59ff8a4585
6 changed files with 209 additions and 133 deletions
+149 -126
View File
@@ -4,42 +4,31 @@ import (
"strings" "strings"
) )
func crlf() { // Use to test if connection to module is OK.
uart.Write([]byte("\r\n"))
}
// Use to test if connection of module is OK.
func quicktest() { func quicktest() {
uart.Write([]byte("+AT: OK")) writeCommandOutput("AT", "OK")
crlf()
} }
// Check firmware version. // Check firmware version.
func version() { func version() {
uart.Write([]byte("+VER: ")) writeCommandOutput("VER", currentVersion())
uart.Write([]byte(currentVersion()))
crlf()
} }
// Use to check the ID of the LoRaWAN module, or change the ID. // Use to check the ID of the LoRaWAN module, or change the ID.
func id(args string) error { func id(args string) error {
cmd := "ID"
// look for comma in args // look for comma in args
param, val, hasComma := strings.Cut(args, ",") param, val, hasComma := strings.Cut(args, ",")
if hasComma { if hasComma {
// set // set
switch param { switch param {
case "DevAddr": case "DevAddr":
uart.Write([]byte("+ID: DevAddr, ")) writeCommandOutput(cmd, "DevAddr, "+val)
uart.Write([]byte(val))
crlf()
case "DevEui": case "DevEui":
uart.Write([]byte("+ID: DevEui, ")) writeCommandOutput(cmd, "DevEui, "+val)
uart.Write([]byte(val))
crlf()
case "AppEui": case "AppEui":
uart.Write([]byte("+ID: AppEui, ")) writeCommandOutput(cmd, "AppEui, "+val)
uart.Write([]byte(val))
crlf()
default: default:
return errInvalidCommand return errInvalidCommand
} }
@@ -50,21 +39,15 @@ func id(args string) error {
// get // get
switch param { switch param {
case "DevAddr": case "DevAddr":
uart.Write([]byte("+ID: DevAddr, xx:xx:xx:xx")) writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
crlf()
case "DevEui": case "DevEui":
uart.Write([]byte("+ID: DevEui, xx:xx:xx:xx:xx:xx:xx:xx")) writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
crlf()
case "AppEui": case "AppEui":
uart.Write([]byte("+ID: AppEui, xx:xx:xx:xx:xx:xx:xx:xx")) writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
crlf()
default: default:
uart.Write([]byte("+ID: DevAddr, xx:xx:xx:xx")) writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
crlf() writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
uart.Write([]byte("+ID: DevEui, xx:xx:xx:xx:xx:xx:xx:xx")) writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
crlf()
uart.Write([]byte("+ID: AppEui, xx:xx:xx:xx:xx:xx:xx:xx"))
crlf()
} }
return nil return nil
@@ -72,62 +55,78 @@ func id(args string) error {
// Use to reset the module. If module returns error, then reset function is invalid. // Use to reset the module. If module returns error, then reset function is invalid.
func reset() error { func reset() error {
uart.Write([]byte("+RESET: OK")) radio.Reset()
crlf()
writeCommandOutput("RESET", "OK")
return nil return nil
} }
// Use to send string format frame which is no need to be confirmed by the server. // Use to send string format frame which is no need to be confirmed by the server.
func msg(data string) error { func msg(data string) error {
uart.Write([]byte("+MSG: Start")) cmd := "MSG"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+MSG: Done"))
crlf() if err := radio.LoraTx([]byte(data), defaultTimeout); err != nil {
writeCommandOutput(cmd, err.Error())
return err
}
writeCommandOutput(cmd, "Done")
return nil return nil
} }
// Use to send string format frame which must be confirmed by the server // Use to send string format frame which must be confirmed by the server
func cmsg(data string) error { func cmsg(data string) error {
uart.Write([]byte("+CMSG: Start")) cmd := "CMSG"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+CMSG: Done"))
crlf() if err := radio.LoraTx([]byte(data), defaultTimeout); err != nil {
writeCommandOutput(cmd, err.Error())
return err
}
// TODO: confirmation
writeCommandOutput(cmd, "Done")
return nil return nil
} }
// Use to send hex format frame which is no need to be confirmed by the server // Use to send hex format frame which is no need to be confirmed by the server
func msghex(data string) error { func msghex(data string) error {
uart.Write([]byte("+MSGHEX: Start")) cmd := "MSGHEX"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+MSGHEX: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
// Use to send hex format frame which must be confirmed by the server. // Use to send hex format frame which must be confirmed by the server.
func cmsghex(data string) error { func cmsghex(data string) error {
uart.Write([]byte("+CMSGHEX: Start")) cmd := "CMSGHEX"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+CMSGHEX: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
// Use to send string format LoRaWAN proprietary frames // Use to send string format LoRaWAN proprietary frames
func pmsg(data string) error { func pmsg(data string) error {
uart.Write([]byte("+PMSG: Start")) cmd := "PMSG"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+PMSG: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
// Use to send hex format LoRaWAN proprietary frames. // Use to send hex format LoRaWAN proprietary frames.
func pmsghex(data string) error { func pmsghex(data string) error {
uart.Write([]byte("+PMSGHEX: Start")) cmd := "PMSGHEX"
crlf() writeCommandOutput(cmd, "Start")
uart.Write([]byte("+PMSGHEX: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
@@ -135,49 +134,49 @@ func pmsghex(data string) error {
// message, port number should range from 1 to 255. User should refer to LoRaWAN // message, port number should range from 1 to 255. User should refer to LoRaWAN
// specification to choose port. // specification to choose port.
func port(p string) error { func port(p string) error {
uart.Write([]byte("+PORT: ")) cmd := "PMSG"
uart.Write([]byte(p)) writeCommandOutput(cmd, p)
crlf()
return nil return nil
} }
// Set ADR function of LoRaWAN module // Set ADR function of LoRaWAN module
func adr(state string) error { func adr(state string) error {
uart.Write([]byte("+ADR: ")) cmd := "ADR"
uart.Write([]byte(state)) writeCommandOutput(cmd, state)
crlf()
return nil return nil
} }
// Use LoRaWAN defined DRx to set datarate of LoRaWAN AT modem. // Use LoRaWAN defined DRx to set datarate of LoRaWAN AT modem.
func dr(rate string) error { func dr(rate string) error {
uart.Write([]byte("+ADR: ")) cmd := "DR"
uart.Write([]byte(rate)) writeCommandOutput(cmd, rate)
crlf()
return nil return nil
} }
// Channel Configuration // Channel Configuration
func ch(channel string) error { func ch(channel string) error {
uart.Write([]byte("+CH: ")) cmd := "CH"
uart.Write([]byte(channel)) writeCommandOutput(cmd, channel)
crlf()
return nil return nil
} }
// Set and Check Power // Set and Check Power
func power(setting string) error { func power(setting string) error {
uart.Write([]byte("+POWER: ")) cmd := "POWER"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
// Unconfirmed message repeats times. // Unconfirmed message repeats times.
func rept(setting string) error { func rept(setting string) error {
uart.Write([]byte("+REPT: ")) cmd := "REPT"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
@@ -186,132 +185,156 @@ func rept(setting string) error {
// be sent. Random delay 3 - 10s between each retry // be sent. Random delay 3 - 10s between each retry
// (band duty cycle limitation has the priority) // (band duty cycle limitation has the priority)
func retry(setting string) error { func retry(setting string) error {
uart.Write([]byte("+RETRY: ")) cmd := "RETRY"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
func rxwin2(setting string) error { func rxwin2(setting string) error {
uart.Write([]byte("+RXWIN2: ")) cmd := "RXWIN2"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
func rxwin1(setting string) error { func rxwin1(setting string) error {
uart.Write([]byte("+RXWIN1: ")) cmd := "RXWIN1"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
func key(setting string) error { func key(setting string) error {
uart.Write([]byte("+RETRY: ")) cmd := "KEY"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
func fdefault(setting string) error { func fdefault(setting string) error {
uart.Write([]byte("+FDEFAULT: OK")) cmd := "FDEFAULT"
crlf() writeCommandOutput(cmd, "OK")
return nil return nil
} }
func mode(setting string) error { func mode(setting string) error {
uart.Write([]byte("+MODE: ")) cmd := "MODE"
uart.Write([]byte(setting)) writeCommandOutput(cmd, setting)
crlf()
return nil return nil
} }
func join(setting string) error { func join(setting string) error {
uart.Write([]byte("+JOIN: Starting")) cmd := "JOIN"
crlf() writeCommandOutput(cmd, "Starting")
uart.Write([]byte("+JOIN: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
func beacon(setting string) error { func beacon(setting string) error {
uart.Write([]byte("+BEACON: Starting")) cmd := "BEACON"
crlf() writeCommandOutput(cmd, "Starting")
uart.Write([]byte("+BEACON: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
func class(setting string) error { func class(setting string) error {
uart.Write([]byte("+CLASS: Starting")) cmd := "CLASS"
crlf() writeCommandOutput(cmd, "Starting")
uart.Write([]byte("+CLASS: Done"))
crlf() writeCommandOutput(cmd, "Done")
return nil return nil
} }
func delay(setting string) error { func delay(setting string) error {
uart.Write([]byte("+DELAY: Starting")) cmd := "DELAY"
crlf() writeCommandOutput(cmd, setting)
return nil return nil
} }
func lw(setting string) error { func lw(setting string) error {
uart.Write([]byte("+LW: Starting")) cmd := "LW"
crlf() writeCommandOutput(cmd, setting)
return nil return nil
} }
func wdt(setting string) error { func wdt(setting string) error {
uart.Write([]byte("+WDT: Not implemented")) cmd := "WDT"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func lowpower(setting string) error { func lowpower(setting string) error {
uart.Write([]byte("+LOWPOWER: Not implemented")) cmd := "LOWPOWER"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func vdd(setting string) error { func vdd(setting string) error {
uart.Write([]byte("+VDD: Not implemented")) cmd := "VDD"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func temp(setting string) error { func temp(setting string) error {
uart.Write([]byte("+TEMP: Not implemented")) cmd := "TEMP"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func rtc(setting string) error { func rtc(setting string) error {
uart.Write([]byte("+RTC: Not implemented")) cmd := "RTC"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func eeprom(setting string) error { func eeprom(setting string) error {
uart.Write([]byte("+EEPROM: Not implemented")) cmd := "EEPROM"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func uartcmd(setting string) error { func uartcmd(setting string) error {
uart.Write([]byte("+UART: Not implemented")) cmd := "UART"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func test(setting string) error { func test(setting string) error {
uart.Write([]byte("+TEST: Not implemented")) cmd := "TEST"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func log(setting string) error { func log(setting string) error {
uart.Write([]byte("+LOG: Not implemented")) cmd := "LOG"
crlf() writeCommandOutput(cmd, "Not implemented")
return nil return nil
} }
func crlf() {
uart.Write([]byte("\r\n"))
}
func writeCommandOutput(cmd, data string) {
uart.Write([]byte("+"+cmd+": "))
uart.Write([]byte(data))
crlf()
}
+18
View File
@@ -19,11 +19,20 @@ var (
tx = machine.UART_TX_PIN tx = machine.UART_TX_PIN
rx = machine.UART_RX_PIN rx = machine.UART_RX_PIN
input = make([]byte, 0, 64) input = make([]byte, 0, 64)
radio LoraRadio
defaultTimeout uint32 = 1000
) )
func main() { func main() {
uart.Configure(machine.UARTConfig{TX: tx, RX: rx}) uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
var err error
radio, err = setupLora()
if err != nil {
fail(err.Error())
}
for { for {
if uart.Buffered() > 0 { if uart.Buffered() > 0 {
data, _ := uart.ReadByte() data, _ := uart.ReadByte()
@@ -45,3 +54,12 @@ func main() {
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
} }
} }
func fail(msg string) {
for {
uart.Write([]byte(msg))
crlf()
time.Sleep(time.Minute)
}
}
+13
View File
@@ -0,0 +1,13 @@
package main
type LoraRadio interface {
Reset() error
LoraTx(pkt []uint8, timeoutMs uint32) error
LoraRx(timeoutMs uint32) ([]uint8, error)
SetLoraFrequency(freq uint32)
SetLoraIqMode(mode uint8)
SetLoraCodingRate(cr uint8)
SetLoraBandwidth(bw uint8)
SetLoraCrc(enable bool)
SetLoraSpreadingFactor(sf uint8)
}
+23 -1
View File
@@ -3,6 +3,28 @@
package main package main
// do simulator setup here // do simulator setup here
func setup() error { func setupLora() (LoraRadio, error) {
return &SimLoraRadio{}, nil
}
type SimLoraRadio struct {
}
func (sr *SimLoraRadio) Reset() error {
return nil return nil
} }
func (sr *SimLoraRadio) LoraTx(pkt []uint8, timeoutMs uint32) error {
return nil
}
func (sr *SimLoraRadio) LoraRx(timeoutMs uint32) ([]uint8, error) {
return nil, nil
}
func (sr *SimLoraRadio) SetLoraFrequency(freq uint32) {}
func (sr *SimLoraRadio) SetLoraIqMode(mode uint8) {}
func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {}
func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {}
func (sr *SimLoraRadio) SetLoraCrc(enable bool) {}
func (sr *SimLoraRadio) SetLoraSpreadingFactor(sf uint8) {}
+2 -2
View File
@@ -3,6 +3,6 @@
package main package main
// do sx126x setup here // do sx126x setup here
func setup() error { func setupLora() (LoraRadio, error) {
return nil return nil, nil
} }
+2 -2
View File
@@ -3,6 +3,6 @@
package main package main
// do sx127x setup here // do sx127x setup here
func setup() error { func setupLora() (LoraRadio, error) {
return nil return nil, nil
} }