Compare commits
26 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
555f8aa46f | |
|
|
ebb9be6b0f | |
|
|
038a7a1215 | |
|
|
111fd2cd52 | |
|
|
ec7a10f5ff | |
|
|
384d262905 | |
|
|
cc14992ba0 | |
|
|
2add3ddf9f | |
|
|
82ffafa6c7 | |
|
|
2850593631 | |
|
|
93b42435c2 | |
|
|
e8b3391be1 | |
|
|
392232fed2 | |
|
|
58831dc9cb | |
|
|
3db4cd6ca2 | |
|
|
9ad28bda51 | |
|
|
97992f9682 | |
|
|
a6d5d045f1 | |
|
|
74bca8966b | |
|
|
1772cf6f7e | |
|
|
cefc4473e5 | |
|
|
c88c1a68be | |
|
|
1651aab05b | |
|
|
2cff8e78c8 | |
|
|
15d75f10fc | |
|
|
4adaf33835 |
|
|
@ -1 +1,5 @@
|
||||||
mini-beieli-provision-cubecell
|
mini-beieli-provision-cubecell
|
||||||
|
appkey
|
||||||
|
calibrate
|
||||||
|
MiniBeieliNodeSketch/build
|
||||||
|
cyacd
|
||||||
|
|
|
||||||
|
|
@ -12,19 +12,16 @@ BME280 bme280;
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* Firmware Version */
|
/* Firmware Version */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
static const int32_t fwVersion = 20210205;
|
static const int32_t fwVersion = 20250726;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* LoraWAN Settings */
|
/* LoraWAN Settings */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
/* OTAA para*/
|
/* OTAA para*/
|
||||||
//uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 };
|
uint8_t devEui[8];
|
||||||
//uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
uint8_t appEui[8];
|
||||||
//uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 };
|
uint8_t appKey[16];
|
||||||
uint8_t devEui[] = { 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25 };
|
|
||||||
uint8_t appEui[] = { 0x70, 0xb3, 0xd5, 0x7e, 0xd0, 0x02, 0xe5, 0xab };
|
|
||||||
uint8_t appKey[] = { 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25, 0x00, 0x00, 0xd3, 0xa6, 0x59, 0x83, 0x3b, 0x25 };
|
|
||||||
|
|
||||||
/* ABP para*/
|
/* ABP para*/
|
||||||
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda, 0x85 };
|
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda, 0x85 };
|
||||||
|
|
@ -170,6 +167,9 @@ typedef struct {
|
||||||
float cal_a_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
|
float cal_a_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 1
|
||||||
float cal_b_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
|
float cal_b_factor; // 4 Bytes, Kalibrationsfaktor Waegezelle 2
|
||||||
byte debug_level; // 0 => no debugging, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level
|
byte debug_level; // 0 => no debugging, no led, 1 => infos, no led, 2 => infos, 3 => error, 4 => highest level
|
||||||
|
uint8_t devEui[8]; // keep OTAA settings so that new fw-updates does not overwrite it
|
||||||
|
uint8_t appEui[8]; // dito
|
||||||
|
uint8_t appKey[16]; // dito
|
||||||
} __attribute__((packed)) CONFIG_data;
|
} __attribute__((packed)) CONFIG_data;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -225,6 +225,8 @@ bool stop_iterations = false;
|
||||||
bool start_new_iteration = false;
|
bool start_new_iteration = false;
|
||||||
bool next_package_is_init_package = true;
|
bool next_package_is_init_package = true;
|
||||||
uint32_t gRebootMs;
|
uint32_t gRebootMs;
|
||||||
|
char last_channel = 'X';
|
||||||
|
bool toobigweightchange = false;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* Functions for Global Data Structures */
|
/* Functions for Global Data Structures */
|
||||||
|
|
@ -404,6 +406,9 @@ bool InitializeScales()
|
||||||
result &= nau7802.setSampleRate(NAU7802_SPS_40); //Set samples per second to 40
|
result &= nau7802.setSampleRate(NAU7802_SPS_40); //Set samples per second to 40
|
||||||
result &= nau7802.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
|
result &= nau7802.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
|
||||||
|
|
||||||
|
// see https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library/issues/7
|
||||||
|
//result &= nau7802.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -446,14 +451,22 @@ long ReadScale(char channel)
|
||||||
channelNumber = NAU7802_CHANNEL_2;
|
channelNumber = NAU7802_CHANNEL_2;
|
||||||
}
|
}
|
||||||
unsigned long startTime = millis();
|
unsigned long startTime = millis();
|
||||||
nau7802.setChannel(channelNumber);
|
if (channel != last_channel) {
|
||||||
bool calibrate_success = nau7802.calibrateAFE();
|
last_channel = channel;
|
||||||
if (! calibrate_success) {
|
nau7802.setChannel(channelNumber);
|
||||||
|
bool calibrate_success = nau7802.calibrateAFE();
|
||||||
|
if (! calibrate_success) {
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
Serial.printf("Error: Calibration not successful!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
Serial.printf("Error: Calibration not successful!\n");
|
Serial.printf("getChannel1Gain: %d\n", nau7802.getChannel1Gain());
|
||||||
|
Serial.printf("getChannel1Offset: %d\n", nau7802.getChannel1Offset());
|
||||||
|
Serial.printf("NAU7802_GCAL2_B3: %d\n", nau7802.get32BitRegister(NAU7802_GCAL2_B3));
|
||||||
|
Serial.printf("NAU7802_OCAL2_B2: %d\n", nau7802.get32BitRegister(NAU7802_OCAL2_B2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nau7802.available()) {
|
if (nau7802.available()) {
|
||||||
long dummy = nau7802.getReading();
|
long dummy = nau7802.getReading();
|
||||||
}
|
}
|
||||||
|
|
@ -655,6 +668,19 @@ bool checkUserAt(char *cmd, char *content)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp(cmd, "SAVE_OTAA_CONFIG") == 0)
|
||||||
|
{
|
||||||
|
if (content[0] == '1')
|
||||||
|
{
|
||||||
|
memcpy(config_data.devEui, devEui, sizeof(devEui));
|
||||||
|
memcpy(config_data.appEui, appEui, sizeof(appEui));
|
||||||
|
memcpy(config_data.appKey, appKey, sizeof(appKey));
|
||||||
|
WriteConfigDataToFlash();
|
||||||
|
Serial.printf("saved OTAA configuration to flash\n");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(cmd, "READ_SENSORS") == 0)
|
if (strcmp(cmd, "READ_SENSORS") == 0)
|
||||||
{
|
{
|
||||||
if (content[0] == '?')
|
if (content[0] == '?')
|
||||||
|
|
@ -667,7 +693,7 @@ bool checkUserAt(char *cmd, char *content)
|
||||||
Serial.printf(" \"temperature\": \"%d\",\n", sensor_data.temperature);
|
Serial.printf(" \"temperature\": \"%d\",\n", sensor_data.temperature);
|
||||||
Serial.printf(" \"humidity\": \"%d\",\n", sensor_data.humidity);
|
Serial.printf(" \"humidity\": \"%d\",\n", sensor_data.humidity);
|
||||||
Serial.printf(" \"pressure\": \"%d\",\n", sensor_data.pressure);
|
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");
|
Serial.printf("}\n");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -763,6 +789,10 @@ void setup_platform(void)
|
||||||
|
|
||||||
// read config_data from flash...
|
// read config_data from flash...
|
||||||
ReadConfigDataFromFlash();
|
ReadConfigDataFromFlash();
|
||||||
|
memcpy(devEui, config_data.devEui, sizeof(config_data.devEui));
|
||||||
|
memcpy(appEui, config_data.appEui, sizeof(config_data.appEui));
|
||||||
|
memcpy(appKey, config_data.appKey, sizeof(config_data.appKey));
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
Serial.printf("Setup_platform, this is the configuration\n");
|
Serial.printf("Setup_platform, this is the configuration\n");
|
||||||
Serial.printf("cal_a_0: %d\n", config_data.cal_a_0);
|
Serial.printf("cal_a_0: %d\n", config_data.cal_a_0);
|
||||||
|
|
@ -833,12 +863,14 @@ void AddSensorDataToLoraData()
|
||||||
timer_pos0 = millis();
|
timer_pos0 = millis();
|
||||||
}
|
}
|
||||||
my_position++;
|
my_position++;
|
||||||
|
|
||||||
last_sensor_reading = sensor_data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TooBigWeightChange()
|
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);
|
bool big_difference = (abs(last_sensor_reading.weight - sensor_data.weight) > SEND_DIFF_THRESHOLD_5GRAMS);
|
||||||
if (big_difference) {
|
if (big_difference) {
|
||||||
lora_data.weight_last = sensor_data.weight;
|
lora_data.weight_last = sensor_data.weight;
|
||||||
|
|
@ -848,6 +880,7 @@ bool TooBigWeightChange()
|
||||||
Serial.printf("TooBigWeightChange (my_position: %d): %d...\n", my_position, big_difference);
|
Serial.printf("TooBigWeightChange (my_position: %d): %d...\n", my_position, big_difference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toobigweightchange = big_difference;
|
||||||
return big_difference;
|
return big_difference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -890,7 +923,8 @@ bool ReadSensors(bool read_only)
|
||||||
if (!read_only) {
|
if (!read_only) {
|
||||||
AddSensorDataToLoraData();
|
AddSensorDataToLoraData();
|
||||||
send_data = (TooBigWeightChange()) || (my_position >= MAX_VALUES_TO_SEND) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
|
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 (config_data.debug_level > 0) {
|
||||||
if (send_data) {
|
if (send_data) {
|
||||||
Serial.printf("Iteration: %d, we send the data...\n", iteration);
|
Serial.printf("Iteration: %d, we send the data...\n", iteration);
|
||||||
|
|
@ -947,6 +981,8 @@ void loop()
|
||||||
case DEVICE_STATE_SEND:
|
case DEVICE_STATE_SEND:
|
||||||
{
|
{
|
||||||
if (ReadSensors(false)) {
|
if (ReadSensors(false)) {
|
||||||
|
// we send confirmed packages when weight change is too big or for every init package
|
||||||
|
isTxConfirmed = (toobigweightchange) || (iteration <= INIT_PACKETS) || (iteration % INIT_PACKAGE_INTERVAL == 0);
|
||||||
prepareTxFrame(appPort);
|
prepareTxFrame(appPort);
|
||||||
LoRaWAN.send();
|
LoRaWAN.send();
|
||||||
package_counter++;
|
package_counter++;
|
||||||
77
README.md
77
README.md
|
|
@ -20,10 +20,77 @@ Autor: Joerg Lehmann, nbit Informatik GmbH
|
||||||
|
|
||||||
Das sind die verwendeten Libraries [1]:
|
Das sind die verwendeten Libraries [1]:
|
||||||
|
|
||||||
| URL | Commit | Commit Date |
|
| URL | Branch | Commit | Commit Date |
|
||||||
| --- | ----- | ----------- |
|
| --- | ------ | ----- | ----------- |
|
||||||
| https://github.com/HelTecAutomation/ASR650x-Arduino.git | 9856523 | Thu, 4 Feb 2021 18:02:48 +0800 |
|
| https://github.com/HelTecAutomation/ASR650x-Arduino.git | d9402fe | Tue, 8 Jul 2025 09:52:16 +0800 |
|
||||||
| https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
|
| https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | e6b91f8 | Mon, 26 Feb 2024 09:55:18 +0000 |
|
||||||
| https://github.com/sparkfun/SparkFun_BME280_Arduino_Library.git | 0b5eabf | Wed, 30 Dec 2020 20:44:27 -0700 |
|
| https://github.com/sparkfun/SparkFun_BME280_Arduino_Library.git | c42e780 | Tue, 19 Sep 2023 14:53:45 -0600 |
|
||||||
|
|
||||||
|
`
|
||||||
[1]: echo "| $(git remote -v |grep fetch |awk '{print $2}' |tr '\n' ' ') | $(git log --pretty=format:'%h | %cD ' -n 1) |"
|
[1]: echo "| $(git remote -v |grep fetch |awk '{print $2}' |tr '\n' ' ') | $(git log --pretty=format:'%h | %cD ' -n 1) |"
|
||||||
|
`
|
||||||
|
|
||||||
|
## Wir verwenden arduino-cli
|
||||||
|
|
||||||
|
Installation:
|
||||||
|
`
|
||||||
|
$ brew install arduino-cli
|
||||||
|
$ arduino-cli version
|
||||||
|
arduino-cli Version: 1.2.2 Commit: Homebrew Datum: 2025-04-22T13:49:40Z
|
||||||
|
|
||||||
|
$ arduino-cli sketch new MiniBeieliNodeSketch
|
||||||
|
|
||||||
|
Install Board aus Git
|
||||||
|
|
||||||
|
mkdir -p ~/Documents/Arduino/hardware/CubeCell
|
||||||
|
cd ~/Documents/Arduino/hardware/CubeCell
|
||||||
|
git clone https://github.com/HelTecAutomation/ASR650x-Arduino.git CubeCell
|
||||||
|
cd CubeCell/tools
|
||||||
|
python get.py
|
||||||
|
|
||||||
|
Install Libraries (from Git...):
|
||||||
|
|
||||||
|
$ cd ~/Documents/Arduino/libraries
|
||||||
|
$ git clone https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git
|
||||||
|
$ git clone https://github.com/sparkfun/SparkFun_BME280_Arduino_Library.git
|
||||||
|
|
||||||
|
Set FQBN (show possible boards with 'arduino-cli board listall', without '-V2' for old CubeCell):
|
||||||
|
$ FQBN="CubeCell:CubeCell:CubeCell-Board-V2: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"
|
||||||
|
|
||||||
|
Compile:
|
||||||
|
$ arduino-cli compile --export-binaries --clean --fqbn ${FQBN} MiniBeieliNodeSketch
|
||||||
|
|
||||||
|
=== Beispiel ===
|
||||||
|
joerg@mbp mini-beieli-node-cubecell % arduino-cli compile --export-binaries --clean --fqbn ${FQBN} MiniBeieliNodeSketch
|
||||||
|
Der Sketch verwendet 114188 Bytes (87%) des Programmspeicherplatzes. Das Maximum sind 131072 Bytes.
|
||||||
|
|
||||||
|
Benutzte Bibliothek Version Pfad
|
||||||
|
LoRa 1.0 /Users/joerg/Documents/Arduino/hardware/CubeCell/CubeCell/libraries/LoRa
|
||||||
|
LoraMac_102 1.0.2 /Users/joerg/Documents/Arduino/hardware/CubeCell/CubeCell/libraries/LoraWan102
|
||||||
|
SparkFun BME280 2.0.10 /Users/joerg/Documents/Arduino/libraries/SparkFun_BME280_Arduino_Library
|
||||||
|
SparkFun Qwiic Scale NAU7802 Arduino Library 1.0.5 /Users/joerg/Documents/Arduino/libraries/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library
|
||||||
|
|
||||||
|
Verwendete Plattform Version Pfad
|
||||||
|
CubeCell:CubeCell 1.0.0 /Users/joerg/Documents/Arduino/hardware/CubeCell/CubeCell
|
||||||
|
=============
|
||||||
|
|
||||||
|
Upload:
|
||||||
|
$ arduino-cli upload --verbose --fqbn ${FQBN} -p /dev/tty.usbserial-0001 MiniBeieliNodeSketch
|
||||||
|
|
||||||
|
*.cyacd-File kann kopiert werden nach cyacd-Directory:
|
||||||
|
Beispiel:
|
||||||
|
|
||||||
|
joerg@z240 mini-beieli-node-cubecell % arduino-cli upload --verbose --fqbn ${FQBN} -p /dev/tty.usbserial-0001 MiniBeieliNodeSketch
|
||||||
|
"/Users/joerg/Documents/Arduino/hardware/CubeCell/CubeCell/tools/CubeCellflash/CubeCellflash" -serial "/dev/tty.usbserial-0001" "/private/var/folders/n6/jjw6yp4s2dz0sd76rz3k7mzm0000gn/T/arduino/sketches/97CDDDDD36F10A320334F076837B4C34/CubeCell_Board_V2_REGION_EU868_RGB_0.cyacd"
|
||||||
|
|
||||||
|
$ mkdir -p cyacd/v2/20240909
|
||||||
|
$ cp /private/var/folders/n6/jjw6yp4s2dz0sd76rz3k7mzm0000gn/T/arduino/sketches/97CDDDDD36F10A320334F076837B4C34/CubeCell_Board_V2_REGION_EU868_RGB_0.cyacd cyacd/v2/20240909
|
||||||
|
|
||||||
|
Alternative zum Upload der Firmware:
|
||||||
|
$ /Users/joerg/Documents/Arduino/hardware/CubeCell/CubeCell/tools/CubeCellflash/CubeCellflash -serial /dev/tty.usbserial-0001 cyacd/v2/20240909/CubeCell_Board_REGION_EU868_RGB_0.cyacd
|
||||||
|
|
||||||
|
USB-Device kann am einfachsten wie folgt bestimmt werden:
|
||||||
|
|
||||||
|
joerg@mbp mini-beieli-node-cubecell % ls -l /dev/tty.usbserial*
|
||||||
|
crw-rw-rw- 1 root wheel 0x9000004 28 Dez 19:38 /dev/tty.usbserial-0001
|
||||||
|
`
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
module gitlab.com/mini-beieli-node-cubecell/v2
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
|
||||||
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||||
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|
@ -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() {
|
func main() {
|
||||||
if len(os.Args) != 3 {
|
if len(os.Args) != 3 {
|
||||||
fmt.Println("usage: " + filepath.Base(os.Args[0]) + " <SerialDevice> <appEui>")
|
fmt.Println("usage: " + filepath.Base(os.Args[0]) + " <SerialDevice> <appEui>")
|
||||||
|
|
@ -75,10 +120,12 @@ func main() {
|
||||||
|
|
||||||
sendCommand(s, "AT+XXX")
|
sendCommand(s, "AT+XXX")
|
||||||
devEui := getDevEui(s)
|
devEui := getDevEui(s)
|
||||||
|
appKey := getAppKey(devEui)
|
||||||
if devEui != "" {
|
if devEui != "" {
|
||||||
sendCommand(s, "AT+DevEui="+devEui)
|
sendCommand(s, "AT+DevEui="+devEui)
|
||||||
sendCommand(s, "AT+AppEui="+appEui)
|
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")
|
sendCommand(s, "AT+RESET=1")
|
||||||
readSerial(s)
|
readSerial(s)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue