diff --git a/main.cpp b/main.cpp index cfb249f..b718174 100644 --- a/main.cpp +++ b/main.cpp @@ -17,12 +17,6 @@ using namespace events; uint8_t rx_buffer[LORAMAC_PHY_MAXPAYLOAD]; -/* - * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for - * testing - */ -#define TX_TIMER 30000 //600000 - /** * Maximum number of events for the event queue. * 16 is the safe number for the stack events, however, if application @@ -60,37 +54,37 @@ int main(void) { lorawan_status_t retcode; #if MBED_CONF_APP_HX711_ENABLED + loadcell.powerDown(); loadcell.setScale(MBED_CONF_APP_HX711_SCALE); loadcell.setOffset(MBED_CONF_APP_HX711_OFFSET); - loadcell.powerDown(); #endif // Initialize LoRaWAN stack if(lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { - printf("\r\n LoRa initialization failed! \r\n"); + debug("\r\n LoRa initialization failed! \r\n"); return -1; } - printf("\r\n Mbed LoRaWANStack initialized \r\n"); + debug("\r\n Mbed LoRaWANStack initialized \r\n"); callbacks.events = mbed::callback(lora_event_handler); lorawan.add_app_callbacks(&callbacks); // Set number of retries in case of CONFIRMED messages if(lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) != LORAWAN_STATUS_OK) { - printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); + debug("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); return -1; } - printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER); + debug("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER); // Enable adaptive data rate if(lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { - printf("\r\n enable_adaptive_datarate failed! \r\n"); + debug("\r\n enable_adaptive_datarate failed! \r\n"); return -1; } - printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); + debug("\r\n Adaptive data rate (ADR) - Enabled \r\n"); //led = 0; @@ -98,11 +92,11 @@ int main(void) { if(retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { } else { - printf("\r\n Connection error, code = %d \r\n", retcode); + debug("\r\n Connection error, code = %d \r\n", retcode); return -1; } - printf("\r\n Connection - In Progress ...\r\n"); + debug("\r\n Connection - In Progress ...\r\n"); // make your event queue dispatching events forever ev_queue.dispatch_forever(); @@ -118,7 +112,7 @@ static void send_message() { #if MBED_CONF_APP_BATTERY_ENABLED float vbat = bat.read(); - vbat = vbat * 5 / 3 * 3.3f; + vbat = vbat * MBED_CONF_APP_BATTERY_RATIO * 3.3f; cayenne.addAnalogInput(1, vbat); #endif @@ -126,15 +120,17 @@ static void send_message() { int err = dht.read(); if(err == DHT::SUCCESS) { cayenne.addTemperature(1, dht.getTemperature()); - cayenne.addRelativeHumidity(2, dht.getHumidity()); + cayenne.addRelativeHumidity(1, dht.getHumidity()); } else { - printf("Error code : %d\r\n", err); + debug("Error code : %d\r\n", err); } #endif #if MBED_CONF_APP_HX711_ENABLED loadcell.powerUp(); - cayenne.addAnalogInput(2, loadcell.getUnits(5)); + if (loadcell.waitReadyRetry(20, 100)) { + cayenne.addAnalogInput(2, loadcell.getUnits(5)); + } loadcell.powerDown(); #endif @@ -147,12 +143,12 @@ static void send_message() { MSG_UNCONFIRMED_FLAG); if(retcode < 0) { - retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") - : printf("\r\n send() - Error code %d \r\n", retcode); + retcode == LORAWAN_STATUS_WOULD_BLOCK ? debug("send - WOULD BLOCK\r\n") + : debug("\r\n send() - Error code %d \r\n", retcode); return; } - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + debug("\r\n %d bytes scheduled for transmission \r\n", retcode); cayenne.reset(); } @@ -165,17 +161,17 @@ static void receive_message() { MSG_CONFIRMED_FLAG | MSG_UNCONFIRMED_FLAG); if(retcode < 0) { - printf("\r\n receive() - Error code %d \r\n", retcode); + debug("\r\n receive() - Error code %d \r\n", retcode); return; } - printf(" Data:"); + debug(" Data:"); for(uint8_t i = 0; i < retcode; i++) { - printf("%x", rx_buffer[i]); + debug("%x", rx_buffer[i]); } - printf("\r\n Data Length: %d\r\n", retcode); + debug("\r\n Data Length: %d\r\n", retcode); memset(rx_buffer, 0, LORAMAC_PHY_MAXPAYLOAD); } @@ -184,20 +180,21 @@ static void lora_event_handler(lorawan_event_t event) { switch(event) { case CONNECTED: //led = 1; - printf("\r\n Connection - Successful \r\n"); + debug("\r\n Connection - Successful \r\n"); if(MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } else { - ev_queue.call_every(TX_TIMER, send_message); + send_message(); + ev_queue.call_every(MBED_CONF_APP_TX_TIMER, send_message); } break; case DISCONNECTED: ev_queue.break_dispatch(); - printf("\r\n Disconnected Successfully \r\n"); + debug("\r\n Disconnected Successfully \r\n"); break; case TX_DONE: - printf("\r\n Message Sent to Network Server \r\n"); + debug("\r\n Message Sent to Network Server \r\n"); if(MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } @@ -206,22 +203,22 @@ static void lora_event_handler(lorawan_event_t event) { case TX_ERROR: case TX_CRYPTO_ERROR: case TX_SCHEDULING_ERROR: - printf("\r\n Transmission Error - EventCode = %d \r\n", event); + debug("\r\n Transmission Error - EventCode = %d \r\n", event); // try again if(MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } break; case RX_DONE: - printf("\r\n Received message from Network Server \r\n"); + debug("\r\n Received message from Network Server \r\n"); receive_message(); break; case RX_TIMEOUT: case RX_ERROR: - printf("\r\n Error in reception - Code = %d \r\n", event); + debug("\r\n Error in reception - Code = %d \r\n", event); break; case JOIN_FAILURE: - printf("\r\n OTAA Failed - Check Keys \r\n"); + debug("\r\n OTAA Failed - Check Keys \r\n"); break; default: MBED_ASSERT("Unknown Event"); diff --git a/mbed_app.json.sample b/mbed_app.json.sample index 3095ecd..dbf8cd4 100644 --- a/mbed_app.json.sample +++ b/mbed_app.json.sample @@ -1,5 +1,6 @@ { "config": { + "tx-timer": { "value": 30000 }, "lora-radio": { "help": "Which radio to use (options: SX1272,SX1276)", "value": "SX1276" @@ -25,6 +26,7 @@ "lora-tcxo": { "value": "NC" }, "battery-enabled": { "value": "false" }, "battery-adc": { "value": "NC" }, + "battery-ratio": { "value": "2.0f" }, "dht-enabled": { "value": "false" }, "dht-type": { "value": "DHT::DHT22" }, "dht-data": { "value": "NC" }, @@ -256,8 +258,8 @@ "main_stack_size": 1024, "target.lpticker_lptim": 0, "target.clock_source": "USE_PLL_HSI", - "target.stdio_uart_tx": "PC_10", - "target.stdio_uart_rx": "PC_11", + "lora.duty-cycle-on": false, + "tx-timer": 600000, "lora-radio": "SX1272", "lora-spi-mosi": "D11", "lora-spi-miso": "D12", @@ -277,8 +279,9 @@ "lora-ant-switch": "A4", "lora-pwr-amp-ctl": "NC", "lora-tcxo": "NC", - "battery-enabled": false, - "battery-adc": "PA_2", + "battery-enabled": true, + "battery-adc": "A1", + "battery-ratio": "2.0f", "dht-enabled": true, "dht-data": "D8", "dht-type": "DHT::DHT22", @@ -286,7 +289,7 @@ "hx711-clk": "D7", "hx711-data": "D6", "hx711-scale": "21000.0f", - "hx711-offset": -2000 + "hx711-offset": 59600 } }, "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]