From 3d03d0b52dc62f2e0fe60d7dd44813679a87b2ef Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Sat, 10 Aug 2019 20:06:25 +0200 Subject: [PATCH] refactor code --- mini-beieli-node.ino | 139 +++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 65 deletions(-) diff --git a/mini-beieli-node.ino b/mini-beieli-node.ino index 5079310..8d90192 100644 --- a/mini-beieli-node.ino +++ b/mini-beieli-node.ino @@ -235,7 +235,7 @@ bool fUsbPower; // have we printed the sleep info? bool g_fPrintedSleeping = false; -// the job that's used to synchronize us with the LMIC code +// tFhe job that's used to synchronize us with the LMIC code static osjob_t sensorJob; void sensorJob_cb(osjob_t* pJob); @@ -270,18 +270,18 @@ void setup_platform(void) ); // read config_data from fram... - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("Reading Calibration Config from FRAM...\n"); - #endif +#endif gCatena.getFram()->getField(cFramStorage::kBme680Cal, (uint8_t *)&config_data, sizeof(config_data)); - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("cal_w1_0: %d\n", config_data.cal_w1_0); gCatena.SafePrintf("cal_w2_0: %d\n", config_data.cal_w2_0); gCatena.SafePrintf("cal_w1_factor: %d.%03d\n", (int)config_data.cal_w1_factor, (int)(config_data.cal_w1_factor * 1000) % 1000); gCatena.SafePrintf("cal_w2_factor: %d.%03d\n", (int)config_data.cal_w2_factor, (int)(config_data.cal_w2_factor * 1000) % 1000); gCatena.SafePrintf("Size of config_data: %d\n", sizeof(config_data)); - #endif +#endif #ifdef USBCON // if running unattended, don't wait for USB connect. @@ -365,6 +365,9 @@ void setup_platform(void) gCatena.SafePrintf("**** no platform, check provisioning ****\n"); flags = 0; } + // Because of Alarms on Swisscom Network + LMIC_setDrTxpow(DR_SF10, 14); + LMIC_setAdrMode(1); } void setup_bme280(void) @@ -378,11 +381,14 @@ void setup_bme280(void) } } -void setup_scales(void) +bool setup_scales(void) { - #ifdef DEBUG + bool res; + res = true; + +#ifdef DEBUG gCatena.SafePrintf("Setup Scales...\n"); - #endif +#endif // Enable Power digitalWrite(D10, HIGH); @@ -393,18 +399,21 @@ void setup_scales(void) // - With a gain factor of 32, channel B is selected // By omitting the gain factor parameter, the library // default "128" (Channel A) is used here. - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("Setup Scale 1...\n"); - #endif +#endif LoadCell.begin(A1, A0); - if (!(LoadCell.wait_ready_timeout(1000))) { - gCatena.SafePrintf("Scale not ready.\n"); + if (!(LoadCell.wait_ready_timeout(2000))) { + gCatena.SafePrintf("Scale not ready after Init.\n"); + res = false; } - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("Setup Scale is complete\n"); - #endif +#endif + + return res; } void setup_flash(void) @@ -557,8 +566,8 @@ uint8_t GetVBatValue(int millivolts) void DoDeepSleep() { - gCatena.SafePrintf("Now going to deep sleep: %d\n",millis()); - + gCatena.SafePrintf("Now going to deep sleep: %d\n", millis()); + // Prepare Deep Sleep gLed.Set(LedPattern::Off); @@ -577,7 +586,7 @@ void DoDeepSleep() SPI.begin(); if (fFlash) gSPI2.begin(); - gCatena.SafePrintf("Done with deep sleep: %d\n",millis()); + gCatena.SafePrintf("Done with deep sleep: %d\n", millis()); } @@ -592,65 +601,42 @@ void ReadSensors(bool firstTime, bool readOnly) int32_t weight_current32; uint16_t weight_current; - if (fBme) { - /* warm up the BME280 by discarding a measurement */ - (void)gBME280.readTemperature(); - - Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity(); - // temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C - // pressure is 2 bytes, hPa * 10. - // humidity is one byte, where 0 == 0/256 and 0xFF == 255/256. - #ifdef DEBUG - gCatena.SafePrintf( - "BME280: T: %d P: %d RH: %d\n", - (int)m.Temperature, - (int)m.Pressure, - (int)m.Humidity); - #endif - temp_current = (int16_t)((m.Temperature) * 10); - humidity_current = (uint8_t)m.Humidity; - pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET); - #ifdef DEBUG - gCatena.SafePrintf("pressure_current: %d\n", pressure_current); - #endif - } - - // vBat float vBat = gCatena.ReadVbat(); - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("vBat: %d mV\n", (int)(vBat * 1000.0f)); - #endif +#endif // vBus float vBus = gCatena.ReadVbus(); - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("vBus: %d mV\n", (int)(vBus * 1000.0f)); - #endif +#endif fUsbPower = (vBus > 3.0) ? true : false; // Setup Scales setup_scales(); // Read Scales - if (LoadCell.is_ready()) { - #ifdef DEBUG + //if (LoadCell.is_ready()) { + if (setup_scales()) { +#ifdef DEBUG Serial.println("HX711 LoadCell is ready."); - #endif - LoadCell.set_gain(128); +#endif + LoadCell.set_gain(128); long w1 = LoadCell.read_average(5); weight1_current = (int32_t)w1; - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("Load_cell 1 output val: %ld\n", w1); gCatena.SafePrintf("Load_cell 1 weight1_current: %ld\n", weight1_current); - #endif - LoadCell.set_gain(32); +#endif + LoadCell.set_gain(32); long w2 = LoadCell.read_average(5); weight2_current = (int32_t)w2; - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("Load_cell 2 output val: %ld\n", w2); gCatena.SafePrintf("Load_cell 2 weight2_current: %ld\n", weight2_current); - #endif +#endif } else { Serial.println("HX711 LoadCell not ready."); @@ -668,6 +654,29 @@ void ReadSensors(bool firstTime, bool readOnly) } weight_current = (uint16_t)weight_current32; + if (fBme) { + /* warm up the BME280 by discarding a measurement */ + (void)gBME280.readTemperature(); + + Adafruit_BME280::Measurements m = gBME280.readTemperaturePressureHumidity(); + // temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C + // pressure is 2 bytes, hPa * 10. + // humidity is one byte, where 0 == 0/256 and 0xFF == 255/256. +#ifdef DEBUG + gCatena.SafePrintf( + "BME280: T: %d P: %d RH: %d\n", + (int)m.Temperature, + (int)m.Pressure, + (int)m.Humidity); +#endif + temp_current = (int16_t)((m.Temperature) * 10); + humidity_current = (uint8_t)m.Humidity; + pressure_current = (uint8_t)((m.Pressure / 100) - PRESSURE_OFFSET); +#ifdef DEBUG + gCatena.SafePrintf("pressure_current: %d\n", pressure_current); +#endif + } + if (not(readOnly)) { if (firstTime) { @@ -748,21 +757,21 @@ void startSendingUplink(bool firstTime) bool fConfirmed = false; if (gCatena.GetOperatingFlags() & (1 << 16)) { - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("requesting confirmed tx\n"); - #endif +#endif fConfirmed = true; } if (firstTime) { - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("SendBuffer firstTime\n"); - #endif +#endif gLoRaWAN.SendBuffer((uint8_t*)&lora_data_first, sizeof(LORA_data_first), sendBufferDoneCb, NULL, fConfirmed); } else { - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("SendBuffer not firstTime\n"); - #endif +#endif gLoRaWAN.SendBuffer((uint8_t*)&lora_data, sizeof(LORA_data), sendBufferDoneCb, NULL, fConfirmed); } ClearLoraData(); @@ -801,9 +810,9 @@ static void txFailedDoneCb( static void settleDoneCb( osjob_t* pSendJob) { - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("settleDoneCb\n"); - #endif +#endif sleepDoneCb(pSendJob); } @@ -827,10 +836,10 @@ static void receiveMessage(void *pContext, uint8_t port, const uint8_t *pMessage unsigned txCycle; unsigned txCount; - #ifdef DEBUG +#ifdef DEBUG gCatena.SafePrintf("receiveMessage was called!!!\n"); - #endif - +#endif + if (! (port == 1 && 2 <= nMessage && nMessage <= 3)) { gCatena.SafePrintf("invalid message port(%02x)/length(%zx)\n",