Skip to content

Commit

Permalink
Add saving BPM on timed reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Jan 5, 2020
1 parent 283697d commit 7e19740
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/bubbles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,7 @@ float Bubbles::getAvgBpm() {
}
return(avg);
}

void Bubbles::setLast(double last) {
bubAvg.push(last);
}
1 change: 1 addition & 0 deletions src/bubbles.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Bubbles {
float getAvgVessel();
float getAvgBpm();
bool doBub;
void setLast(double); // Push last reading on reboot
};

#endif // _BUBBLES_H
8 changes: 3 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void setup() {
NtpHandler *ntpTime = NtpHandler::getInstance();
ntpTime->start();

execspiffs(); // Check for pending SPIFFS update
execspiffs(); // Check for pending SPIFFS update
loadBpm() ; // Get last Bpm reading if it was a controlled reboot

Log.notice(F("Started %s version %s (%s) [%s]." CR), API_KEY, version(), branch(), build());
}
Expand Down Expand Up @@ -94,10 +95,7 @@ void loop() {
// Reboot timer - I wish controllers could be counted on to be more
// stable but at least it only takes a few seconds.
Ticker rebootTimer;
rebootTimer.attach(REBOOTTIMER, [](){
Log.notice(F("Reboot timer - rebooting system."));
ESP.restart();
});
rebootTimer.attach(REBOOTTIMER, reboot);

while (true) {

Expand Down
61 changes: 61 additions & 0 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,64 @@ void _delay(unsigned long ulDelay) {
yield();
}
}

void reboot() {
Log.notice(F("Reboot timer - rebooting system." CR));
saveBpm();
ESP.restart();
}

void loadBpm() {
Bubbles *bubble = Bubbles::getInstance();
const size_t capacity = JSON_OBJECT_SIZE(1) + 10;
DynamicJsonDocument doc(capacity);
const char * bpmFileName = "lastBpm.json";

// Mount SPIFFS
if (!SPIFFS.begin()) {
Log.error(F("CONFIG: Failed to mount SPIFFS." CR));
return;
}

// Open file for reading
File file = SPIFFS.open(bpmFileName, "r");
if (!SPIFFS.exists(bpmFileName) || !file) {
Log.notice(F("No lastBpm available." CR));
} else {
// Parse the JSON object in the file
DeserializationError err = deserializeJson(doc, file);
if (err) {
Log.error(F("Failed to deserialize lastBpm." CR));
Log.error(err.c_str());
} else {
bubble->setLast(doc["lastBpm"]);
Log.notice(F("Loaded lastBpm." CR));
}
// Delete file
SPIFFS.remove(bpmFileName);
}
}

void saveBpm() {
Bubbles *bubble = Bubbles::getInstance();
const size_t capacity = JSON_OBJECT_SIZE(1) + 10;
DynamicJsonDocument doc(capacity);
const char * bpmFileName = "lastBpm.json";

doc["lastBpm"] = bubble->getAvgBpm();

// Open file for writing
File file = SPIFFS.open(bpmFileName, "w");
if (!file) {
Log.error(F("Failed to open lastBpm file." CR));
} else {
// Serialize the JSON object to the file
bool success = serializeJson(doc, file);
// This may fail if the JSON is invalid
if (!success) {
Log.error(F("Failed to serialize lastBpm." CR));
} else {
Log.verbose(F("Saved lastBpm." CR), bpmFileName);
}
}
}
3 changes: 3 additions & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ SOFTWARE. */
#define EEPROM_ADDRESS 0x00

void _delay(unsigned long);
void reboot();
void loadBpm();
void saveBpm();

#endif

0 comments on commit 7e19740

Please sign in to comment.