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:
- 42:d59e50622292
- Parent:
- 41:98266e454444
- Child:
- 43:c900cd8ea072
--- a/main.cpp Tue Sep 04 18:49:43 2018 +0000
+++ b/main.cpp Thu Sep 06 14:53:02 2018 +0000
@@ -21,6 +21,7 @@
//#define SENSOR_TEMP 1
#include "foo.h"
+int weGetSignal = 0;
#define DCHILL 600 // Cycle delay in chill mode.
#define DPANIC 60 // Cycle delay in panic mode.
@@ -359,9 +360,6 @@
int eepromCounter = 0;
int registerLocation = 0;
-
-
-
time_t tInit = 0;
time_t tBump = 0;
time_t tLast = 0;
@@ -385,6 +383,7 @@
int gpsRead();
void ubxRead();
void send_message();
+void send_backlog();
// HUOT ADDITION -- 08/31/2018
void backupPacket();
void prepBacklogSend();
@@ -442,7 +441,8 @@
}
// Clear any pending MAG IRQs
-inline void magWipeIrq() {
+inline void magWipeIrq()
+{
cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
i2c.write(LSM303_ADR_MAG, cmd, 1);
i2c.read(LSM303_ADR_MAG, &ret, 1);
@@ -677,27 +677,84 @@
*/
tSend = time(NULL) + 1;
while (1) {
+ continueSending = true;
while (time(NULL) < tSend)
wait(0.1);
if (tSend == 0)
tCalm = time(NULL) + (DPANIC * NPANIC);
tSend = time(NULL) + (time(NULL) < tCalm ? DPANIC : DCHILL);
magWipeIrq();
+ printf("RTC: %08X\r\n", time(NULL));
printf("IT'S PACKET TIME!\r\n");
+ // HUOT ADDITION -- 08/31/2018 -- Implementation of the packet
+ if (eepromCounter != 0)
+ {
+ int backsize = eepromCounter;
+ printf("BACKLOG LOOP, FLUSHING %d ENTRIES\r\n", backsize);
+ for (int i = 0; i < backsize && continueSending; i++)
+ {
+ printf("BACKLOG LOOP %d of %d\r\n", i, backsize);
+ wasEEPROM = true;
+ ev_queue.call(send_backlog);
+ ev_queue.dispatch();
+ printf("Backlog sent, waiting 5 secs.\r\n");
+ wait(5);
+ 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));
+ }
+ if (continueSending)
+ printf("BACKLOG FLUSH COMPLETE!\r\n");
+ else
+ printf("BACKLOG FLUSH ABORTED!\r\n");
+ }
ev_queue.call(send_message);
ev_queue.dispatch();
printf("\r\n- = - = - = - = - = - DISPATCH 2 COMPLETE - = - = - = - = - = -\r\n");
- //tSend = time(NULL) + 300;
- //tSend = time(NULL) + (time(NULL) < tCalm ? DPANIC : DCHILL);
- // Clear any pending IRQs
-// cmd[0] = LSM303_REG_MAG_INT_SOURCE_REG_M;
-// i2c.write(LSM303_ADR_MAG, cmd, 1);
-// i2c.read(LSM303_ADR_MAG, &ret, 1);
}
return 0;
}
+static void send_backlog()
+{
+ printf("In send_backlog()...\r\n");
+ int16_t retcode;
+ //time_t tNow = time(NULL);
+ //printf("Clock: %d\r\n", tNow);
+ weGetSignal = 0;
+ printf("Head Cleared ACK flag: %d\r\n", weGetSignal);
+ prepBacklogSend();
+ 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;
+ }
+ 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;
+ }
+}
+
/**
* Sends a message to the Network Server
*/
@@ -719,27 +776,12 @@
// return;
// }
- time_t tNow = time(NULL);
- printf("Clock: %d\r\n", tNow);
+ //time_t tNow = time(NULL);
+ //printf("Clock: %d\r\n", tNow);
-// 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));
- }
- }
+ weGetSignal = 0;
+ printf("Head Cleared ACK flag: %d\r\n", weGetSignal);
- if (eepromCounter == 0 && continueSending)
- {
#if defined(SENSOR_TEMP)
tmpRead();
#endif
@@ -793,7 +835,7 @@
}
printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
- memset(tx_buffer, 0, sizeof(tx_buffer));
+ //memset(tx_buffer, 0, sizeof(tx_buffer));
//LED Confirmation Output - MESSAGE SENT
for (int i = 0; i < 10; i++) {
@@ -807,11 +849,6 @@
wait(0.1);
myLedB = 0;
}
-
- }
-
- // ENSURE'S A FULL SEND ATTEMPT ON NEXT DISPATCH RUN THROUGH
- continueSending = true;
}
/**
@@ -878,19 +915,28 @@
break;
case TX_DONE:
printf("\r\n Message Sent to Network Server \r\n");
+ printf("Dumping ACK Flag: %d\r\n", weGetSignal);
+ if (weGetSignal == 0)
+ {
+ printf("No ACK received.\r\n");
+ if (wasEEPROM)
+ continueSending = false;
+ else
+ backupPacket();
+ }
+ if (wasEEPROM && weGetSignal)
+ {
+ eepromCounter--;
+ }
+ wasEEPROM = false;
+ weGetSignal = 0;
+ printf("Tail Cleared ACK Flag: %d\r\n", weGetSignal);
ev_queue.break_dispatch();
/*
if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
send_message();
}
*/
-
- if (wasEEPROM)
- {
- eepromCounter--;
- wasEEPROM = false;
- }
-
break;
case TX_TIMEOUT:
case TX_ERROR:
@@ -1488,8 +1534,8 @@
double fldLat, fldLon;
char fldN_S, fldE_W;
int fldFix, fldSat;
- sscanf(buf, "$GNGGA,%f,%lf,%c,%lf,%c,%d,%d,%*f,%f", &fldTim, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldFix, &fldSat, &fldAlt);
- printf("Sec: %.2f, Lat: %.5f %c, Lon: %.5f %c, Fix: %d, Sat: %d, Alt: %.1f M\r\n", fldTim, fldLat, fldN_S, fldLon, fldE_W, fldFix, fldSat, fldAlt);
+ if (sscanf(buf, "$GNGGA,%f,%lf,%c,%lf,%c,%d,%d,%*f,%f", &fldTim, &fldLat, &fldN_S, &fldLon, &fldE_W, &fldFix, &fldSat, &fldAlt) == 8)
+ printf("Sec: %.2f, Lat: %.5f %c, Lon: %.5f %c, Fix: %d, Sat: %d, Alt: %.1f M\r\n", fldTim, fldLat, fldN_S, fldLon, fldE_W, fldFix, fldSat, fldAlt);
if (clr == '2')
{
mylat = fldLat / (fldN_S == 'S' ? -100 : 100);
@@ -1660,6 +1706,10 @@
{
printf("====== IN BACKUP PACKET METHOD ======= \r\n");
int location = eepromCounter * 11;
+ printf("Writing entry #%d to 0x%08d\r\n", eepromCounter, location);
+ printf("BUF: |");
+ for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); }
+ printf("|\r\n");
for (int i = 0; i < 11; i++)
{
writeEEPROMByte(location++, tx_buffer[i]);
@@ -1672,10 +1722,14 @@
{
printf("====== IN PREP BACKLOG SEND METHOD ======= \r\n");
int location = (eepromCounter - 1) * 11;
+ printf("Reading entry #%d from 0x%08d\r\n", eepromCounter - 1, location);
for (int i = 0; i < 11; i++)
{
tx_buffer[i] = readEEPROMByte(location++);
}
+ printf("BUF: |");
+ for (int i = 0; i < PACKET_LEN; i++) { printf("%02X", tx_buffer[i]); }
+ printf("|\r\n");
}
// EOF
