Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ad28bda51 | ||
|
|
97992f9682 | ||
|
|
a6d5d045f1 | ||
|
|
74bca8966b | ||
|
|
1772cf6f7e | ||
|
|
cefc4473e5 | ||
|
|
c88c1a68be | ||
|
|
1651aab05b | ||
|
|
2cff8e78c8 | ||
|
|
15d75f10fc | ||
|
|
4adaf33835 | ||
|
|
fe434b1918 | ||
|
|
67546aa925 | ||
|
|
0aef4ac99a |
@@ -0,0 +1,3 @@
|
|||||||
|
mini-beieli-provision-cubecell
|
||||||
|
appkey
|
||||||
|
calibrate
|
||||||
@@ -7,14 +7,21 @@
|
|||||||
NAU7802 nau7802;
|
NAU7802 nau7802;
|
||||||
BME280 bme280;
|
BME280 bme280;
|
||||||
|
|
||||||
|
//#define Vext GPIO6
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
/* Firmware Version */
|
||||||
|
/******************************************************************************/
|
||||||
|
static const int32_t fwVersion = 20210428;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* LoraWAN Settings */
|
/* LoraWAN Settings */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
/* OTAA para*/
|
/* OTAA para*/
|
||||||
uint8_t devEui[] = { 0x00, 0x72, 0x4A, 0x61, 0xB5, 0xB6, 0x20, 0xBB };
|
uint8_t devEui[8];
|
||||||
uint8_t appEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0xE5, 0xAB };
|
uint8_t appEui[8];
|
||||||
uint8_t appKey[] = { 0x55, 0x11, 0xF4, 0x55, 0x71, 0x81, 0xC0, 0xBC, 0x04, 0xA9, 0x16, 0x7F, 0x75, 0x8A, 0xEA, 0xB4 };
|
uint8_t appKey[16];
|
||||||
|
|
||||||
/* 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 };
|
||||||
@@ -140,7 +147,6 @@ float stddev(long samples[], int m) //calculate the stdandard deviation
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* Global Data Structures */
|
/* Global Data Structures */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
static const int32_t fwVersion = 20201020;
|
|
||||||
|
|
||||||
// send an init package every 100 packages;
|
// send an init package every 100 packages;
|
||||||
static const byte INIT_PACKAGE_INTERVAL = 100;
|
static const byte INIT_PACKAGE_INTERVAL = 100;
|
||||||
@@ -161,11 +167,14 @@ 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 {
|
||||||
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
|
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
|
||||||
uint8_t vsupercap; // Spannung Supercap in mV / 20
|
uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
|
||||||
int16_t temperature; // Temperatur (Startwert) in 1/10 Grad Celsius
|
int16_t temperature; // Temperatur (Startwert) in 1/10 Grad Celsius
|
||||||
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
||||||
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
||||||
@@ -179,7 +188,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
|
uint8_t version; // Version of Packet Format (must be increased every time format changes...)
|
||||||
int32_t fw_version; // Version of Firmware, Nummer entspricht YYYYMMDD
|
int32_t fw_version; // Version of Firmware, Nummer entspricht YYYYMMDD
|
||||||
uint8_t vsupercap; // Spannung Supercap in mV / 20
|
uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
|
||||||
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
||||||
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
||||||
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
||||||
@@ -192,7 +201,7 @@ typedef struct {
|
|||||||
} __attribute__((packed)) LORA_data_first;
|
} __attribute__((packed)) LORA_data_first;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t vsupercap; // Spannung Supercap in mV / 20
|
uint8_t vbat; // Batteriespannung (0: <= 2510mV, 70: 3000mV, 170: 3700mV, 255: >= 4295mV [1 Einheit => 7mV])
|
||||||
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
int16_t temperature; // Temperatur in 1/10 Grad Celsius
|
||||||
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
uint8_t humidity; // Luftfeuchtigkeit in Prozent
|
||||||
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
uint8_t pressure; // Luftdruck in Hekto-Pascal (0 entspricht 825 hPa)
|
||||||
@@ -224,7 +233,7 @@ uint32_t gRebootMs;
|
|||||||
void ClearLoraData(bool clearLastValues)
|
void ClearLoraData(bool clearLastValues)
|
||||||
{
|
{
|
||||||
lora_data.version = LORA_DATA_VERSION;
|
lora_data.version = LORA_DATA_VERSION;
|
||||||
lora_data.vsupercap = 0;
|
lora_data.vbat = 0;
|
||||||
lora_data.temperature = 0;
|
lora_data.temperature = 0;
|
||||||
lora_data.humidity = 0;
|
lora_data.humidity = 0;
|
||||||
lora_data.pressure = 0;
|
lora_data.pressure = 0;
|
||||||
@@ -241,7 +250,7 @@ void ClearLoraData(bool clearLastValues)
|
|||||||
|
|
||||||
lora_data_first.version = LORA_DATA_VERSION_FIRST_PACKAGE;
|
lora_data_first.version = LORA_DATA_VERSION_FIRST_PACKAGE;
|
||||||
lora_data_first.fw_version = fwVersion;
|
lora_data_first.fw_version = fwVersion;
|
||||||
lora_data_first.vsupercap = 0;
|
lora_data_first.vbat = 0;
|
||||||
lora_data_first.weight_a = 0;
|
lora_data_first.weight_a = 0;
|
||||||
lora_data_first.weight_b = 0;
|
lora_data_first.weight_b = 0;
|
||||||
lora_data_first.cal_a_0 = config_data.cal_a_0;
|
lora_data_first.cal_a_0 = config_data.cal_a_0;
|
||||||
@@ -256,7 +265,7 @@ void ClearLoraData(bool clearLastValues)
|
|||||||
|
|
||||||
// We initialize last_sensor_reading
|
// We initialize last_sensor_reading
|
||||||
if (clearLastValues) {
|
if (clearLastValues) {
|
||||||
last_sensor_reading.vsupercap = 0;
|
last_sensor_reading.vbat = 0;
|
||||||
last_sensor_reading.weight_a = 0;
|
last_sensor_reading.weight_a = 0;
|
||||||
last_sensor_reading.weight_b = 0;
|
last_sensor_reading.weight_b = 0;
|
||||||
last_sensor_reading.weight = 0;
|
last_sensor_reading.weight = 0;
|
||||||
@@ -275,7 +284,7 @@ void ShowLORAData(bool firstTime)
|
|||||||
Serial.printf("{\n");
|
Serial.printf("{\n");
|
||||||
Serial.printf(" \"version\": \"%d\",\n", lora_data_first.version);
|
Serial.printf(" \"version\": \"%d\",\n", lora_data_first.version);
|
||||||
Serial.printf(" \"fw_version\": \"%d\",\n", lora_data_first.fw_version);
|
Serial.printf(" \"fw_version\": \"%d\",\n", lora_data_first.fw_version);
|
||||||
Serial.printf(" \"vsupercap:\": \"%u\",\n", lora_data_first.vsupercap);
|
Serial.printf(" \"vbat:\": \"%u\",\n", lora_data_first.vbat);
|
||||||
Serial.printf(" \"humidity\": \"%u\",\n", lora_data_first.humidity);
|
Serial.printf(" \"humidity\": \"%u\",\n", lora_data_first.humidity);
|
||||||
Serial.printf(" \"pressure\": \"%u\",\n", lora_data_first.pressure);
|
Serial.printf(" \"pressure\": \"%u\",\n", lora_data_first.pressure);
|
||||||
Serial.printf(" \"weight_a\": \"%ld\",\n", lora_data_first.weight_a);
|
Serial.printf(" \"weight_a\": \"%ld\",\n", lora_data_first.weight_a);
|
||||||
@@ -291,7 +300,7 @@ void ShowLORAData(bool firstTime)
|
|||||||
|
|
||||||
Serial.printf("{\n");
|
Serial.printf("{\n");
|
||||||
Serial.printf(" \"version\": \"%d\",\n", lora_data.version);
|
Serial.printf(" \"version\": \"%d\",\n", lora_data.version);
|
||||||
Serial.printf(" \"vsupercap\": \"%u\",\n", lora_data.vsupercap);
|
Serial.printf(" \"vbat\": \"%u\",\n", lora_data.vbat);
|
||||||
Serial.printf(" \"temperature\": \"%u\",\n", lora_data.temperature);
|
Serial.printf(" \"temperature\": \"%u\",\n", lora_data.temperature);
|
||||||
Serial.printf(" \"humidity\": \"%u\",\n", lora_data.humidity);
|
Serial.printf(" \"humidity\": \"%u\",\n", lora_data.humidity);
|
||||||
Serial.printf(" \"pressure\": \"%u\",\n", lora_data.pressure);
|
Serial.printf(" \"pressure\": \"%u\",\n", lora_data.pressure);
|
||||||
@@ -534,12 +543,58 @@ void PowerupScale()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CalculateWeight()
|
||||||
|
{
|
||||||
|
int32_t weight_current32;
|
||||||
|
int32_t weight_current32_a;
|
||||||
|
int32_t weight_current32_b;
|
||||||
|
|
||||||
|
bool plausible;
|
||||||
|
bool plausible_a;
|
||||||
|
bool plausible_b;
|
||||||
|
|
||||||
|
// Gewicht berechnen
|
||||||
|
weight_current32_a = (int32_t)((sensor_data.weight_a - config_data.cal_a_0) / config_data.cal_a_factor);
|
||||||
|
weight_current32_b = (int32_t)((sensor_data.weight_b - config_data.cal_b_0) / config_data.cal_b_factor);
|
||||||
|
weight_current32 = (int32_t)((weight_current32_a + weight_current32_b) / 5.0);
|
||||||
|
|
||||||
|
// we check if weights are plausible
|
||||||
|
plausible_a = (weight_current32_a > -10000) && (weight_current32_a < 150000);
|
||||||
|
plausible_b = (weight_current32_b > -10000) && (weight_current32_b < 150000);
|
||||||
|
plausible = (plausible_a && plausible_b);
|
||||||
|
|
||||||
|
if (weight_current32 < 0) {
|
||||||
|
if (plausible) {
|
||||||
|
weight_current32 = 0;
|
||||||
|
}
|
||||||
|
} else if (weight_current32 > UINT16_MAX) {
|
||||||
|
//weight_current32 = UINT16_MAX;
|
||||||
|
// we set the weight to 0, as such high values are not realistic and probably a sign for bad calibration...
|
||||||
|
weight_current32 = 0;
|
||||||
|
}
|
||||||
|
if (!plausible) {
|
||||||
|
weight_current32 = NOT_PLAUSIBLE_16;
|
||||||
|
//if (!plausible_a) {
|
||||||
|
// sensor_data.weight_a = NOT_PLAUSIBLE_32;
|
||||||
|
//}
|
||||||
|
//if (!plausible_b) {
|
||||||
|
// sensor_data.weight_b = NOT_PLAUSIBLE_32;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
sensor_data.weight = (uint16_t)weight_current32;
|
||||||
|
}
|
||||||
|
|
||||||
void ReadScales()
|
void ReadScales()
|
||||||
{
|
{
|
||||||
PowerupScale();
|
PowerupScale();
|
||||||
sensor_data.weight_a = ReadScale('A');
|
sensor_data.weight_a = ReadScale('A');
|
||||||
sensor_data.weight_b = ReadScale('B');
|
sensor_data.weight_b = ReadScale('B');
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
Serial.printf("Reading A, B: %d, %d\n", sensor_data.weight_a, sensor_data.weight_b);
|
||||||
|
|
||||||
|
}
|
||||||
PowerdownScale();
|
PowerdownScale();
|
||||||
|
CalculateWeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
@@ -600,6 +655,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] == '?')
|
||||||
@@ -612,19 +680,19 @@ 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(" \"vsupercap\": \"%d\",\n", sensor_data.vsupercap);
|
Serial.printf(" \"vbat\": \"%d\"\n", sensor_data.vbat);
|
||||||
Serial.printf("}\n");
|
Serial.printf("}\n");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd, "SUPERCAP") == 0)
|
if (strcmp(cmd, "VBAT") == 0)
|
||||||
{
|
{
|
||||||
if (content[0] == '?')
|
if (content[0] == '?')
|
||||||
{
|
{
|
||||||
uint16_t BatteryVoltage = getBatteryVoltage();
|
uint16_t VBatVoltage = getBatteryVoltage();
|
||||||
Serial.print("+SUPERCAP=");
|
Serial.print("+VBAT=");
|
||||||
Serial.print(BatteryVoltage);
|
Serial.print(VBatVoltage);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -708,6 +776,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);
|
||||||
@@ -729,7 +801,7 @@ void AddSensorDataToLoraData()
|
|||||||
iteration++;
|
iteration++;
|
||||||
next_package_is_init_package = ((iteration <= INIT_PACKETS) || ((package_counter % INIT_PACKAGE_INTERVAL) == 0));
|
next_package_is_init_package = ((iteration <= INIT_PACKETS) || ((package_counter % INIT_PACKAGE_INTERVAL) == 0));
|
||||||
if (next_package_is_init_package) {
|
if (next_package_is_init_package) {
|
||||||
lora_data_first.vsupercap = sensor_data.vsupercap;
|
lora_data_first.vbat = sensor_data.vbat;
|
||||||
lora_data_first.weight_a = sensor_data.weight_a;
|
lora_data_first.weight_a = sensor_data.weight_a;
|
||||||
lora_data_first.weight_b = sensor_data.weight_b;
|
lora_data_first.weight_b = sensor_data.weight_b;
|
||||||
lora_data_first.temperature = sensor_data.temperature;
|
lora_data_first.temperature = sensor_data.temperature;
|
||||||
@@ -739,7 +811,7 @@ void AddSensorDataToLoraData()
|
|||||||
ShowLORAData(true);
|
ShowLORAData(true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lora_data.vsupercap = sensor_data.vsupercap;
|
lora_data.vbat = sensor_data.vbat;
|
||||||
if (my_position == 0) {
|
if (my_position == 0) {
|
||||||
lora_data.temperature = sensor_data.temperature;
|
lora_data.temperature = sensor_data.temperature;
|
||||||
lora_data.weight_first = sensor_data.weight;
|
lora_data.weight_first = sensor_data.weight;
|
||||||
@@ -778,12 +850,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;
|
||||||
@@ -796,6 +870,22 @@ bool TooBigWeightChange()
|
|||||||
return big_difference;
|
return big_difference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t GetVBatValue(int millivolts)
|
||||||
|
{
|
||||||
|
uint8_t res;
|
||||||
|
if (millivolts <= 2510) {
|
||||||
|
res = 0;
|
||||||
|
|
||||||
|
} else if (millivolts >= 4295) {
|
||||||
|
res = 255;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
res = (millivolts - 2510) / 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
// returns true if data should be sent; read_only: when true, do not use as lora_data
|
// returns true if data should be sent; read_only: when true, do not use as lora_data
|
||||||
bool ReadSensors(bool read_only)
|
bool ReadSensors(bool read_only)
|
||||||
{
|
{
|
||||||
@@ -811,7 +901,7 @@ bool ReadSensors(bool read_only)
|
|||||||
disableVext();
|
disableVext();
|
||||||
|
|
||||||
uint16_t voltage = getBatteryVoltage();
|
uint16_t voltage = getBatteryVoltage();
|
||||||
sensor_data.vsupercap = (uint8_t)(voltage / 20);
|
sensor_data.vbat = GetVBatValue(voltage);
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
Serial.printf("Read ADC, %d Millivolts...\n", voltage);
|
Serial.printf("Read ADC, %d Millivolts...\n", voltage);
|
||||||
}
|
}
|
||||||
@@ -819,6 +909,7 @@ 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) {
|
||||||
@@ -857,6 +948,9 @@ void loop()
|
|||||||
{
|
{
|
||||||
case DEVICE_STATE_INIT:
|
case DEVICE_STATE_INIT:
|
||||||
{
|
{
|
||||||
|
#if(LORAWAN_DEVEUI_AUTO)
|
||||||
|
LoRaWAN.generateDeveuiByChipID();
|
||||||
|
#endif
|
||||||
#if(AT_SUPPORT)
|
#if(AT_SUPPORT)
|
||||||
getDevParam();
|
getDevParam();
|
||||||
#endif
|
#endif
|
||||||
@@ -20,10 +20,52 @@ 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 | f468b40 | Sat, 10 Oct 2020 16:45:30 +0800 |
|
| https://github.com/HelTecAutomation/CubeCell-Arduino.git | 58ed094 | Mon, 26 Apr 2021 09:48:49 +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 | master | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
|
||||||
| https://github.com/mcci-catena/Adafruit_BME280_Library.git | 3dafbe1 | Wed, 13 Dec 2017 13:56:30 -0500 |
|
| https://github.com/sparkfun/SparkFun_BME280_Arduino_Library.git | master | 0b5eabf | Wed, 30 Dec 2020 20:44:27 -0700 |
|
||||||
|
|
||||||
[1]: Commit String: 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:
|
||||||
|
`
|
||||||
|
$ curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=~/bin sh
|
||||||
|
$ arduino-cli version
|
||||||
|
arduino-cli alpha Version: 0.18.1 Commit: b3cf8e19 Date: 2021-04-13T13:08:30Z
|
||||||
|
|
||||||
|
$ arduino-cli sketch new MiniBeieliNodeSketch
|
||||||
|
|
||||||
|
$ arduino-cli core update-index
|
||||||
|
|
||||||
|
$ 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
|
||||||
|
|
||||||
|
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/
|
||||||
|
|
||||||
|
Upload:
|
||||||
|
/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
|
||||||
|
`
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
+149
@@ -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]) + " <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')
|
||||||
|
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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
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 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>")
|
||||||
|
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)
|
||||||
|
appKey := getAppKey(devEui)
|
||||||
|
if devEui != "" {
|
||||||
|
sendCommand(s, "AT+DevEui="+devEui)
|
||||||
|
sendCommand(s, "AT+AppEui="+appEui)
|
||||||
|
sendCommand(s, "AT+AppKey="+appKey)
|
||||||
|
sendCommand(s, "AT+SAVE_OTAA_CONFIG=1")
|
||||||
|
sendCommand(s, "AT+RESET=1")
|
||||||
|
readSerial(s)
|
||||||
|
} else {
|
||||||
|
log.Fatal("Could not get devEui (that is: ChipID)")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user