new provision program
This commit is contained in:
parent
86539f43fc
commit
0aef4ac99a
|
|
@ -0,0 +1 @@
|
|||
mini-beieli-provision-cubecell
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tarm/serial"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// returns last line of output
|
||||
func sendCommand(p *serial.Port, command string) string {
|
||||
fmt.Printf("%s\n", command)
|
||||
var last_line string = ""
|
||||
p.Flush()
|
||||
_, err := p.Write([]byte(command))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
buf := make([]byte, 1024)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
n, _ := p.Read(buf)
|
||||
if n > 0 {
|
||||
last_line_arr := strings.Split(string(buf[:n]), "\n")
|
||||
//fmt.Printf("AAA: %+q\n", last_line_arr)
|
||||
last_line = last_line_arr[len(last_line_arr)-2]
|
||||
|
||||
fmt.Print(string(buf[:n]))
|
||||
}
|
||||
return last_line
|
||||
}
|
||||
|
||||
func getDevEui(p *serial.Port) string {
|
||||
var reply = sendCommand(p, "AT+ChipID=?")
|
||||
//fmt.Printf("XXX%sYYY\n",reply)
|
||||
if len(reply) >= 20 {
|
||||
return "0000" + reply[8:20]
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func readSerial(p *serial.Port) {
|
||||
p.Flush()
|
||||
buf := make([]byte, 32)
|
||||
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
n, _ := p.Read(buf)
|
||||
for n > 0 {
|
||||
// ignoring error as EOF raises error on Linux
|
||||
reply := string(buf[:n])
|
||||
fmt.Print(reply)
|
||||
n, _ = p.Read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Println("usage: " + filepath.Base(os.Args[0]) + " <SerialDevice> <appEui>")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
serialdev := os.Args[1]
|
||||
appEui := os.Args[2]
|
||||
|
||||
c := &serial.Config{Name: serialdev, Baud: 115200, ReadTimeout: time.Second * 1}
|
||||
s, err := serial.OpenPort(c)
|
||||
if err != nil {
|
||||
log.Fatal("Problem mit Serial Interface")
|
||||
}
|
||||
|
||||
sendCommand(s, "AT+XXX")
|
||||
devEui := getDevEui(s)
|
||||
if devEui != "" {
|
||||
sendCommand(s, "AT+DevEui="+devEui)
|
||||
sendCommand(s, "AT+AppEui="+appEui)
|
||||
sendCommand(s, "AT+AppKey="+devEui+devEui)
|
||||
sendCommand(s, "AT+RESET=1")
|
||||
readSerial(s)
|
||||
} else {
|
||||
log.Fatal("Could not get devEui (that is: ChipID)")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue