Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59ce7ee44d | ||
|
|
f8909fc353 | ||
|
|
ab86c843b1 | ||
|
|
6d3053d8ad | ||
|
|
85b0f6db06 | ||
|
|
0c5c673e5e | ||
|
|
e30a8293ac | ||
|
|
174f8b360f | ||
|
|
d0389af061 | ||
|
|
7c8a52cafe | ||
|
|
b71e0c0f0c | ||
|
|
d2e4b81416 | ||
|
|
de0fef6971 |
+84
-187
@@ -143,8 +143,6 @@ bool fFlash;
|
|||||||
bool fUsbPower;
|
bool fUsbPower;
|
||||||
|
|
||||||
// have we printed the sleep info?
|
// have we printed the sleep info?
|
||||||
bool g_fPrintedSleeping = false;
|
|
||||||
|
|
||||||
// the job that's used to synchronize us with the LMIC code
|
// the job that's used to synchronize us with the LMIC code
|
||||||
static osjob_t iterationJob;
|
static osjob_t iterationJob;
|
||||||
static osjob_t sendJob;
|
static osjob_t sendJob;
|
||||||
@@ -677,7 +675,7 @@ void StartNewIteration() {
|
|||||||
// we send data the first time the system is started, when the array is full
|
// 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
|
// or when the weight has fallen more than threshold or the first measurement is
|
||||||
// more than one hour old (which should not happen :-) )
|
// 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);
|
lora_data.offset_last_reading = (uint8_t)((millis() - timer_pos0) / 1000 / 60);
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("startSendingUplink(), my_position: %d, iteration: %d, package_counter: %d\n", my_position, iteration, package_counter);
|
gCatena.SafePrintf("startSendingUplink(), my_position: %d, iteration: %d, package_counter: %d\n", my_position, iteration, package_counter);
|
||||||
@@ -690,7 +688,7 @@ void StartNewIteration() {
|
|||||||
gLed.Set(LedPattern::TwoShort);
|
gLed.Set(LedPattern::TwoShort);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop while sending is in progress, timeout just in case after 300 seconds
|
// Loop sending is in progress, timeout just in case after 300 seconds
|
||||||
long start_time = millis();
|
long start_time = millis();
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("waiting while send is in progress\n");
|
gCatena.SafePrintf("waiting while send is in progress\n");
|
||||||
@@ -700,6 +698,22 @@ void StartNewIteration() {
|
|||||||
gCatena.poll();
|
gCatena.poll();
|
||||||
yield();
|
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);
|
||||||
|
}
|
||||||
|
LMIC_clrTxData();
|
||||||
|
// we sleep 10 seconds...
|
||||||
|
start_time = millis();
|
||||||
|
while (send_in_progress && ((millis() - start_time) < 10000))
|
||||||
|
{
|
||||||
|
gCatena.poll();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
send_in_progress = false;
|
||||||
|
}
|
||||||
wait_time = (uint32_t)((millis() - start_time) / 1000);
|
wait_time = (uint32_t)((millis() - start_time) / 1000);
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("end waiting, wait time was %d seconds\n", wait_time);
|
gCatena.SafePrintf("end waiting, wait time was %d seconds\n", wait_time);
|
||||||
@@ -719,9 +733,9 @@ void StartNewIteration() {
|
|||||||
sleep_time_sec = 5;
|
sleep_time_sec = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
// for the first <INIT_PACKETS> iterations, we set the sleep time to 10 seconds only...
|
// for the first <INIT_PACKETS> iterations, we set the sleep time to 120 seconds only...
|
||||||
if (iteration <= INIT_PACKETS) {
|
if (iteration <= INIT_PACKETS) {
|
||||||
sleep_time_sec = 10;
|
sleep_time_sec = 120;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
@@ -737,18 +751,29 @@ void StartNewIteration() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we need to periodically reboot, we can do it now...
|
||||||
|
if (uint32_t(millis()) > gRebootMs) {
|
||||||
|
// time to reboot
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("Reached threshold to reboot...\n");
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
NVIC_SystemReset();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just before Sleeping: %#x\n", LMIC.opmode);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fUsbPower) {
|
if (!fUsbPower) {
|
||||||
DoDeepSleep(sleep_time_sec);
|
DoDeepSleep(sleep_time_sec);
|
||||||
if (! stop_iterations) {
|
if (! stop_iterations) {
|
||||||
StartNewIteration();
|
StartNewIteration();
|
||||||
}
|
}
|
||||||
|
|
||||||
//os_setTimedCallback(
|
|
||||||
// &iterationJob,
|
|
||||||
// os_getTime() + sec2osticks(2),
|
|
||||||
// startNewIterationCb);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
if (! stop_iterations) {
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("light sleep; os_setTimedCallback for startNewIterationCb in %d...seconds\n", sleep_time_sec);
|
gCatena.SafePrintf("light sleep; os_setTimedCallback for startNewIterationCb in %d...seconds\n", sleep_time_sec);
|
||||||
}
|
}
|
||||||
@@ -758,6 +783,7 @@ void StartNewIteration() {
|
|||||||
startNewIterationCb);
|
startNewIterationCb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void startSendingUplink(bool firstTime)
|
void startSendingUplink(bool firstTime)
|
||||||
{
|
{
|
||||||
@@ -787,14 +813,27 @@ void startSendingUplink(bool firstTime)
|
|||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("SendBuffer firstTime\n");
|
gCatena.SafePrintf("SendBuffer firstTime\n");
|
||||||
}
|
}
|
||||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
if (gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort)) {
|
||||||
package_counter++;
|
package_counter++;
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just after SendBuffer (successful): %#x\n", LMIC.opmode);
|
||||||
|
} else {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just before SendBuffer (failed): %#x\n", LMIC.opmode);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just before SendBuffer: %#x\n", LMIC.opmode);
|
||||||
gCatena.SafePrintf("SendBuffer not firstTime\n");
|
gCatena.SafePrintf("SendBuffer not firstTime\n");
|
||||||
}
|
}
|
||||||
gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort);
|
if (gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed, kUplinkPort)) {
|
||||||
package_counter++;
|
package_counter++;
|
||||||
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just after SendBuffer (successful): %#x\n", LMIC.opmode);
|
||||||
|
} else {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode just before SendBuffer (failed): %#x\n", LMIC.opmode);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearLoraData();
|
ClearLoraData();
|
||||||
@@ -808,6 +847,7 @@ static void sendBufferDoneCb(
|
|||||||
|
|
||||||
if (config_data.debug_level > 1) {
|
if (config_data.debug_level > 1) {
|
||||||
gLed.Set(LedPattern::Settling);
|
gLed.Set(LedPattern::Settling);
|
||||||
|
gCatena.SafePrintf("LMIC.opmode in sendBufferDoneCb: %#x\n", LMIC.opmode);
|
||||||
}
|
}
|
||||||
|
|
||||||
pFn = settleDoneCb;
|
pFn = settleDoneCb;
|
||||||
@@ -822,7 +862,7 @@ static void sendBufferDoneCb(
|
|||||||
gLoRaWAN.Shutdown();
|
gLoRaWAN.Shutdown();
|
||||||
}
|
}
|
||||||
else if (config_data.debug_level > 0) {
|
else if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("send buffer failed\n");
|
gCatena.SafePrintf("send buffer failed, LMIC.opmode: %#x\n", LMIC.opmode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -846,128 +886,36 @@ static void txNotProvisionedCb(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void settleDoneCb(
|
|
||||||
osjob_t* pSendJob)
|
|
||||||
{
|
|
||||||
const bool fDeepSleep = checkDeepSleep();
|
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
|
||||||
gCatena.SafePrintf("settleDoneCb\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config_data.debug_level > 2) {
|
|
||||||
// Terry vv
|
|
||||||
gCatena.SafePrintf("LMIC.rxDelay: %i\n", LMIC.rxDelay);
|
|
||||||
gCatena.SafePrintf("LMIC.dn2Dr: %i\n", LMIC.dn2Dr);
|
|
||||||
gCatena.SafePrintf("LMIC.dn2Freq: %i\n", LMIC.dn2Freq);
|
|
||||||
gCatena.SafePrintf("LMIC.rx1DrOffset: %i\n", LMIC.rx1DrOffset);
|
|
||||||
gCatena.SafePrintf("LMIC.adrAckReq: %i\n", LMIC.adrAckReq);
|
|
||||||
gCatena.SafePrintf("LMIC.adrEnabled: %i\n", LMIC.adrEnabled);
|
|
||||||
// Terry ^^
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uint32_t(millis()) > gRebootMs) {
|
|
||||||
// time to reboot
|
|
||||||
NVIC_SystemReset();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! g_fPrintedSleeping)
|
|
||||||
doSleepAlert(fDeepSleep);
|
|
||||||
|
|
||||||
/* count what we're up to */
|
|
||||||
updateSleepCounters();
|
|
||||||
|
|
||||||
if (fDeepSleep)
|
|
||||||
doDeepSleep(pSendJob);
|
|
||||||
else
|
|
||||||
doLightSleep(pSendJob);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool checkDeepSleep(void)
|
bool checkDeepSleep(void)
|
||||||
{
|
{
|
||||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
return !fUsbPower;
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
|
||||||
bool fDeepSleep;
|
|
||||||
|
|
||||||
if (fDeepSleepTest)
|
|
||||||
{
|
|
||||||
fDeepSleep = true;
|
|
||||||
}
|
|
||||||
#ifdef USBCON
|
|
||||||
else if (Serial.dtr())
|
|
||||||
{
|
|
||||||
fDeepSleep = false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else if (gCatena.GetOperatingFlags() &
|
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDisableDeepSleep))
|
|
||||||
{
|
|
||||||
fDeepSleep = false;
|
|
||||||
}
|
|
||||||
else if ((gCatena.GetOperatingFlags() &
|
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended)) != 0)
|
|
||||||
{
|
|
||||||
fDeepSleep = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fDeepSleep = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fDeepSleep;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doSleepAlert(const bool fDeepSleep)
|
void doSleepAlert(const bool fDeepSleep)
|
||||||
{
|
{
|
||||||
g_fPrintedSleeping = true;
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("We wait until is is safe to go to sleep...\n");
|
||||||
if (fDeepSleep)
|
|
||||||
{
|
|
||||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
|
||||||
const uint32_t deepSleepDelay = fDeepSleepTest ? 10 : 30;
|
|
||||||
if (config_data.debug_level > 2) {
|
|
||||||
gCatena.SafePrintf("using deep sleep in %u secs"
|
|
||||||
#ifdef USBCON
|
|
||||||
" (USB will disconnect while asleep)"
|
|
||||||
#endif
|
|
||||||
": ",
|
|
||||||
deepSleepDelay
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
// sleep and print
|
|
||||||
if (config_data.debug_level > 1) {
|
|
||||||
gLed.Set(LedPattern::TwoShort);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto n = deepSleepDelay; n > 0; --n)
|
for (int i = 0; i <= 15; i++) {
|
||||||
{
|
long prevPrint = millis();
|
||||||
uint32_t tNow = millis();
|
while (os_queryTimeCriticalJobs(ms2osticks(2000)) != 0)
|
||||||
|
|
||||||
while (uint32_t(millis() - tNow) < 1000)
|
|
||||||
{
|
{
|
||||||
gCatena.poll();
|
gCatena.poll();
|
||||||
yield();
|
yield();
|
||||||
}
|
if (millis() - prevPrint > 1000) {
|
||||||
if (config_data.debug_level > 2) {
|
prevPrint = millis();
|
||||||
gCatena.SafePrintf(".");
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("LMIC.opmode: %#x in loop %d\n", LMIC.opmode, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (config_data.debug_level > 2) {
|
|
||||||
gCatena.SafePrintf("\nStarting deep sleep.\n");
|
|
||||||
}
|
|
||||||
uint32_t tNow = millis();
|
|
||||||
while (uint32_t(millis() - tNow) < 100)
|
|
||||||
{
|
|
||||||
gCatena.poll();
|
|
||||||
yield();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (config_data.debug_level > 2) {
|
|
||||||
gCatena.SafePrintf("using light sleep\n");
|
if (config_data.debug_level > 0) {
|
||||||
|
gCatena.SafePrintf("Now it is safe to go to sleep\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateSleepCounters(void)
|
void updateSleepCounters(void)
|
||||||
@@ -981,10 +929,9 @@ void updateSleepCounters(void)
|
|||||||
else if (gTxCycleCount == 1)
|
else if (gTxCycleCount == 1)
|
||||||
{
|
{
|
||||||
// it's now one (otherwise we couldn't be here.)
|
// it's now one (otherwise we couldn't be here.)
|
||||||
if (config_data.debug_level > 2) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE);
|
gCatena.SafePrintf("resetting tx cycle to default: %u\n", CATCFG_T_CYCLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
gTxCycleCount = 0;
|
gTxCycleCount = 0;
|
||||||
gTxCycle = CATCFG_T_CYCLE;
|
gTxCycle = CATCFG_T_CYCLE;
|
||||||
}
|
}
|
||||||
@@ -994,35 +941,34 @@ void updateSleepCounters(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void doDeepSleep(osjob_t *pJob)
|
|
||||||
{
|
|
||||||
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
|
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
|
|
||||||
uint32_t const sleepInterval = CATCFG_GetInterval(
|
|
||||||
fDeepSleepTest ? CATCFG_T_CYCLE_TEST : gTxCycle
|
|
||||||
);
|
|
||||||
|
|
||||||
|
static void settleDoneCb(
|
||||||
|
osjob_t* pSendJob)
|
||||||
|
{
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("doDeepSleep, sleepInterval: %d...\n", sleepInterval);
|
gCatena.SafePrintf("settleDoneCb - we are at the end of the callback chain!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ok... now it's time for a deep sleep */
|
const bool fDeepSleep = checkDeepSleep();
|
||||||
gLed.Set(LedPattern::Off);
|
|
||||||
deepSleepPrepare();
|
|
||||||
|
|
||||||
/* sleep */
|
if (uint32_t(millis()) > gRebootMs)
|
||||||
gCatena.Sleep(sleepInterval);
|
{
|
||||||
|
// time to reboot
|
||||||
|
NVIC_SystemReset();
|
||||||
|
}
|
||||||
|
|
||||||
/* recover from sleep */
|
doSleepAlert(fDeepSleep);
|
||||||
deepSleepRecovery();
|
|
||||||
|
|
||||||
/* and now... we're awake again. trigger another measurement */
|
/* count what we're up to */
|
||||||
sleepDoneCb(pJob);
|
updateSleepCounters();
|
||||||
|
|
||||||
|
send_in_progress = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deepSleepPrepare(void)
|
void deepSleepPrepare(void)
|
||||||
{
|
{
|
||||||
Serial.end();
|
Serial.end();
|
||||||
|
Wire.endTransmission(true);
|
||||||
Wire.end();
|
Wire.end();
|
||||||
SPI.end();
|
SPI.end();
|
||||||
if (fFlash)
|
if (fFlash)
|
||||||
@@ -1038,53 +984,6 @@ void deepSleepRecovery(void)
|
|||||||
gSPI2.begin();
|
gSPI2.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void doLightSleep(osjob_t *pJob)
|
|
||||||
{
|
|
||||||
uint32_t interval = sec2osticks(CATCFG_GetInterval(gTxCycle));
|
|
||||||
|
|
||||||
|
|
||||||
if (config_data.debug_level > 1) {
|
|
||||||
gLed.Set(LedPattern::Sleeping);
|
|
||||||
gCatena.SafePrintf("doLightSleep\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gCatena.GetOperatingFlags() &
|
|
||||||
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fQuickLightSleep))
|
|
||||||
{
|
|
||||||
interval = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
os_setTimedCallback(
|
|
||||||
&iterationJob,
|
|
||||||
os_getTime() + interval,
|
|
||||||
sleepDoneCb
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sleepDoneCb(osjob_t* pJob)
|
|
||||||
{
|
|
||||||
if (config_data.debug_level > 1) {
|
|
||||||
gLed.Set(LedPattern::WarmingUp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
|
||||||
gCatena.SafePrintf("sleepDoneCb\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
os_setTimedCallback(
|
|
||||||
pJob,
|
|
||||||
os_getTime() + sec2osticks(CATCFG_T_WARMUP),
|
|
||||||
warmupDoneCb);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void warmupDoneCb(osjob_t* pJob)
|
|
||||||
{
|
|
||||||
if (config_data.debug_level > 0) {
|
|
||||||
gCatena.SafePrintf("warmupDoneCb\n");
|
|
||||||
}
|
|
||||||
send_in_progress = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void startNewIterationCb(osjob_t* pJob)
|
static void startNewIterationCb(osjob_t* pJob)
|
||||||
{
|
{
|
||||||
@@ -1112,8 +1011,6 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage
|
|||||||
float fval;
|
float fval;
|
||||||
} u;
|
} u;
|
||||||
|
|
||||||
SENSOR_data temp_sensor_data;
|
|
||||||
|
|
||||||
if (config_data.debug_level > 0) {
|
if (config_data.debug_level > 0) {
|
||||||
gCatena.SafePrintf("receiveMessage was called!!!\n");
|
gCatena.SafePrintf("receiveMessage was called!!!\n");
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -56,7 +56,7 @@ enum {
|
|||||||
|
|
|
|
||||||
\****************************************************************************/
|
\****************************************************************************/
|
||||||
|
|
||||||
static const int32_t fwVersion = 20200523;
|
static const int32_t fwVersion = 20200602;
|
||||||
|
|
||||||
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
|
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
|
||||||
static const byte MAX_VALUES_TO_SEND = 8;
|
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 = 1;
|
||||||
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 128;
|
static const uint8_t LORA_DATA_VERSION_FIRST_PACKAGE = 128;
|
||||||
static const uint32_t PRESSURE_OFFSET = 825;
|
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 long NOT_ATTACHED = -2147483648;
|
||||||
static const byte INIT_PACKETS = 5;
|
static const byte INIT_PACKETS = 5;
|
||||||
|
|
||||||
|
|||||||
+23
-36
@@ -8,14 +8,13 @@
|
|||||||
|
|
||||||
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
|
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
|
||||||
|
|
||||||
#define SAMPLES 10
|
#define SAMPLES 5
|
||||||
#define IGNORE_READINGS 5
|
|
||||||
|
|
||||||
NAU7802 myScale; //Create instance of the NAU7802 class
|
NAU7802 myScale; //Create instance of the NAU7802 class
|
||||||
|
|
||||||
byte debug_level;
|
byte debug_level;
|
||||||
|
|
||||||
byte interruptPin = A0;
|
//byte interruptPin = A0;
|
||||||
|
|
||||||
void SetScalesDebugLevel(byte dbg_level)
|
void SetScalesDebugLevel(byte dbg_level)
|
||||||
{
|
{
|
||||||
@@ -25,20 +24,17 @@ void SetScalesDebugLevel(byte dbg_level)
|
|||||||
bool InitializeScales()
|
bool InitializeScales()
|
||||||
{
|
{
|
||||||
bool result;
|
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
|
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.setIntPolarityHigh();
|
||||||
result &= myScale.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
|
result &= myScale.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
|
||||||
result &= myScale.setGain(NAU7802_GAIN_128); //Set gain to 128
|
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.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.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR);
|
||||||
result &= myScale.setRegister(NAU7802_OTP_B1, 0x30);
|
//result &= myScale.setRegister(NAU7802_OTP_B1, 0x30);
|
||||||
result &= myScale.setRegister(NAU7802_PGA, NAU7802_PGA_OUT_EN | NAU7802_PGA_CHP_DIS);
|
//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
|
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) {
|
if (debug_level > 0) {
|
||||||
gCatena.SafePrintf("SetupScales start\n");
|
gCatena.SafePrintf("SetupScales start\n");
|
||||||
}
|
}
|
||||||
pinMode(interruptPin, INPUT);
|
// pinMode(interruptPin, INPUT);
|
||||||
|
|
||||||
if (!myScale.begin(Wire, false))
|
if (!myScale.begin(Wire, false))
|
||||||
{
|
{
|
||||||
@@ -91,34 +87,21 @@ long ReadScale(char channel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t dummy;
|
if (myScale.available()) {
|
||||||
int const ignore_readings = IGNORE_READINGS; // number of first <n> readings to ignore
|
long dummy = myScale.getReading();
|
||||||
|
}
|
||||||
int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median
|
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
|
// 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
|
long readings[num_scale_readings]; // create arry to hold readings
|
||||||
for (int i = 0; i < num_scale_readings; i++) {
|
for (int i = 0; i < num_scale_readings; i++) {
|
||||||
//while (digitalRead(interruptPin) == LOW) {
|
//while (digitalRead(interruptPin) == LOW) {
|
||||||
|
long mytimer = millis();
|
||||||
while (! myScale.available()) {
|
while (! myScale.available()) {
|
||||||
// we set a timeout of 60 seconds for the measurement...
|
// we set a timeout of 10 seconds for the measurement...
|
||||||
if ((millis() - startTime) > 60000) {
|
if ((millis() - mytimer) > 10000) {
|
||||||
|
// Timeout reading scale...
|
||||||
|
Wire.endTransmission(true);
|
||||||
if (debug_level > 0) {
|
if (debug_level > 0) {
|
||||||
gCatena.SafePrintf("Timeout while reading scale...\n");
|
gCatena.SafePrintf("Timeout while reading scale...\n");
|
||||||
}
|
}
|
||||||
@@ -126,11 +109,15 @@ long ReadScale(char channel)
|
|||||||
}
|
}
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
int32_t reading = myScale.getReading();
|
long reading;
|
||||||
if (debug_level > 0) {
|
if (myScale.available()) {
|
||||||
gCatena.SafePrintf("Reading int32_t: %d\n", reading);
|
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;
|
long duration = millis() - startTime;
|
||||||
|
|||||||
Reference in New Issue
Block a user