Compare commits

..

49 Commits

Author SHA1 Message Date
Joerg Lehmann cee3752349 some changes 2022-05-21 17:40:50 +02:00
Joerg Lehmann c3aa3b7845 with fixed ADC code 2020-08-04 16:19:15 +02:00
Joerg Lehmann 3bb6edd1e8 only format code 2020-08-04 16:16:30 +02:00
Joerg Lehmann 0aa2c11402 try with other nau7802 settings because of rare wrong readings 2020-07-21 14:27:58 +02:00
Joerg Lehmann 7ef7d25693 try some nau7802 settings because of rare wrong readings 2020-07-21 10:33:14 +02:00
Joerg Lehmann 1f0072797f millis() is unsigned long 2020-07-14 19:45:25 +02:00
Joerg Lehmann cfaccfb14a plausibility checks, eliminiate NOT_ATTACHED 2020-07-01 13:11:56 +02:00
Joerg Lehmann 90782229d5 fix bug 2020-07-01 11:11:16 +02:00
Joerg Lehmann 363e8ecc23 read sensors a second time if values do not seem plausible 2020-07-01 11:05:25 +02:00
Joerg Lehmann 924ec0719f handle scale timeouts better 2020-06-30 09:51:57 +02:00
Joerg Lehmann 3cf651b338 bug fix 2020-06-29 21:50:23 +02:00
Joerg Lehmann 0c553eed56 confirmed only if big difference in weight 2020-06-29 21:13:36 +02:00
Joerg Lehmann fecc1bf67b fix bug with last_values 2020-06-29 20:51:59 +02:00
Joerg Lehmann ddec85f57e fix bug with last_values 2020-06-29 20:10:04 +02:00
Joerg Lehmann 2ebcac04b7 enhance debug message 2020-06-29 17:57:01 +02:00
Joerg Lehmann 592bccdeb5 wait longer for sending to happen 2020-06-29 17:09:11 +02:00
Joerg Lehmann 3028836e2f try to make measurements more stable... 2020-06-29 16:54:59 +02:00
Joerg Lehmann f846c0d82a README update 2020-06-29 15:59:46 +02:00
Joerg Lehmann 875b1fef02 do not go to deep sleep if we do a stop_iterations 2020-06-12 19:54:43 +02:00
Joerg Lehmann 9cc59aadbb new release with patched library Catena-Arduino-Platform 2020-06-12 15:46:22 +02:00
Joerg Lehmann 22982235fb do not send on first position 2020-06-08 20:40:57 +02:00
Joerg Lehmann 23de8b8f4b use SF12 for initial Join 2020-06-08 16:06:52 +02:00
Joerg Lehmann f0b4b0d286 add delay after wakeup of deep sleep; add command calibrate_scales 2020-06-08 14:29:34 +02:00
Joerg Lehmann 3a8764dcb4 avoid recursion 2020-06-06 19:34:47 +02:00
Joerg Lehmann 57d795c36b 20200605 2020-06-06 08:37:28 +02:00
Joerg Lehmann d0a173a54a 20200604 2020-06-04 20:16:01 +02:00
Joerg Lehmann a8fe52fa0e reduce timeout again to 300sec, but sleep 10sec after timeout 2020-06-03 17:05:46 +02:00
Joerg Lehmann eb4da9d637 add debugging info, bug fix 2020-06-03 16:04:47 +02:00
Joerg Lehmann a4c0c71e54 README: patch arduino-lmic to use SF10 2020-06-03 10:37:26 +02:00
Joerg Lehmann fcfc596506 increase timeout from 300 to 900 seconds 2020-06-03 08:56:10 +02:00
Joerg Lehmann 59ce7ee44d 20200602 2020-06-02 21:21:05 +02:00
Joerg Lehmann f8909fc353 print LMIC.opmode before sleep 2020-06-01 19:31:36 +02:00
Joerg Lehmann ab86c843b1 cancel transactions at timeout 2020-06-01 19:04:39 +02:00
Joerg Lehmann 6d3053d8ad try to make it more stable... 2020-06-01 12:57:54 +02:00
Joerg Lehmann 85b0f6db06 refactor 2020-05-30 20:11:19 +02:00
Joerg Lehmann 0c5c673e5e add delay, see https://github.com/mcci-catena/arduino-lmic/issues/547 2020-05-30 18:01:13 +02:00
Joerg Lehmann e30a8293ac do not try a second time to use SendBuffer at failure 2020-05-29 17:12:20 +02:00
Joerg Lehmann 174f8b360f refactor a bit 2020-05-29 16:16:12 +02:00
Joerg Lehmann d0389af061 try second time if first try fails (SendBuffer) 2020-05-28 20:08:21 +02:00
Joerg Lehmann 7c8a52cafe tune code 2020-05-28 13:27:19 +02:00
Joerg Lehmann b71e0c0f0c make it more stable 2020-05-27 15:10:40 +02:00
Joerg Lehmann d2e4b81416 remove unused variable 2020-05-27 10:20:45 +02:00
Joerg Lehmann de0fef6971 some more tuning 2020-05-25 18:07:56 +02:00
Joerg Lehmann 1ce266ab22 remove timestamps in debug messages, debug nau7802 i2c connection 2020-05-23 18:13:08 +02:00
Joerg Lehmann 775eafc74f revised version 20200522 2020-05-22 14:52:09 +02:00
Joerg Lehmann 760ae035fd new version 20200522 2020-05-22 11:14:51 +02:00
Joerg Lehmann bf8c9f8441 optimize power 2020-05-18 16:06:31 +02:00
Joerg Lehmann 14490795af power optimizations 2020-05-15 10:10:29 +02:00
Joerg Lehmann 9c8b63fda8 new ADC: NAU7802, new library versions 2020-05-13 19:30:13 +02:00
6 changed files with 694 additions and 447 deletions

View File

@ -20,16 +20,56 @@ Das sind die verwendeten Libraries [1]:
| --- | ----- | ----------- |
| https://github.com/mcci-catena/Adafruit_BME280_Library.git | 3dafbe1 | Wed, 13 Dec 2017 13:56:30 -0500 |
| https://github.com/mcci-catena/Adafruit_Sensor.git | f2af6f4 | Tue, 1 Sep 2015 15:57:59 +0200 |
| https://github.com/mcci-catena/arduino-lmic.git | f67121c | Mon, 10 Feb 2020 10:57:04 -0500 |
| https://github.com/mcci-catena/arduino-lorawan.git | a0577e1 | Mon, 10 Feb 2020 13:21:30 -0500 |
| https://github.com/mcci-catena/Catena-Arduino-Platform.git | 85c010c | Tue, 11 Feb 2020 19:58:25 -0500 |
| https://github.com/mcci-catena/arduino-lmic.git | 9191f0c | Tue, 30 Jun 2020 09:56:19 -0400 |
| https://github.com/mcci-catena/arduino-lorawan.git | 4bc0d48 | Sat, 9 May 2020 12:38:28 -0400 |
| https://github.com/mcci-catena/Catena-Arduino-Platform.git | 7620a89 | Fri, 31 Jul 2020 14:14:30 -0400 |
| https://github.com/mcci-catena/Catena-mcciadk.git | a428006 | Sat, 21 Dec 2019 20:45:26 -0500 |
| https://github.com/mcci-catena/MCCI_FRAM_I2C.git | f0a5ea5 | Sat, 21 Dec 2019 16:17:01 -0500 |
| https://github.com/tatobari/Q2-HX711-Arduino-Library.git | ccda8d8 | Wed, 13 Mar 2019 12:41:44 -0300 |
| https://github.com/sparkfun/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.git | 688f255 | Fri, 3 Jan 2020 12:35:22 -0700 |
| https://github.com/mcci-catena/OneWire.git | d814a7b | Thu, 26 Apr 2018 03:45:27 +0800 |
| https://github.com/mcci-catena/SHT1x.git | be7042c | Tue, 20 Sep 2011 13:56:23 +1000 |
Patch arduino-lmic, so initial SF12 is used initially:
`
[joerg@cinnamon src]$ git diff
diff --git a/src/lmic/lmic_bandplan_eu868.h b/src/lmic/lmic_bandplan_eu868.h
index efff7d5..74efb37 100644
--- a/src/lmic/lmic_bandplan_eu868.h
+++ b/src/lmic/lmic_bandplan_eu868.h
@@ -61,7 +61,7 @@ LMICeu868_isValidBeacon1(const uint8_t *d) {
#undef LMICbandplan_isFSK
#define LMICbandplan_isFSK() (/* RX datarate */LMIC.dndr == EU868_DR_FSK)
-#define LMICbandplan_getInitialDrJoin() (EU868_DR_SF7)
+#define LMICbandplan_getInitialDrJoin() (EU868_DR_SF12)
void LMICeu868_setBcnRxParams(void);
#define LMICbandplan_setBcnRxParams() LMICeu868_setBcnRxParams()
`
`[1]:
[joerg@cinnamon libraries]$ for i in Adafruit_BME280_Library Adafruit_Sensor arduino-lmic arduino-lorawan Catena-Arduino-Platform Catena-mcciadk MCCI_FRAM_I2C Q2-HX711-Arduino-Library OneWire SHT1x ; do cd $i; echo "| $(git remote -v |grep fetch |awk '{print $2}' |tr '\n' ' ') | $(git log --pretty=format:'%h | %cD ' -n 1) |" ; cd ..; done`
[joerg@cinnamon libraries]$ for i in Adafruit_BME280_Library Adafruit_Sensor arduino-lmic arduino-lorawan Catena-Arduino-Platform Catena-mcciadk MCCI_FRAM_I2C Q2-HX711-Arduino-Library SparkFun_Qwiic_Scale_NAU7802_Arduino_Library OneWire SHT1x ; do cd $i; echo "| $(git remote -v |grep fetch |awk '{print $2}' |tr '\n' ' ') | $(git log --pretty=format:'%h | %cD ' -n 1) |" ; cd ..; done`
## Some Facts about RSSI and SNR
https://lora.readthedocs.io/en/latest/#rssi
RSSI minimum = -120 dBm.
RSSI < -90 dBm: this signal is extremely weak, at the edge of what a receiver can receive.
RSSI -67dBm: this is a fairly strong signal.
RSSI > -55dBm: this is a very strong signal.
RSSI > -30dBm: your sniffer is sitting right next to the transmitter.
https://lora.readthedocs.io/en/latest/#snr
Typical LoRa SNR values are between: -20dB and +10dB
A value closer to +10dB means the received signal is less corrupted.
LoRa can demodulate signals which are -7.5 dB to -20 dB below the noise floor.

78
helper.h Normal file
View File

@ -0,0 +1,78 @@
#ifndef _HELPER_H_
#define _HELPER_H
#pragma once
#ifndef _CATENA_H_
#include <Catena.h>
#endif
using namespace McciCatena;
// the primary object
Catena gCatena;
//Following functions are based on "https://github.com/dndubins/QuickStats", by David Dubins
void bubbleSort(long A[], int len) {
unsigned long newn;
unsigned long n = len;
long temp = 0;
do {
newn = 1;
for (int p = 1; p < len; p++) {
if (A[p - 1] > A[p]) {
temp = A[p]; //swap places in array
A[p] = A[p - 1];
A[p - 1] = temp;
newn = p;
} //end if
} //end for
n = newn;
} while (n > 1);
}
long median(long samples[], int m) //calculate the median
{
//First bubble sort the values: https://en.wikipedia.org/wiki/Bubble_sort
long sorted[m]; // Define and initialize sorted array.
long temp = 0; // Temporary float for swapping elements
for (int i = 0; i < m; i++) {
sorted[i] = samples[i];
}
bubbleSort(sorted, m); // Sort the values
if (bitRead(m, 0) == 1) { //If the last bit of a number is 1, it's odd. This is equivalent to "TRUE". Also use if m%2!=0.
return sorted[m / 2]; //If the number of data points is odd, return middle number.
} else {
return (sorted[(m / 2) - 1] + sorted[m / 2]) / 2; //If the number of data points is even, return avg of the middle two numbers.
}
}
// Joergs STDDEV
float stddev(long samples[], int m) //calculate the stdandard deviation
{
float sum_x;
float sum_x2;
float mean;
float stdev;
sum_x = 0;
sum_x2 = 0;
for (int i = 0; i < m; i++) {
sum_x = sum_x + samples[i];
}
mean = sum_x / m;
for (int i = 0; i < m; i++) {
sum_x2 = sum_x2 + ((samples[i] - mean) * (samples[i] - mean));
}
stdev = sqrt(sum_x2 / m);
return stdev;
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -14,11 +14,11 @@ enum {
// Actual time will be a little longer because have to
// add measurement and broadcast time, but we attempt
// to compensate for the gross effects below.
CATCFG_T_CYCLE = 6 * 60, // every 6 minutes
//CATCFG_T_CYCLE = 30, // for Testing
CATCFG_T_CYCLE_TEST = 30, // every 10 seconds
CATCFG_T_CYCLE_INITIAL = 30, // every 30 seconds initially
CATCFG_INTERVAL_COUNT_INITIAL = 30, // repeat for 15 minutes
CATCFG_T_CYCLE = 6 * 60, // every 6 minutes
//CATCFG_T_CYCLE = 30, // for Testing (Swisscom Compliance)
CATCFG_T_CYCLE_TEST = 30, // every 30 seconds
CATCFG_T_CYCLE_INITIAL = 30, // every 30 seconds initially
CATCFG_INTERVAL_COUNT_INITIAL = 10, // repeat for 5 minutes
CATCFG_T_REBOOT = 30 * 24 * 60 * 60, // reboot every 30 days
};
@ -26,7 +26,7 @@ enum {
enum {
CATCFG_T_WARMUP = 1,
CATCFG_T_SETTLE = 5,
CATCFG_T_OVERHEAD = (CATCFG_T_WARMUP + CATCFG_T_SETTLE),
CATCFG_T_OVERHEAD = (CATCFG_T_WARMUP + CATCFG_T_SETTLE + 4),
CATCFG_T_MIN = CATCFG_T_OVERHEAD,
CATCFG_T_MAX = CATCFG_T_CYCLE < 60 * 60 ? 60 * 60 : CATCFG_T_CYCLE, // normally one hour max.
CATCFG_INTERVAL_COUNT = 30,
@ -56,7 +56,7 @@ enum {
|
\****************************************************************************/
static const int32_t fwVersion = 20200229;
static const int32_t fwVersion = 20200804;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8;
@ -64,8 +64,10 @@ static const byte MAX_VALUES_TO_SEND = 8;
static const uint8_t LORA_DATA_VERSION = 1;
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 128;
static const uint32_t PRESSURE_OFFSET = 825;
static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 10; // when weight value drops by 50g, then send data
static const long NOT_ATTACHED = -2147483648;
static const uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 20; // when weight changes by 100g, then send data
static const long NOT_PLAUSIBLE_16 = 65535;
static const long NOT_PLAUSIBLE_32 = 2147483647;
static const byte INIT_PACKETS = 5;
// must be 64 bytes long (size of kAppConf)
typedef struct {

94
mini_beieli_node_hx711.h Normal file
View File

@ -0,0 +1,94 @@
#define SAMPLES 10
#include <Q2HX711.h>
#ifndef _HELPER_H_
#include "helper.h"
#endif
// Scales
Q2HX711 hx711(A1, A0);
byte debug_level;
void SetScalesDebugLevel(byte dbg_level)
{
debug_level = dbg_level;
}
bool SetupScales(byte dbg_level)
{
debug_level = dbg_level;
if (debug_level > 0) {
gCatena.SafePrintf("setup_scales\n");
}
bool res;
res = true;
// Use D10 to regulate power
pinMode(D10, OUTPUT);
if (debug_level > 0) {
gCatena.SafePrintf("setup_scale done\n");
}
return res;
}
long ReadScale(char channel)
{
if (channel == 'B') {
hx711.setGain(128);
} else {
hx711.setGain(32);
}
delay(500);
long res;
int const num_scale_readings = 25; // number of instantaneous scale readings to calculate the median
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13
long readings[num_scale_readings]; // create arry to hold readings
if (debug_level > 0) {
gCatena.SafePrintf("my_read_average, measurements:\n");
}
for (int i = 0; i < num_scale_readings; i++) {
readings[i] = hx711.read(); // fill the array with instantaneous readings from the scale
if (debug_level > 1) {
gCatena.SafePrintf("Reading %d: %d\n", i, readings[i]);
}
}
res = median(readings, num_scale_readings); // calculate median
if (debug_level > 0) {
gCatena.SafePrintf("Median of %d samples: %d\n", num_scale_readings, res);
float sdev;
sdev = stddev(readings, num_scale_readings);
gCatena.SafePrintf("Standard Deviation: %d.%03d\n", (int)sdev, (int)abs(sdev * 1000) % 1000);
}
return res;
}
void PowerdownScale()
{
// Disable Power
digitalWrite(D10, LOW);
}
void PowerupScale()
{
// Enable Power
digitalWrite(D10, HIGH);
// we wait 400ms (settling time according HX711 datasheet @ 10 SPS
delay(400);
if (debug_level > 0) {
gCatena.SafePrintf("setup_scale done\n");
}
}

174
mini_beieli_node_nau7802.h Normal file
View File

@ -0,0 +1,174 @@
#pragma once
#include <Wire.h>
#ifndef _HELPER_H_
#include "helper.h"
#endif
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
#define SAMPLES 5
byte debug_level;
//byte interruptPin = A0;
void SetScalesDebugLevel(byte dbg_level)
{
debug_level = dbg_level;
}
bool InitializeScales()
{
bool result;
result = myScale.reset(); //Reset all registers
result &= myScale.powerUp(); //Power on analog and digital sections of the scale
result &= myScale.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
result &= myScale.setGain(NAU7802_GAIN_128); //Set gain to 128
result &= myScale.setSampleRate(NAU7802_SPS_40); //Set samples per second to 40
result &= myScale.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
result &= myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
return result;
}
bool SetupScales(byte dbg_level)
{
debug_level = dbg_level;
if (debug_level > 0) {
gCatena.SafePrintf("SetupScales start\n");
}
// pinMode(interruptPin, INPUT);
if (!myScale.begin(Wire, false))
{
gCatena.SafePrintf("Scale not detected. Please check wiring. Freezing...\n");
return false;
}
gCatena.SafePrintf("Scale detected!\n");
bool result = InitializeScales();
if (debug_level > 0) {
gCatena.SafePrintf("SetupScales done, result: %d\n", result);
}
return result;
}
long ReadScale(char channel)
{
long res;
if (debug_level > 0) {
gCatena.SafePrintf("ReadScale Start, Channel %c\n", channel);
}
uint8_t channelNumber;
if (channel == 'B') {
channelNumber = NAU7802_CHANNEL_1;
} else {
channelNumber = NAU7802_CHANNEL_2;
}
unsigned long startTime = millis();
myScale.setChannel(channelNumber);
bool calibrate_success = myScale.calibrateAFE();
if (! calibrate_success) {
if (debug_level > 0) {
gCatena.SafePrintf("Error: Calibration not successful!\n");
}
}
if (myScale.available()) {
long dummy = myScale.getReading();
}
int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13
long readings[num_scale_readings]; // create array to hold readings
for (int i = 0; i < num_scale_readings; i++) {
//while (digitalRead(interruptPin) == LOW) {
unsigned long mytimer = millis();
int timeouts = 0;
while (! myScale.available() && (timeouts < 3)) {
// we set a timeout of 10 seconds for the measurement...
if ((millis() - mytimer) > 10000) {
timeouts = timeouts + 1;
// Timeout reading scale...
Wire.endTransmission(true);
delay(50);
InitializeScales();
if (debug_level > 0) {
gCatena.SafePrintf("Timeout while reading scale...\n");
}
}
delay(50);
}
long reading;
if (myScale.available()) {
reading = myScale.getReading();
readings[i] = reading;
}
if (debug_level > 0) {
gCatena.SafePrintf("Reading: %d\n", reading);
}
delay(50);
}
unsigned long duration = millis() - startTime;
res = median(readings, num_scale_readings); // calculate median
if (debug_level > 0) {
gCatena.SafePrintf("Median of %d samples: %d\n", num_scale_readings, res);
float sdev;
sdev = stddev(readings, num_scale_readings);
float sdev_proc;
sdev_proc = 100 * (sdev / float(res));
gCatena.SafePrintf("Measurements: [");
for (int i = 0; i < num_scale_readings; i++) {
gCatena.SafePrintf("%d", readings[i]);
if (i < (SAMPLES - 1)) {
gCatena.SafePrintf(",");
}
}
gCatena.SafePrintf("]\n");
gCatena.SafePrintf("Standard Deviation: %d.%03d\n", (int)sdev, (int)abs(sdev * 1000) % 1000);
gCatena.SafePrintf("Standard Deviation / Median (Percent): %d.%03d\n", (int)sdev_proc, (int)abs(sdev_proc * 1000) % 1000);
gCatena.SafePrintf("Duration (ms): %d\n", duration);
}
if (debug_level > 0) {
gCatena.SafePrintf("ReadScale Done\n");
}
return res;
}
void PowerdownScale()
{
if (debug_level > 0) {
gCatena.SafePrintf("PowerdownScale Start\n");
}
myScale.powerDown();
if (debug_level > 0) {
gCatena.SafePrintf("PowerdownScale Done\n");
}
}
void PowerupScale()
{
if (debug_level > 0) {
gCatena.SafePrintf("PowerupScale Start\n");
}
InitializeScales();
if (debug_level > 0) {
gCatena.SafePrintf("PowerupScale Done\n");
}
}