Compare commits

...
7 Commits
Author SHA1 Message Date
jlehmann e30a8293ac do not try a second time to use SendBuffer at failure 2020-05-29 17:12:20 +02:00
jlehmann 174f8b360f refactor a bit 2020-05-29 16:16:12 +02:00
jlehmann d0389af061 try second time if first try fails (SendBuffer) 2020-05-28 20:08:21 +02:00
jlehmann 7c8a52cafe tune code 2020-05-28 13:27:19 +02:00
jlehmann b71e0c0f0c make it more stable 2020-05-27 15:10:40 +02:00
jlehmann d2e4b81416 remove unused variable 2020-05-27 10:20:45 +02:00
jlehmann de0fef6971 some more tuning 2020-05-25 18:07:56 +02:00
3 changed files with 47 additions and 47 deletions
+22 -9
View File
@@ -677,7 +677,7 @@ void StartNewIteration() {
// we send data the first time the system is started, when the array is full
// or when the weight has fallen more than threshold or the first measurement is
// more than one hour old (which should not happen :-) )
if ( (next_package_is_init_package) || (my_position >= MAX_VALUES_TO_SEND) || ((last_sensor_reading.weight - current_sensor_reading.weight) > SEND_DIFF_THRESHOLD_5GRAMS) || ((millis() - timer_pos0) > 3600000)) {
if ( (next_package_is_init_package) || (my_position >= MAX_VALUES_TO_SEND) || (abs(last_sensor_reading.weight - current_sensor_reading.weight) > SEND_DIFF_THRESHOLD_5GRAMS) || ((millis() - timer_pos0) > 3600000)) {
lora_data.offset_last_reading = (uint8_t)((millis() - timer_pos0) / 1000 / 60);
if (config_data.debug_level > 0) {
gCatena.SafePrintf("startSendingUplink(), my_position: %d, iteration: %d, package_counter: %d\n", my_position, iteration, package_counter);
@@ -697,8 +697,16 @@ void StartNewIteration() {
}
while (send_in_progress && ((millis() - start_time) < 300000))
{
gCatena.poll();
yield();
os_runloop_once();
// gCatena.poll();
// yield();
}
// handle timeout...
if (send_in_progress) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("looks like we timed out waiting for sending to finish...\n", wait_time);
}
send_in_progress = false;
}
wait_time = (uint32_t)((millis() - start_time) / 1000);
if (config_data.debug_level > 0) {
@@ -783,6 +791,9 @@ void startSendingUplink(bool firstTime)
fConfirmed = true;
}
os_runloop_once();
//gCatena.poll();
if (firstTime) {
if (config_data.debug_level > 0) {
gCatena.SafePrintf("SendBuffer firstTime\n");
@@ -813,6 +824,7 @@ static void sendBufferDoneCb(
pFn = settleDoneCb;
if (! fStatus)
{
send_in_progress = false;
if (!gLoRaWAN.IsProvisioned())
{
// we'll talk about it at the callback.
@@ -948,8 +960,9 @@ void doSleepAlert(const bool fDeepSleep)
while (uint32_t(millis() - tNow) < 1000)
{
gCatena.poll();
yield();
os_runloop_once();
//gCatena.poll();
//yield();
}
if (config_data.debug_level > 2) {
gCatena.SafePrintf(".");
@@ -961,8 +974,9 @@ void doSleepAlert(const bool fDeepSleep)
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 100)
{
gCatena.poll();
yield();
os_runloop_once();
//gCatena.poll();
//yield();
}
}
else if (config_data.debug_level > 2) {
@@ -1023,6 +1037,7 @@ void doDeepSleep(osjob_t *pJob)
void deepSleepPrepare(void)
{
Serial.end();
Wire.endTransmission(true);
Wire.end();
SPI.end();
if (fFlash)
@@ -1112,8 +1127,6 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
float fval;
} u;
SENSOR_data temp_sensor_data;
if (config_data.debug_level > 0) {
gCatena.SafePrintf("receiveMessage was called!!!\n");
}
+2 -2
View File
@@ -56,7 +56,7 @@ enum {
|
\****************************************************************************/
static const int32_t fwVersion = 20200523;
static const int32_t fwVersion = 20200529;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8;
@@ -64,7 +64,7 @@ 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 uint16_t SEND_DIFF_THRESHOLD_5GRAMS = 20; // when weight changes by 100g, then send data
static const long NOT_ATTACHED = -2147483648;
static const byte INIT_PACKETS = 5;
+23 -36
View File
@@ -8,14 +8,13 @@
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
#define SAMPLES 10
#define IGNORE_READINGS 5
#define SAMPLES 5
NAU7802 myScale; //Create instance of the NAU7802 class
byte debug_level;
byte interruptPin = A0;
//byte interruptPin = A0;
void SetScalesDebugLevel(byte dbg_level)
{
@@ -25,20 +24,17 @@ void SetScalesDebugLevel(byte dbg_level)
bool InitializeScales()
{
bool result;
result &= myScale.reset(); //Reset all registers
result = myScale.reset(); //Reset all registers
result &= myScale.powerUp(); //Power on analog and digital sections of the scale
// we wait 100 ms to give it time to stabilze
delay(100);
result &= myScale.setIntPolarityHigh();
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_80); //Set samples per second to 10
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.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR);
result &= myScale.setRegister(NAU7802_OTP_B1, 0x30);
result &= myScale.setRegister(NAU7802_PGA, NAU7802_PGA_OUT_EN | NAU7802_PGA_CHP_DIS);
//result &= myScale.setRegister(NAU7802_OTP_B1, 0x30);
//result &= myScale.setRegister(NAU7802_PGA, NAU7802_PGA_OUT_EN | NAU7802_PGA_CHP_DIS);
result &= myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
@@ -51,7 +47,7 @@ bool SetupScales(byte dbg_level)
if (debug_level > 0) {
gCatena.SafePrintf("SetupScales start\n");
}
pinMode(interruptPin, INPUT);
// pinMode(interruptPin, INPUT);
if (!myScale.begin(Wire, false))
{
@@ -91,34 +87,21 @@ long ReadScale(char channel)
}
}
int32_t dummy;
int const ignore_readings = IGNORE_READINGS; // number of first <n> readings to ignore
if (myScale.available()) {
long dummy = myScale.getReading();
}
int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median
for (int i = 0; i < ignore_readings; i++) {
//while (digitalRead(interruptPin) == LOW) {
while (! myScale.available()) {
if ((millis() - startTime) > 60000) {
if (debug_level > 0) {
gCatena.SafePrintf("Timeout while reading scale (dummy values)...\n");
}
break;
}
delay(1);
}
dummy = myScale.getReading();
if (debug_level > 0) {
gCatena.SafePrintf("Dummy Reading int32_t: %d\n", dummy);
}
}
// 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
for (int i = 0; i < num_scale_readings; i++) {
//while (digitalRead(interruptPin) == LOW) {
long mytimer = millis();
while (! myScale.available()) {
// we set a timeout of 60 seconds for the measurement...
if ((millis() - startTime) > 60000) {
// we set a timeout of 10 seconds for the measurement...
if ((millis() - mytimer) > 10000) {
// Timeout reading scale...
Wire.endTransmission(true);
if (debug_level > 0) {
gCatena.SafePrintf("Timeout while reading scale...\n");
}
@@ -126,11 +109,15 @@ long ReadScale(char channel)
}
delay(1);
}
int32_t reading = myScale.getReading();
if (debug_level > 0) {
gCatena.SafePrintf("Reading int32_t: %d\n", reading);
long reading;
if (myScale.available()) {
reading = myScale.getReading();
readings[i] = reading;
}
readings[i] = long(reading); // fill the array with instantaneous readings from the scale
if (debug_level > 0) {
gCatena.SafePrintf("Reading: %d\n", reading);
}
delay(10);
}
long duration = millis() - startTime;