diff --git a/include/lorawan.h b/include/lorawan.h index 74a67c04..5c5c67cb 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -49,6 +49,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg, void myTxCallback(void *pUserData, int fSuccess); void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], const uint8_t tablesize); +uint8_t getBattLevel(void); #if (TIME_SYNC_LORAWAN) void user_request_network_time_callback(void *pVoidUserUTCTime, diff --git a/include/power.h b/include/power.h index 3622f643..c27daa6c 100644 --- a/include/power.h +++ b/include/power.h @@ -19,6 +19,6 @@ void AXP192_init(void); void AXP192_displaypower(void); uint16_t read_voltage(void); void calibrate_voltage(void); -uint8_t getBattLevel(void); +bool batt_sufficient(void); #endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 1cdd9b14..d728d1bc 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -57,13 +57,16 @@ void doHousekeeping() { // read battery voltage into global variable #if (defined BAT_MEASURE_ADC || defined HAS_PMU) batt_voltage = read_voltage(); - ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage); + if (batt_voltage = 0xffff) + ESP_LOGI(TAG, "Battery: external power"); + else + ESP_LOGI(TAG, "Battery: %dmV", batt_voltage); #ifdef HAS_PMU if (I2C_MUTEX_LOCK()) { AXP192_displaypower(); I2C_MUTEX_UNLOCK(); } -#endif // HAS_PMU +#endif #endif // display BME680/280 sensor data diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 5d8359d5..bd5284f8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -234,13 +234,10 @@ void onEvent(ev_t ev) { sprintf(display_line6, " "); // clear previous lmic status // set data rate adaptation according to saved setting LMIC_setAdrMode(cfg.adrmode); - // set cyclic lmic link check to off if no ADR because is not supported by - // ttn (but enabled by lmic after join) - LMIC_setLinkCheckMode(cfg.adrmode); - // Set data rate and transmit power (note: txpower seems to be ignored by - // the library) - switch_lora(cfg.lorasf, cfg.txpower); - // show effective LoRa parameters after join + // set data rate and transmit power if we have no ADR + if (!cfg.adrmode) + switch_lora(cfg.lorasf, cfg.txpower); + // show current devaddr ESP_LOGI(TAG, "DEVaddr=%08X", LMIC.devaddr); break; @@ -541,6 +538,7 @@ void lmictask(void *pvParameters) { os_init(); // initialize lmic run-time environment LMIC_reset(); // initialize lmic MAC LMIC_setLinkCheckMode(0); + // This tells LMIC to make the receive windows bigger, in case your clock is // faster or slower. This causes the transceiver to be earlier switched on, // so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE @@ -548,15 +546,9 @@ void lmictask(void *pvParameters) { #ifdef CLOCK_ERROR_PROCENTAGE LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100); #endif - // Set the data rate to Spreading Factor 7. This is the fastest supported - // rate for 125 kHz channels, and it minimizes air time and battery power. - // Set the transmission power to 14 dBi (25 mW). - LMIC_setDrTxpow(DR_SF7, 14); - // register a callback for downlink messages. We aren't trying to write - // reentrant code, so pUserData is NULL. - LMIC_registerRxMessageCb(myRxCallback, NULL); -#if defined(CFG_US915) || defined(CFG_au921) +//#if defined(CFG_US915) || defined(CFG_au921) +#if CFG_LMIC_US_like // in the US, with TTN, it saves join time if we start on subband 1 // (channels 8-15). This will get overridden after the join by parameters // from the network. If working with other networks or in other regions, @@ -564,6 +556,15 @@ void lmictask(void *pvParameters) { LMIC_selectSubBand(1); #endif + // Set the data rate to Spreading Factor 7. This is the fastest supported + // rate for 125 kHz channels, and it minimizes air time and battery power. + // Set the transmission power to 14 dBi (25 mW). + LMIC_setDrTxpow(DR_SF7, 14); + + // register a callback for downlink messages. We aren't trying to write + // reentrant code, so pUserData is NULL. + LMIC_registerRxMessageCb(myRxCallback, NULL); + while (1) { os_runloop_once(); // execute lmic scheduled jobs and events delay(2); // yield to CPU @@ -665,4 +666,30 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], } // mac_decode() +uint8_t getBattLevel() { + /* + return values: + MCMD_DEVS_EXT_POWER = 0x00, // external power supply + MCMD_DEVS_BATT_MIN = 0x01, // min battery value + MCMD_DEVS_BATT_MAX = 0xFE, // max battery value + MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level + */ +#if (defined HAS_PMU || defined BAT_MEASURE_ADC) + uint16_t voltage = read_voltage(); + + switch (voltage) { + case 0: + return MCMD_DEVS_BATT_NOINFO; + case 0xffff: + return MCMD_DEVS_EXT_POWER; + default: + return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN); + } +#else // we don't have any info on battery level + return MCMD_DEVS_BATT_NOINFO; +#endif +} // getBattLevel() + +//u1_t os_getBattLevel(void) { return getBattLevel(); }; + #endif // HAS_LORA \ No newline at end of file diff --git a/src/ota.cpp b/src/ota.cpp index 1a1e9052..18eaa84e 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -41,7 +41,7 @@ inline String getHeaderValue(String header, String headerName) { void start_ota_update() { // check battery status if we can before doing ota - if (getBattLevel() == MCMD_DEVS_BATT_MIN) { + if (!batt_sufficient()) { ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage); return; } diff --git a/src/power.cpp b/src/power.cpp index 5dccf6d0..877fc953 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -78,12 +78,18 @@ void AXP192_power(bool on) { void AXP192_displaypower(void) { if (pmu.isBatteryConnect()) if (pmu.isChargeing()) - ESP_LOGI(TAG, "Battery charging @ %.0fmAh", pmu.getBattChargeCurrent()); + ESP_LOGI(TAG, "Battery charging %.0fmAh @ Temp %.1f°C", + pmu.getBattChargeCurrent(), pmu.getTSTemp()); else - ESP_LOGI(TAG, "Battery discharging @ %0.fmAh", - pmu.getBattDischargeCurrent()); + ESP_LOGI(TAG, "Battery not charging, Temp %.1f°C", pmu.getTSTemp()); else ESP_LOGI(TAG, "No Battery"); + + if (pmu.isVBUSPlug()) + ESP_LOGI(TAG, "USB present, %.0fmAh @ %.1fV", pmu.getVbusCurrent(), + pmu.getVbusVoltage()); + else + ESP_LOGI(TAG, "USB not present"); } void AXP192_init(void) { @@ -111,7 +117,7 @@ void AXP192_init(void) { pmu.clearIRQ(); #endif // PMU_INT - ESP_LOGI(TAG, "AXP192 PMU initialized."); + ESP_LOGI(TAG, "AXP192 PMU initialized, chip Temp %.1f°C", pmu.getTemp()); AXP192_displaypower(); } I2C_MUTEX_UNLOCK(); // release i2c bus access @@ -161,31 +167,15 @@ void calibrate_voltage(void) { #endif } -uint8_t getBattLevel() { - /* - return values: - MCMD_DEVS_EXT_POWER = 0x00, // external power supply - MCMD_DEVS_BATT_MIN = 0x01, // min battery value - MCMD_DEVS_BATT_MAX = 0xFE, // max battery value - MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level - */ +bool batt_sufficient() { #if (defined HAS_PMU || defined BAT_MEASURE_ADC) - uint16_t voltage = read_voltage(); - - switch (voltage) { - case 0: - return MCMD_DEVS_BATT_NOINFO; - case 0xffff: - return MCMD_DEVS_EXT_POWER; - default: - return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN); - } -#else // we don't have any info on battery level - return MCMD_DEVS_BATT_NOINFO; + uint16_t volts = read_voltage(); + return ((volts < 1000) || + (volts > OTA_MIN_BATT)); // no battery or battery sufficient +#else + return true; #endif -} // getBattLevel() - -// u1_t os_getBattLevel(void) { return getBattLevel(); }; +} uint16_t read_voltage() {