Default to a frame counter of 0

This commit is contained in:
Starbeamrainbowlabs 2019-07-02 14:07:11 +01:00
parent 3251a1d4a2
commit 135c7694f8
5 changed files with 29 additions and 19 deletions

View file

@ -19,7 +19,7 @@ GPSLocation gps_fetch() {
// The struct that will hold the result // The struct that will hold the result
GPSLocation result; GPSLocation result;
Serial.print(F("[gps] Working: ")); Serial.print(F("[gps] Working "));
uint32_t start = millis(), ms_last_update = millis(); uint32_t start = millis(), ms_last_update = millis();
uint8_t ticks = 0; uint8_t ticks = 0;
unsigned long fix_age = TinyGPS::GPS_INVALID_AGE; unsigned long fix_age = TinyGPS::GPS_INVALID_AGE;
@ -33,7 +33,7 @@ GPSLocation gps_fetch() {
gps.stats(&chars, &sentences, &failed_checksum); gps.stats(&chars, &sentences, &failed_checksum);
if(millis() - start > 5000 && chars < 10) { if(millis() - start > 5000 && chars < 10) {
Serial.println(F("\n[error] GPS device init failed")); Serial.println(F("\n[error/gps] Init failed"));
while(true) { delay(1); } while(true) { delay(1); }
} }
@ -57,11 +57,6 @@ GPSLocation gps_fetch() {
Serial.println(); Serial.println();
ticks = 0; ticks = 0;
} }
// Serial.println(gps.location.lat());
// Serial.println(gps.location.lng());
// Serial.println(gps.date.year());
// Serial.println(gps.time.hour());
// Serial.println(gps.time.minute());
} }
ms_last_update = millis(); ms_last_update = millis();
continue; continue;

View file

@ -60,16 +60,17 @@ void setup() {
const uint8_t* bytes_lat = reinterpret_cast<uint8_t const *>(&(gps_data.lat)); const uint8_t* bytes_lat = reinterpret_cast<uint8_t const *>(&(gps_data.lat));
const uint8_t* bytes_lng = reinterpret_cast<uint8_t const *>(&(gps_data.lat)); const uint8_t* bytes_lng = reinterpret_cast<uint8_t const *>(&(gps_data.lat));
for(int i = 0; i < sizeof(uint32_t); i++) { // for(int i = 0; i < sizeof(uint32_t); i++) {
message[i] = bytes_id[i]; // }
} // The size of a float == the size of a uint32_t
for(int i = 0; i < sizeof(float); i++) { for(int i = 0; i < sizeof(float); i++) {
message[i] = bytes_id[i];
message[sizeof(uint32_t) + i] = bytes_lat[i]; message[sizeof(uint32_t) + i] = bytes_lat[i];
message[sizeof(uint32_t) + sizeof(float) + i] = bytes_lng[i]; message[sizeof(uint32_t) + sizeof(float) + i] = bytes_lng[i];
} }
#ifdef ENABLE_RADIO #ifdef ENABLE_RADIO
Serial.println(F("[main] Transmitting")); // Serial.println(F("[main] Transmitting"));
transmit_init(); transmit_init();
transmit_send(message, message_size); transmit_send(message, message_size);
#endif #endif

View file

@ -6,8 +6,6 @@ void power_off() {
Serial.println(F("[power] Shutting down")); Serial.println(F("[power] Shutting down"));
// Serial.println(F("[power] Switching GPS module to standby")); // Serial.println(F("[power] Switching GPS module to standby"));
// power_gps_standby(); // power_gps_standby();
Serial.println(F("[power] Signalling TPL5111"));
pinMode(PIN_TPL_DONE, OUTPUT); pinMode(PIN_TPL_DONE, OUTPUT);
digitalWrite(PIN_TPL_DONE, HIGH); digitalWrite(PIN_TPL_DONE, HIGH);

View file

@ -8,6 +8,13 @@
// Global static variable that's used to detect when LMIC has finished doing it's thing // Global static variable that's used to detect when LMIC has finished doing it's thing
bool is_sending_complete = false; bool is_sending_complete = false;
void transmit_counter_save() {
store_eeprom_uint32_save(0, LMIC.seqnoUp);
store_eeprom_uint32_save(sizeof(uint32_t), LMIC.seqnoDn);
}
void transmit_init() { void transmit_init() {
Serial.println(F("[LMIC] Init")); Serial.println(F("[LMIC] Init"));
// LMIC init // LMIC init
@ -62,7 +69,11 @@ void transmit_init() {
// Restore the frame counters // Restore the frame counters
LMIC.seqnoUp = store_eeprom_uint32_retrieve(0); LMIC.seqnoUp = store_eeprom_uint32_retrieve(0);
LMIC.seqnoDn = store_eeprom_uint32_retrieve(sizeof(uint32_t)); LMIC.seqnoDn = store_eeprom_uint32_retrieve(sizeof(uint32_t));
Serial.println(LMIC.seqnoUp); // Serial.println(LMIC.seqnoUp);
if(LMIC.seqnoUp >= 4294967295) { // The default value
LMIC.seqnoUp = LMIC.seqnoDn = 0; // Reset to 0
transmit_counter_save();
}
} }
/** /**
@ -78,7 +89,7 @@ bool transmit_send(uint8_t* data, int length) {
} }
// Prepare upstream data transmission at the next possible time. // Prepare upstream data transmission at the next possible time.
LMIC_setTxData2(1, data, length, 0); LMIC_setTxData2(1, data, length, 0);
Serial.println(F("Packet queued")); Serial.println(F("done"));
// Run the LMIC loop, but only until it's finished sending the packet // Run the LMIC loop, but only until it's finished sending the packet
while (!is_sending_complete) { while (!is_sending_complete) {
@ -86,8 +97,7 @@ bool transmit_send(uint8_t* data, int length) {
} }
// Persist the frame counters // Persist the frame counters
store_eeprom_uint32_save(0, LMIC.seqnoUp); transmit_counter_save();
store_eeprom_uint32_save(sizeof(uint32_t), LMIC.seqnoDn);
// Reset it for next time (just in case) // Reset it for next time (just in case)
is_sending_complete = false; is_sending_complete = false;
@ -127,7 +137,7 @@ void onEvent (ev_t ev) {
Serial.println(F("EV_REJOIN_FAILED")); Serial.println(F("EV_REJOIN_FAILED"));
break;*/ break;*/
case EV_TXCOMPLETE: case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (incl. RX window wait)")); Serial.println(F("EV_TXCOMPLETE incl. RX"));
if(LMIC.dataLen) { if(LMIC.dataLen) {
// data received in rx slot after tx // data received in rx slot after tx
Serial.print(F("Received: ")); Serial.print(F("Received: "));
@ -154,7 +164,8 @@ void onEvent (ev_t ev) {
Serial.println(F("EV_LINK_ALIVE")); Serial.println(F("EV_LINK_ALIVE"));
break;*/ break;*/
default: default:
Serial.println(F("Unknown event")); Serial.print(F("Unknown event "));
Serial.println(ev);
break; break;
} }
} }

View file

@ -1,5 +1,10 @@
#pragma once #pragma once
/**
* Internal use only. Persists the LMIC frame counters to disk.
*/
void transmit_counter_save();
/** /**
* Initialises the RFM95 LoRa radio. * Initialises the RFM95 LoRa radio.
*/ */