Skip to content

Commit

Permalink
Merge in configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Mar 21, 2020
1 parent bb064c5 commit 3f377b4
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 163 deletions.
5 changes: 2 additions & 3 deletions src/bftarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ BFTarget* BFTarget::single = NULL;
BFTarget* BFTarget::getInstance() {
if (!single) {
single = new BFTarget();
single->config = JsonConfig::getInstance();
single->target = new PushTarget;
single->target->ip = INADDR_NONE;

Expand Down Expand Up @@ -70,9 +69,9 @@ BFTarget* BFTarget::getInstance() {

bool BFTarget::push() {
Log.verbose(F("Triggered %s push." CR), single->target->target.name);
if (single->target->apiName.enabled && sizeof(single->config->bfkey)) { // Key target is unique per target (for now)
if (single->target->apiName.enabled && sizeof(config.brewersfriend.key)) { // Key target is unique per target (for now)
single->target->target.enabled = true;
strlcpy(single->target->key.name, single->config->bfkey, sizeof(single->config->bfkey)); // Key target is unique per target (for now)
strlcpy(single->target->key.name, config.brewersfriend.key, sizeof(config.brewersfriend.key)); // Key target is unique per target (for now)
} else {
single->target->target.enabled = false;
strlcpy(single->target->key.name, "", sizeof(""));
Expand Down
3 changes: 2 additions & 1 deletion src/bftarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class BFTarget {
static BFTarget *single;
// External Declarations
PushTarget *target;
JsonConfig *config;
// Private Methods

// Private Properties
Expand Down Expand Up @@ -96,4 +95,6 @@ class BFTarget {

};

extern struct Config config;

#endif // _BFTARGET_H
1 change: 0 additions & 1 deletion src/bubbles.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ SOFTWARE. */

#include "config.h"
#include "sensors.h"
#include "jsonconfig.h"
#include "ntp.h"
#include <ArduinoLog.h>
#include <CircularBuffer.h>
Expand Down
46 changes: 22 additions & 24 deletions src/execota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ SOFTWARE. */
#include "execota.h"

void execfw() {
JsonConfig *config = JsonConfig::getInstance();
Log.notice(F("Starting the Firmware OTA pull, will reboot without notice." CR));
_delay(5000); // Let page finish loading

// Stop web server before OTA update - will restart on reset
stopWebServer();

// Have to set this here because we have no chance after update
config->dospiffs1 = true;
config->dospiffs2 = false;
config->didupdate = false;
config->save();
config.dospiffs1 = true;
config.dospiffs2 = false;
config.didupdate = false;
saveConfig();

ESPhttpUpdate.setLedPin(LED, LOW);
// "http://www.brewbubbles.com/firmware/firmware.bin"
Expand All @@ -45,19 +44,19 @@ void execfw() {
case HTTP_UPDATE_FAILED:
Log.error(F("HTTP Firmware OTA Update failed error (%d): %s" CR), ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
// Don't allow anything to proceed
config->dospiffs1 = false;
config->dospiffs2 = false;
config->didupdate = false;
config->save();
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = false;
saveConfig();
break;

case HTTP_UPDATE_NO_UPDATES:
Log.notice(F("HTTP Firmware OTA Update: No updates." CR));
// Don't allow anything to proceed
config->dospiffs1 = false;
config->dospiffs2 = false;
config->didupdate = false;
config->save();
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = false;
saveConfig();
break;

case HTTP_UPDATE_OK:
Expand All @@ -71,17 +70,16 @@ void execfw() {
}

void execspiffs() {
JsonConfig *config = JsonConfig::getInstance(); // Must instantiate the config to save later
if (config->dospiffs1) {
if (config.dospiffs1) {
Log.notice(F("Rebooting a second time before SPIFFS OTA pull." CR));
config->dospiffs1 = false;
config->dospiffs2 = true;
config->didupdate = false;
config->save();
config.dospiffs1 = false;
config.dospiffs2 = true;
config.didupdate = false;
saveConfig();
_delay(3000);
ESP.restart();
_delay(1000);
} else if (config->dospiffs2) {
} else if (config.dospiffs2) {
Log.notice(F("Starting the SPIFFS OTA pull." CR));

// Stop web server before OTA update - will restart on reset
Expand All @@ -103,10 +101,10 @@ void execspiffs() {

case HTTP_UPDATE_OK:
// Reset SPIFFS update flag
config->dospiffs1 = false;
config->dospiffs2 = false;
config->didupdate = true;
config->save(); // This not only saves the flags, it (re)saves the whole config after SPIFFS wipes it
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = true;
saveConfig(); // This not only saves the flags, it (re)saves the whole config after SPIFFS wipes it
_delay(1000);
Log.notice(F("HTTP SPIFFS OTA Update complete, restarting." CR));
ESP.restart();
Expand Down
3 changes: 3 additions & 0 deletions src/execota.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ SOFTWARE. */
void execfw();
void execspiffs();

extern bool saveConfig();
extern struct Config config;

#endif //_EXECOTA_H
27 changes: 16 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ DoubleResetDetect drd(DRD_TIMEOUT, DRD_ADDRESS);
void setup() {
bool rst = drd.detect(); // Check for double-reset
serial();

if (loadConfig())
Log.notice(F("Configuration loaded." CR));
else
Log.error(F("Unable to load cofiguration." CR));

pinMode(LED, OUTPUT);

_delay(200); // Let pins settle, else detect is inconsistent
Expand All @@ -52,7 +58,6 @@ void setup() {
}

void loop() {
JsonConfig *config = JsonConfig::getInstance();
Bubbles *bubble = Bubbles::getInstance();

// Bubble loop to create 60 second readings
Expand All @@ -61,11 +66,11 @@ void loop() {

// Target timer
Ticker urlTarget;
urlTarget.attach(config->targetfreq * 60, setDoURLTarget);
urlTarget.attach(config.urltarget.freq * 60, setDoURLTarget);

// Brewer's friend timer
Ticker bfTimer;
bfTimer.attach(config->bffreq * 60, setDoBFTarget);
bfTimer.attach(config.brewersfriend.freq * 60, setDoBFTarget);

// mDNS Reset Timer - Helps avoid the host not found issues
Ticker mDNSTimer;
Expand All @@ -84,17 +89,17 @@ void loop() {
MDNS.update(); // Handle mDNS requests

// If target frequencies needs to be updated, update here
if (config->updateTargetFreq) {
Log.notice(F("Resetting URL Target frequency timer to %l minutes." CR), config->targetfreq);
if (config.urltarget.update) {
Log.notice(F("Resetting URL Target frequency timer to %l minutes." CR), config.urltarget.freq);
urlTarget.detach();
urlTarget.attach(config->targetfreq * 60, setDoURLTarget);
config->updateTargetFreq = false;
urlTarget.attach(config.urltarget.freq * 60, setDoURLTarget);
config.urltarget.update = false;
}
if (config->updateBFFreq) {
Log.notice(F("Resetting Brewer's Friend frequency timer to %l minutes." CR), config->bffreq);
if (config.brewersfriend.update) {
Log.notice(F("Resetting Brewer's Friend frequency timer to %l minutes." CR), config.brewersfriend.freq);
bfTimer.detach();
bfTimer.attach(config->bffreq * 60, setDoBFTarget);
config->updateBFFreq = false;
bfTimer.attach(config.brewersfriend.freq * 60, setDoBFTarget);
config.brewersfriend.update = false;
}

yield();
Expand Down
8 changes: 6 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ SOFTWARE. */
#ifndef _MAIN_H
#define _MAIN_H

#include "bubserial.h"
#include "serial_setup.h"
#include "config.h"
#include "execota.h"
#include "jsonconfig.h"
#include "webserver.h"
#include "wifi.h"
#include "version.h"
#include "pushtarget.h"
#include "urltarget.h"
#include "target.h"
#include "bftarget.h"
#include "pushhelper.h"
#include "bubbles.h"
Expand All @@ -49,4 +49,8 @@ SOFTWARE. */
#define DRD_TIMEOUT 3.0
#define DRD_ADDRESS 0x00

extern struct Config config;
extern bool loadConfig();
extern const char *filename;

#endif // _MAIN_H
5 changes: 2 additions & 3 deletions src/mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ SOFTWARE. */
#include "mdns.h"

void mdnssetup() {
JsonConfig *config = JsonConfig::getInstance();
if (!MDNS.begin(config->hostname)) { // Start the mDNS responder
if (!MDNS.begin(config.hostname)) { // Start the mDNS responder
Log.error(F("Error setting up mDNS responder." CR));
} else {
Log.notice(F("mDNS responder started for %s.local." CR), config->hostname);
Log.notice(F("mDNS responder started for %s.local." CR), config.hostname);
if (!MDNS.addService("http", "tcp", PORT)) {
Log.error(F("Failed to register mDNS service." CR));
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ SOFTWARE. */
void mdnssetup();
void mdnsreset();

extern struct Config config;

#endif // _MDNS_H
9 changes: 4 additions & 5 deletions src/pushhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ bool pushToTarget(PushTarget *target, IPAddress targetIP, int port) {
lcburl.setUrl(String(target->url) + String(target->key.name));

Bubbles *bubble = Bubbles::getInstance();
JsonConfig *config = JsonConfig::getInstance();
const size_t capacity = JSON_OBJECT_SIZE(8) + 210;
StaticJsonDocument<capacity> doc;

if (target->apiName.enabled) doc[target->apiName.name] = F(API_KEY);
if (target->bubName.enabled) doc[target->bubName.name] = config->bubname;
if (target->bubName.enabled) doc[target->bubName.name] = config.bubble.name;
if (target->bpm.enabled) doc[target->bpm.name] = bubble->getAvgBpm();
if (target->ambientTemp.enabled) doc[target->ambientTemp.name] = bubble->getAvgAmbient();
if (target->vesselTemp.enabled) doc[target->vesselTemp.name] = bubble->getAvgVessel();
if (target->tempFormat.enabled) {
if (config->tempinf == true) doc[target->tempFormat.name] = F("F");
if (config.bubble.tempinf == true) doc[target->tempFormat.name] = F("F");
else doc[target->tempFormat.name] = F("C");
}
String json;
Expand Down Expand Up @@ -162,7 +161,7 @@ void setDoBRFTarget() {

void tickerLoop() {
Bubbles *bubble = Bubbles::getInstance();
URLTarget *urlTarget = URLTarget::getInstance();
Target *target = Target::getInstance();
BFTarget *bfTarget = BFTarget::getInstance();
// BRFTarget *brfTarget = BRFTarget::getInstance();

Expand All @@ -171,7 +170,7 @@ void tickerLoop() {
// Do URL Target post
if (doURLTarget) {
doURLTarget = false;
urlTarget->push();
target->push();
}
//
// Do Brewer's Friend Post
Expand Down
2 changes: 1 addition & 1 deletion src/pushhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SOFTWARE. */

#include "bubbles.h"
#include "pushtarget.h"
#include "urltarget.h"
#include "target.h"
#include "bftarget.h"
//#include "brftarget.h"
#include <ESP8266WiFi.h>
Expand Down
11 changes: 5 additions & 6 deletions src/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ SOFTWARE. */

double getTemp(uint8_t pin) {
float retVal;
JsonConfig *config = JsonConfig::getInstance();
OneWire oneWire(pin);
DS18B20 sensor(&oneWire);
sensor.begin();
Expand All @@ -33,23 +32,23 @@ double getTemp(uint8_t pin) {
while (!sensor.isConversionComplete());
retVal = sensor.getTempC();

if (config->tempinf) {
if (config.bubble.tempinf) {
retVal = sensor.getTempF();
if (retVal == float(DEVICE_DISCONNECTED_F)) {
retVal = -100.0;
} else if (pin == AMBSENSOR) {
retVal = retVal + config->calAmbient;
retVal = retVal + config.calibrate.room;
} else if (pin == VESSENSOR) {
retVal = retVal + config->calVessel;
retVal = retVal + config.calibrate.vessel;
}
} else {
retVal = sensor.getTempC();
if (retVal == float(DEVICE_DISCONNECTED_C)) {
retVal = -100.0;
} else if (pin == AMBSENSOR) {
retVal = retVal + config->calAmbient;
retVal = retVal + config.calibrate.room;
} else if (pin == VESSENSOR) {
retVal = retVal + config->calVessel;
retVal = retVal + config.calibrate.vessel;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ SOFTWARE. */

double getTemp(uint8_t);

extern struct Config config;

#endif // _SENSORS_H
16 changes: 0 additions & 16 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ SOFTWARE. */

#include "tools.h"

bool ipl() { // Determine if this is the first start after loading image
char thisver[20] = __DATE__ __TIME__; // Sets at compile-time
char savever[20] = "";
bool _ipl = false;

EEPROM.begin(20);
EEPROM.get(EEPROM_ADDRESS, savever);
if (strcmp (thisver, savever) != 0) {
EEPROM.put(EEPROM_ADDRESS, thisver);
EEPROM.commit();
_ipl = true;
}
EEPROM.end();
return _ipl;
}

void _delay(unsigned long ulDelay) {
// Safe blocking delay() replacement with yield()
unsigned long ulNow = millis();
Expand Down
1 change: 0 additions & 1 deletion src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SOFTWARE. */
#ifndef _TOOLS_H
#define _TOOLS_H

// #include "wifi.h"
#include "bubbles.h"
#include <FS.h>
#include <ArduinoLog.h>
Expand Down
Loading

0 comments on commit 3f377b4

Please sign in to comment.