From e71159cd3552bcec5980dbe4c3921ba9914ef035 Mon Sep 17 00:00:00 2001 From: Anthony Hinsinger Date: Mon, 11 Mar 2019 09:15:24 +0100 Subject: [PATCH] Added battery level in lora stack and nano libc profile --- README.md | 2 +- main.cpp | 34 +++++++++++++++++++++++----------- mbed_app.json.sample | 2 ++ profiles/release.json | 2 +- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 278e1c2..12d8d0f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ mbed target NUCLEO_L073RZ mbed config -G GCC_ARM_PATH /opt/gcc-arm-none-eabi-7-2017-q4-major/bin cp mbed_app.json.sample mbed_app.json # modify lora.device-eui, lora.application-eui and lora.application-key in mbed_app.json -mbed compile --profile minimal-printf/profiles/release.json +mbed compile --profile profiles/release.json ``` Then you can use stm32flash or - may be better - a stlink interface to copy the generated .bin file onto the RAK811. diff --git a/main.cpp b/main.cpp index b718174..695f1fe 100644 --- a/main.cpp +++ b/main.cpp @@ -37,6 +37,23 @@ static CayenneLPP cayenne(51); #if MBED_CONF_APP_BATTERY_ENABLED AnalogIn bat(MBED_CONF_APP_BATTERY_ADC); +AnalogIn vrefint(ADC_VREF); + +static float battery_voltage() { + float vbat = bat.read(); + double vdd = (1.224f) / vrefint.read(); + + return vbat * MBED_CONF_APP_BATTERY_RATIO * vdd; +} + +static uint8_t battery_level() { + float vbat = battery_voltage(); + + double ratio = 253.0f / (4.2f - 3.3f); + int value = ratio * (vbat - 3.4f) + 1.0f; + + return value < 1 ? 1 : (value > 254 ? 254 : value); +} #endif #if MBED_CONF_APP_DHT_ENABLED @@ -47,10 +64,7 @@ DHT dht(MBED_CONF_APP_DHT_DATA, MBED_CONF_APP_DHT_TYPE); HX711 loadcell(MBED_CONF_APP_HX711_CLK, MBED_CONF_APP_HX711_DATA); #endif -//DigitalOut led(LED1, 1); - int main(void) { - lorawan_status_t retcode; #if MBED_CONF_APP_HX711_ENABLED @@ -68,6 +82,9 @@ int main(void) { debug("\r\n Mbed LoRaWANStack initialized \r\n"); callbacks.events = mbed::callback(lora_event_handler); +#if MBED_CONF_APP_BATTERY_ENABLED + callbacks.battery_level = mbed::callback(battery_level); +#endif lorawan.add_app_callbacks(&callbacks); // Set number of retries in case of CONFIRMED messages @@ -86,8 +103,6 @@ int main(void) { debug("\r\n Adaptive data rate (ADR) - Enabled \r\n"); - //led = 0; - retcode = lorawan.connect(); if(retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { @@ -110,10 +125,8 @@ int main(void) { static void send_message() { int16_t retcode; -#if MBED_CONF_APP_BATTERY_ENABLED - float vbat = bat.read(); - vbat = vbat * MBED_CONF_APP_BATTERY_RATIO * 3.3f; - cayenne.addAnalogInput(1, vbat); +#if MBED_CONF_APP_BATTERY_ENABLED && MBED_CONF_APP_BATTERY_IN_LPP + cayenne.addAnalogInput(1, battery_voltage()); #endif #if MBED_CONF_APP_DHT_ENABLED @@ -128,7 +141,7 @@ static void send_message() { #if MBED_CONF_APP_HX711_ENABLED loadcell.powerUp(); - if (loadcell.waitReadyRetry(20, 100)) { + if(loadcell.waitReadyRetry(20, 100)) { cayenne.addAnalogInput(2, loadcell.getUnits(5)); } loadcell.powerDown(); @@ -179,7 +192,6 @@ static void receive_message() { static void lora_event_handler(lorawan_event_t event) { switch(event) { case CONNECTED: - //led = 1; debug("\r\n Connection - Successful \r\n"); if(MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); diff --git a/mbed_app.json.sample b/mbed_app.json.sample index dbf8cd4..e198d9a 100644 --- a/mbed_app.json.sample +++ b/mbed_app.json.sample @@ -25,6 +25,7 @@ "lora-pwr-amp-ctl": { "value": "NC" }, "lora-tcxo": { "value": "NC" }, "battery-enabled": { "value": "false" }, + "battery-in-lpp": { "value": "false" }, "battery-adc": { "value": "NC" }, "battery-ratio": { "value": "2.0f" }, "dht-enabled": { "value": "false" }, @@ -280,6 +281,7 @@ "lora-pwr-amp-ctl": "NC", "lora-tcxo": "NC", "battery-enabled": true, + "battery-in-lpp": true, "battery-adc": "A1", "battery-ratio": "2.0f", "dht-enabled": true, diff --git a/profiles/release.json b/profiles/release.json index 6c71903..ef9b8b9 100644 --- a/profiles/release.json +++ b/profiles/release.json @@ -12,7 +12,7 @@ "ld": ["-Wl,--gc-sections", "-Wl,--wrap,main", "-Wl,--wrap,_malloc_r", "-Wl,--wrap,_free_r", "-Wl,--wrap,_realloc_r", "-Wl,--wrap,_memalign_r", "-Wl,--wrap,_calloc_r", "-Wl,--wrap,exit", "-Wl,--wrap,atexit", - "-Wl,-n", "--specs=nano.specs"] + "-Wl,-n", "--specs=nano.specs", "-u _printf_float"] }, "ARMC6": { "common": ["-c", "--target=arm-arm-none-eabi", "-mthumb", "-Oz",