Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM
Fork of LRAT-example-lorawan by
Diff: main.cpp
- Revision:
- 40:eaf476b94684
- Parent:
- 39:6da6cb44f22e
- Child:
- 41:98266e454444
--- a/main.cpp Thu Aug 30 21:29:22 2018 +0000 +++ b/main.cpp Tue Sep 04 15:03:49 2018 +0000 @@ -36,6 +36,9 @@ #include "trace_helper.h" #include "lora_radio_helper.h" +// HUOT ADDITION -- 08/21/2018 -- Import of EEPROM Library +#include "stm32EEPROM.h" + #include "mbed-trace/mbed_trace.h" #define TRACE_GROUP "MAIN" @@ -62,17 +65,21 @@ /** * Maximum number of retries for CONFIRMED messages before giving up */ -#define CONFIRMED_MSG_RETRY_COUNTER 15 +#define CONFIRMED_MSG_RETRY_COUNTER 3 + +// HUOT NOTE -- THINK THIS CAN BE REMOVED /** * Dummy pin for dummy sensor */ -#define PC_9 0 +//#define PC_9 0 /** * Dummy sensor class object */ -DS1820 ds1820(PC_9); +//DS1820 ds1820(PC_9); + +#define PACKET_LEN 11 /** * This event queue is the global event queue for both the @@ -339,9 +346,20 @@ char cmdResetDefaults[21] {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x03}; // UBX-CFG-CFG -- Reset to Defaults (also leave 2 bytes) // THIS WORKS +char lowPowerMode[10] {0xB5, 0x62, 0x06, 0x11, 0x02, 0x00, 0x48, 0x01}; char secondTimeSleep[12] {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x08, 0x00}; char secondTimeAwake[12] {0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00,0x09, 0x00}; + +// HUOT ADDITIONS -- 08/31/2018 -- EEPROM Variables +bool wasEEPROM = false; +bool continueSending = true; +int eepromCounter = 0; +int registerLocation = 0; + + + + time_t tInit = 0; time_t tBump = 0; time_t tLast = 0; @@ -357,6 +375,7 @@ void magInitSequence(); void accInitSequence(); void gpsInitSequence(); +// HUOT ADDITION -- 08/30/2018 void gpsExecuteCommand(char *command, int length); void tmpRead(); void magRead(); @@ -364,6 +383,9 @@ int gpsRead(); void ubxRead(); void send_message(); +// HUOT ADDITION -- 08/31/2018 +void backupPacket(); +void prepBacklogSend(); void onBtnIrq() { @@ -556,26 +578,30 @@ // HUOT ADDITION -- 08/30/2018 -- MAIN LOOP TO TEST GPS SLEEP/WAKE FUNCTIONALITY -- GOT IT WORKING int x = 0; + +// printf("LPM INIT \r\n"); +// gpsExecuteCommand(lowPowerMode, sizeof(lowPowerMode)); +// printf("Putting to sleep... Good Luck ================ \r\n"); gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); - while(1) - { - if ((x != 0) && (x % 10 == 0)) - { - printf("Waking up GPS... Good luck, Huot \r\n"); - gpsExecuteCommand(secondTimeAwake, sizeof(secondTimeAwake)); - - while (gpsRead() == 1); - - printf("GPS Fix is good! Going to sleep... \r\n"); - gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); - } - printf("Waiting 5 seconds... \r\n"); - wait(5); - x++; - printf("=== New loop incoming === \r\n"); - } +// while(1) +// { +// if ((x != 0) && (x % 10 == 0)) +// { +// printf("Waking up GPS... Good luck, Huot \r\n"); +// gpsExecuteCommand(secondTimeAwake, sizeof(secondTimeAwake)); +// +// while (gpsRead() == 1); +// +// printf("GPS Fix is good! Going to sleep... \r\n"); +// gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); +// } +// printf("Waiting 5 seconds... \r\n"); +// wait(5); +// x++; +// printf("=== New loop incoming === \r\n"); +// } // Initialize LoRaWAN stack @@ -676,79 +702,114 @@ static void send_message() { printf("In send_message()...\r\n"); - uint16_t packet_len; int16_t retcode; - float sensor_value; - if (ds1820.begin()) { - ds1820.startConversion(); - sensor_value = ds1820.read(); - printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); - ds1820.startConversion(); - } else { - printf("\r\n No sensor found \r\n"); - return; - } +// HUOT NOTE -- 08/31/2018 -- Think this and all references to the "Dummy Sensor" can be removed at this point +// +// float sensor_value; +// if (ds1820.begin()) { +// ds1820.startConversion(); +// sensor_value = ds1820.read(); +// printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); +// ds1820.startConversion(); +// } else { +// printf("\r\n No sensor found \r\n"); +// return; +// } time_t tNow = time(NULL); printf("Clock: %d\r\n", tNow); -#if defined(SENSOR_TEMP) - tmpRead(); -#endif - magRead(); - accRead(); - gpsRead(); +// HUOT ADDITION -- 08/31/2018 -- Implementation of the packet + if (eepromCounter != 0) + { + for (int i = 0; i < eepromCounter; i++) + { + wasEEPROM = true; + prepBacklogSend(); + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN, + MSG_CONFIRMED_FLAG); + + printf("\r\n EEPROM Backlog Sent out!"); + printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + memset(tx_buffer, 0, sizeof(tx_buffer)); + } + } - int ilat = (int)(mylat * 100000); - int ilon = (int)(mylon * 100000); - printf("TIM: %d, SAT: %d, LAT: %d, LON: %d\r\n", mytime, mybatt, ilat, ilon); - packet_len = 11; - tx_buffer[0] = (mytime >> 24) & 0xFF; - tx_buffer[1] = (mytime >> 16) & 0xFF; - tx_buffer[2] = (mytime >> 8) & 0xFF; - tx_buffer[3] = (mytime >> 0) & 0xFF; - tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F); - tx_buffer[5] = (ilat >> 14) & 0xFF; - tx_buffer[6] = (ilat >> 6) & 0xFF; - tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03); - tx_buffer[8] = (ilon >> 16) & 0xFF; - tx_buffer[9] = (ilon >> 8) & 0xFF; - tx_buffer[10] = (ilon >> 0) & 0xFF; - printf("\r\nBUF: |"); - for (int i = 0; i < packet_len; i++) { printf("%02X", tx_buffer[i]); } - printf("|\r\n"); - retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, - MSG_CONFIRMED_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); - - if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { - //retry in 3 seconds - if (MBED_CONF_LORA_DUTY_CYCLE_ON) { - ev_queue.call_in(3000, send_message); + if (eepromCounter == 0 && continueSending) + { + #if defined(SENSOR_TEMP) + tmpRead(); + #endif + magRead(); + accRead(); + + // HUOT ALTERATION -- 08/31/2018 -- Implement GPS Read Loop into Send Message + // TODO - Implement 3 try counter (or something similar) + printf("Waking up GPS... Good luck, Huot \r\n"); + gpsExecuteCommand(secondTimeAwake, sizeof(secondTimeAwake)); + + while (gpsRead() == 1); + + printf("GPS Fix is good! Going to sleep... \r\n"); + gpsExecuteCommand(secondTimeSleep, sizeof(secondTimeSleep)); + printf("~~~~~~~~~~~~~ Constructing Packet ~~~~~~~~~~~~~\r\n"); + + int ilat = (int)(mylat * 100000); + int ilon = (int)(mylon * 100000); + printf("TIM: %d, SAT: %d, LAT: %d, LON: %d\r\n", mytime, mybatt, ilat, ilon); + // packet_len = 11; + tx_buffer[0] = (mytime >> 24) & 0xFF; + tx_buffer[1] = (mytime >> 16) & 0xFF; + tx_buffer[2] = (mytime >> 8) & 0xFF; + tx_buffer[3] = (mytime >> 0) & 0xFF; + tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F); + tx_buffer[5] = (ilat >> 14) & 0xFF; + tx_buffer[6] = (ilat >> 6) & 0xFF; + tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03); + tx_buffer[8] = (ilon >> 16) & 0xFF; + tx_buffer[9] = (ilon >> 8) & 0xFF; + tx_buffer[10] = (ilon >> 0) & 0xFF; + printf("\r\nBUF: |"); + for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); } + printf("|\r\n"); + retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, PACKET_LEN, + MSG_CONFIRMED_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); + + if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { + //retry in 3 seconds +// HUOT NOTE -- WE SHOULD BE CAREFUL ABOUT THIS AS WITH THE EEPROM BACKLOG WE MAY BLOCK OURSELVES AND OVERFLOW THE STACK WITH SUBSEQUENT CALLS + if (MBED_CONF_LORA_DUTY_CYCLE_ON) { + ev_queue.call_in(3000, send_message); + } } + return; } - return; - } - - printf("\r\n %d bytes scheduled for transmission \r\n", retcode); - memset(tx_buffer, 0, sizeof(tx_buffer)); - //LED Confirmation Output - MESSAGE SENT - for (int i = 0; i < 10; i++) { - myLedG = 1; - wait(0.1); - myLedG = 0; - myLedR = 1; - wait(0.1); - myLedR = 0; - myLedB = 1; - wait(0.1); - myLedB = 0; + printf("\r\n %d bytes scheduled for transmission \r\n", retcode); + memset(tx_buffer, 0, sizeof(tx_buffer)); + + //LED Confirmation Output - MESSAGE SENT + for (int i = 0; i < 10; i++) { + myLedG = 1; + wait(0.1); + myLedG = 0; + myLedR = 1; + wait(0.1); + myLedR = 0; + myLedB = 1; + wait(0.1); + myLedB = 0; + } + } + + // ENSURE'S A FULL SEND ATTEMPT ON NEXT DISPATCH RUN THROUGH + continueSending = true; } /** @@ -821,18 +882,32 @@ send_message(); } */ + + if (wasEEPROM) + { + eepromCounter--; + wasEEPROM = false; + } + break; case TX_TIMEOUT: case TX_ERROR: case TX_CRYPTO_ERROR: case TX_SCHEDULING_ERROR: printf("\r\n Transmission Error - EventCode = %d \r\n", event); + // try again /* if (MBED_CONF_LORA_DUTY_CYCLE_ON) { send_message(); } */ + printf("====== Writing to EEPROM Backlog ====== \r\n"); + backupPacket(); + + if (wasEEPROM) + continueSending = false; + break; case RX_DONE: printf("\r\n Received message from Network Server \r\n"); @@ -1578,4 +1653,27 @@ } } +// HUOT ADDITION -- 08/31/2018 -- Write Packet to EEPROM (on failed send) +void backupPacket() +{ + printf("====== IN BACKUP PACKET METHOD ======= \r\n"); + int location = eepromCounter * 11; + for (int i = 0; i < 11; i++) + { + writeEEPROMByte(location++, tx_buffer[i]); + } + eepromCounter++; +} + +// HUOT ADDITION -- 08/31/2018 -- Read EEPROM Memory into Buffer +void prepBacklogSend() +{ + printf("====== IN PREP BACKLOG SEND METHOD ======= \r\n"); + int location = (eepromCounter - 1) * 11; + for (int i = 0; i < 11; i++) + { + tx_buffer[i] = readEEPROMByte(location++); + } +} + // EOF