From 33ad3149d93ae3fa8883f8321710dbeb652c534e Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 14:06:34 +1300 Subject: [PATCH 01/57] Added structure for sd card positions backup implementation --- GpsTracker/Core.cpp | 4 +--- GpsTracker/Positions.cpp | 41 +++++++++++++++++++++------------- GpsTracker/Positions.h | 6 +++-- GpsTracker/PositionsBackup.h | 12 ++++++++++ GpsTracker/SdPositionsBackup.h | 13 +++++++++++ 5 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 GpsTracker/PositionsBackup.h create mode 100644 GpsTracker/SdPositionsBackup.h diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index d6461a3..20c7f71 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -16,9 +16,7 @@ namespace core { setSleepTime(); } - if (positions::needsToSend()) { - positions::send(); - } + positions::doBackup(); } void setSleepTime() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 38f6121..d720539 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -1,4 +1,5 @@ #include "Positions.h" +#include "SdPositionsBackup.h" #include "Debug.h" #include "Config.h" @@ -12,11 +13,23 @@ namespace positions { - uint16_t _maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; + backup::PositionsBackup **_backups; + size_t _backupLength; - uint16_t getEntryAddress(uint16_t index) { - if (index > _maxEntryIndex) return -1; - return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); + namespace details { + uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; + + uint16_t getEntryAddress(uint16_t index) { + if (index > maxEntryIndex) return -1; + return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index); + } + } + + void setup() { + //TODO : enable/disable based on config + _backupLength = 1; + _backups = new backup::PositionsBackup*[_backupLength]; + _backups[0] = new backup::SdPositionsbackup(); } bool acquire(PositionEntryMetadata &metadata) { @@ -60,11 +73,11 @@ namespace positions { Config config = config::get(); config.lastEntry++; - if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0; + if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; if (config.lastEntry == config.firstEntry) config.firstEntry++; - if (config.firstEntry > _maxEntryIndex) config.firstEntry = 0; + if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0; - entryAddress = getEntryAddress(config.lastEntry); + entryAddress = details::getEntryAddress(config.lastEntry); hardware::i2c::eeprom.writeBlock(entryAddress, entry); VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); @@ -74,7 +87,7 @@ namespace positions { } bool get(uint16_t index, PositionEntry &entry) { - uint16_t entryAddress = getEntryAddress(index); + uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress); @@ -90,17 +103,15 @@ namespace positions { bool moveNext(uint16_t &index) { if (index == config::get().lastEntry) return false; - if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way + if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; return true; } - bool needsToSend() { - return false; - } - - void send() { - + void doBackup() { + for (int i = 0; i < _backupLength; i++) { + _backups[i]->backup(); + } } } \ No newline at end of file diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index a2c914f..e91fb87 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -1,6 +1,7 @@ #pragma once #include +#include "PositionsBackup.h" #define POSITION_SIZE 115 @@ -18,12 +19,13 @@ struct PositionEntry { }; //sizeof = 125 namespace positions { + + void setup(); bool acquire(PositionEntryMetadata &metadata); void appendLast(const PositionEntryMetadata &metadata); bool get(uint16_t index, PositionEntry &entry); bool moveNext(uint16_t &index); - bool needsToSend(); - void send(); + void doBackup(); } \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h new file mode 100644 index 0000000..590459b --- /dev/null +++ b/GpsTracker/PositionsBackup.h @@ -0,0 +1,12 @@ +#pragma once + +namespace positions { + namespace backup { + + class PositionsBackup { + public: + virtual void backup(); + }; + + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h new file mode 100644 index 0000000..56f4bec --- /dev/null +++ b/GpsTracker/SdPositionsBackup.h @@ -0,0 +1,13 @@ +#pragma once + +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + + class SdPositionsbackup : public PositionsBackup { + public: + void backup(); + }; + } +} \ No newline at end of file From e1babd94b54dea9f778747f36a3cfb3f4cdd43d1 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 17:13:23 +1300 Subject: [PATCH 02/57] Reorganized config code to allow for main vs sd vs another config --- GpsTracker/Config.cpp | 78 ++++++++++++++++---------------- GpsTracker/Config.h | 23 +++++----- GpsTracker/Debug.cpp | 6 +-- GpsTracker/GpsTracker.h | 1 + GpsTracker/GpsTracker.ino | 3 +- GpsTracker/Hardware.cpp | 2 +- GpsTracker/Positions.cpp | 8 ++-- GpsTracker/PositionsBackup.h | 1 + GpsTracker/RawSdFile.cpp | 40 ++++++++++++++++ GpsTracker/RawSdFile.h | 32 +++++++++++++ GpsTracker/SdCard.cpp | 13 ++++++ GpsTracker/SdCard.h | 13 ++++++ GpsTracker/SdPositionsBackup.cpp | 23 ++++++++++ GpsTracker/SdPositionsBackup.h | 19 ++++++-- GpsTracker/SdPositionsConfig.cpp | 13 ++++++ GpsTracker/SdPositionsConfig.h | 23 ++++++++++ 16 files changed, 236 insertions(+), 62 deletions(-) create mode 100644 GpsTracker/RawSdFile.cpp create mode 100644 GpsTracker/RawSdFile.h create mode 100644 GpsTracker/SdCard.cpp create mode 100644 GpsTracker/SdCard.h create mode 100644 GpsTracker/SdPositionsBackup.cpp create mode 100644 GpsTracker/SdPositionsConfig.cpp create mode 100644 GpsTracker/SdPositionsConfig.h diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 02fc246..99811e2 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -5,53 +5,55 @@ #define LOGGER_NAME "Config" namespace config { - Config value; + namespace main { - namespace details { + Config value; - void read() { - VERBOSE("read"); - hardware::i2c::powerOn(); - hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (!String(CONFIG_SEED).equals(value.seed)) reset(); - hardware::i2c::powerOff(); + namespace details { - } + void read() { + VERBOSE("read"); + hardware::i2c::powerOn(); + hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); + if (!String(CONFIG_SEED).equals(value.seed)) reset(); + hardware::i2c::powerOff(); + + } - void write() { - VERBOSE_FORMAT("write", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + void write() { + VERBOSE_FORMAT("write", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); - hardware::i2c::powerOn(); - int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); - hardware::i2c::powerOff(); + hardware::i2c::powerOn(); + int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); + hardware::i2c::powerOff(); + } } - } - Config get() { - if (value.seed[0] == '\0') details::read(); + Config get() { + if (value.seed[0] == '\0') details::read(); - VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); - return value; - } + VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + return value; + } - void set(Config config) { - value = config; - details::write(); - } + void set(const Config config) { + value = config; + details::write(); + } - void reset() { - VERBOSE("reset"); - Config config = { - CONFIG_SEED, - VERSION, - "Vodafone", //TODO : read from SD - 0xFFFF, - 0xFFFF, - }; - - value = config; - details::write(); - } + void reset() { + VERBOSE("reset"); + Config config = { + CONFIG_SEED, + VERSION, + "Vodafone", //TODO : read from SD + 0xFFFF, + 0xFFFF, + }; + + value = config; + details::write(); + } - + } } \ No newline at end of file diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 5de2870..ef03867 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -28,18 +28,19 @@ namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { { 5, SLEEP_DEFAULT_TIME_SECONDS }, - { 10, 1200 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, + { 10, 1200 }, + { 20, 600 }, + { 30, 540 }, + { 50, 480 }, + { 80, 240 }, + { 100, 210 }, + { 180, 180 }, }; - Config get(); - void set(Config config); - - void reset(); + namespace main { + Config get(); + void set(const Config config); + void reset(); + } } \ No newline at end of file diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index f54508a..615294d 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -180,7 +180,7 @@ namespace debug { } void getAndDisplayEepromConfig() { - config::get(); + config::main::get(); } void getAndDisplayEepromContent() { @@ -200,7 +200,7 @@ namespace debug { } void getAndDisplayEepromPositions() { - uint16_t currentEntryIndex = config::get().firstEntry; + uint16_t currentEntryIndex = config::main::get().firstEntry; PositionEntry currentEntry; hardware::i2c::powerOn(); @@ -212,7 +212,7 @@ namespace debug { } void getAndDisplayEepromLastPosition() { - uint16_t lastEntryIndex = config::get().lastEntry; + uint16_t lastEntryIndex = config::main::get().lastEntry; PositionEntry lastEntry; positions::get(lastEntryIndex, lastEntry); diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index 8e90e49..c1c5eca 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -7,5 +7,6 @@ #include "Debug.h" #include "Config.h" #include "Core.h" +#include "SdCard.h" #define LOGGER_NAME "GpsTracker" diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 440ca89..75589bc 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -12,6 +12,7 @@ void setup() { #endif rtc::setup(); + hardware::sdcard::setup(); hardware::sim808::setup(); } @@ -55,7 +56,7 @@ void loop() { debug::getAndDisplayEepromConfig(); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG: - config::reset(); + config::main::reset(); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT: debug::getAndDisplayEepromContent(); diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 378ea82..1f36283 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -65,7 +65,7 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs(config::get().apn); + device.enableGprs(config::main::get().apn); } void networkPowerOff() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index d720539..606a4aa 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -29,7 +29,7 @@ namespace positions { //TODO : enable/disable based on config _backupLength = 1; _backups = new backup::PositionsBackup*[_backupLength]; - _backups[0] = new backup::SdPositionsbackup(); + _backups[0] = new backup::sd::SdPositionsBackup(); } bool acquire(PositionEntryMetadata &metadata) { @@ -70,7 +70,7 @@ namespace positions { strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); - Config config = config::get(); + Config config = config::main::get(); config.lastEntry++; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; @@ -82,7 +82,7 @@ namespace positions { VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); - config::set(config); + config::main::set(config); hardware::i2c::powerOff(); } @@ -101,7 +101,7 @@ namespace positions { } bool moveNext(uint16_t &index) { - if (index == config::get().lastEntry) return false; + if (index == config::main::get().lastEntry) return false; if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h index 590459b..f3ebaaf 100644 --- a/GpsTracker/PositionsBackup.h +++ b/GpsTracker/PositionsBackup.h @@ -5,6 +5,7 @@ namespace positions { class PositionsBackup { public: + virtual void setup(); virtual void backup(); }; diff --git a/GpsTracker/RawSdFile.cpp b/GpsTracker/RawSdFile.cpp new file mode 100644 index 0000000..099d111 --- /dev/null +++ b/GpsTracker/RawSdFile.cpp @@ -0,0 +1,40 @@ +#include "RawSdFile.h" + +namespace sd { + + RawSdFile::RawSdFile(SdFat *sd, const char *filename) { + _sd = sd; + _filename = filename; + } + + RawSdFile::~RawSdFile() { + flush(); + } + + void RawSdFile::open(const uint32_t pos) { + if (!_file.isOpen()) { + _sd->chdir(); + _file = _sd->open(_filename, O_RDWR | O_CREAT); + } + + _file.seek(pos); + } + + void RawSdFile::flush() { + if (!_file.isOpen()) return; + + _file.close(); + } + + void RawSdFile::write(const uint32_t pos, const void *val, const size_t size) { + open(pos); + _file.write(val, size); + } + + void RawSdFile::read(const uint32_t pos, void *val, const size_t size) { + open(pos); + if (!_file.available()) return; + + _file.read(val, size); + } +} \ No newline at end of file diff --git a/GpsTracker/RawSdFile.h b/GpsTracker/RawSdFile.h new file mode 100644 index 0000000..bb3bb32 --- /dev/null +++ b/GpsTracker/RawSdFile.h @@ -0,0 +1,32 @@ +#pragma once + +#include + +namespace sd { + class RawSdFile { + private: + SdFat * _sd; + const char *_filename; + File _file; + + void open(const uint32_t pos); + + public: + RawSdFile(SdFat *sd, const char *filename); + ~RawSdFile(); + + void flush(); + + void write(const uint32_t pos, const void *val, const size_t size); + template void write(const uint32_t pos, const T &val) + { + write(pos, (void*)&val, sizeof(val)); + } + + void read(const uint32_t pos, void *val, const size_t size); + template void read(const uint32_t pos, const T &val) + { + read(pos, (void*)&val, sizeof(val)); + } + }; +} diff --git a/GpsTracker/SdCard.cpp b/GpsTracker/SdCard.cpp new file mode 100644 index 0000000..33243ad --- /dev/null +++ b/GpsTracker/SdCard.cpp @@ -0,0 +1,13 @@ +#include "SdCard.h" + +namespace hardware { + namespace sdcard { + + SdFat filesystem; + + void setup() { + filesystem.begin(SD_SS); + } + + } +} \ No newline at end of file diff --git a/GpsTracker/SdCard.h b/GpsTracker/SdCard.h new file mode 100644 index 0000000..9596915 --- /dev/null +++ b/GpsTracker/SdCard.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include "Pins.h" + +namespace hardware { + namespace sdcard { + + extern SdFat filesystem; + + void setup(); + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp new file mode 100644 index 0000000..d470e12 --- /dev/null +++ b/GpsTracker/SdPositionsBackup.cpp @@ -0,0 +1,23 @@ +#include "SdPositionsBackup.h" +#include "SdPositionsConfig.h" +#include "SdCard.h" + +#include "Config.h" + +namespace positions { + namespace backup { + namespace sd { + + void SdPositionsBackup::setup() { + config::backup::sd::setup(); + _config = new RawSdFile(&hardware::sdcard::filesystem, POSITIONS_CONFIG_FILENAME); + } + + void SdPositionsBackup::backup() { + Config referenceConfig = config::main::get(); + + } + + } + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index 56f4bec..9e4d8f0 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -1,13 +1,24 @@ #pragma once #include "PositionsBackup.h" +#include "RawSdFile.h" + +#define POSITIONS_CONFIG_FILENAME "positions.config" + +using namespace sd; namespace positions { namespace backup { + namespace sd { + + class SdPositionsBackup : public PositionsBackup { + private: + RawSdFile * _config; + public: + void setup(); + void backup(); + }; - class SdPositionsbackup : public PositionsBackup { - public: - void backup(); - }; + } } } \ No newline at end of file diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp new file mode 100644 index 0000000..3ab0add --- /dev/null +++ b/GpsTracker/SdPositionsConfig.cpp @@ -0,0 +1,13 @@ +#include "SdPositionsConfig.h" + +namespace config { + namespace backup { + namespace sd { + + void setup() { + + } + + } + } +} \ No newline at end of file diff --git a/GpsTracker/SdPositionsConfig.h b/GpsTracker/SdPositionsConfig.h new file mode 100644 index 0000000..9922cd8 --- /dev/null +++ b/GpsTracker/SdPositionsConfig.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +struct SdPositionConfig_t { + uint16_t fileIndex; + uint32_t position; + size_t size; +}; + +namespace config { + namespace backup { + namespace sd { + + void setup(); + + SdPositionConfig_t get(); + void set(SdPositionConfig_t config); + void reset(); + + } + } +} \ No newline at end of file From 626bfa8a5c94f906d9a92f71c6558d93d0626ca5 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 18:53:13 +1300 Subject: [PATCH 03/57] Started real implementation of sdcard backup process --- GpsTracker/Config.cpp | 9 +++-- GpsTracker/Config.h | 6 ++-- GpsTracker/Positions.cpp | 9 ++++- GpsTracker/Positions.h | 1 + GpsTracker/SdPositionsBackup.cpp | 42 ++++++++++++++++++++-- GpsTracker/SdPositionsBackup.h | 3 -- GpsTracker/SdPositionsConfig.cpp | 62 +++++++++++++++++++++++++++++++- GpsTracker/SdPositionsConfig.h | 11 ++++-- 8 files changed, 125 insertions(+), 18 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 99811e2..b73ce02 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -7,7 +7,7 @@ namespace config { namespace main { - Config value; + Config_t value; namespace details { @@ -17,7 +17,6 @@ namespace config { hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (!String(CONFIG_SEED).equals(value.seed)) reset(); hardware::i2c::powerOff(); - } void write() { @@ -29,21 +28,21 @@ namespace config { } } - Config get() { + Config_t get() { if (value.seed[0] == '\0') details::read(); VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); return value; } - void set(const Config config) { + void set(const Config_t config) { value = config; details::write(); } void reset() { VERBOSE("reset"); - Config config = { + Config_t config = { CONFIG_SEED, VERSION, "Vodafone", //TODO : read from SD diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index ef03867..c9f8786 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -7,7 +7,7 @@ struct sleepTimings_t { uint16_t seconds; }; -struct Config { +struct Config_t { char seed[5]; char version[5]; char apn[20]; @@ -38,8 +38,8 @@ namespace config { }; namespace main { - Config get(); - void set(const Config config); + Config_t get(); + void set(const Config_t config); void reset(); } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 606a4aa..85b2030 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -70,7 +70,7 @@ namespace positions { strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); - Config config = config::main::get(); + Config_t config = config::main::get(); config.lastEntry++; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; @@ -109,6 +109,13 @@ namespace positions { return true; } + uint16_t count(uint16_t fromIndex) { + Config_t config = config::main::get(); + if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; } + + return config.lastEntry - fromIndex; + } + void doBackup() { for (int i = 0; i < _backupLength; i++) { _backups[i]->backup(); diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index e91fb87..5cd9c80 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -26,6 +26,7 @@ namespace positions { bool get(uint16_t index, PositionEntry &entry); bool moveNext(uint16_t &index); + uint16_t count(uint16_t fromIndex); void doBackup(); } \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp index d470e12..1ede67d 100644 --- a/GpsTracker/SdPositionsBackup.cpp +++ b/GpsTracker/SdPositionsBackup.cpp @@ -1,21 +1,57 @@ #include "SdPositionsBackup.h" #include "SdPositionsConfig.h" #include "SdCard.h" - +#include "Hardware.h" #include "Config.h" +#include "SdFat.h" +#include "Positions.h" namespace positions { namespace backup { namespace sd { + namespace details { + + bool isBackupNeeded(SdPositionConfig_t &sdConfig) { + Config_t referenceConfig = config::main::get(); + sdConfig = config::backup::sd::get(); + + return sdConfig.lastSavedEntry == 0xFFFF || + positions::count(sdConfig.lastSavedEntry) > sdConfig.saveThreshold; + } + + void appendPositions(const File &file, SdPositionConfig_t &sdConfig) { + uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1; + PositionEntry currentEntry; + + hardware::i2c::powerOn(); + do { + if (!positions::get(currentEntryIndex, currentEntry)) break; + + sdConfig.filePosition = file.position(); + } while (true); + hardware::i2c::powerOff(); + + } + } + void SdPositionsBackup::setup() { config::backup::sd::setup(); - _config = new RawSdFile(&hardware::sdcard::filesystem, POSITIONS_CONFIG_FILENAME); } void SdPositionsBackup::backup() { - Config referenceConfig = config::main::get(); + SdPositionConfig_t sdConfig; + + if (!details::isBackupNeeded(sdConfig)) return; + + char *filename; + hardware::sdcard::filesystem.chdir("positions"); + File file = hardware::sdcard::filesystem.open(filename, O_RDWR | O_CREAT); + + file.seek(sdConfig.filePosition); + file.find('\n'); + details::appendPositions(file, sdConfig); } } diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index 9e4d8f0..de5f5ac 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -3,8 +3,6 @@ #include "PositionsBackup.h" #include "RawSdFile.h" -#define POSITIONS_CONFIG_FILENAME "positions.config" - using namespace sd; namespace positions { @@ -13,7 +11,6 @@ namespace positions { class SdPositionsBackup : public PositionsBackup { private: - RawSdFile * _config; public: void setup(); void backup(); diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp index 3ab0add..de40a10 100644 --- a/GpsTracker/SdPositionsConfig.cpp +++ b/GpsTracker/SdPositionsConfig.cpp @@ -1,11 +1,71 @@ #include "SdPositionsConfig.h" +#include "SdCard.h" +#include "RawSdFile.h" +#include "Debug.h" + +using namespace sd; + +#define LOGGER_NAME "Config::backup::sd" namespace config { namespace backup { namespace sd { - void setup() { + SdPositionConfig_t value; + File configFile; + + namespace details { + + void ensureOpened() { + if (!configFile.isOpen()) { + hardware::sdcard::filesystem.chdir(); + configFile.open(POSITIONS_CONFIG_FILENAME, O_RDWR | O_CREAT); + } + + configFile.rewind(); + } + + void read() { + VERBOSE("read"); + ensureOpened(); + configFile.read((void*)&value, sizeof(value)); + if (value.seed != POSITIONS_CONFIG_SEED) reset(); + } + + void write() { + VERBOSE("write"); + ensureOpened(); + configFile.write((void*)&value, sizeof(value)); + } + + } + + void setup() { } + + SdPositionConfig_t get() { + if (value.seed != POSITIONS_CONFIG_SEED) details::read(); + + return value; + } + + void set(SdPositionConfig_t config) { + value = config; + details::write(); + } + + void reset() { + VERBOSE("reset"); + SdPositionConfig_t config = { + POSITIONS_CONFIG_SEED, + POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD, + 0xFFFF, + 0, + 0, + 0 + }; + value = config; + details::write(); } } diff --git a/GpsTracker/SdPositionsConfig.h b/GpsTracker/SdPositionsConfig.h index 9922cd8..04b78d2 100644 --- a/GpsTracker/SdPositionsConfig.h +++ b/GpsTracker/SdPositionsConfig.h @@ -2,10 +2,17 @@ #include +#define POSITIONS_CONFIG_FILENAME "positions.config" +#define POSITIONS_CONFIG_SEED 45 +#define POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD 10 + struct SdPositionConfig_t { + uint8_t seed; + uint8_t saveThreshold; + uint16_t lastSavedEntry; uint16_t fileIndex; - uint32_t position; - size_t size; + uint32_t filePosition; + size_t fileRecords; }; namespace config { From f712c3b45502f7a6190a37b8254322d69d09be43 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 20:07:28 +1300 Subject: [PATCH 04/57] Non tested implementaion of sd card positions backup --- GpsTracker/GpsTracker.ino | 2 + GpsTracker/Positions.cpp | 2 + GpsTracker/SdCard.cpp | 3 +- GpsTracker/SdCard.h | 1 + GpsTracker/SdPositionsBackup.cpp | 89 ++++++++++++++++++++++++++------ GpsTracker/SdPositionsBackup.h | 5 ++ GpsTracker/SdPositionsConfig.cpp | 5 +- GpsTracker/SdPositionsConfig.h | 4 +- 8 files changed, 90 insertions(+), 21 deletions(-) diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 75589bc..64c8a8e 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -14,6 +14,8 @@ void setup() { rtc::setup(); hardware::sdcard::setup(); hardware::sim808::setup(); + + positions::setup(); } void loop() { diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 85b2030..115ef22 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -29,7 +29,9 @@ namespace positions { //TODO : enable/disable based on config _backupLength = 1; _backups = new backup::PositionsBackup*[_backupLength]; + _backups[0] = new backup::sd::SdPositionsBackup(); + _backups[0]->setup(); } bool acquire(PositionEntryMetadata &metadata) { diff --git a/GpsTracker/SdCard.cpp b/GpsTracker/SdCard.cpp index 33243ad..d33185a 100644 --- a/GpsTracker/SdCard.cpp +++ b/GpsTracker/SdCard.cpp @@ -4,9 +4,10 @@ namespace hardware { namespace sdcard { SdFat filesystem; + bool available = false; void setup() { - filesystem.begin(SD_SS); + available = filesystem.begin(SD_SS); } } diff --git a/GpsTracker/SdCard.h b/GpsTracker/SdCard.h index 9596915..fde853a 100644 --- a/GpsTracker/SdCard.h +++ b/GpsTracker/SdCard.h @@ -7,6 +7,7 @@ namespace hardware { namespace sdcard { extern SdFat filesystem; + extern bool available; void setup(); } diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp index 1ede67d..c971a20 100644 --- a/GpsTracker/SdPositionsBackup.cpp +++ b/GpsTracker/SdPositionsBackup.cpp @@ -5,6 +5,9 @@ #include "Config.h" #include "SdFat.h" #include "Positions.h" +#include "Debug.h" + +#define LOGGER_NAME "Positions::backup::sd" namespace positions { namespace backup { @@ -20,38 +23,92 @@ namespace positions { positions::count(sdConfig.lastSavedEntry) > sdConfig.saveThreshold; } - void appendPositions(const File &file, SdPositionConfig_t &sdConfig) { + void getPositionsFileName(uint16_t fileIndex, char *buffer) { + sprintf(buffer, POSITIONS_FILENAME, fileIndex); + } + + void ensurePositionsFolder() { + char positionsFolder[] = POSITIONS_FOLDER; + + hardware::sdcard::filesystem.chdir(); + if (!hardware::sdcard::filesystem.exists(positionsFolder)) { + hardware::sdcard::filesystem.mkdir(positionsFolder, true); + } + + hardware::sdcard::filesystem.chdir(positionsFolder); + } + + void selectFile(SdPositionConfig_t &sdConfig, File &file) { + char positionFileName[POSITIONS_FILENAME_LENGTH]; + + if (sdConfig.fileRecords >= sdConfig.maxRecordsPerFile) { + if (file.isOpen()) { + file.close(); + config::backup::sd::set(sdConfig); + } + + sdConfig.fileIndex++; + sdConfig.filePosition = 0; + sdConfig.fileRecords = 0; + } + + if (!file.isOpen()) { + ensurePositionsFolder(); + getPositionsFileName(sdConfig.fileIndex, positionFileName); + file.open(positionFileName, O_RDWR | O_CREAT); + } + } + + void appendPosition(File &file, SdPositionConfig_t &sdConfig, PositionEntry &entry) { + VERBOSE("appendPosition"); + + const char fieldTerminator = ','; + + file.printField(entry.metadata.batteryLevel, fieldTerminator); + file.printField(entry.metadata.batteryVoltage, fieldTerminator); + file.printField(entry.metadata.temperature, fieldTerminator); + file.printField(entry.metadata.timeToFix, fieldTerminator); + file.printField(static_cast(entry.metadata.status), fieldTerminator); + file.println(entry.position); + + sdConfig.filePosition = file.position(); + } + + void appendPositions(SdPositionConfig_t &sdConfig) { + VERBOSE("appendPositions"); + uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1; PositionEntry currentEntry; + File file; hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; - - sdConfig.filePosition = file.position(); - } while (true); + selectFile(sdConfig, file); + appendPosition(file, sdConfig, currentEntry); + sdConfig.lastSavedEntry = currentEntryIndex; + } while (positions::moveNext(currentEntryIndex)); hardware::i2c::powerOff(); + if (file.isOpen()) file.close(); + config::backup::sd::set(sdConfig); } } - void SdPositionsBackup::setup() { - config::backup::sd::setup(); - } + void SdPositionsBackup::setup() { } void SdPositionsBackup::backup() { - SdPositionConfig_t sdConfig; - - if (!details::isBackupNeeded(sdConfig)) return; + VERBOSE("backup"); - char *filename; - hardware::sdcard::filesystem.chdir("positions"); - File file = hardware::sdcard::filesystem.open(filename, O_RDWR | O_CREAT); + if (!hardware::sdcard::available) { + VERBOSE_MSG("backup", "not available"); + return; + } - file.seek(sdConfig.filePosition); - file.find('\n'); + SdPositionConfig_t sdConfig; - details::appendPositions(file, sdConfig); + if (!details::isBackupNeeded(sdConfig)) return; + details::appendPositions(sdConfig); } } diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index de5f5ac..98ac21e 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -3,6 +3,11 @@ #include "PositionsBackup.h" #include "RawSdFile.h" +#define POSITIONS_FOLDER "positions" +#define POSITIONS_FILENAME "positions-%05d.csv" +#define POSITIONS_FILENAME_LENGTH 19 + + using namespace sd; namespace positions { diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp index de40a10..6d50885 100644 --- a/GpsTracker/SdPositionsConfig.cpp +++ b/GpsTracker/SdPositionsConfig.cpp @@ -40,8 +40,6 @@ namespace config { } - void setup() { } - SdPositionConfig_t get() { if (value.seed != POSITIONS_CONFIG_SEED) details::read(); @@ -57,7 +55,8 @@ namespace config { VERBOSE("reset"); SdPositionConfig_t config = { POSITIONS_CONFIG_SEED, - POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD, + POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, + POSITIONS_CONFIG_DEFAULT_MAX_RECORDS_PER_FILE, 0xFFFF, 0, 0, diff --git a/GpsTracker/SdPositionsConfig.h b/GpsTracker/SdPositionsConfig.h index 04b78d2..935448e 100644 --- a/GpsTracker/SdPositionsConfig.h +++ b/GpsTracker/SdPositionsConfig.h @@ -4,11 +4,13 @@ #define POSITIONS_CONFIG_FILENAME "positions.config" #define POSITIONS_CONFIG_SEED 45 -#define POSITIONS_CONFIG_DeFAULT_SAVE_THRESHOLD 10 +#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 +#define POSITIONS_CONFIG_DEFAULT_MAX_RECORDS_PER_FILE 5 struct SdPositionConfig_t { uint8_t seed; uint8_t saveThreshold; + uint8_t maxRecordsPerFile; uint16_t lastSavedEntry; uint16_t fileIndex; uint32_t filePosition; From 56fab86c216e0de5dd034e130ba01002aa827ebd Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 22:34:00 +1300 Subject: [PATCH 05/57] Remove/replace some Serial.print --- GpsTracker/Debug.cpp | 1 - GpsTracker/GpsTracker.ino | 2 +- GpsTracker/Positions.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 615294d..eeefe85 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -114,7 +114,6 @@ namespace debug { void waitForSerial() { while (!Serial); Serial.begin(DEBUG_SERIAL_SPEED); - Serial.println("Starting !"); } int freeRam() { diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 64c8a8e..2aa55b3 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -80,6 +80,6 @@ void loop() { break; case debug::GPSTRACKER_DEBUG_COMMAND::SD_WRITE_TEST: default: - Serial.println(F("Unsupported command !")); + NOTICE_MSG("loop", "Unsupported"); } } diff --git a/GpsTracker/Positions.h b/GpsTracker/Positions.h index 5cd9c80..df9b29d 100644 --- a/GpsTracker/Positions.h +++ b/GpsTracker/Positions.h @@ -1,7 +1,6 @@ #pragma once #include -#include "PositionsBackup.h" #define POSITION_SIZE 115 From 79678ab34a8769f61c34a58ccdc82dda30e36618 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 12 Mar 2018 23:50:51 +1300 Subject: [PATCH 06/57] Removed RawSdFile --- GpsTracker/RawSdFile.cpp | 40 -------------------------------- GpsTracker/RawSdFile.h | 32 ------------------------- GpsTracker/SdPositionsBackup.h | 3 --- GpsTracker/SdPositionsConfig.cpp | 3 --- 4 files changed, 78 deletions(-) delete mode 100644 GpsTracker/RawSdFile.cpp delete mode 100644 GpsTracker/RawSdFile.h diff --git a/GpsTracker/RawSdFile.cpp b/GpsTracker/RawSdFile.cpp deleted file mode 100644 index 099d111..0000000 --- a/GpsTracker/RawSdFile.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "RawSdFile.h" - -namespace sd { - - RawSdFile::RawSdFile(SdFat *sd, const char *filename) { - _sd = sd; - _filename = filename; - } - - RawSdFile::~RawSdFile() { - flush(); - } - - void RawSdFile::open(const uint32_t pos) { - if (!_file.isOpen()) { - _sd->chdir(); - _file = _sd->open(_filename, O_RDWR | O_CREAT); - } - - _file.seek(pos); - } - - void RawSdFile::flush() { - if (!_file.isOpen()) return; - - _file.close(); - } - - void RawSdFile::write(const uint32_t pos, const void *val, const size_t size) { - open(pos); - _file.write(val, size); - } - - void RawSdFile::read(const uint32_t pos, void *val, const size_t size) { - open(pos); - if (!_file.available()) return; - - _file.read(val, size); - } -} \ No newline at end of file diff --git a/GpsTracker/RawSdFile.h b/GpsTracker/RawSdFile.h deleted file mode 100644 index bb3bb32..0000000 --- a/GpsTracker/RawSdFile.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include - -namespace sd { - class RawSdFile { - private: - SdFat * _sd; - const char *_filename; - File _file; - - void open(const uint32_t pos); - - public: - RawSdFile(SdFat *sd, const char *filename); - ~RawSdFile(); - - void flush(); - - void write(const uint32_t pos, const void *val, const size_t size); - template void write(const uint32_t pos, const T &val) - { - write(pos, (void*)&val, sizeof(val)); - } - - void read(const uint32_t pos, void *val, const size_t size); - template void read(const uint32_t pos, const T &val) - { - read(pos, (void*)&val, sizeof(val)); - } - }; -} diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index 98ac21e..33cf0d3 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -1,15 +1,12 @@ #pragma once #include "PositionsBackup.h" -#include "RawSdFile.h" #define POSITIONS_FOLDER "positions" #define POSITIONS_FILENAME "positions-%05d.csv" #define POSITIONS_FILENAME_LENGTH 19 -using namespace sd; - namespace positions { namespace backup { namespace sd { diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp index 6d50885..f4fb33f 100644 --- a/GpsTracker/SdPositionsConfig.cpp +++ b/GpsTracker/SdPositionsConfig.cpp @@ -1,10 +1,7 @@ #include "SdPositionsConfig.h" #include "SdCard.h" -#include "RawSdFile.h" #include "Debug.h" -using namespace sd; - #define LOGGER_NAME "Config::backup::sd" namespace config { From fda3aaa2c7b21b7ff5fd36cf3b7710801cc9e802 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Tue, 13 Mar 2018 19:52:00 +1300 Subject: [PATCH 07/57] Conditionnaly include sdcard backup into code --- GpsTracker/Config.h | 2 ++ GpsTracker/GpsTracker.h | 2 -- GpsTracker/GpsTracker.ino | 2 -- GpsTracker/Positions.cpp | 31 ++++++++++++++++++++++--------- GpsTracker/SdCard.cpp | 6 +++++- GpsTracker/SdPositionsBackup.cpp | 9 ++++++--- GpsTracker/SdPositionsBackup.h | 5 ----- GpsTracker/SdPositionsConfig.cpp | 4 +++- GpsTracker/SdPositionsConfig.h | 3 +++ 9 files changed, 41 insertions(+), 23 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index c9f8786..f8c6ead 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -2,6 +2,8 @@ #include +//#define BACKUP_ENABLE_SDCARD 1 + struct sleepTimings_t { uint8_t speed; uint16_t seconds; diff --git a/GpsTracker/GpsTracker.h b/GpsTracker/GpsTracker.h index c1c5eca..6863ac7 100644 --- a/GpsTracker/GpsTracker.h +++ b/GpsTracker/GpsTracker.h @@ -1,12 +1,10 @@ #pragma once #include -#include #include #include "Debug.h" #include "Config.h" #include "Core.h" -#include "SdCard.h" #define LOGGER_NAME "GpsTracker" diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 2aa55b3..782c2ab 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -12,9 +12,7 @@ void setup() { #endif rtc::setup(); - hardware::sdcard::setup(); hardware::sim808::setup(); - positions::setup(); } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 115ef22..b7f9a80 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -1,8 +1,15 @@ +#include "Config.h" #include "Positions.h" + +#if defined(BACKUP_ENABLE_SDCARD) +#define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD +#endif + +#ifdef BACKUP_ENABLE_SDCARD #include "SdPositionsBackup.h" +#endif #include "Debug.h" -#include "Config.h" #include "Gps.h" #include "Network.h" @@ -12,9 +19,9 @@ #define ENTRIES_ADDR ENTRY_RESERVED_SIZE namespace positions { - +#ifdef BACKUPS_ENABLED backup::PositionsBackup **_backups; - size_t _backupLength; +#endif namespace details { uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; @@ -27,11 +34,15 @@ namespace positions { void setup() { //TODO : enable/disable based on config - _backupLength = 1; - _backups = new backup::PositionsBackup*[_backupLength]; - - _backups[0] = new backup::sd::SdPositionsBackup(); - _backups[0]->setup(); +#ifdef BACKUPS_ENABLED + uint8_t backupIdx = 0; + _backups = new backup::PositionsBackup*[BACKUPS_ENABLED]; + +#ifdef BACKUP_ENABLE_SDCARD + _backups[backupIdx] = new backup::sd::SdPositionsBackup(); + _backups[backupIdx++]->setup(); +#endif +#endif } bool acquire(PositionEntryMetadata &metadata) { @@ -119,8 +130,10 @@ namespace positions { } void doBackup() { - for (int i = 0; i < _backupLength; i++) { +#ifdef BACKUPS_ENABLED + for (int i = 0; i < BACKUPS_ENABLED; i++) { _backups[i]->backup(); } +#endif } } \ No newline at end of file diff --git a/GpsTracker/SdCard.cpp b/GpsTracker/SdCard.cpp index d33185a..406258a 100644 --- a/GpsTracker/SdCard.cpp +++ b/GpsTracker/SdCard.cpp @@ -1,3 +1,6 @@ +#include "Config.h" + +#ifdef BACKUP_ENABLE_SDCARD #include "SdCard.h" namespace hardware { @@ -11,4 +14,5 @@ namespace hardware { } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp index c971a20..baa3f2b 100644 --- a/GpsTracker/SdPositionsBackup.cpp +++ b/GpsTracker/SdPositionsBackup.cpp @@ -3,12 +3,12 @@ #include "SdCard.h" #include "Hardware.h" #include "Config.h" -#include "SdFat.h" #include "Positions.h" #include "Debug.h" #define LOGGER_NAME "Positions::backup::sd" +#ifdef BACKUP_ENABLE_SDCARD namespace positions { namespace backup { namespace sd { @@ -95,7 +95,9 @@ namespace positions { } } - void SdPositionsBackup::setup() { } + void SdPositionsBackup::setup() { + hardware::sdcard::setup(); + } void SdPositionsBackup::backup() { VERBOSE("backup"); @@ -113,4 +115,5 @@ namespace positions { } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/GpsTracker/SdPositionsBackup.h b/GpsTracker/SdPositionsBackup.h index 33cf0d3..f26aad9 100644 --- a/GpsTracker/SdPositionsBackup.h +++ b/GpsTracker/SdPositionsBackup.h @@ -2,11 +2,6 @@ #include "PositionsBackup.h" -#define POSITIONS_FOLDER "positions" -#define POSITIONS_FILENAME "positions-%05d.csv" -#define POSITIONS_FILENAME_LENGTH 19 - - namespace positions { namespace backup { namespace sd { diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp index f4fb33f..529db60 100644 --- a/GpsTracker/SdPositionsConfig.cpp +++ b/GpsTracker/SdPositionsConfig.cpp @@ -4,6 +4,7 @@ #define LOGGER_NAME "Config::backup::sd" +#ifdef BACKUP_ENABLE_SDCARD namespace config { namespace backup { namespace sd { @@ -66,4 +67,5 @@ namespace config { } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/GpsTracker/SdPositionsConfig.h b/GpsTracker/SdPositionsConfig.h index 935448e..f6f06fc 100644 --- a/GpsTracker/SdPositionsConfig.h +++ b/GpsTracker/SdPositionsConfig.h @@ -2,6 +2,9 @@ #include +#define POSITIONS_FOLDER "positions" +#define POSITIONS_FILENAME "positions-%05d.csv" +#define POSITIONS_FILENAME_LENGTH 19 #define POSITIONS_CONFIG_FILENAME "positions.config" #define POSITIONS_CONFIG_SEED 45 #define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 From 9143e494ebabd579b6bdcd544ea1b75af06dac51 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Tue, 13 Mar 2018 20:20:28 +1300 Subject: [PATCH 08/57] Conditionnal includes of backup code work for more than one implementation --- GpsTracker/Config.h | 24 +++++++++++++----------- GpsTracker/NetworkPositionsBackup.h | 3 +++ GpsTracker/Positions.cpp | 19 ++++++++++--------- GpsTracker/SdCard.cpp | 2 +- GpsTracker/SdPositionsBackup.cpp | 2 +- GpsTracker/SdPositionsConfig.cpp | 2 +- 6 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 GpsTracker/NetworkPositionsBackup.h diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index f8c6ead..7805c2c 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -2,7 +2,18 @@ #include -//#define BACKUP_ENABLE_SDCARD 1 +#define BACKUP_ENABLE_SDCARD 0 +#define BACKUP_ENABLE_NETWORK 0 + +#define CONFIG_ADDR 0 +#define CONFIG_RESERVED_SIZE 128 +#define CONFIG_SEED "UIYA" +#define VERSION "1.00" + +#define SLEEP_DEFAULT_TIME_SECONDS 1800 + +#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 +#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 struct sleepTimings_t { uint8_t speed; @@ -17,19 +28,10 @@ struct Config_t { uint16_t lastEntry; }; -#define CONFIG_ADDR 0 -#define CONFIG_SEED "UIYA" -#define VERSION "1.00" - -#define SLEEP_DEFAULT_TIME_SECONDS 1800 - -#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 -#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 - namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - { 5, SLEEP_DEFAULT_TIME_SECONDS }, + { 5, SLEEP_DEFAULT_TIME_SECONDS }, { 10, 1200 }, { 20, 600 }, { 30, 540 }, diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h new file mode 100644 index 0000000..8c75944 --- /dev/null +++ b/GpsTracker/NetworkPositionsBackup.h @@ -0,0 +1,3 @@ +#pragma once + +#include "PositionsBackup.h" \ No newline at end of file diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index b7f9a80..493b543 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -1,22 +1,24 @@ #include "Config.h" +#include "Debug.h" #include "Positions.h" +#include "Gps.h" -#if defined(BACKUP_ENABLE_SDCARD) -#define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD || BACKUP_ENABLE_NETWORK +#define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK #endif -#ifdef BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD #include "SdPositionsBackup.h" #endif -#include "Debug.h" -#include "Gps.h" -#include "Network.h" +#if BACKUP_ENABLE_NETWORK +#include "NetworkPositionsBackup.h" +#endif #define LOGGER_NAME "Positions" #define ENTRY_RESERVED_SIZE 128 -#define ENTRIES_ADDR ENTRY_RESERVED_SIZE +#define ENTRIES_ADDR CONFIG_RESERVED_SIZE namespace positions { #ifdef BACKUPS_ENABLED @@ -33,12 +35,11 @@ namespace positions { } void setup() { - //TODO : enable/disable based on config #ifdef BACKUPS_ENABLED uint8_t backupIdx = 0; _backups = new backup::PositionsBackup*[BACKUPS_ENABLED]; -#ifdef BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD _backups[backupIdx] = new backup::sd::SdPositionsBackup(); _backups[backupIdx++]->setup(); #endif diff --git a/GpsTracker/SdCard.cpp b/GpsTracker/SdCard.cpp index 406258a..9a37dd3 100644 --- a/GpsTracker/SdCard.cpp +++ b/GpsTracker/SdCard.cpp @@ -1,6 +1,6 @@ #include "Config.h" -#ifdef BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD #include "SdCard.h" namespace hardware { diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp index baa3f2b..426e583 100644 --- a/GpsTracker/SdPositionsBackup.cpp +++ b/GpsTracker/SdPositionsBackup.cpp @@ -8,7 +8,7 @@ #define LOGGER_NAME "Positions::backup::sd" -#ifdef BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD namespace positions { namespace backup { namespace sd { diff --git a/GpsTracker/SdPositionsConfig.cpp b/GpsTracker/SdPositionsConfig.cpp index 529db60..d963f36 100644 --- a/GpsTracker/SdPositionsConfig.cpp +++ b/GpsTracker/SdPositionsConfig.cpp @@ -4,7 +4,7 @@ #define LOGGER_NAME "Config::backup::sd" -#ifdef BACKUP_ENABLE_SDCARD +#if BACKUP_ENABLE_SDCARD namespace config { namespace backup { namespace sd { From f7679c8187d9205e87a69d1b33ab7df55ebe5046 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Tue, 13 Mar 2018 20:27:16 +1300 Subject: [PATCH 09/57] Transformed config seed into uint8_t and removed apn from config --- GpsTracker/Config.cpp | 9 ++++----- GpsTracker/Config.h | 7 +++---- GpsTracker/Hardware.cpp | 2 +- GpsTracker/NetworkPositionsConfig.h | 4 ++++ 4 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 GpsTracker/NetworkPositionsConfig.h diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index b73ce02..7ff59e4 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -15,12 +15,12 @@ namespace config { VERBOSE("read"); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (!String(CONFIG_SEED).equals(value.seed)) reset(); + if (CONFIG_SEED != value.seed) reset(); hardware::i2c::powerOff(); } void write() { - VERBOSE_FORMAT("write", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + VERBOSE_FORMAT("write", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); @@ -29,9 +29,9 @@ namespace config { } Config_t get() { - if (value.seed[0] == '\0') details::read(); + if (value.seed == 0) details::read(); - VERBOSE_FORMAT("get", "%s, %s, %s, %d, %d", value.seed, value.version, value.apn, value.firstEntry, value.lastEntry); + VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); return value; } @@ -45,7 +45,6 @@ namespace config { Config_t config = { CONFIG_SEED, VERSION, - "Vodafone", //TODO : read from SD 0xFFFF, 0xFFFF, }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 7805c2c..af74d10 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -3,11 +3,11 @@ #include #define BACKUP_ENABLE_SDCARD 0 -#define BACKUP_ENABLE_NETWORK 0 +#define BACKUP_ENABLE_NETWORK 1 #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 -#define CONFIG_SEED "UIYA" +#define CONFIG_SEED 13 #define VERSION "1.00" #define SLEEP_DEFAULT_TIME_SECONDS 1800 @@ -21,9 +21,8 @@ struct sleepTimings_t { }; struct Config_t { - char seed[5]; + uint8_t seed; char version[5]; - char apn[20]; uint16_t firstEntry; uint16_t lastEntry; }; diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 1f36283..316c69b 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -65,7 +65,7 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs(config::main::get().apn); + device.enableGprs("Vodafone"); //TODO : get from config } void networkPowerOff() { diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h new file mode 100644 index 0000000..760d147 --- /dev/null +++ b/GpsTracker/NetworkPositionsConfig.h @@ -0,0 +1,4 @@ +#pragma once + + +#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 \ No newline at end of file From 1ce692f9e412e5314e664cfde2218a7cebfcf7ab Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Tue, 13 Mar 2018 22:13:06 +1300 Subject: [PATCH 10/57] Blind implementation of network positions backup --- GpsTracker/Config.cpp | 18 ++++-- GpsTracker/Config.h | 13 ++++- GpsTracker/Hardware.cpp | 3 +- GpsTracker/NetworkPositionsBackup.cpp | 79 +++++++++++++++++++++++++++ GpsTracker/NetworkPositionsBackup.h | 17 +++++- GpsTracker/NetworkPositionsConfig.h | 11 +++- GpsTracker/Positions.cpp | 4 +- 7 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 GpsTracker/NetworkPositionsBackup.cpp diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 7ff59e4..c6c569d 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -7,7 +7,7 @@ namespace config { namespace main { - Config_t value; + config_t value; namespace details { @@ -15,7 +15,7 @@ namespace config { VERBOSE("read"); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (CONFIG_SEED != value.seed) reset(); + if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); } @@ -28,25 +28,33 @@ namespace config { } } - Config_t get() { + config_t get() { if (value.seed == 0) details::read(); VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); return value; } - void set(const Config_t config) { + void set(const config_t config) { value = config; details::write(); } void reset() { VERBOSE("reset"); - Config_t config = { + config_t config = { CONFIG_SEED, VERSION, 0xFFFF, 0xFFFF, +#if BACKUP_ENABLE_NETWORK + { + POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, + 0xFFFF, + POSITIONS_CONFIG_DEFAULT_APN, + POSITIONS_CONFIG_DEFAULT_URL, + }, +#endif }; value = config; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index af74d10..28c298a 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -5,6 +5,10 @@ #define BACKUP_ENABLE_SDCARD 0 #define BACKUP_ENABLE_NETWORK 1 +#if BACKUP_ENABLE_NETWORK +#include "NetworkPositionsConfig.h" +#endif + #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 @@ -20,11 +24,14 @@ struct sleepTimings_t { uint16_t seconds; }; -struct Config_t { +struct config_t { uint8_t seed; char version[5]; uint16_t firstEntry; uint16_t lastEntry; +#if BACKUP_ENABLE_NETWORK + networkConfig_t network; +#endif }; namespace config { @@ -41,8 +48,8 @@ namespace config { }; namespace main { - Config_t get(); - void set(const Config_t config); + config_t get(); + void set(const config_t config); void reset(); } diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 316c69b..d262bc3 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -1,3 +1,4 @@ +#include "Config.h" #include "Hardware.h" #include "Pins.h" #include "Debug.h" @@ -65,7 +66,7 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs("Vodafone"); //TODO : get from config + device.enableGprs(config::main::get().network.apn); } void networkPowerOff() { diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp new file mode 100644 index 0000000..f0ba000 --- /dev/null +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -0,0 +1,79 @@ +#pragma once + +#include "NetworkPositionsBackup.h" +#include "Debug.h" +#include "Positions.h" +#include "Config.h" +#include "Hardware.h" + +#define LOGGER_NAME "Positions::backup::network" +#define BUFFER_SIZE 160 +#define EXPECTED_HTTP_RESPONSE_CODE 201 + +namespace positions { + namespace backup { + namespace network { + + namespace details { + + bool isBackupNeeded(config_t &config) { + config = config::main::get(); + + return config.network.lastSavedEntry == 0xFFFF || + positions::count(config.network.lastSavedEntry) > config.network.saveThreshold; + } + + bool appendPosition(config_t &config, PositionEntry &entry) { + char buffer[BUFFER_SIZE]; + snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"), + entry.metadata.batteryLevel, + entry.metadata.batteryVoltage, + entry.metadata.temperature, + static_cast(entry.metadata.status), + entry.position); + + return hardware::sim808::device.httpPost( + config.network.url, + PSTR("text/csv"), + buffer, + buffer, + BUFFER_SIZE + ) == EXPECTED_HTTP_RESPONSE_CODE; + } + + void appendPositions(config_t &config) { + VERBOSE("appendPositions"); + + uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; + PositionEntry currentEntry; + + hardware::i2c::powerOn(); + hardware::sim808::networkPowerOn(); + do { + if (!positions::get(currentEntryIndex, currentEntry)) break; + if (!appendPosition(config, currentEntry)) break; + + config.network.lastSavedEntry = currentEntryIndex; + config::main::set(config); + + } while (positions::moveNext(currentEntryIndex)); + hardware::sim808::networkPowerOff(); + hardware::i2c::powerOff(); + } + + } + + void NetworkPositionsBackup::setup() { + VERBOSE("backup"); + } + + void NetworkPositionsBackup::backup() { + VERBOSE("backup"); + + config_t config; + if (!details::isBackupNeeded(config)) return; + details::appendPositions(config); + } + } + } +} diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h index 8c75944..0fe578f 100644 --- a/GpsTracker/NetworkPositionsBackup.h +++ b/GpsTracker/NetworkPositionsBackup.h @@ -1,3 +1,18 @@ #pragma once -#include "PositionsBackup.h" \ No newline at end of file +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + namespace network { + + class NetworkPositionsBackup : public PositionsBackup { + private: + public: + void setup(); + void backup(); + }; + + } + } +} \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 760d147..24a661c 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -1,4 +1,13 @@ #pragma once -#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 \ No newline at end of file +#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 +#define POSITIONS_CONFIG_DEFAULT_APN "Vodafone" +#define POSITIONS_CONFIG_DEFAULT_URL "http://default.url" + +struct networkConfig_t { + uint8_t saveThreshold; + uint16_t lastSavedEntry; + char apn[20]; + char url[50]; +}; \ No newline at end of file diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 493b543..412273c 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -84,7 +84,7 @@ namespace positions { strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); - Config_t config = config::main::get(); + config_t config = config::main::get(); config.lastEntry++; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; @@ -124,7 +124,7 @@ namespace positions { } uint16_t count(uint16_t fromIndex) { - Config_t config = config::main::get(); + config_t config = config::main::get(); if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; } return config.lastEntry - fromIndex; From 2c35fbe0f1b67449c8aeab16f9b3ce5c3eaa35b9 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 00:28:34 +1300 Subject: [PATCH 11/57] Implemented wait for network. Call backups directly to face what seems to be a segfault in positions::doBackup --- GpsTracker/Config.cpp | 17 +++++++++++------ GpsTracker/Config.h | 3 +++ GpsTracker/GpsTracker.ino | 1 + GpsTracker/Hardware.cpp | 1 - GpsTracker/Network.cpp | 26 ++++++++++++++++++++++++++ GpsTracker/Network.h | 4 ++++ GpsTracker/NetworkPositionsBackup.cpp | 25 +++++++++++++++++++------ GpsTracker/NetworkPositionsBackup.h | 2 +- GpsTracker/NetworkPositionsConfig.h | 8 +++++--- GpsTracker/Positions.cpp | 17 +++++++++++++++-- GpsTracker/PositionsBackup.cpp | 9 +++++++++ GpsTracker/PositionsBackup.h | 5 +++-- 12 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 GpsTracker/PositionsBackup.cpp diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index c6c569d..0c9e3d3 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -20,8 +20,10 @@ namespace config { } void write() { - VERBOSE_FORMAT("write", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); - + VERBOSE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); +#if BACKUP_ENABLE_NETWORK + VERBOSE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); hardware::i2c::powerOff(); @@ -31,7 +33,10 @@ namespace config { config_t get() { if (value.seed == 0) details::read(); - VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + VERBOSE_FORMAT("get", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); +#if BACKUP_ENABLE_NETWORK + VERBOSE_FORMAT("get", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif return value; } @@ -49,10 +54,10 @@ namespace config { 0xFFFF, #if BACKUP_ENABLE_NETWORK { - POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, + POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, 0xFFFF, - POSITIONS_CONFIG_DEFAULT_APN, - POSITIONS_CONFIG_DEFAULT_URL, + POSITIONS_CONFIG_NET_DEFAULT_APN, + POSITIONS_CONFIG_NET_DEFAULT_URL, }, #endif }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 28c298a..fee1d3e 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -19,6 +19,9 @@ #define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 #define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 2000 +#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 60000 + struct sleepTimings_t { uint8_t speed; uint16_t seconds; diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 782c2ab..27d7ac5 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -69,6 +69,7 @@ void loop() { break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY: debug::addLastPositionToEeprom(); + positions::doBackup(); break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: mainunit::sleep(period_t::SLEEP_8S); diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index d262bc3..352cb79 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -66,7 +66,6 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs(config::main::get().network.apn); } void networkPowerOff() { diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 0372b00..9794149 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,8 +1,34 @@ +#include "Config.h" #include "Network.h" #include "Hardware.h" +#include "MainUnit.h" + #define LOGGER_NAME "Network" namespace network { + SIM808RegistrationStatus waitForRegistered(uint16_t timeout) { + SIM808RegistrationStatus currentStatus; + + do { + currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); + if (isAvailable(currentStatus.stat)) break; + + mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); + timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; + } while (timeout > 1); + + return currentStatus; + } + + bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state) { + return state == SIM808_NETWORK_REGISTRATION_STATE::REGISTERED || + state == SIM808_NETWORK_REGISTRATION_STATE::ROAMING; + } + + bool enableGprs() { + return hardware::sim808::device.enableGprs(config::main::get().network.apn); + } + } \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index df89526..79ba1ee 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -6,4 +6,8 @@ namespace network { inline void powerOn() { hardware::sim808::networkPowerOn(); } inline void powerOff() { hardware::sim808::networkPowerOff(); } + + SIM808RegistrationStatus waitForRegistered(uint16_t timeout); + bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); + bool enableGprs(); } \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index f0ba000..5be479e 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -5,14 +5,14 @@ #include "Positions.h" #include "Config.h" #include "Hardware.h" +#include "Network.h" #define LOGGER_NAME "Positions::backup::network" #define BUFFER_SIZE 160 -#define EXPECTED_HTTP_RESPONSE_CODE 201 namespace positions { namespace backup { - namespace network { + namespace net { namespace details { @@ -38,7 +38,7 @@ namespace positions { buffer, buffer, BUFFER_SIZE - ) == EXPECTED_HTTP_RESPONSE_CODE; + ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } void appendPositions(config_t &config) { @@ -46,9 +46,22 @@ namespace positions { uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; PositionEntry currentEntry; + SIM808RegistrationStatus networkStatus; hardware::i2c::powerOn(); - hardware::sim808::networkPowerOn(); + network::powerOn(); + networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); + + if (!network::isAvailable(networkStatus.stat)) { + VERBOSE_MSG("appendPositions", "network unavailable"); + return; + } + + if (!network::enableGprs()) { + VERBOSE_MSG("appendPositions", "gprs unavailable"); + return; + } + do { if (!positions::get(currentEntryIndex, currentEntry)) break; if (!appendPosition(config, currentEntry)) break; @@ -57,14 +70,14 @@ namespace positions { config::main::set(config); } while (positions::moveNext(currentEntryIndex)); - hardware::sim808::networkPowerOff(); + network::powerOff(); hardware::i2c::powerOff(); } } void NetworkPositionsBackup::setup() { - VERBOSE("backup"); + VERBOSE("setup"); } void NetworkPositionsBackup::backup() { diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h index 0fe578f..da79400 100644 --- a/GpsTracker/NetworkPositionsBackup.h +++ b/GpsTracker/NetworkPositionsBackup.h @@ -4,7 +4,7 @@ namespace positions { namespace backup { - namespace network { + namespace net { class NetworkPositionsBackup : public PositionsBackup { private: diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 24a661c..36a010f 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -1,9 +1,11 @@ #pragma once -#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 -#define POSITIONS_CONFIG_DEFAULT_APN "Vodafone" -#define POSITIONS_CONFIG_DEFAULT_URL "http://default.url" +#define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 2 +#define POSITIONS_CONFIG_NET_DEFAULT_APN "Vodafone" +#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/16q71o61" +#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 201 + struct networkConfig_t { uint8_t saveThreshold; diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 412273c..0a0674b 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -43,6 +43,13 @@ namespace positions { _backups[backupIdx] = new backup::sd::SdPositionsBackup(); _backups[backupIdx++]->setup(); #endif +#if BACKUP_ENABLE_NETWORK + _backups[backupIdx] = new backup::net::NetworkPositionsBackup(); + _backups[backupIdx]->setup(); + //_backups[backupIdx]->backup(); + backupIdx++; + +#endif #endif } @@ -132,9 +139,15 @@ namespace positions { void doBackup() { #ifdef BACKUPS_ENABLED - for (int i = 0; i < BACKUPS_ENABLED; i++) { + VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); + + _backups[0]->backup(); + + /*for (int i = 0; i < BACKUPS_ENABLED; i++) { + VERBOSE_FORMAT("doBackup", "calling backup %d", i); + delay(1000); _backups[i]->backup(); - } + }*/ #endif } } \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.cpp b/GpsTracker/PositionsBackup.cpp new file mode 100644 index 0000000..ea17229 --- /dev/null +++ b/GpsTracker/PositionsBackup.cpp @@ -0,0 +1,9 @@ +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + + PositionsBackup::~PositionsBackup() {} + + } +} \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h index f3ebaaf..0cd6f77 100644 --- a/GpsTracker/PositionsBackup.h +++ b/GpsTracker/PositionsBackup.h @@ -5,8 +5,9 @@ namespace positions { class PositionsBackup { public: - virtual void setup(); - virtual void backup(); + ~PositionsBackup(); + virtual void setup()=0; + virtual void backup()=0; }; } From 0be4128850341add0746e900b7a4e6a340969aa4 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 11:00:48 +1300 Subject: [PATCH 12/57] Removed sim808 powerOn / off on startup to improve startup speed --- GpsTracker/Hardware.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 352cb79..660f55f 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -43,11 +43,8 @@ namespace hardware { void setup() { VERBOSE("setup"); - device.powerOnOff(true); simSerial.begin(4800); - device.begin(simSerial); - device.powerOnOff(false); } void gpsPowerOn() { From 5d9920d3c44707a9a87ceba1b3da1ef71ecc98ee Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 14:49:40 +1300 Subject: [PATCH 13/57] Added doBackup debug command --- GpsTracker/Debug.cpp | 4 +++- GpsTracker/Debug.h | 5 +++-- GpsTracker/Gps.cpp | 1 + GpsTracker/GpsTracker.ino | 2 ++ GpsTracker/Network.cpp | 2 ++ 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index cbbf7c3..6d69f53 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -28,6 +28,7 @@ MENU_ENTRY(EEPROM_GET_CONTENT, "[E] Get EEPROM content"); MENU_ENTRY(EEPROM_GET_ENTRIES, "[P] Get EEPROM entries"); MENU_ENTRY(EEPROM_GET_LAST_ENTRY, "[p] Get EEPROM last entry"); MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); +MENU_ENTRY(EEPROM_BACKUP_ENTRIES, "[B] Backup EEPROM entries"); MENU_ENTRY(SLEEP, "[S] Sleep for 8s"); MENU_ENTRY(SLEEP_DEEP, "[s] Deep sleep for 10s"); MENU_ENTRY(QUESTION, "?"); @@ -50,6 +51,7 @@ const PROGMEM uint8_t commandIdMapping[] = { 'P', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES), 'p', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY), 'a', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), + 'B', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES), 'S', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP), 's', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP), }; @@ -88,7 +90,7 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_EEPROM_GET_ENTRIES, MENU_EEPROM_GET_LAST_ENTRY, MENU_EEPROM_ADD_ENTRY, - + MENU_EEPROM_BACKUP_ENTRIES, MENU_SEPARATOR, MENU_SLEEP, diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 624d99f..e6dc10c 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -54,8 +54,9 @@ namespace debug { EEPROM_GET_LAST_ENTRY = 17, EEPROM_GET_ENTRIES = 18, EEPROM_ADD_ENTRY = 19, - SLEEP = 20, - SLEEP_DEEP = 21 + EEPROM_BACKUP_ENTRIES = 20, + SLEEP = 21, + SLEEP_DEEP = 22 }; void waitForSerial(); diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index cbe80c8..5a62010 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -32,6 +32,7 @@ namespace gps { currentStatus = hardware::sim808::device.getGpsStatus(); if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break; + VERBOSE_FORMAT("acquireCurrentPosition", "%d", currentStatus); mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 27d7ac5..b5c711d 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -69,6 +69,8 @@ void loop() { break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY: debug::addLastPositionToEeprom(); + break; + case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES: positions::doBackup(); break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 9794149..f9e5992 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,4 +1,5 @@ #include "Config.h" +#include "Debug.h" #include "Network.h" #include "Hardware.h" #include "MainUnit.h" @@ -14,6 +15,7 @@ namespace network { currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); if (isAvailable(currentStatus.stat)) break; + VERBOSE_FORMAT("waitForRegistered", "%d", currentStatus.stat); mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); From c7c7652d1fb47d28e7ffeca6a83629a3938e2810 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 14:53:35 +1300 Subject: [PATCH 14/57] Fixed main unit being woke up by SoftwareSerial interrupts --- GpsTracker/Config.h | 2 ++ GpsTracker/Hardware.cpp | 2 +- GpsTracker/Hardware.h | 2 ++ GpsTracker/MainUnit.cpp | 25 +++++++++++++++++++++---- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index fee1d3e..bd468a4 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -14,6 +14,8 @@ #define CONFIG_SEED 13 #define VERSION "1.00" +#define SIM808_BAUDRATE 4800 + #define SLEEP_DEFAULT_TIME_SECONDS 1800 #define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 660f55f..d62d15f 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -43,7 +43,7 @@ namespace hardware { void setup() { VERBOSE("setup"); - simSerial.begin(4800); + simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); } diff --git a/GpsTracker/Hardware.h b/GpsTracker/Hardware.h index df67034..4e1353a 100644 --- a/GpsTracker/Hardware.h +++ b/GpsTracker/Hardware.h @@ -1,11 +1,13 @@ #pragma once +#include #include #include namespace hardware { namespace sim808 { + extern SoftwareSerial simSerial; extern SIM808 device; void powerOn(); diff --git a/GpsTracker/MainUnit.cpp b/GpsTracker/MainUnit.cpp index eebcc74..b665c04 100644 --- a/GpsTracker/MainUnit.cpp +++ b/GpsTracker/MainUnit.cpp @@ -7,6 +7,23 @@ namespace mainunit { + namespace details { + + void prepareSleep() { + hardware::sim808::simSerial.end(); //avoid woke up by SoftwareSerial interrupt + delay(5); //ensure message have been printed out + } + + void wokeUp() { + tmElements_t wokeUpTime; + rtc::getTime(wokeUpTime); + NOTICE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.Hour, wokeUpTime.Minute, wokeUpTime.Second); + + hardware::sim808::simSerial.listen(); + } + + } + void interrupt() { detachInterrupt(digitalPinToInterrupt(RTC_WAKE)); } @@ -20,17 +37,17 @@ namespace mainunit { void sleep(period_t period) { NOTICE_FORMAT("sleep", "Sleeping for period : %d", period); - delay(5); + details::prepareSleep(); LowPower.powerDown(period, ADC_OFF, BOD_OFF); - NOTICE_MSG("sleep", "Woke up"); + details::wokeUp(); } void deepSleep(uint16_t seconds) { NOTICE_FORMAT("deepSleep", "Deep sleeping for %d seconds", seconds); interruptIn(seconds); - delay(5); + details::prepareSleep(); LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); - NOTICE_MSG("deepSleep", "Woke up"); + details::wokeUp(); } } \ No newline at end of file From 7a9987c78a52e5bfa1a3aa84b04d81dbf0d93d35 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 14:55:19 +1300 Subject: [PATCH 15/57] Improved gprs activation in NetworkPositionsBackup --- GpsTracker/Config.h | 4 ++-- GpsTracker/NetworkPositionsBackup.cpp | 29 +++++++++++---------------- GpsTracker/Positions.cpp | 7 +++---- GpsTracker/Rtc.cpp | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index bd468a4..ebecca3 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -21,8 +21,8 @@ #define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 #define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 -#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 2000 -#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 60000 +#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 +#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 1800000 struct sleepTimings_t { uint8_t speed; diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 5be479e..10b9636 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -48,30 +48,25 @@ namespace positions { PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; - hardware::i2c::powerOn(); network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat)) { - VERBOSE_MSG("appendPositions", "network unavailable"); - return; - } - - if (!network::enableGprs()) { - VERBOSE_MSG("appendPositions", "gprs unavailable"); - return; - } + if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); + else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); + else { + /*hardware::i2c::powerOn(); + do { + if (!positions::get(currentEntryIndex, currentEntry)) break; + if (!appendPosition(config, currentEntry)) break; - do { - if (!positions::get(currentEntryIndex, currentEntry)) break; - if (!appendPosition(config, currentEntry)) break; + config.network.lastSavedEntry = currentEntryIndex; + config::main::set(config); - config.network.lastSavedEntry = currentEntryIndex; - config::main::set(config); + } while (positions::moveNext(currentEntryIndex)); + hardware::i2c::powerOff(); */ + } - } while (positions::moveNext(currentEntryIndex)); network::powerOff(); - hardware::i2c::powerOff(); } } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 0a0674b..4f18d01 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -41,14 +41,13 @@ namespace positions { #if BACKUP_ENABLE_SDCARD _backups[backupIdx] = new backup::sd::SdPositionsBackup(); - _backups[backupIdx++]->setup(); + _backups[backupIdx]->setup(); + backupIdx++; #endif #if BACKUP_ENABLE_NETWORK _backups[backupIdx] = new backup::net::NetworkPositionsBackup(); _backups[backupIdx]->setup(); - //_backups[backupIdx]->backup(); backupIdx++; - #endif #endif } @@ -140,7 +139,7 @@ namespace positions { void doBackup() { #ifdef BACKUPS_ENABLED VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); - + Serial.println((unsigned int)_backups[0], HEX); _backups[0]->backup(); /*for (int i = 0; i < BACKUPS_ENABLED; i++) { diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 308ff4c..b971178 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -17,7 +17,7 @@ namespace rtc { hardware::i2c::powerOn(); RTC.control(DS3231_12H, DS3231_OFF); //24 hours clock RTC.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF - RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN OFF + RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON hardware::i2c::powerOff(); //TODO : check wether the osc has been halted (meaning the battery could be dead) From 00f5660f52b7888c7c5c55c6c47a74c44dcdd1bb Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 14:55:46 +1300 Subject: [PATCH 16/57] Fixed Gprs being disabled after CFUN has been set to the minimum --- GpsTracker/Hardware.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index d62d15f..230c974 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -45,6 +45,7 @@ namespace hardware { VERBOSE("setup"); simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); + powerOff(); //ensure powerOff on start } void gpsPowerOn() { @@ -67,8 +68,8 @@ namespace hardware { void networkPowerOff() { VERBOSE("networkPowerOff"); - device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); device.disableGprs(); + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); powerOffIfUnused(); } From dfae1a9a343605b58b3f80a7b6f54f3d5aa000a7 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 16:56:55 +1300 Subject: [PATCH 17/57] Fixed timeout for gsm and gps overflow uint16 --- GpsTracker/Config.h | 2 +- GpsTracker/Gps.cpp | 2 +- GpsTracker/Gps.h | 2 +- GpsTracker/Network.cpp | 2 +- GpsTracker/Network.h | 2 +- GpsTracker/NetworkPositionsBackup.cpp | 10 ++++++---- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index ebecca3..4ca382c 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -22,7 +22,7 @@ #define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 #define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 -#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 1800000 +#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 180000 struct sleepTimings_t { uint8_t speed; diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index 5a62010..e5840f1 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -25,7 +25,7 @@ namespace gps { char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; - SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout) { + SIM808_GPS_STATUS acquireCurrentPosition(uint32_t timeout) { SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; do { diff --git a/GpsTracker/Gps.h b/GpsTracker/Gps.h index f5be055..f698af8 100644 --- a/GpsTracker/Gps.h +++ b/GpsTracker/Gps.h @@ -15,7 +15,7 @@ namespace gps { inline void powerOn() { hardware::sim808::gpsPowerOn(); } inline void powerOff() { hardware::sim808::gpsPowerOff(); } - SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout); + SIM808_GPS_STATUS acquireCurrentPosition(uint32_t timeout); uint8_t getVelocity(); void getTime(tmElements_t &time); diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index f9e5992..b77f4e9 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -8,7 +8,7 @@ namespace network { - SIM808RegistrationStatus waitForRegistered(uint16_t timeout) { + SIM808RegistrationStatus waitForRegistered(uint32_t timeout) { SIM808RegistrationStatus currentStatus; do { diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index 79ba1ee..33ee41e 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -7,7 +7,7 @@ namespace network { inline void powerOn() { hardware::sim808::networkPowerOn(); } inline void powerOff() { hardware::sim808::networkPowerOff(); } - SIM808RegistrationStatus waitForRegistered(uint16_t timeout); + SIM808RegistrationStatus waitForRegistered(uint32_t timeout); bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); bool enableGprs(); } \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 10b9636..3a0e0ab 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -24,7 +24,9 @@ namespace positions { } bool appendPosition(config_t &config, PositionEntry &entry) { - char buffer[BUFFER_SIZE]; + debug::displayFreeRam(); + return false; + /*char buffer[BUFFER_SIZE]; snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, @@ -38,7 +40,7 @@ namespace positions { buffer, buffer, BUFFER_SIZE - ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; + ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;*/ } void appendPositions(config_t &config) { @@ -54,7 +56,7 @@ namespace positions { if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); else { - /*hardware::i2c::powerOn(); + hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; if (!appendPosition(config, currentEntry)) break; @@ -63,7 +65,7 @@ namespace positions { config::main::set(config); } while (positions::moveNext(currentEntryIndex)); - hardware::i2c::powerOff(); */ + hardware::i2c::powerOff(); } network::powerOff(); From 7159d8300972165fadfee575637dfb879617fc26 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 16:57:44 +1300 Subject: [PATCH 18/57] Fixed debug print from Positions::get --- GpsTracker/Positions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 4f18d01..0c4c109 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -116,7 +116,7 @@ namespace positions { hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); return true; } From ba9531f2dd91e1d514b349d2bbaa2ea93418b4ba Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 16:58:28 +1300 Subject: [PATCH 19/57] Display signal quality while searching for network --- GpsTracker/Network.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index b77f4e9..d01acda 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -15,7 +15,8 @@ namespace network { currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); if (isAvailable(currentStatus.stat)) break; - VERBOSE_FORMAT("waitForRegistered", "%d", currentStatus.stat); + SIM808SignalQualityReport report = hardware::sim808::device.getSignalQuality(); + VERBOSE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); From 4864d8d9c1ee40cb4357c9622a5ec8a1d9b7b7a1 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 19:15:00 +1300 Subject: [PATCH 20/57] Messed up state where I try to gain some RAM --- GpsTracker/Config.cpp | 20 ++++----- GpsTracker/Config.h | 8 ++-- GpsTracker/Debug.cpp | 10 +++-- GpsTracker/Debug.h | 2 +- GpsTracker/GpsTracker.ino | 2 + GpsTracker/Network.cpp | 2 +- GpsTracker/NetworkPositionsBackup.cpp | 62 ++++++++++++++++----------- GpsTracker/Positions.cpp | 31 ++++++++------ GpsTracker/SdPositionsBackup.cpp | 2 +- 9 files changed, 80 insertions(+), 59 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 0c9e3d3..4c5c21a 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -17,6 +17,11 @@ namespace config { hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); + + VERBOSE_FORMAT("read", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); +#if BACKUP_ENABLE_NETWORK + VERBOSE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif } void write() { @@ -30,18 +35,11 @@ namespace config { } } - config_t get() { - if (value.seed == 0) details::read(); - - VERBOSE_FORMAT("get", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); -#if BACKUP_ENABLE_NETWORK - VERBOSE_FORMAT("get", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); -#endif - return value; + void setup() { + details::read(); } - void set(const config_t config) { - value = config; + void save() { details::write(); } @@ -63,7 +61,7 @@ namespace config { }; value = config; - details::write(); + save(); } } diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 4ca382c..259ad82 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -52,9 +52,11 @@ namespace config { { 180, 180 }, }; - namespace main { - config_t get(); - void set(const config_t config); + namespace main { + extern config_t value; + + void setup(); + void save(); void reset(); } diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 6d69f53..129cbd0 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -124,6 +124,10 @@ namespace debug { return freeRam2(); } + void displayFreeRam() { + Log.notice(F("RAM: %d\n"), freeRam()); + } + GPSTRACKER_DEBUG_COMMAND parseCommand(char id) { size_t mappingArraySize = flash::getArraySize(commandIdMapping); char commandId; @@ -183,7 +187,7 @@ namespace debug { } void getAndDisplayEepromConfig() { - config::main::get(); + config::main::setup(); //forcing read again } void getAndDisplayEepromContent() { @@ -203,7 +207,7 @@ namespace debug { } void getAndDisplayEepromPositions() { - uint16_t currentEntryIndex = config::main::get().firstEntry; + uint16_t currentEntryIndex = config::main::value.firstEntry; PositionEntry currentEntry; hardware::i2c::powerOn(); @@ -215,7 +219,7 @@ namespace debug { } void getAndDisplayEepromLastPosition() { - uint16_t lastEntryIndex = config::main::get().lastEntry; + uint16_t lastEntryIndex = config::main::value.lastEntry; PositionEntry lastEntry; positions::get(lastEntryIndex, lastEntry); diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index e6dc10c..dd06cb2 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -61,6 +61,7 @@ namespace debug { void waitForSerial(); int freeRam(); + void displayFreeRam(); GPSTRACKER_DEBUG_COMMAND menu(); @@ -77,6 +78,5 @@ namespace debug { void getAndDisplayEepromLastPosition(); void addLastPositionToEeprom(); - inline void displayFreeRam() { Serial.println(freeRam()); } } diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index b5c711d..d823820 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -11,6 +11,8 @@ void setup() { if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); #endif + if (Serial) Serial.println(F("=============================")); + config::main::setup(); rtc::setup(); hardware::sim808::setup(); positions::setup(); diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index d01acda..0a74b91 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -30,7 +30,7 @@ namespace network { } bool enableGprs() { - return hardware::sim808::device.enableGprs(config::main::get().network.apn); + return hardware::sim808::device.enableGprs(config::main::value.network.apn); } diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 3a0e0ab..11b7819 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -8,7 +8,7 @@ #include "Network.h" #define LOGGER_NAME "Positions::backup::network" -#define BUFFER_SIZE 160 +#define BUFFER_SIZE 170 namespace positions { namespace backup { @@ -16,25 +16,30 @@ namespace positions { namespace details { - bool isBackupNeeded(config_t &config) { - config = config::main::get(); - - return config.network.lastSavedEntry == 0xFFFF || - positions::count(config.network.lastSavedEntry) > config.network.saveThreshold; + bool isBackupNeeded() { + config_t *config = &config::main::value; + return config->network.lastSavedEntry == 0xFFFF || + positions::count(config->network.lastSavedEntry) > config->network.saveThreshold; } - bool appendPosition(config_t &config, PositionEntry &entry) { + bool appendPosition(PositionEntry &entry) { + VERBOSE("appendPosition"); debug::displayFreeRam(); - return false; - /*char buffer[BUFFER_SIZE]; - snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"), + delay(100); + char buffer[BUFFER_SIZE]; + sprintf(buffer, "%d", 2100); + /*snprintf(buffer, BUFFER_SIZE, "%d,%d,%.2f,%d,", entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, - static_cast(entry.metadata.status), - entry.position); + static_cast(entry.metadata.status));*/ + + ////strcat(buffer, entry.position); + //Serial.println(buffer); + debug::displayFreeRam(); + return false; - return hardware::sim808::device.httpPost( + /*return hardware::sim808::device.httpPost( config.network.url, PSTR("text/csv"), buffer, @@ -43,32 +48,37 @@ namespace positions { ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;*/ } - void appendPositions(config_t &config) { + void appendPositions() { VERBOSE("appendPositions"); + debug::displayFreeRam(); - uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; + uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; - network::powerOn(); + /*network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); - else { + else {*/ hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; - if (!appendPosition(config, currentEntry)) break; + debug::displayFreeRam(); - config.network.lastSavedEntry = currentEntryIndex; - config::main::set(config); + //if (!appendPosition(currentEntry)) break; + /*config::main::value.network.lastSavedEntry = currentEntryIndex; + config::main::save(); +*/ } while (positions::moveNext(currentEntryIndex)); + debug::displayFreeRam(); hardware::i2c::powerOff(); - } + //} - network::powerOff(); + //network::powerOff(); + debug::displayFreeRam(); } } @@ -79,10 +89,12 @@ namespace positions { void NetworkPositionsBackup::backup() { VERBOSE("backup"); + debug::displayFreeRam(); + + if (!details::isBackupNeeded()) return; + debug::displayFreeRam(); - config_t config; - if (!details::isBackupNeeded(config)) return; - details::appendPositions(config); + details::appendPositions(); } } } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 0c4c109..224cb12 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -85,24 +85,27 @@ namespace positions { void appendLast(const PositionEntryMetadata &metadata) { VERBOSE("appendLast"); + uint16_t entryIndex; uint16_t entryAddress; PositionEntry entry = { metadata }; strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); - hardware::i2c::powerOn(); - config_t config = config::main::get(); + config_t* config = &config::main::value; + entryIndex = config->lastEntry + 1; - config.lastEntry++; - if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; - if (config.lastEntry == config.firstEntry) config.firstEntry++; - if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0; + entryAddress = details::getEntryAddress(entryIndex); - entryAddress = details::getEntryAddress(config.lastEntry); + hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); - config::main::set(config); + config->lastEntry++; + if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; + if (config->lastEntry == config->firstEntry) config->firstEntry++; + if (config->firstEntry > details::maxEntryIndex) config->firstEntry = 0; + + config::main::save(); hardware::i2c::powerOff(); } @@ -121,7 +124,7 @@ namespace positions { } bool moveNext(uint16_t &index) { - if (index == config::main::get().lastEntry) return false; + if (index == config::main::value.lastEntry) return false; if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; @@ -130,18 +133,18 @@ namespace positions { } uint16_t count(uint16_t fromIndex) { - config_t config = config::main::get(); - if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; } + config_t *config = &config::main::value; + if (config->lastEntry < config->firstEntry) { config->lastEntry += details::maxEntryIndex; } - return config.lastEntry - fromIndex; + return config->lastEntry - fromIndex; } void doBackup() { #ifdef BACKUPS_ENABLED + debug::displayFreeRam(); VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); - Serial.println((unsigned int)_backups[0], HEX); _backups[0]->backup(); - + debug::displayFreeRam(); /*for (int i = 0; i < BACKUPS_ENABLED; i++) { VERBOSE_FORMAT("doBackup", "calling backup %d", i); delay(1000); diff --git a/GpsTracker/SdPositionsBackup.cpp b/GpsTracker/SdPositionsBackup.cpp index 426e583..4bfcb50 100644 --- a/GpsTracker/SdPositionsBackup.cpp +++ b/GpsTracker/SdPositionsBackup.cpp @@ -16,7 +16,7 @@ namespace positions { namespace details { bool isBackupNeeded(SdPositionConfig_t &sdConfig) { - Config_t referenceConfig = config::main::get(); + Config_t referenceConfig = config::main::value; sdConfig = config::backup::sd::get(); return sdConfig.lastSavedEntry == 0xFFFF || From 5bbf96694ec3ede73d96e62b7fee0bb368dd86fe Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 20:17:22 +1300 Subject: [PATCH 21/57] For now expected code is 200 as most of the online request bin like respond with that code --- GpsTracker/NetworkPositionsConfig.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 36a010f..7b59169 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -4,7 +4,7 @@ #define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 2 #define POSITIONS_CONFIG_NET_DEFAULT_APN "Vodafone" #define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/16q71o61" -#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 201 +#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 200 struct networkConfig_t { From 30a99cb251bce63a46cdcdec89cf96e8fddac508 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 20:17:54 +1300 Subject: [PATCH 22/57] Working network positions backup --- GpsTracker/NetworkPositionsBackup.cpp | 37 ++++++++++++--------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 11b7819..de7b062 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -25,27 +25,24 @@ namespace positions { bool appendPosition(PositionEntry &entry) { VERBOSE("appendPosition"); debug::displayFreeRam(); - delay(100); + char buffer[BUFFER_SIZE]; - sprintf(buffer, "%d", 2100); - /*snprintf(buffer, BUFFER_SIZE, "%d,%d,%.2f,%d,", + snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,", entry.metadata.batteryLevel, entry.metadata.batteryVoltage, - entry.metadata.temperature, - static_cast(entry.metadata.status));*/ + static_cast(entry.metadata.temperature * 100), + static_cast(entry.metadata.status)); - ////strcat(buffer, entry.position); - //Serial.println(buffer); + strcat(buffer, entry.position); debug::displayFreeRam(); - return false; - /*return hardware::sim808::device.httpPost( - config.network.url, - PSTR("text/csv"), + return hardware::sim808::device.httpPost( + config::main::value.network.url, + F("text/csv"), buffer, buffer, BUFFER_SIZE - ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;*/ + ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } void appendPositions() { @@ -56,28 +53,28 @@ namespace positions { PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; - /*network::powerOn(); + network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); - else {*/ + else { hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; debug::displayFreeRam(); - //if (!appendPosition(currentEntry)) break; + if (!appendPosition(currentEntry)) break; + + config::main::value.network.lastSavedEntry = currentEntryIndex; + //config::main::save(); - /*config::main::value.network.lastSavedEntry = currentEntryIndex; - config::main::save(); -*/ } while (positions::moveNext(currentEntryIndex)); debug::displayFreeRam(); hardware::i2c::powerOff(); - //} + } - //network::powerOff(); + network::powerOff(); debug::displayFreeRam(); } From 4d8986d48526db62560626b04523471c847abeb7 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 14 Mar 2018 20:23:16 +1300 Subject: [PATCH 23/57] Saving network backup progress into eeprom --- GpsTracker/Debug.cpp | 2 +- GpsTracker/NetworkPositionsBackup.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 129cbd0..08e97b9 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -239,7 +239,7 @@ namespace debug { SIM808_GPS_STATUS::OFF }; - positions::appendLast(metadata); + for(int i = 0; i < 3; i++) positions::appendLast(metadata); } void setRtcTime() { diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index de7b062..7301af4 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -67,7 +67,7 @@ namespace positions { if (!appendPosition(currentEntry)) break; config::main::value.network.lastSavedEntry = currentEntryIndex; - //config::main::save(); + config::main::save(); } while (positions::moveNext(currentEntryIndex)); debug::displayFreeRam(); From 01f3a488a1a93aebe009b903d2cd852302d5733b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 16 Mar 2018 22:46:24 +1300 Subject: [PATCH 24/57] Added some code to reduce the effect of a gps fix that would indicate no speed, leaving the device in sleep mode for too long --- GpsTracker/Core.cpp | 30 +++++++++++++++++++++--------- GpsTracker/Core.h | 4 ++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 477001c..ce756f3 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -8,6 +8,7 @@ using namespace utils; namespace core { uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;; + uint8_t increaseInARow = 0; void main() { VERBOSE("main"); @@ -15,29 +16,40 @@ namespace core { PositionEntryMetadata metadata; if (positions::acquire(metadata)) { positions::appendLast(metadata); - setSleepTime(); + updateSleepTime(gps::getVelocity()); } - positions::doBackup(); + positions::doBackup(); + mainunit::deepSleep(sleepTime); } - void setSleepTime() { - setSleepTime(gps::getVelocity()); + void updateSleepTime(uint8_t velocity) { + uint16_t result = computeSleepTime(velocity); + + if (result > sleepTime) { + increaseInARow++; + if (increaseInARow < SLEEP_DEFAULT_INCREASE_THRESHOLD) return; + + } + else increaseInARow = 0; + + sleepTime = result; } - void setSleepTime(uint8_t velocity) { - sleepTime = SLEEP_DEFAULT_TIME_SECONDS; + uint16_t computeSleepTime(uint8_t velocity) { + uint16_t result; for (uint8_t i = 0; i < flash::getArraySize(config::defaultSleepTimings); i++) { sleepTimings_t timing; flash::read(&config::defaultSleepTimings[i], timing); if (velocity > timing.speed) continue; - - sleepTime = timing.seconds; + + result = timing.seconds; break; } - VERBOSE_FORMAT("setSleepTime", "%d", sleepTime); + VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result); + return result; } } \ No newline at end of file diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index 78172ab..7539d51 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -14,6 +14,6 @@ namespace core { extern uint16_t sleepTime; void main(); - void setSleepTime(uint8_t velocity); - void setSleepTime(); + void updateSleepTime(uint8_t velocity); + uint16_t computeSleepTime(uint8_t velocity); } \ No newline at end of file From fe7723c9d9da28e30ab6067274699dcc60ef88a6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 16 Mar 2018 22:52:08 +1300 Subject: [PATCH 25/57] getVelocity() returns 0 when parsing from lastPosition fails --- GpsTracker/Gps.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index e5840f1..9dbe1c4 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -47,7 +47,7 @@ namespace gps { uint8_t getVelocity() { uint8_t velocity; - hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity); + if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0; VERBOSE_FORMAT("getVelocity", "%d", velocity); From 5815e5c8d4e13430053f8be3278f0daa5d971747 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 16 Mar 2018 22:53:41 +1300 Subject: [PATCH 26/57] Avoid to backup positions if no positions have been saved at all --- GpsTracker/NetworkPositionsBackup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 7301af4..e47eafd 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -18,7 +18,7 @@ namespace positions { bool isBackupNeeded() { config_t *config = &config::main::value; - return config->network.lastSavedEntry == 0xFFFF || + return (config->network.lastSavedEntry == 0xFFFF && config ->lastEntry != 0xFFFF) || positions::count(config->network.lastSavedEntry) > config->network.saveThreshold; } From 4ecb38a8ea6f9a2e8aa21d5cf2792a5565d58f74 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 16 Mar 2018 22:54:48 +1300 Subject: [PATCH 27/57] Added debug functions to print sleeep times based on velocity --- GpsTracker/Debug.cpp | 27 +++++++++++++++++++-------- GpsTracker/Debug.h | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 08e97b9..7892bbe 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -1,6 +1,7 @@ #include "Debug.h" #include "Flash.h" #include "Positions.h" +#include "Core.h" #define LOGGER_NAME "Debug" @@ -186,6 +187,24 @@ namespace debug { NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); } + void setRtcTime() { + tmElements_t time; + gps::getTime(time); + rtc::setTime(time); + } + + void getAndDisplaySleepTimes() { + size_t arraySize = flash::getArraySize(config::defaultSleepTimings); + sleepTimings_t maxSpeedTiming; + utils::flash::read(&config::defaultSleepTimings[arraySize - 1], maxSpeedTiming); + + for (int i = 0; i <= maxSpeedTiming.speed; i++) { + core::computeSleepTime(i); + } + + NOTICE_MSG("getAndDisplaySleepTimes", "Done"); + } + void getAndDisplayEepromConfig() { config::main::setup(); //forcing read again } @@ -241,12 +260,4 @@ namespace debug { for(int i = 0; i < 3; i++) positions::appendLast(metadata); } - - void setRtcTime() { - tmElements_t time; - gps::getTime(time); - rtc::setTime(time); - - NOTICE_MSG("setRtcTime", "Done"); - } } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index dd06cb2..f5e7bcd 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -72,6 +72,8 @@ namespace debug { void getAndDisplayRtcTime(); void setRtcTime(); + void getAndDisplaySleepTimes(); + void getAndDisplayEepromConfig(); void getAndDisplayEepromContent(); void getAndDisplayEepromPositions(); From bbbc829b7f129b8a8cde118000bb89f1806aff25 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 16 Mar 2018 22:56:35 +1300 Subject: [PATCH 28/57] Defined configuration constants. Added a timeout to the menu. The goal is to automatically run normally at boot if no input is detected --- GpsTracker/Config.h | 19 +++++++++++++++---- GpsTracker/Debug.cpp | 13 ++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 259ad82..54e8239 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -3,7 +3,7 @@ #include #define BACKUP_ENABLE_SDCARD 0 -#define BACKUP_ENABLE_NETWORK 1 +#define BACKUP_ENABLE_NETWORK 0 #if BACKUP_ENABLE_NETWORK #include "NetworkPositionsConfig.h" @@ -16,14 +16,25 @@ #define SIM808_BAUDRATE 4800 -#define SLEEP_DEFAULT_TIME_SECONDS 1800 +#pragma region Default configuration values -#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 -#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define MENU_TIMEOUT 10000 +/** + \def SLEEP_DEFAULT_TIME_SECONDS + Hard coded value for default sleep time between position acquisitions. + Exprimed in seconds +*/ +#define SLEEP_DEFAULT_TIME_SECONDS 1800 +#define SLEEP_DEFAULT_INCREASE_THRESHOLD 3 + +#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 +#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 #define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 #define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#pragma endregion + struct sleepTimings_t { uint8_t speed; uint16_t seconds; diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 7892bbe..dbbf6f3 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -147,12 +147,23 @@ namespace debug { GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); + int16_t timeout = MENU_TIMEOUT; + do { for (uint8_t i = 0; i < menuSize; i++) { Serial.println(reinterpret_cast(pgm_read_word_near(&MENU_ENTRIES[i]))); } - while (!Serial.available()); + while (!Serial.available()) { +#ifndef _DEBUG + delay(50); + timeout -= 50; + if (timeout < 0) { + NOTICE_MSG("menu", "Timeout expired."); + return GPSTRACKER_DEBUG_COMMAND::RUN; + } +#endif + } command = parseCommand(Serial.read()); while (Serial.available()) Serial.read(); } while (command == GPSTRACKER_DEBUG_COMMAND::NONE); From dc106a1ce20e834a0c64a7dc52ddf1d21d827231 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 17 Mar 2018 19:52:56 +1300 Subject: [PATCH 30/57] Fixed _DEBUG vs !_DEBUG. Fixed code delcarations when network backup is disabled --- GpsTracker/Config.h | 4 ++-- GpsTracker/Debug.h | 25 +------------------------ GpsTracker/GpsTracker.ino | 7 +++++-- GpsTracker/Log.h | 22 ++++++++++++++++++++++ GpsTracker/Network.cpp | 5 ++++- GpsTracker/NetworkPositionsBackup.cpp | 5 ++++- GpsTracker/Positions.cpp | 2 +- 7 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 GpsTracker/Log.h diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 54e8239..9250b83 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -9,13 +9,13 @@ #include "NetworkPositionsConfig.h" #endif +#define SIM808_BAUDRATE 4800 + #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 #define VERSION "1.00" -#define SIM808_BAUDRATE 4800 - #pragma region Default configuration values #define MENU_TIMEOUT 10000 diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index f5e7bcd..3585551 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -1,36 +1,13 @@ #pragma once #include -#include +#include "Log.h" #include "Config.h" #include "Hardware.h" #include "Gps.h" #include "Rtc.h" -#define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) -#define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) -#define LOG_FORMAT(level, f, msg, ...) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) - -#ifdef _DEBUG - -#define VERBOSE(f) LOG(verbose, f) -#define VERBOSE_MSG(f, msg) LOG_MSG(verbose, f, msg) -#define VERBOSE_FORMAT(f, msg, ...) LOG_FORMAT(verbose, f, msg, __VA_ARGS__) -#else - -#define DISABLE_LOGGING 1 //TODO : does nothing if not included before ArduinoLog.h - -#define VERBOSE(f) -#define VERBOSE_MSG(f, msg) -#define VERBOSE_FORMAT(f, msg, ...) - -#endif - -#define NOTICE(f) LOG(notice, f) -#define NOTICE_MSG(f, msg) LOG_MSG(notice, f, msg) -#define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) - #define DEBUG_SERIAL_SPEED 115200 namespace debug { diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index d823820..1346fec 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -4,11 +4,14 @@ bool bypassMenu = false; void setup() { -#ifdef _DEBUG +#if _DEBUG debug::waitForSerial(); Log.begin(LOG_LEVEL_VERBOSE, &Serial); #else - if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); + if (Serial) { + Serial.begin(DEBUG_SERIAL_SPEED); + Log.begin(LOG_LEVEL_NOTICE, &Serial); + } #endif if (Serial) Serial.println(F("=============================")); diff --git a/GpsTracker/Log.h b/GpsTracker/Log.h new file mode 100644 index 0000000..f74952b --- /dev/null +++ b/GpsTracker/Log.h @@ -0,0 +1,22 @@ +#pragma once + +#define DISABLE_LOGGING 1 +#include + +#define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) +#define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) +#define LOG_FORMAT(level, f, msg, ...) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) + +#if _DEBUG +#define VERBOSE(f) LOG(verbose, f) +#define VERBOSE_MSG(f, msg) LOG_MSG(verbose, f, msg) +#define VERBOSE_FORMAT(f, msg, ...) LOG_FORMAT(verbose, f, msg, __VA_ARGS__) +#else +#define VERBOSE(f) +#define VERBOSE_MSG(f, msg) +#define VERBOSE_FORMAT(f, msg, ...) +#endif + +#define NOTICE(f) LOG(notice, f) +#define NOTICE_MSG(f, msg) LOG_MSG(notice, f, msg) +#define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 0a74b91..589baa1 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,4 +1,6 @@ #include "Config.h" + +#if BACKUP_ENABLE_NETWORK #include "Debug.h" #include "Network.h" #include "Hardware.h" @@ -34,4 +36,5 @@ namespace network { } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index e47eafd..c2bf8fd 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -1,9 +1,11 @@ #pragma once +#include "Config.h" + +#if BACKUP_ENABLE_NETWORK #include "NetworkPositionsBackup.h" #include "Debug.h" #include "Positions.h" -#include "Config.h" #include "Hardware.h" #include "Network.h" @@ -96,3 +98,4 @@ namespace positions { } } } +#endif \ No newline at end of file diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 224cb12..4251cbe 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -143,7 +143,7 @@ namespace positions { #ifdef BACKUPS_ENABLED debug::displayFreeRam(); VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); - _backups[0]->backup(); + //_backups[0]->backup(); //disabled for first real running test debug::displayFreeRam(); /*for (int i = 0; i < BACKUPS_ENABLED; i++) { VERBOSE_FORMAT("doBackup", "calling backup %d", i); From d66789faedf730d10b896a9d9d3d5ec9c0883797 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 17 Mar 2018 22:04:54 +1300 Subject: [PATCH 31/57] Code cleanup etc --- GpsTracker/Debug.cpp | 32 +++++++++--------------- GpsTracker/Debug.h | 51 ++++++++++++++++++++------------------- GpsTracker/GpsTracker.ino | 20 +++++++-------- GpsTracker/Log.cpp | 27 +++++++++++++++++++++ GpsTracker/Log.h | 7 ++++++ 5 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 GpsTracker/Log.cpp diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index dbbf6f3..8df8fd6 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -9,8 +9,8 @@ const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; -MENU_ENTRY(HEADER, "-- Debug Menu --"); -MENU_ENTRY(SEPARATOR, "----"); +MENU_ENTRY(HEADER, "-- Debug Menu --"); +MENU_ENTRY(SEPARATOR, "----"); MENU_ENTRY(RUN, "[R] Run"); MENU_ENTRY(RUN_ONCE, "[r] Run once"); @@ -116,11 +116,6 @@ namespace debug { } } - void waitForSerial() { - while (!Serial); - Serial.begin(DEBUG_SERIAL_SPEED); - } - int freeRam() { return freeRam2(); } @@ -141,13 +136,10 @@ namespace debug { return GPSTRACKER_DEBUG_COMMAND::NONE; } - GPSTRACKER_DEBUG_COMMAND menu() { - if (!Serial) return GPSTRACKER_DEBUG_COMMAND::RUN; - + GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout) { GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); - - int16_t timeout = MENU_TIMEOUT; + uint8_t intermediate_timeout = 50; do { for (uint8_t i = 0; i < menuSize; i++) { @@ -155,17 +147,17 @@ namespace debug { } while (!Serial.available()) { -#ifndef _DEBUG - delay(50); - timeout -= 50; - if (timeout < 0) { - NOTICE_MSG("menu", "Timeout expired."); - return GPSTRACKER_DEBUG_COMMAND::RUN; + if (timeout > 0) { + delay(intermediate_timeout); + timeout -= intermediate_timeout; + if (timeout <= 0) { + NOTICE_MSG("menu", "Timeout expired."); + return GPSTRACKER_DEBUG_COMMAND::RUN; + } } -#endif } command = parseCommand(Serial.read()); - while (Serial.available()) Serial.read(); + while (Serial.available()) Serial.read(); //flushing input } while (command == GPSTRACKER_DEBUG_COMMAND::NONE); return command; diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 3585551..e0ca6f7 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -1,9 +1,11 @@ #pragma once #include - -#include "Log.h" #include "Config.h" +#include "Log.h" + +#include "Core.h" + #include "Hardware.h" #include "Gps.h" #include "Rtc.h" @@ -13,34 +15,33 @@ namespace debug { enum class GPSTRACKER_DEBUG_COMMAND : uint8_t { - NONE = 0, - RUN = 1, - ONCE = 2, - RAM = 3, - BATTERY = 4, - GPS_ON = 5, - GPS_OFF = 6, - GPS_GET = 7, - GPS_SET = 8, - RTC_GET = 11, - RTC_SET = 12, - SD_WRITE_TEST = 13, - EEPROM_GET_CONFIG = 14, - EEPROM_RESET_CONFIG = 15, - EEPROM_GET_CONTENT = 16, - EEPROM_GET_LAST_ENTRY = 17, - EEPROM_GET_ENTRIES = 18, - EEPROM_ADD_ENTRY = 19, - EEPROM_BACKUP_ENTRIES = 20, - SLEEP = 21, - SLEEP_DEEP = 22 + NONE, + RUN, + ONCE, + RAM, + BATTERY, + GPS_ON, + GPS_OFF, + GPS_GET, + GPS_SET, + RTC_GET, + RTC_SET, + SD_WRITE_TEST, + EEPROM_GET_CONFIG, + EEPROM_RESET_CONFIG, + EEPROM_GET_CONTENT, + EEPROM_GET_LAST_ENTRY, + EEPROM_GET_ENTRIES, + EEPROM_ADD_ENTRY, + EEPROM_BACKUP_ENTRIES, + SLEEP, + SLEEP_DEEP }; - void waitForSerial(); int freeRam(); void displayFreeRam(); - GPSTRACKER_DEBUG_COMMAND menu(); + GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout); void getAndDisplayBattery(); void getAndDisplayGpsPosition(); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 1346fec..eecf0ba 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -1,30 +1,28 @@ #include "GpsTracker.h" #include "Positions.h" -bool bypassMenu = false; - -void setup() { #if _DEBUG - debug::waitForSerial(); - Log.begin(LOG_LEVEL_VERBOSE, &Serial); +#define MENU_TIMEOUT 0 #else - if (Serial) { - Serial.begin(DEBUG_SERIAL_SPEED); - Log.begin(LOG_LEVEL_NOTICE, &Serial); - } +#define MENU_TIMEOUT 10000 + #endif +bool bypassMenu = false; + +void setup() { + log::setup(); - if (Serial) Serial.println(F("=============================")); config::main::setup(); rtc::setup(); hardware::sim808::setup(); + positions::setup(); } void loop() { debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; - if(!bypassMenu) command = debug::menu(); + if (Serial && !bypassMenu) command = debug::menu(MENU_TIMEOUT); bypassMenu = command == debug::GPSTRACKER_DEBUG_COMMAND::RUN; diff --git a/GpsTracker/Log.cpp b/GpsTracker/Log.cpp new file mode 100644 index 0000000..57a66f2 --- /dev/null +++ b/GpsTracker/Log.cpp @@ -0,0 +1,27 @@ +#include "Log.h" + +namespace log { + + namespace details { + + void waitForSerial() { + while (!Serial); + Serial.begin(DEBUG_SERIAL_SPEED); + } + + } + void setup() { +#if _DEBUG + details::waitForSerial(); + Log.begin(LOG_LEVEL_VERBOSE, &Serial); +#else + if (Serial) { + Serial.begin(DEBUG_SERIAL_SPEED); + Log.begin(LOG_LEVEL_NOTICE, &Serial); + } +#endif + + if (Serial) Serial.println(F("=============================")); + } + +} \ No newline at end of file diff --git a/GpsTracker/Log.h b/GpsTracker/Log.h index f74952b..572a4ca 100644 --- a/GpsTracker/Log.h +++ b/GpsTracker/Log.h @@ -3,6 +3,8 @@ #define DISABLE_LOGGING 1 #include +#include "Config.h" + #define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) #define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) #define LOG_FORMAT(level, f, msg, ...) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) @@ -20,3 +22,8 @@ #define NOTICE(f) LOG(notice, f) #define NOTICE_MSG(f, msg) LOG_MSG(notice, f, msg) #define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) + + +namespace log { + void setup(); +} \ No newline at end of file From 6a2a305cd1aa0ab5c44fe5cc02ad3358e1114eec Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 17 Mar 2018 23:03:30 +1300 Subject: [PATCH 32/57] Adjusted log levels --- GpsTracker/Core.cpp | 1 + GpsTracker/Debug.cpp | 6 +++--- GpsTracker/Debug.h | 4 +--- GpsTracker/GpsTracker.ino | 2 +- GpsTracker/Log.cpp | 27 --------------------------- GpsTracker/Logging.cpp | 18 ++++++++++++++++++ GpsTracker/{Log.h => Logging.h} | 12 +++++++++--- GpsTracker/MainUnit.cpp | 4 ++-- GpsTracker/Positions.cpp | 6 ++++-- GpsTracker/Rtc.cpp | 2 +- 10 files changed, 40 insertions(+), 42 deletions(-) delete mode 100644 GpsTracker/Log.cpp create mode 100644 GpsTracker/Logging.cpp rename GpsTracker/{Log.h => Logging.h} (81%) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index ce756f3..492b278 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -34,6 +34,7 @@ namespace core { else increaseInARow = 0; sleepTime = result; + NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime); } uint16_t computeSleepTime(uint8_t velocity) { diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 8df8fd6..a095885 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -9,7 +9,7 @@ const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; -MENU_ENTRY(HEADER, "-- Debug Menu --"); +MENU_ENTRY(HEADER, "-- Menu --"); MENU_ENTRY(SEPARATOR, "----"); MENU_ENTRY(RUN, "[R] Run"); @@ -244,8 +244,8 @@ namespace debug { uint16_t lastEntryIndex = config::main::value.lastEntry; PositionEntry lastEntry; - positions::get(lastEntryIndex, lastEntry); - details::displayPosition(lastEntry); + if(positions::get(lastEntryIndex, lastEntry)) details::displayPosition(lastEntry); + else Log.notice(F("No position recorded\n")); } void addLastPositionToEeprom() { diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index e0ca6f7..71644bc 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -2,7 +2,7 @@ #include #include "Config.h" -#include "Log.h" +#include "Logging.h" #include "Core.h" @@ -10,8 +10,6 @@ #include "Gps.h" #include "Rtc.h" -#define DEBUG_SERIAL_SPEED 115200 - namespace debug { enum class GPSTRACKER_DEBUG_COMMAND : uint8_t { diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index eecf0ba..6d2cde8 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -10,7 +10,7 @@ bool bypassMenu = false; void setup() { - log::setup(); + logging::setup(); config::main::setup(); rtc::setup(); diff --git a/GpsTracker/Log.cpp b/GpsTracker/Log.cpp deleted file mode 100644 index 57a66f2..0000000 --- a/GpsTracker/Log.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Log.h" - -namespace log { - - namespace details { - - void waitForSerial() { - while (!Serial); - Serial.begin(DEBUG_SERIAL_SPEED); - } - - } - void setup() { -#if _DEBUG - details::waitForSerial(); - Log.begin(LOG_LEVEL_VERBOSE, &Serial); -#else - if (Serial) { - Serial.begin(DEBUG_SERIAL_SPEED); - Log.begin(LOG_LEVEL_NOTICE, &Serial); - } -#endif - - if (Serial) Serial.println(F("=============================")); - } - -} \ No newline at end of file diff --git a/GpsTracker/Logging.cpp b/GpsTracker/Logging.cpp new file mode 100644 index 0000000..0c1d2d9 --- /dev/null +++ b/GpsTracker/Logging.cpp @@ -0,0 +1,18 @@ +#include "Logging.h" + +namespace logging { + + void setup() { +#if _DEBUG + while (!Serial); +#endif + + if (Serial) { + Serial.begin(LOG_SERIAL_SPEED); + Serial.println(F("=============================")); + + Log.begin(LOG_LEVEL, &Serial); + } + } + +} \ No newline at end of file diff --git a/GpsTracker/Log.h b/GpsTracker/Logging.h similarity index 81% rename from GpsTracker/Log.h rename to GpsTracker/Logging.h index 572a4ca..c8460a3 100644 --- a/GpsTracker/Log.h +++ b/GpsTracker/Logging.h @@ -1,9 +1,15 @@ #pragma once +#include "Config.h" -#define DISABLE_LOGGING 1 +//#define DISABLE_LOGGING 1 #include -#include "Config.h" +#define LOG_SERIAL_SPEED 115200 +#if _DEBUG +#define LOG_LEVEL LOG_LEVEL_VERBOSE +#else +#define LOG_LEVEL LOG_LEVEL_NOTICE +#endif #define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) #define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) @@ -24,6 +30,6 @@ #define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) -namespace log { +namespace logging { void setup(); } \ No newline at end of file diff --git a/GpsTracker/MainUnit.cpp b/GpsTracker/MainUnit.cpp index b665c04..80b3448 100644 --- a/GpsTracker/MainUnit.cpp +++ b/GpsTracker/MainUnit.cpp @@ -36,7 +36,7 @@ namespace mainunit { } void sleep(period_t period) { - NOTICE_FORMAT("sleep", "Sleeping for period : %d", period); + NOTICE_FORMAT("sleep", "Period : %d", period); details::prepareSleep(); LowPower.powerDown(period, ADC_OFF, BOD_OFF); details::wokeUp(); @@ -44,7 +44,7 @@ namespace mainunit { } void deepSleep(uint16_t seconds) { - NOTICE_FORMAT("deepSleep", "Deep sleeping for %d seconds", seconds); + NOTICE_FORMAT("deepSleep", "%d seconds", seconds); interruptIn(seconds); details::prepareSleep(); LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 4251cbe..569b078 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -53,7 +53,7 @@ namespace positions { } bool acquire(PositionEntryMetadata &metadata) { - VERBOSE("acquire"); + NOTICE("acquire"); timestamp_t before; @@ -63,6 +63,8 @@ namespace positions { SIM808ChargingStatus battery = hardware::sim808::device.getChargingState(); gps::powerOff(); + NOTICE_FORMAT("acquire", "status : %d", gpsStatus); + if (gpsStatus < SIM808_GPS_STATUS::FIX) return false; uint16_t timeToFix = rtc::getTime() - before; @@ -98,7 +100,7 @@ namespace positions { hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + NOTICE_FORMAT("appendLast", "Saved @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); config->lastEntry++; if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index b971178..019d577 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -71,7 +71,7 @@ namespace rtc { RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON - NOTICE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.Hour, time.Minute, time.Second); + VERBOSE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.Hour, time.Minute, time.Second); hardware::i2c::powerOff(); } From 838c2c3b625bee3cfd52c5fcb646197fde66fc4b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 12:00:39 +1300 Subject: [PATCH 33/57] Fixed maxEntryIndex being set to 6 as eeprom is not initialized on static init. --- GpsTracker/Positions.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 569b078..91d1015 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -26,7 +26,7 @@ namespace positions { #endif namespace details { - uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; + uint16_t maxEntryIndex = 0; uint16_t getEntryAddress(uint16_t index) { if (index > maxEntryIndex) return -1; @@ -35,6 +35,7 @@ namespace positions { } void setup() { + details::maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; #ifdef BACKUPS_ENABLED uint8_t backupIdx = 0; _backups = new backup::PositionsBackup*[BACKUPS_ENABLED]; From 039e2c98ad509cf164e46e78fc62a55b1014f140 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 12:15:37 +1300 Subject: [PATCH 34/57] Removed menu timeout, that was a bad idea --- GpsTracker/Config.cpp | 4 ++-- GpsTracker/Debug.cpp | 18 ++++-------------- GpsTracker/Debug.h | 2 +- GpsTracker/GpsTracker.ino | 4 ++-- GpsTracker/Logging.cpp | 2 -- 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 4c5c21a..cc3a236 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -18,14 +18,14 @@ namespace config { if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); - VERBOSE_FORMAT("read", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + NOTICE_FORMAT("read", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); #if BACKUP_ENABLE_NETWORK VERBOSE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif } void write() { - VERBOSE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + NOTICE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); #if BACKUP_ENABLE_NETWORK VERBOSE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index a095885..ef2a0a4 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -9,7 +9,7 @@ const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; -MENU_ENTRY(HEADER, "-- Menu --"); +MENU_ENTRY(HEADER, "========================\n-- Menu --"); MENU_ENTRY(SEPARATOR, "----"); MENU_ENTRY(RUN, "[R] Run"); @@ -136,26 +136,16 @@ namespace debug { return GPSTRACKER_DEBUG_COMMAND::NONE; } - GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout) { + GPSTRACKER_DEBUG_COMMAND menu() { GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); - uint8_t intermediate_timeout = 50; do { for (uint8_t i = 0; i < menuSize; i++) { Serial.println(reinterpret_cast(pgm_read_word_near(&MENU_ENTRIES[i]))); } - while (!Serial.available()) { - if (timeout > 0) { - delay(intermediate_timeout); - timeout -= intermediate_timeout; - if (timeout <= 0) { - NOTICE_MSG("menu", "Timeout expired."); - return GPSTRACKER_DEBUG_COMMAND::RUN; - } - } - } + while (!Serial.available()); delay(50); command = parseCommand(Serial.read()); while (Serial.available()) Serial.read(); //flushing input } while (command == GPSTRACKER_DEBUG_COMMAND::NONE); @@ -261,6 +251,6 @@ namespace debug { SIM808_GPS_STATUS::OFF }; - for(int i = 0; i < 3; i++) positions::appendLast(metadata); + for(int i = 0; i < 10; i++) positions::appendLast(metadata); } } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 71644bc..a9ddd38 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -39,7 +39,7 @@ namespace debug { int freeRam(); void displayFreeRam(); - GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout); + GPSTRACKER_DEBUG_COMMAND menu(); void getAndDisplayBattery(); void getAndDisplayGpsPosition(); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 6d2cde8..6f5d4f2 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -4,7 +4,7 @@ #if _DEBUG #define MENU_TIMEOUT 0 #else -#define MENU_TIMEOUT 10000 +#define MENU_TIMEOUT 0 #endif bool bypassMenu = false; @@ -22,7 +22,7 @@ void setup() { void loop() { debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; - if (Serial && !bypassMenu) command = debug::menu(MENU_TIMEOUT); + if (Serial && !bypassMenu) command = debug::menu(); bypassMenu = command == debug::GPSTRACKER_DEBUG_COMMAND::RUN; diff --git a/GpsTracker/Logging.cpp b/GpsTracker/Logging.cpp index 0c1d2d9..b1f8db3 100644 --- a/GpsTracker/Logging.cpp +++ b/GpsTracker/Logging.cpp @@ -9,8 +9,6 @@ namespace logging { if (Serial) { Serial.begin(LOG_SERIAL_SPEED); - Serial.println(F("=============================")); - Log.begin(LOG_LEVEL, &Serial); } } From c0febe7fd6d079a562ae51dbc405d582057ab9b5 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 12:23:33 +1300 Subject: [PATCH 35/57] Force the hand to obtain an accurate fix when possible. To be improved (intermediate timeout between fix / accurate fix and no fix at all) --- GpsTracker/Gps.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index 9dbe1c4..37a66af 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -30,7 +30,7 @@ namespace gps { do { currentStatus = hardware::sim808::device.getGpsStatus(); - if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break; + if (currentStatus > SIM808_GPS_STATUS::FIX) break //if we have an accurate fix, break right now VERBOSE_FORMAT("acquireCurrentPosition", "%d", currentStatus); mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); From a093a1ea47c2e910cda25a9b7c83cdcc58ea5505 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 12:28:08 +1300 Subject: [PATCH 36/57] Revert "Removed menu timeout, that was a bad idea" This reverts commit 92c6a34fef8dc6d7f276bce0e481b3146f57b1bb. --- GpsTracker/Debug.cpp | 14 ++++++++++++-- GpsTracker/Debug.h | 2 +- GpsTracker/GpsTracker.ino | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index ef2a0a4..4e41311 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -136,16 +136,26 @@ namespace debug { return GPSTRACKER_DEBUG_COMMAND::NONE; } - GPSTRACKER_DEBUG_COMMAND menu() { + GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout) { GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); + uint8_t intermediate_timeout = 50; do { for (uint8_t i = 0; i < menuSize; i++) { Serial.println(reinterpret_cast(pgm_read_word_near(&MENU_ENTRIES[i]))); } - while (!Serial.available()); delay(50); + while (!Serial.available()) { + if (timeout > 0) { + delay(intermediate_timeout); + timeout -= intermediate_timeout; + if (timeout <= 0) { + NOTICE_MSG("menu", "Timeout expired."); + return GPSTRACKER_DEBUG_COMMAND::RUN; + } + } + } command = parseCommand(Serial.read()); while (Serial.available()) Serial.read(); //flushing input } while (command == GPSTRACKER_DEBUG_COMMAND::NONE); diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index a9ddd38..71644bc 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -39,7 +39,7 @@ namespace debug { int freeRam(); void displayFreeRam(); - GPSTRACKER_DEBUG_COMMAND menu(); + GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout); void getAndDisplayBattery(); void getAndDisplayGpsPosition(); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 6f5d4f2..8d55721 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -22,7 +22,7 @@ void setup() { void loop() { debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; - if (Serial && !bypassMenu) command = debug::menu(); + if (Serial && !bypassMenu) command = debug::menu(MENU_TIMEOUT); bypassMenu = command == debug::GPSTRACKER_DEBUG_COMMAND::RUN; From 878f509e4b5741684ad577edb2b804853e47b458 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 12:34:03 +1300 Subject: [PATCH 37/57] Reduced sleeping time for debugging purpose, and menu timeout is not effective after first command --- GpsTracker/Config.h | 13 +++++++++++-- GpsTracker/GpsTracker.ino | 10 +++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 9250b83..d42180b 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -53,7 +53,7 @@ struct config_t { namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - { 5, SLEEP_DEFAULT_TIME_SECONDS }, + /*{ 5, SLEEP_DEFAULT_TIME_SECONDS }, { 10, 1200 }, { 20, 600 }, { 30, 540 }, @@ -61,8 +61,17 @@ namespace config { { 80, 240 }, { 100, 210 }, { 180, 180 }, - }; + };*/ + { 5, 600 }, + { 10, 600 }, + { 20, 600 }, + { 30, 540 }, + { 50, 480 }, + { 80, 240 }, + { 100, 210 }, + { 180, 180 }, + }; namespace main { extern config_t value; diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 8d55721..0b908c8 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -2,12 +2,13 @@ #include "Positions.h" #if _DEBUG -#define MENU_TIMEOUT 0 +#define MENU_DEFAULT_TIMEOUT 0 #else -#define MENU_TIMEOUT 0 +#define MENU_DEFAULT_TIMEOUT 5000 #endif bool bypassMenu = false; +uint16_t menuTimeout = MENU_DEFAULT_TIMEOUT; void setup() { logging::setup(); @@ -22,7 +23,10 @@ void setup() { void loop() { debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; - if (Serial && !bypassMenu) command = debug::menu(MENU_TIMEOUT); + if (Serial && !bypassMenu) command = debug::menu(menuTimeout); + + if (command == debug::GPSTRACKER_DEBUG_COMMAND::RUN) bypassMenu = true; + else menuTimeout = 0; //disable timeout once a command has been entered bypassMenu = command == debug::GPSTRACKER_DEBUG_COMMAND::RUN; From 6b19ce95d30b1216b564bee9f6327c337fba53ac Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 17:36:03 +1300 Subject: [PATCH 38/57] Adjusted speed timings --- GpsTracker/Config.h | 4 ++-- GpsTracker/Gps.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index d42180b..48ea242 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -62,8 +62,8 @@ namespace config { { 100, 210 }, { 180, 180 }, };*/ - - { 5, 600 }, + { 3, 1800 }, + { 5, 900 }, { 10, 600 }, { 20, 600 }, { 30, 540 }, diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index 37a66af..05aba7d 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -30,9 +30,9 @@ namespace gps { do { currentStatus = hardware::sim808::device.getGpsStatus(); - if (currentStatus > SIM808_GPS_STATUS::FIX) break //if we have an accurate fix, break right now + if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now - VERBOSE_FORMAT("acquireCurrentPosition", "%d", currentStatus); + NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); From 27c290dd5b82c9a9d8b58b0c2b100c1221e500b0 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 21:38:56 +1300 Subject: [PATCH 39/57] Added detected velocity and sleep timing when setting false gps coordinate --- GpsTracker/Debug.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 4e41311..42dcb41 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -173,6 +173,8 @@ namespace debug { strlcpy_P(gps::lastPosition, FAKE_GPS_ENTRY, GPS_POSITION_SIZE); NOTICE_FORMAT("setFakeGpsPosition", "Last position set to : %s", gps::lastPosition); + NOTICE_FORMAT("setFakeGpsPosition", "Speed : %d", gps::getVelocity()); + NOTICE_FORMAT("setFakeGpsPosition", "Sleep time : %d", core::computeSleepTime(gps::getVelocity())); } void getAndDisplayBattery() { From 1b8e898a9bdd0061550e5daefc2bc36f89b7bb30 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 18 Mar 2018 22:37:53 +1300 Subject: [PATCH 40/57] Introduced speed timing variations based on time of the day --- GpsTracker/Config.h | 36 ++++++++++++++++++------------------ GpsTracker/Core.cpp | 11 ++++++++++- GpsTracker/Debug.cpp | 2 +- GpsTracker/GpsTracker.ino | 2 +- GpsTracker/Rtc.cpp | 9 +++++++++ GpsTracker/Rtc.h | 1 + 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 48ea242..c12cb1c 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -35,8 +35,14 @@ #pragma endregion +#define SLEEP_TIMING_TIME(hours, minutes) hours * 60 + minutes +#define SLEEP_TIMING_MIN SLEEP_TIMING_TIME(0, 0) +#define SLEEP_TIMING_MAX SLEEP_TIMING_TIME(23, 59) + struct sleepTimings_t { uint8_t speed; + uint16_t timeMin; + uint16_t timeMax; uint16_t seconds; }; @@ -53,25 +59,19 @@ struct config_t { namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - /*{ 5, SLEEP_DEFAULT_TIME_SECONDS }, - { 10, 1200 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, - };*/ - { 3, 1800 }, - { 5, 900 }, - { 10, 600 }, - { 20, 600 }, - { 30, 540 }, - { 50, 480 }, - { 80, 240 }, - { 100, 210 }, - { 180, 180 }, + { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_TIME(8, 30), SLEEP_DEFAULT_TIME_SECONDS }, + { 3, SLEEP_TIMING_MIN, SLEEP_TIMING_TIME(15, 59), 10800 }, + { 3, SLEEP_TIMING_TIME(8, 29), SLEEP_TIMING_MAX, 10800 }, + + { 5, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 900 }, + { 20, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 600 }, + { 30, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 540 }, + { 50, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 480 }, + { 80, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 240 }, + { 100, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 210 }, + { 180, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 180 }, }; + namespace main { extern config_t value; diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 492b278..74135e0 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -39,13 +39,22 @@ namespace core { uint16_t computeSleepTime(uint8_t velocity) { uint16_t result; + uint16_t currentTime = 0xFFFF; + + if (rtc::isAccurate()) { + tmElements_t time; + rtc::getTime(time); + currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute); + } + for (uint8_t i = 0; i < flash::getArraySize(config::defaultSleepTimings); i++) { sleepTimings_t timing; flash::read(&config::defaultSleepTimings[i], timing); if (velocity > timing.speed) continue; - + if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue; + result = timing.seconds; break; } diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 42dcb41..c15821b 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -7,7 +7,7 @@ #define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text -const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,67.99,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; +const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,2.70,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; MENU_ENTRY(HEADER, "========================\n-- Menu --"); MENU_ENTRY(SEPARATOR, "----"); diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 0b908c8..2a1ec23 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -4,7 +4,7 @@ #if _DEBUG #define MENU_DEFAULT_TIMEOUT 0 #else -#define MENU_DEFAULT_TIMEOUT 5000 +#define MENU_DEFAULT_TIMEOUT 10000 #endif bool bypassMenu = false; diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 019d577..1f58059 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -31,6 +31,14 @@ namespace rtc { return temperature; } + bool isAccurate() { + hardware::i2c::powerOn(); + bool accurate = RTC.status(DS3231_HALTED_FLAG) == DS3231_OFF; + hardware::i2c::powerOff(); + + return accurate; + } + timestamp_t getTime() { tmElements_t time; getTime(time); @@ -50,6 +58,7 @@ namespace rtc { hardware::i2c::powerOn(); RTC.writeTime(time); + RTC.control(DS3231_HALTED_FLAG, DS3231_OFF); hardware::i2c::powerOff(); } diff --git a/GpsTracker/Rtc.h b/GpsTracker/Rtc.h index 1236f53..e752893 100644 --- a/GpsTracker/Rtc.h +++ b/GpsTracker/Rtc.h @@ -7,6 +7,7 @@ namespace rtc { float getTemperature(); + bool isAccurate(); timestamp_t getTime(); void getTime(tmElements_t &time); void setTime(const tmElements_t &time); From c12fc78f7dc9b807f3204c9df415a8e07b9a77c3 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 19 Mar 2018 22:31:40 +1300 Subject: [PATCH 41/57] Fix sleep time at night --- GpsTracker/Config.h | 6 +++--- GpsTracker/Debug.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index c12cb1c..c21770d 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -59,9 +59,9 @@ struct config_t { namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_TIME(8, 30), SLEEP_DEFAULT_TIME_SECONDS }, - { 3, SLEEP_TIMING_MIN, SLEEP_TIMING_TIME(15, 59), 10800 }, - { 3, SLEEP_TIMING_TIME(8, 29), SLEEP_TIMING_MAX, 10800 }, + { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_MAX, SLEEP_DEFAULT_TIME_SECONDS }, + { 3, SLEEP_TIMING_MIN, SLEEP_TIMING_TIME(8, 29), SLEEP_DEFAULT_TIME_SECONDS }, + { 3, SLEEP_TIMING_TIME(8, 30), SLEEP_TIMING_TIME(15, 59), 10800 }, { 5, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 900 }, { 20, SLEEP_TIMING_MIN, SLEEP_TIMING_MAX, 600 }, diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index c15821b..79cc86f 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -112,7 +112,7 @@ namespace debug { namespace details { inline void displayPosition(PositionEntry entry) { - Log.notice(F("%d%%, %dmV, %f°C, %ds %d, %s\n"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + Log.notice(F("%d%%, %dmV, %f°C, %ds, %d, %s\n"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); } } From f5b38feed376dd21bb77f403fba4c7e02fadcd20 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 19 Mar 2018 22:44:29 +1300 Subject: [PATCH 42/57] Do not wait the full time when no gsm signal can be found --- GpsTracker/Network.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 589baa1..fd57245 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -12,6 +12,7 @@ namespace network { SIM808RegistrationStatus waitForRegistered(uint32_t timeout) { SIM808RegistrationStatus currentStatus; + uint8_t noNetwork = 0; do { currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); @@ -19,6 +20,14 @@ namespace network { SIM808SignalQualityReport report = hardware::sim808::device.getSignalQuality(); VERBOSE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); + + if (report.ssri == 0) noNetwork++; + else noNetwork = 0; + if (noNetwork > 3) { + VERBOSE_MSG("waitForRegistered", "No signal"); + break; //after a while, not network really means no network. Bailing out + } + mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); From 2273ed1464e81d3b7e6a8d05c97acb6ae8ac2639 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Tue, 20 Mar 2018 23:20:20 +1300 Subject: [PATCH 43/57] Adjusted sleep timings --- GpsTracker/Config.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index c21770d..b31a0a4 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -59,7 +59,8 @@ struct config_t { namespace config { static const sleepTimings_t defaultSleepTimings[] PROGMEM = { - { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_MAX, SLEEP_DEFAULT_TIME_SECONDS }, + { 3, SLEEP_TIMING_TIME(16, 00), SLEEP_TIMING_TIME(19, 59), 3600 }, + { 3, SLEEP_TIMING_TIME(20, 00), SLEEP_TIMING_MAX, SLEEP_DEFAULT_TIME_SECONDS }, { 3, SLEEP_TIMING_MIN, SLEEP_TIMING_TIME(8, 29), SLEEP_DEFAULT_TIME_SECONDS }, { 3, SLEEP_TIMING_TIME(8, 30), SLEEP_TIMING_TIME(15, 59), 10800 }, From cb7522513953b2273086d9d6ea3accfb6d79e63a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:15:13 +1300 Subject: [PATCH 44/57] Cleaned up code of freeRam debugs. Cleaned commented code --- GpsTracker/Config.cpp | 12 ++++++++++-- GpsTracker/Config.h | 2 +- GpsTracker/NetworkPositionsBackup.cpp | 9 --------- GpsTracker/Positions.cpp | 11 +++++------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index cc3a236..6f0f7d5 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -20,14 +20,21 @@ namespace config { NOTICE_FORMAT("read", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); #if BACKUP_ENABLE_NETWORK - VERBOSE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + //networkConfig_t c = { + // POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, + // 0xFFFF, + // POSITIONS_CONFIG_NET_DEFAULT_APN, + // POSITIONS_CONFIG_NET_DEFAULT_URL, + //}; + //value.network = c; #endif } void write() { NOTICE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); #if BACKUP_ENABLE_NETWORK - VERBOSE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); @@ -37,6 +44,7 @@ namespace config { void setup() { details::read(); + //details::write(); } void save() { diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index b31a0a4..604c6b7 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -3,7 +3,7 @@ #include #define BACKUP_ENABLE_SDCARD 0 -#define BACKUP_ENABLE_NETWORK 0 +#define BACKUP_ENABLE_NETWORK 1 #if BACKUP_ENABLE_NETWORK #include "NetworkPositionsConfig.h" diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index c2bf8fd..6e0f499 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -26,7 +26,6 @@ namespace positions { bool appendPosition(PositionEntry &entry) { VERBOSE("appendPosition"); - debug::displayFreeRam(); char buffer[BUFFER_SIZE]; snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,", @@ -36,7 +35,6 @@ namespace positions { static_cast(entry.metadata.status)); strcat(buffer, entry.position); - debug::displayFreeRam(); return hardware::sim808::device.httpPost( config::main::value.network.url, @@ -49,7 +47,6 @@ namespace positions { void appendPositions() { VERBOSE("appendPositions"); - debug::displayFreeRam(); uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; @@ -64,7 +61,6 @@ namespace positions { hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; - debug::displayFreeRam(); if (!appendPosition(currentEntry)) break; @@ -72,12 +68,10 @@ namespace positions { config::main::save(); } while (positions::moveNext(currentEntryIndex)); - debug::displayFreeRam(); hardware::i2c::powerOff(); } network::powerOff(); - debug::displayFreeRam(); } } @@ -88,11 +82,8 @@ namespace positions { void NetworkPositionsBackup::backup() { VERBOSE("backup"); - debug::displayFreeRam(); if (!details::isBackupNeeded()) return; - debug::displayFreeRam(); - details::appendPositions(); } } diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 91d1015..63f4999 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -113,6 +113,8 @@ namespace positions { } bool get(uint16_t index, PositionEntry &entry) { + VERBOSE("get"); + uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; @@ -144,15 +146,12 @@ namespace positions { void doBackup() { #ifdef BACKUPS_ENABLED - debug::displayFreeRam(); VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); - //_backups[0]->backup(); //disabled for first real running test - debug::displayFreeRam(); - /*for (int i = 0; i < BACKUPS_ENABLED; i++) { + + for (int i = 0; i < BACKUPS_ENABLED; i++) { VERBOSE_FORMAT("doBackup", "calling backup %d", i); - delay(1000); _backups[i]->backup(); - }*/ + } #endif } } \ No newline at end of file From f47ab930e4abd3db348c5aff156a2f221fe7a999 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:15:33 +1300 Subject: [PATCH 45/57] Changed network backup configuration --- GpsTracker/NetworkPositionsConfig.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 7b59169..1465b0e 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -1,9 +1,9 @@ #pragma once -#define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 2 +#define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 10 #define POSITIONS_CONFIG_NET_DEFAULT_APN "Vodafone" -#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/16q71o61" +#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/1fgc99k1" #define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 200 From 9ccd312144c972271247d63d1badf282928762c6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:16:25 +1300 Subject: [PATCH 46/57] Sending freeRam and gsm signal attenuation along with position for debugging purpose --- GpsTracker/NetworkPositionsBackup.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 6e0f499..28fb063 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -28,7 +28,9 @@ namespace positions { VERBOSE("appendPosition"); char buffer[BUFFER_SIZE]; - snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,", + snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,%d,%d,", + debug::freeRam(), + hardware::sim808::device.getSignalQuality().attenuation, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, static_cast(entry.metadata.temperature * 100), From e77f5aac487866594e09b4c1f2423f1c400ca116 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:16:46 +1300 Subject: [PATCH 47/57] Gave some more space for the "no network" bail out --- GpsTracker/Network.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index fd57245..5ec3f85 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -11,6 +11,8 @@ namespace network { SIM808RegistrationStatus waitForRegistered(uint32_t timeout) { + VERBOSE("waitForRegistered"); + SIM808RegistrationStatus currentStatus; uint8_t noNetwork = 0; @@ -23,7 +25,7 @@ namespace network { if (report.ssri == 0) noNetwork++; else noNetwork = 0; - if (noNetwork > 3) { + if (noNetwork > 10) { VERBOSE_MSG("waitForRegistered", "No signal"); break; //after a while, not network really means no network. Bailing out } From b8aed994184ba1ba4bd936ae695cdff92d8acf61 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:17:16 +1300 Subject: [PATCH 48/57] Added project for python server that will receive gps tracker positions --- GpsTrackerServer/GpsTrackerServer.pyproj | 77 ++++++++++++++++++++++++ GpsTrackerServer/main.py | 35 +++++++++++ 2 files changed, 112 insertions(+) create mode 100644 GpsTrackerServer/GpsTrackerServer.pyproj create mode 100644 GpsTrackerServer/main.py diff --git a/GpsTrackerServer/GpsTrackerServer.pyproj b/GpsTrackerServer/GpsTrackerServer.pyproj new file mode 100644 index 0000000..d12a86b --- /dev/null +++ b/GpsTrackerServer/GpsTrackerServer.pyproj @@ -0,0 +1,77 @@ + + + 10.0 + Debug + 2.0 + 38a5dbf7-8f3d-4c79-b589-ab52837ba180 + . + {1b580a1a-fdb3-4b32-83e1-6407eb2722e6};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52} + main.py + + + . + Web launcher + http://localhost + . + true + GpsTrackerServer + GpsTrackerServer + Global|PythonCore|3.6 + + + true + false + + + true + false + + + + Code + + + + + + + + + + + + + + + + True + True + http://localhost + False + + + + + + + CurrentPage + True + False + False + False + + + + + + + + + False + False + + + + + \ No newline at end of file diff --git a/GpsTrackerServer/main.py b/GpsTrackerServer/main.py new file mode 100644 index 0000000..3258d82 --- /dev/null +++ b/GpsTrackerServer/main.py @@ -0,0 +1,35 @@ +""" +Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker +""" + +import falcon + +class RequireCSV: + + def process_request(self, req, resp): + if req.method in ('POST', 'PUT'): + if 'text/csv' not in req.content_type: + raise falcon.HTTPUnsupportedMediaType('Only CSV is supported') + +class PositionsResource: + + def on_post(self, req, resp): + data = req.bounded_stream.read() + resp.body = data + resp.status = falcon.HTTP_201 + +positions = PositionsResource() + +api = falcon.API(middleware=[ + RequireCSV() +]) + +api.add_route('/positions', positions) + +if __name__ == "__main__": + # Use python built-in WSGI reference implemention to run a web server + from wsgiref.simple_server import make_server + + print('Starting web server...') + srv = make_server('localhost', 8080, api) + srv.serve_forever() \ No newline at end of file From ba971f712ac472ed5c68beedc00d81dfcda46213 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 15:18:59 +1300 Subject: [PATCH 49/57] Introduced constant for the "no network" bail out --- GpsTracker/Config.h | 1 + GpsTracker/Network.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 604c6b7..88d7ee8 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -32,6 +32,7 @@ #define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 #define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define NETWORK_DEFAULT_NO_NETWORK_TRIES 10 #pragma endregion diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 5ec3f85..eff8c44 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -25,7 +25,7 @@ namespace network { if (report.ssri == 0) noNetwork++; else noNetwork = 0; - if (noNetwork > 10) { + if (noNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { VERBOSE_MSG("waitForRegistered", "No signal"); break; //after a while, not network really means no network. Bailing out } From 32be4cf9e56d72eaf64cb232f0d833ac546f8510 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 16:37:32 +1300 Subject: [PATCH 50/57] Started some real work around a gps tracker postions server. --- GpsTrackerServer/GpsTrackerServer.pyproj | 12 ++++++++++ GpsTrackerServer/api.py | 16 ++++++++++++++ GpsTrackerServer/constants.py | 4 ++++ GpsTrackerServer/main.py | 28 +++--------------------- GpsTrackerServer/middlewares.py | 24 ++++++++++++++++++++ GpsTrackerServer/resources.py | 11 ++++++++++ 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 GpsTrackerServer/api.py create mode 100644 GpsTrackerServer/constants.py create mode 100644 GpsTrackerServer/middlewares.py create mode 100644 GpsTrackerServer/resources.py diff --git a/GpsTrackerServer/GpsTrackerServer.pyproj b/GpsTrackerServer/GpsTrackerServer.pyproj index d12a86b..7082fa1 100644 --- a/GpsTrackerServer/GpsTrackerServer.pyproj +++ b/GpsTrackerServer/GpsTrackerServer.pyproj @@ -27,9 +27,21 @@ false + + Code + + + Code + Code + + Code + + + Code + diff --git a/GpsTrackerServer/api.py b/GpsTrackerServer/api.py new file mode 100644 index 0000000..b2fd90b --- /dev/null +++ b/GpsTrackerServer/api.py @@ -0,0 +1,16 @@ +import falcon +import middlewares +import resources +import constants + +#from constants import * + +def create(): + api = falcon.API(media_type=constants.MEDIA_CSV, middleware=[ + middlewares.AuthMiddleware(constants.USER_AGENT), + middlewares.RequireCSV() + ]) + + api.add_route(constants.ROUTES_POSITIONS, resources.PositionsResource()) + + return api \ No newline at end of file diff --git a/GpsTrackerServer/constants.py b/GpsTrackerServer/constants.py new file mode 100644 index 0000000..6cbbbba --- /dev/null +++ b/GpsTrackerServer/constants.py @@ -0,0 +1,4 @@ +USER_AGENT = 'SIMCOM_MODULE' +MEDIA_CSV = 'text/csv' + +ROUTES_POSITIONS = '/positions' diff --git a/GpsTrackerServer/main.py b/GpsTrackerServer/main.py index 3258d82..b559f47 100644 --- a/GpsTrackerServer/main.py +++ b/GpsTrackerServer/main.py @@ -2,34 +2,12 @@ Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker """ -import falcon - -class RequireCSV: - - def process_request(self, req, resp): - if req.method in ('POST', 'PUT'): - if 'text/csv' not in req.content_type: - raise falcon.HTTPUnsupportedMediaType('Only CSV is supported') - -class PositionsResource: - - def on_post(self, req, resp): - data = req.bounded_stream.read() - resp.body = data - resp.status = falcon.HTTP_201 - -positions = PositionsResource() - -api = falcon.API(middleware=[ - RequireCSV() -]) - -api.add_route('/positions', positions) +import api if __name__ == "__main__": # Use python built-in WSGI reference implemention to run a web server from wsgiref.simple_server import make_server print('Starting web server...') - srv = make_server('localhost', 8080, api) - srv.serve_forever() \ No newline at end of file + srv = make_server('localhost', 8080, api.create()) + srv.serve_forever() diff --git a/GpsTrackerServer/middlewares.py b/GpsTrackerServer/middlewares.py new file mode 100644 index 0000000..51d801a --- /dev/null +++ b/GpsTrackerServer/middlewares.py @@ -0,0 +1,24 @@ +import falcon +import constants + +class RequireCSV(object): + + def process_request(self, req, resp): + if req.method in ('POST', 'PUT'): + if constants.MEDIA_CSV not in req.content_type: + raise falcon.HTTPUnsupportedMediaType + +class AuthMiddleware(object): + + def __init__(self, userAgentRequired): + self._userAgentRequired = userAgentRequired + + def process_request(self, req, resp): + authorization = req.get_header('Authorization') + userAgent = req.get_header('User-Agent') + + if userAgent != self._userAgentRequired: + if authorization is None: #make clients believe that Authorization header is the issue + raise falcon.HTTPForbidden + else: + raise falcon.HTTPUnauthorized diff --git a/GpsTrackerServer/resources.py b/GpsTrackerServer/resources.py new file mode 100644 index 0000000..579bdf1 --- /dev/null +++ b/GpsTrackerServer/resources.py @@ -0,0 +1,11 @@ +import falcon + +class PositionsResource: + + def on_post(self, req, resp): + data = req.bounded_stream.read() + resp.body = data + resp.status = falcon.HTTP_201 + + def on_get(self, req, resp): + raise falcon.HTTPNotImplemented From 16a50cf5318e87bd3a32a391996cbeadc2541989 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 21 Mar 2018 22:14:34 +1300 Subject: [PATCH 51/57] The gps tracker server now has its own repository --- GpsTrackerServer/GpsTrackerServer.pyproj | 89 ------------------------ GpsTrackerServer/api.py | 16 ----- GpsTrackerServer/constants.py | 4 -- GpsTrackerServer/main.py | 13 ---- GpsTrackerServer/middlewares.py | 24 ------- GpsTrackerServer/resources.py | 11 --- 6 files changed, 157 deletions(-) delete mode 100644 GpsTrackerServer/GpsTrackerServer.pyproj delete mode 100644 GpsTrackerServer/api.py delete mode 100644 GpsTrackerServer/constants.py delete mode 100644 GpsTrackerServer/main.py delete mode 100644 GpsTrackerServer/middlewares.py delete mode 100644 GpsTrackerServer/resources.py diff --git a/GpsTrackerServer/GpsTrackerServer.pyproj b/GpsTrackerServer/GpsTrackerServer.pyproj deleted file mode 100644 index 7082fa1..0000000 --- a/GpsTrackerServer/GpsTrackerServer.pyproj +++ /dev/null @@ -1,89 +0,0 @@ - - - 10.0 - Debug - 2.0 - 38a5dbf7-8f3d-4c79-b589-ab52837ba180 - . - {1b580a1a-fdb3-4b32-83e1-6407eb2722e6};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52} - main.py - - - . - Web launcher - http://localhost - . - true - GpsTrackerServer - GpsTrackerServer - Global|PythonCore|3.6 - - - true - false - - - true - false - - - - Code - - - Code - - - Code - - - Code - - - Code - - - - - - - - - - - - - - - - True - True - http://localhost - False - - - - - - - CurrentPage - True - False - False - False - - - - - - - - - False - False - - - - - \ No newline at end of file diff --git a/GpsTrackerServer/api.py b/GpsTrackerServer/api.py deleted file mode 100644 index b2fd90b..0000000 --- a/GpsTrackerServer/api.py +++ /dev/null @@ -1,16 +0,0 @@ -import falcon -import middlewares -import resources -import constants - -#from constants import * - -def create(): - api = falcon.API(media_type=constants.MEDIA_CSV, middleware=[ - middlewares.AuthMiddleware(constants.USER_AGENT), - middlewares.RequireCSV() - ]) - - api.add_route(constants.ROUTES_POSITIONS, resources.PositionsResource()) - - return api \ No newline at end of file diff --git a/GpsTrackerServer/constants.py b/GpsTrackerServer/constants.py deleted file mode 100644 index 6cbbbba..0000000 --- a/GpsTrackerServer/constants.py +++ /dev/null @@ -1,4 +0,0 @@ -USER_AGENT = 'SIMCOM_MODULE' -MEDIA_CSV = 'text/csv' - -ROUTES_POSITIONS = '/positions' diff --git a/GpsTrackerServer/main.py b/GpsTrackerServer/main.py deleted file mode 100644 index b559f47..0000000 --- a/GpsTrackerServer/main.py +++ /dev/null @@ -1,13 +0,0 @@ -""" -Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker -""" - -import api - -if __name__ == "__main__": - # Use python built-in WSGI reference implemention to run a web server - from wsgiref.simple_server import make_server - - print('Starting web server...') - srv = make_server('localhost', 8080, api.create()) - srv.serve_forever() diff --git a/GpsTrackerServer/middlewares.py b/GpsTrackerServer/middlewares.py deleted file mode 100644 index 51d801a..0000000 --- a/GpsTrackerServer/middlewares.py +++ /dev/null @@ -1,24 +0,0 @@ -import falcon -import constants - -class RequireCSV(object): - - def process_request(self, req, resp): - if req.method in ('POST', 'PUT'): - if constants.MEDIA_CSV not in req.content_type: - raise falcon.HTTPUnsupportedMediaType - -class AuthMiddleware(object): - - def __init__(self, userAgentRequired): - self._userAgentRequired = userAgentRequired - - def process_request(self, req, resp): - authorization = req.get_header('Authorization') - userAgent = req.get_header('User-Agent') - - if userAgent != self._userAgentRequired: - if authorization is None: #make clients believe that Authorization header is the issue - raise falcon.HTTPForbidden - else: - raise falcon.HTTPUnauthorized diff --git a/GpsTrackerServer/resources.py b/GpsTrackerServer/resources.py deleted file mode 100644 index 579bdf1..0000000 --- a/GpsTrackerServer/resources.py +++ /dev/null @@ -1,11 +0,0 @@ -import falcon - -class PositionsResource: - - def on_post(self, req, resp): - data = req.bounded_stream.read() - resp.body = data - resp.status = falcon.HTTP_201 - - def on_get(self, req, resp): - raise falcon.HTTPNotImplemented From 2575be5e35220861c323ce51a008a355b1cd1c89 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 20:15:15 +1300 Subject: [PATCH 52/57] Enabled network backup to real server --- GpsTracker/NetworkPositionsConfig.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 1465b0e..7e5f40d 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -3,8 +3,8 @@ #define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 10 #define POSITIONS_CONFIG_NET_DEFAULT_APN "Vodafone" -#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/1fgc99k1" -#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 200 +#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://yourserver.com/endpoint" +#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 201 struct networkConfig_t { From 89fbf7537390ccbaa1e30cd2b0c96bbb9735b465 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 20:40:34 +1300 Subject: [PATCH 53/57] Increase conditions before bailing out because of a poor quality network --- GpsTracker/Config.h | 15 ++++++++------- GpsTracker/Logging.cpp | 2 ++ GpsTracker/Network.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 88d7ee8..c3bc7b7 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -24,15 +24,16 @@ Hard coded value for default sleep time between position acquisitions. Exprimed in seconds */ -#define SLEEP_DEFAULT_TIME_SECONDS 1800 -#define SLEEP_DEFAULT_INCREASE_THRESHOLD 3 +#define SLEEP_DEFAULT_TIME_SECONDS 1800 +#define SLEEP_DEFAULT_INCREASE_THRESHOLD 3 -#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 -#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 +#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 -#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 -#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 180000 -#define NETWORK_DEFAULT_NO_NETWORK_TRIES 10 +#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 6000 +#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define NETWORK_DEFAULT_NO_NETWORK_QUALIRY_THRESHOLD 8 +#define NETWORK_DEFAULT_NO_NETWORK_TRIES 5 #pragma endregion diff --git a/GpsTracker/Logging.cpp b/GpsTracker/Logging.cpp index b1f8db3..b7c1d7d 100644 --- a/GpsTracker/Logging.cpp +++ b/GpsTracker/Logging.cpp @@ -10,6 +10,8 @@ namespace logging { if (Serial) { Serial.begin(LOG_SERIAL_SPEED); Log.begin(LOG_LEVEL, &Serial); + + Log.notice("Starting...\n"); } } diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index eff8c44..3acc65b 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -14,18 +14,18 @@ namespace network { VERBOSE("waitForRegistered"); SIM808RegistrationStatus currentStatus; - uint8_t noNetwork = 0; + uint8_t noReliableNetwork = 0; do { currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); if (isAvailable(currentStatus.stat)) break; SIM808SignalQualityReport report = hardware::sim808::device.getSignalQuality(); - VERBOSE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); + NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); - if (report.ssri == 0) noNetwork++; - else noNetwork = 0; - if (noNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { + if (report.ssri < NETWORK_DEFAULT_NO_NETWORK_QUALIRY_THRESHOLD) noReliableNetwork++; + else noReliableNetwork = 0; + if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { VERBOSE_MSG("waitForRegistered", "No signal"); break; //after a while, not network really means no network. Bailing out } From f687c9d11a89204193aa1cbb0adb6c02b722bba7 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 21:16:01 +1300 Subject: [PATCH 54/57] Adjusted log level : verbose is really in a case of a hard debugging session --- GpsTracker/Config.cpp | 2 ++ GpsTracker/Core.cpp | 1 - GpsTracker/Gps.cpp | 2 +- GpsTracker/Hardware.cpp | 7 ++----- GpsTracker/MainUnit.cpp | 2 +- GpsTracker/Network.cpp | 3 ++- GpsTracker/NetworkPositionsBackup.cpp | 19 +++++++++---------- GpsTracker/Positions.cpp | 7 ++----- GpsTracker/Rtc.cpp | 2 +- 9 files changed, 20 insertions(+), 25 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 6f0f7d5..4948580 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -13,6 +13,7 @@ namespace config { void read() { VERBOSE("read"); + hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right @@ -53,6 +54,7 @@ namespace config { void reset() { VERBOSE("reset"); + config_t config = { CONFIG_SEED, VERSION, diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 74135e0..61c8670 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -11,7 +11,6 @@ namespace core { uint8_t increaseInARow = 0; void main() { - VERBOSE("main"); PositionEntryMetadata metadata; if (positions::acquire(metadata)) { diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index 05aba7d..f2211a6 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -68,6 +68,6 @@ namespace gps { time.Minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2); time.Second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2); - VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); + NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); } } \ No newline at end of file diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 230c974..a5fb4a9 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -42,7 +42,7 @@ namespace hardware { } void setup() { - VERBOSE("setup"); + NOTICE("setup"); simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); powerOff(); //ensure powerOff on start @@ -55,7 +55,7 @@ namespace hardware { } void gpsPowerOff() { - VERBOSE("gpsPowerOff"); + NOTICE("gpsPowerOff"); device.disableGps(); powerOffIfUnused(); } @@ -80,11 +80,8 @@ namespace hardware { namespace i2c { E24 eeprom = E24(E24Size_t::E24_512K); - uint8_t poweredCount = 0; - //inline void powered() { digitalRead(I2C_PWR) == HIGH; } //TODO = replace enum with just reading the output pin ? - void powerOn() { if (!poweredCount) { VERBOSE("powerOn"); diff --git a/GpsTracker/MainUnit.cpp b/GpsTracker/MainUnit.cpp index 80b3448..e72e2aa 100644 --- a/GpsTracker/MainUnit.cpp +++ b/GpsTracker/MainUnit.cpp @@ -17,7 +17,7 @@ namespace mainunit { void wokeUp() { tmElements_t wokeUpTime; rtc::getTime(wokeUpTime); - NOTICE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.Hour, wokeUpTime.Minute, wokeUpTime.Second); + VERBOSE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.Hour, wokeUpTime.Minute, wokeUpTime.Second); hardware::sim808::simSerial.listen(); } diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 3acc65b..878c470 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -26,7 +26,7 @@ namespace network { if (report.ssri < NETWORK_DEFAULT_NO_NETWORK_QUALIRY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { - VERBOSE_MSG("waitForRegistered", "No signal"); + NOTICE_MSG("waitForRegistered", "No reliable signal"); break; //after a while, not network really means no network. Bailing out } @@ -34,6 +34,7 @@ namespace network { timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); + NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); return currentStatus; } diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 28fb063..215cd76 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -25,8 +25,6 @@ namespace positions { } bool appendPosition(PositionEntry &entry) { - VERBOSE("appendPosition"); - char buffer[BUFFER_SIZE]; snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,%d,%d,", debug::freeRam(), @@ -38,18 +36,20 @@ namespace positions { strcat(buffer, entry.position); - return hardware::sim808::device.httpPost( + NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); + uint16_t responseCode = hardware::sim808::device.httpPost( config::main::value.network.url, F("text/csv"), buffer, buffer, BUFFER_SIZE ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; + + NOTICE_FORMAT("appendPosition", "Response : %d", responseCode); + return responseCode; } void appendPositions() { - VERBOSE("appendPositions"); - uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; @@ -57,13 +57,12 @@ namespace positions { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); - else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); + if (!network::isAvailable(networkStatus.stat)) NOTICE_MSG("appendPositions", "network unavailable"); + else if (!network::enableGprs()) NOTICE_MSG("appendPositions", "gprs unavailable"); else { hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; - if (!appendPosition(currentEntry)) break; config::main::value.network.lastSavedEntry = currentEntryIndex; @@ -79,11 +78,11 @@ namespace positions { } void NetworkPositionsBackup::setup() { - VERBOSE("setup"); + NOTICE("setup"); } void NetworkPositionsBackup::backup() { - VERBOSE("backup"); + NOTICE("backup"); if (!details::isBackupNeeded()) return; details::appendPositions(); diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 63f4999..707cc9a 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -64,7 +64,7 @@ namespace positions { SIM808ChargingStatus battery = hardware::sim808::device.getChargingState(); gps::powerOff(); - NOTICE_FORMAT("acquire", "status : %d", gpsStatus); + NOTICE_FORMAT("acquire", "Status : %d", gpsStatus); if (gpsStatus < SIM808_GPS_STATUS::FIX) return false; @@ -124,7 +124,7 @@ namespace positions { hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + NOTICE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); return true; } @@ -146,10 +146,7 @@ namespace positions { void doBackup() { #ifdef BACKUPS_ENABLED - VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); - for (int i = 0; i < BACKUPS_ENABLED; i++) { - VERBOSE_FORMAT("doBackup", "calling backup %d", i); _backups[i]->backup(); } #endif diff --git a/GpsTracker/Rtc.cpp b/GpsTracker/Rtc.cpp index 1f58059..26d8b45 100644 --- a/GpsTracker/Rtc.cpp +++ b/GpsTracker/Rtc.cpp @@ -80,7 +80,7 @@ namespace rtc { RTC.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON - VERBOSE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.Hour, time.Minute, time.Second); + NOTICE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.Hour, time.Minute, time.Second); hardware::i2c::powerOff(); } From 6a6b6063a24197cb0980a9c6bbfc089bb81a84ea Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 21:18:07 +1300 Subject: [PATCH 55/57] Removed debug menu SD_WRITE_TEST as sd card support will probably never make it back --- GpsTracker/Debug.cpp | 6 ------ GpsTracker/Debug.h | 1 - GpsTracker/GpsTracker.ino | 1 - 3 files changed, 8 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 79cc86f..ef8a2d0 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -22,7 +22,6 @@ MENU_ENTRY(GPS_GET, "[L] Get GPS position"); MENU_ENTRY(GPS_SET, "[l] Set last GPS position"); MENU_ENTRY(RTC_SET, "[T] Get RTC time"); MENU_ENTRY(RTC_GET, "[t] Set RTC time"); -MENU_ENTRY(SD_WRITE_TEST, "[W] Write to test file"); MENU_ENTRY(EEPROM_GET_CONFIG, "[C] Get EEPROM config"); MENU_ENTRY(EEPROM_RESET_CONFIG, "[c] Reset EEPROM config"); MENU_ENTRY(EEPROM_GET_CONTENT, "[E] Get EEPROM content"); @@ -45,7 +44,6 @@ const PROGMEM uint8_t commandIdMapping[] = { 'l', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::GPS_SET), 'T', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::RTC_GET), 't', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::RTC_SET), - 'W', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SD_WRITE_TEST), 'C', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONFIG), 'c', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_RESET_CONFIG), 'E', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_CONTENT), @@ -81,10 +79,6 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_SEPARATOR, - MENU_SD_WRITE_TEST, - - MENU_SEPARATOR, - MENU_EEPROM_GET_CONFIG, MENU_EEPROM_RESET_CONFIG, MENU_EEPROM_GET_CONTENT, diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 71644bc..ee3f48f 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -24,7 +24,6 @@ namespace debug { GPS_SET, RTC_GET, RTC_SET, - SD_WRITE_TEST, EEPROM_GET_CONFIG, EEPROM_RESET_CONFIG, EEPROM_GET_CONTENT, diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 2a1ec23..9b44f41 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -86,7 +86,6 @@ void loop() { case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP: mainunit::deepSleep(10); break; - case debug::GPSTRACKER_DEBUG_COMMAND::SD_WRITE_TEST: default: NOTICE_MSG("loop", "Unsupported"); } From b75f48671cb2563155b1cb8dfbd9cb5e15ce6d3a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 21:19:30 +1300 Subject: [PATCH 56/57] Fixed constant string being declared in RAM --- GpsTracker/Logging.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Logging.cpp b/GpsTracker/Logging.cpp index b7c1d7d..7c75bbf 100644 --- a/GpsTracker/Logging.cpp +++ b/GpsTracker/Logging.cpp @@ -11,7 +11,7 @@ namespace logging { Serial.begin(LOG_SERIAL_SPEED); Log.begin(LOG_LEVEL, &Serial); - Log.notice("Starting...\n"); + Log.notice(F("Starting...\n")); } } From c24b2c18e042e55f176b77d148041fc07ae6464b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 24 Mar 2018 21:21:07 +1300 Subject: [PATCH 57/57] Added some log to acquireCurrentPosition --- GpsTracker/Gps.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/GpsTracker/Gps.cpp b/GpsTracker/Gps.cpp index f2211a6..e0173ba 100644 --- a/GpsTracker/Gps.cpp +++ b/GpsTracker/Gps.cpp @@ -42,6 +42,7 @@ namespace gps { hardware::sim808::device.getGpsPosition(lastPosition); } + NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); return currentStatus; }