9 Commits
Author SHA1 Message Date
jlehmann 93b42435c2 new hardware library version (2) 2021-05-11 17:37:38 +02:00
jlehmann e8b3391be1 new hardware library version 2021-05-11 17:35:54 +02:00
jlehmann 392232fed2 new CubeCell library version 2021-05-01 17:16:46 +02:00
jlehmann 58831dc9cb duplicate build flag 2021-04-30 14:45:03 +02:00
jlehmann 3db4cd6ca2 set XXX to wakeup if needed 2021-04-28 20:00:34 +02:00
jlehmann 9ad28bda51 calibrate and appkey commands 2021-04-28 19:53:11 +02:00
jlehmann 97992f9682 valid json for READ_SENSORS 2021-04-28 19:52:33 +02:00
jlehmann a6d5d045f1 fix bug with big difference 2021-04-28 07:21:39 +02:00
jlehmann 74bca8966b calculate appkey 2021-04-26 16:49:27 +02:00
6 changed files with 284 additions and 25 deletions
+2
View File
@@ -1 +1,3 @@
mini-beieli-provision-cubecell
appkey
calibrate
@@ -12,7 +12,7 @@ BME280 bme280;
/******************************************************************************/
/* Firmware Version */
/******************************************************************************/
static const int32_t fwVersion = 20210426;
static const int32_t fwVersion = 20210511;
/******************************************************************************/
/* LoraWAN Settings */
@@ -680,7 +680,7 @@ bool checkUserAt(char *cmd, char *content)
Serial.printf(" \"temperature\": \"%d\",\n", sensor_data.temperature);
Serial.printf(" \"humidity\": \"%d\",\n", sensor_data.humidity);
Serial.printf(" \"pressure\": \"%d\",\n", sensor_data.pressure);
Serial.printf(" \"vbat\": \"%d\",\n", sensor_data.vbat);
Serial.printf(" \"vbat\": \"%d\"\n", sensor_data.vbat);
Serial.printf("}\n");
}
return true;
@@ -850,12 +850,14 @@ void AddSensorDataToLoraData()
timer_pos0 = millis();
}
my_position++;
last_sensor_reading = sensor_data;
}
bool TooBigWeightChange()
{
// on first position there cannot be a difference
if (my_position == 1) {
return false;
}
bool big_difference = (abs(last_sensor_reading.weight - sensor_data.weight) > SEND_DIFF_THRESHOLD_5GRAMS);
if (big_difference) {
lora_data.weight_last = sensor_data.weight;
@@ -907,7 +909,8 @@ bool ReadSensors(bool read_only)
if (!read_only) {
AddSensorDataToLoraData();
send_data = (TooBigWeightChange()) || (my_position >= MAX_VALUES_TO_SEND) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
last_sensor_reading = sensor_data;
if (config_data.debug_level > 0) {
if (send_data) {
Serial.printf("Iteration: %d, we send the data...\n", iteration);
+13 -19
View File
@@ -22,7 +22,7 @@ Das sind die verwendeten Libraries [1]:
| URL | Branch | Commit | Commit Date |
| --- | ------ | ----- | ----------- |
| https://github.com/HelTecAutomation/CubeCell-Arduino.git | 58ed094 | Mon, 26 Apr 2021 09:48:49 +0800 |
| https://github.com/HelTecAutomation/CubeCell-Arduino.git | 0582aca | Sun, 9 May 2021 16:29:35 +0800 |
| https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | master | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
| https://github.com/sparkfun/SparkFun_BME280_Arduino_Library.git | master | 0b5eabf | Wed, 30 Dec 2020 20:44:27 -0700 |
@@ -47,25 +47,19 @@ $ arduino-cli board listall
$ arduino-cli core install CubeCell:CubeCell
$ arduino-cli core list
Compile:
$ arduino-cli compile --fqbn CubeCell:CubeCell:CubeCell-Board \
--build-property "build.band=REGION_EU868" \
--build-property "build.LORAWAN_CLASS=CLASS_A" \
--build-property "build.LORAWAN_CLASS=CLASS_A" \
--build-property "build.LORAWAN_DEVEUI_AUTO=0" \
--build-property "build.LORAWAN_NETMODE=true" \
--build-property "build.LORAWAN_ADR=true" \
--build-property "build.LORAWAN_UPLINKMODE=false" \
--build-property "build.LORAWAN_Net_Reserve=false" \
--build-property "build.LORAWAN_AT_SUPPORT=1" \
--build-property "build.RGB=0" \
--build-property "build.LORAWAN_DebugLevel=0" \
MiniBeieliNodeSketch
Set FQBN:
$ FQBN="CubeCell:CubeCell:CubeCell-Board:LORAWAN_REGION=6,LORAWAN_CLASS=0,LORAWAN_DEVEUI=0,LORAWAN_NETMODE=0,LORAWAN_ADR=0,LORAWAN_UPLINKMODE=1,LORAWAN_Net_Reserve=0,LORAWAN_AT_SUPPORT=0,LORAWAN_RGB=0,LORAWAN_DebugLevel=0"
Output ist in /tmp/arduino-sketch-XXXXXXXXXXXXXXXXXXXXXXXXX und kann kopiert werden:
$ mkdir ~/arduino-builds-saved/20210423/
$ cp /tmp/arduino-sketch-0870BAA82DFEB3C4E076C8FBBDEB25B4/CubeCell_Board_REGION_EU868_RGB_0.cyacd ~/arduino-builds-saved/20210423/
Compile:
$ arduino-cli compile --clean --fqbn ${FQBN} MiniBeieliNodeSketch
Upload:
/home/joerg/Arduino/hardware/CubeCell/CubeCell/tools/CubeCellflash/CubeCellflash -serial /dev/ttyUSB0 /home/joerg/arduino-builds-saved/20210423/CubeCell_Board_REGION_EU868_RGB_0.cyacd
$ arduino-cli upload --fqbn ${FQBN} -p /dev/ttyUSB0 MiniBeieliNodeSketch
Output ist in /tmp/arduino-sketch-XXXXXXXXXXXXXXXXXXXXXXXXX und kann kopiert werden:
$ mkdir ~/arduino-builds-saved/20210511/
$ cp /tmp/arduino-sketch-0870BAA82DFEB3C4E076C8FBBDEB25B4/CubeCell_Board_REGION_EU868_RGB_0.cyacd ~/arduino-builds-saved/20210511/
Upload Variante 2:
/home/joerg/Arduino/hardware/CubeCell/CubeCell/tools/CubeCellflash/CubeCellflash -serial /dev/ttyUSB0 /home/joerg/arduino-builds-saved/latest/CubeCell_Board_REGION_EU868_RGB_0.cyacd
`
+62
View File
@@ -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]) + " <DevEui>")
os.Exit(1)
}
devEui := os.Args[1]
appKey := getAppKey(devEui)
fmt.Printf("%s\n", appKey)
}
+152
View File
@@ -0,0 +1,152 @@
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]) + " <SerialDevice> <calibration_weight_in_gram>")
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')
sendCommand(s, "AT+XXX")
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')
sendCommand(s, "AT+XXX")
sensorData = ReadSensorData(s)
aW, _ := strconv.Atoi(sensorData.WeightARaw)
fmt.Println("Put Weight on Scale B, then hit Return")
reader.ReadString('\n')
sendCommand(s, "AT+XXX")
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)")
}
}
+47 -1
View File
@@ -58,6 +58,51 @@ func readSerial(p *serial.Port) {
}
}
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])
fmt.Printf("calculated AppKey: %s\n", appkey)
return appkey
} else {
fmt.Printf("devEui has wrong length: %s\n", deveui)
return ""
}
}
func main() {
if len(os.Args) != 3 {
fmt.Println("usage: " + filepath.Base(os.Args[0]) + " <SerialDevice> <appEui>")
@@ -75,10 +120,11 @@ func main() {
sendCommand(s, "AT+XXX")
devEui := getDevEui(s)
appKey := getAppKey(devEui)
if devEui != "" {
sendCommand(s, "AT+DevEui="+devEui)
sendCommand(s, "AT+AppEui="+appEui)
sendCommand(s, "AT+AppKey="+devEui+devEui)
sendCommand(s, "AT+AppKey="+appKey)
sendCommand(s, "AT+SAVE_OTAA_CONFIG=1")
sendCommand(s, "AT+RESET=1")
readSerial(s)