From 9ad28bda512e4ad4e693fc72abf490ca3a5ba381 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Wed, 28 Apr 2021 19:53:11 +0200 Subject: [PATCH] calibrate and appkey commands --- .gitignore | 2 + appkey.go | 62 +++++++++++++++++++++ calibrate.go | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 appkey.go create mode 100644 calibrate.go diff --git a/.gitignore b/.gitignore index 13017a8..cf927dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ mini-beieli-provision-cubecell +appkey +calibrate diff --git a/appkey.go b/appkey.go new file mode 100644 index 0000000..32f4c31 --- /dev/null +++ b/appkey.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" +) + +func getAppKey(deveui string) string { + if len(deveui) == 16 { + appkey := fmt.Sprintf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", + deveui[10], + deveui[3], + deveui[4], + deveui[7], + deveui[15], + deveui[9], + deveui[11], + deveui[2], + deveui[0], + deveui[8], + deveui[1], + deveui[6], + deveui[5], + deveui[12], + deveui[14], + deveui[13], + deveui[3], + deveui[6], + deveui[12], + deveui[7], + deveui[15], + deveui[1], + deveui[9], + deveui[11], + deveui[2], + deveui[10], + deveui[0], + deveui[8], + deveui[5], + deveui[14], + deveui[4], + deveui[13]) + + return appkey + + } else { + fmt.Printf("devEui has wrong length: %s\n", deveui) + return "" + } +} + +func main() { + if len(os.Args) != 2 { + fmt.Println("usage: " + filepath.Base(os.Args[0]) + " ") + os.Exit(1) + } + + devEui := os.Args[1] + appKey := getAppKey(devEui) + fmt.Printf("%s\n", appKey) +} diff --git a/calibrate.go b/calibrate.go new file mode 100644 index 0000000..ff1b09d --- /dev/null +++ b/calibrate.go @@ -0,0 +1,149 @@ +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "github.com/tarm/serial" + "log" + "os" + "path/filepath" + "strconv" + "strings" + "time" +) + +type SensorData struct { + WeightARaw string `json:"weight_a_raw"` + WeightBRaw string `json:"weight_b_raw"` +} + +// 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 +} + +// returns output +func sendCommandJSON(p *serial.Port, command string) string { + fmt.Printf("%s\n", command) + var res string = "" + p.Flush() + _, err := p.Write([]byte(command)) + if err != nil { + log.Fatal(err) + } + buf := make([]byte, 1024) + + time.Sleep(3000 * time.Millisecond) + + n, _ := p.Read(buf) + if n > 0 { + res = string(buf[:n-1]) + } + return res +} + +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 ReadSensorData(p *serial.Port) SensorData { + var reply = sendCommandJSON(p, "AT+READ_SENSORS=?") + fmt.Printf("XXX%sYYY\n", reply) + + var data SensorData + json.Unmarshal([]byte(reply), &data) + //fmt.Printf("%v \n", data) + + //fmt.Printf("weight_a_raw: %s\n", data.WeightARaw) + //fmt.Printf("weight_b_raw: %s\n", data.WeightBRaw) + + return data +} + +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]) + " ") + os.Exit(1) + } + + serialdev := os.Args[1] + calWeightS := os.Args[2] + calWeight, _ := strconv.Atoi(calWeightS) + + 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) + reader := bufio.NewReader(os.Stdin) + if devEui != "" { + fmt.Println("Hit Return when no weight is on scale (neither on A not on B)") + reader.ReadString('\n') + sensorData := ReadSensorData(s) + fmt.Printf("%v\n", sensorData) + a0, _ := strconv.Atoi(sensorData.WeightARaw) + b0, _ := strconv.Atoi(sensorData.WeightBRaw) + sendCommand(s, "AT+CAL_A_0="+sensorData.WeightARaw) + sendCommand(s, "AT+CAL_B_0="+sensorData.WeightBRaw) + fmt.Println("Put Weight on Scale A, then hit Return") + reader.ReadString('\n') + sensorData = ReadSensorData(s) + aW, _ := strconv.Atoi(sensorData.WeightARaw) + fmt.Println("Put Weight on Scale B, then hit Return") + reader.ReadString('\n') + sensorData = ReadSensorData(s) + bW, _ := strconv.Atoi(sensorData.WeightBRaw) + calA := float64(aW-a0) / float64(calWeight) + calB := float64(bW-b0) / float64(calWeight) + sendCommand(s, "AT+CAL_A_FACTOR="+fmt.Sprintf("%f", calA)) + sendCommand(s, "AT+CAL_B_FACTOR="+fmt.Sprintf("%f", calB)) + sendCommand(s, "AT+READ_CONFIG=?") + sendCommand(s, "AT+RESET=1") + readSerial(s) + } else { + log.Fatal("Could not get devEui (that is: ChipID)") + } +}