Skip to content

Commit

Permalink
Add nodrd flag to config
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Feb 13, 2021
1 parent 37ce69c commit bedbc99
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 28 deletions.
36 changes: 22 additions & 14 deletions json_models/Config JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"dospiffs1": false,
"dospiffs2": false,
"didupdate": false
"didupdate": false,
"nodrd": false
}
```

Expand All @@ -51,18 +52,24 @@ const size_t capacity = 3*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 3*JSON_OBJ
512+728 = 1240
```

## Parsing:
## Deserializing:

```
const size_t capacity = 3*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 3*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(11) + 810;
DynamicJsonDocument doc(capacity);
// char* input;
// size_t inputLength; (optional)
StaticJsonDocument<768> doc;
const char* json = "{\"apconfig\":{\"ssid\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"passphrase\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"},\"hostname\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"bubble\":{\"name\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"tempinf\":false},\"calibrate\":{\"room\":-999.9999,\"vessel\":-999.9999},\"urltarget\":{\"url\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"freq\":999,\"update\":false},\"brewersfriend\":{\"key\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"channel\":0,\"freq\":9999,\"update\":false},\"brewfather\":{\"key\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"channel\":0,\"freq\":9999,\"update\":false},\"thingspeak\":{\"key\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\",\"channel\":999999999,\"freq\":9999,\"update\":false},\"dospiffs1\":false,\"dospiffs2\":false,\"didupdate\":false}";
DeserializationError error = deserializeJson(doc, input, inputLength);
deserializeJson(doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* apconfig_ssid = doc["apconfig"]["ssid"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* apconfig_passphrase = doc["apconfig"]["passphrase"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* apconfig_passphrase = doc["apconfig"]["passphrase"];
const char* hostname = doc["hostname"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Expand All @@ -73,38 +80,38 @@ float calibrate_room = doc["calibrate"]["room"]; // -999.9999
float calibrate_vessel = doc["calibrate"]["vessel"]; // -999.9999
JsonObject urltarget = doc["urltarget"];
const char* urltarget_url = urltarget["url"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* urltarget_url = urltarget["url"];
int urltarget_freq = urltarget["freq"]; // 999
bool urltarget_update = urltarget["update"]; // false
JsonObject brewersfriend = doc["brewersfriend"];
const char* brewersfriend_key = brewersfriend["key"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* brewersfriend_key = brewersfriend["key"];
int brewersfriend_channel = brewersfriend["channel"]; // 0
int brewersfriend_freq = brewersfriend["freq"]; // 9999
bool brewersfriend_update = brewersfriend["update"]; // false
JsonObject brewfather = doc["brewfather"];
const char* brewfather_key = brewfather["key"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* brewfather_key = brewfather["key"];
int brewfather_channel = brewfather["channel"]; // 0
int brewfather_freq = brewfather["freq"]; // 9999
bool brewfather_update = brewfather["update"]; // false
JsonObject thingspeak = doc["thingspeak"];
const char* thingspeak_key = thingspeak["key"]; // "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const char* thingspeak_key = thingspeak["key"];
long thingspeak_channel = thingspeak["channel"]; // 999999999
int thingspeak_freq = thingspeak["freq"]; // 9999
bool thingspeak_update = thingspeak["update"]; // false
bool dospiffs1 = doc["dospiffs1"]; // false
bool dospiffs2 = doc["dospiffs2"]; // false
bool didupdate = doc["didupdate"]; // false
bool nodrd = doc["nodrd"]; // false
```

## Serializing:

```
const size_t capacity = 3*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 3*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(11);
DynamicJsonDocument doc(capacity);
DynamicJsonDocument doc(1536);
JsonObject apconfig = doc.createNestedObject("apconfig");
apconfig["ssid"] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Expand Down Expand Up @@ -144,6 +151,7 @@ thingspeak["update"] = false;
doc["dospiffs1"] = false;
doc["dospiffs2"] = false;
doc["didupdate"] = false;
doc["nodrd"] = false;
serializeJson(doc, Serial);
serializeJson(doc, output);
```
25 changes: 16 additions & 9 deletions src/execota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void execfw()
ESPhttpUpdate.setLedPin(LED, LOW);
// "http://www.brewbubbles.com/firmware/firmware.bin"
WiFiClient _client;
Log.verbose(F("Pulling Firmware from: %s" CR), F(FIRMWAREURL));
config.nodrd = true;
saveConfig();
t_httpUpdate_return ret = ESPhttpUpdate.update(_client, F(FIRMWAREURL), "0");

switch (ret)
Expand All @@ -48,7 +51,9 @@ void execfw()
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = false;
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
break;

Expand All @@ -58,16 +63,20 @@ void execfw()
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = false;
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
break;

case HTTP_UPDATE_OK:
// We should never actually reach this as the controller
// resets after OTA
Log.notice(F("HTTP Firmware OTA Update complete, restarting." CR));
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
_delay(1000);
break;
}
}
Expand All @@ -80,13 +89,10 @@ void execspiffs()
config.dospiffs1 = false;
config.dospiffs2 = true;
config.didupdate = false;
config.nodrd = true;
saveConfig();
_delay(3000);
if (LittleFS.begin())
if (LittleFS.remove("/drd.dat"))
Log.notice(F("Deleted DRD semaphore." CR));
_delay(100);
ESP.restart();
_delay(1000);
}
else if (config.dospiffs2)
{
Expand All @@ -98,6 +104,7 @@ void execspiffs()
ESPhttpUpdate.setLedPin(LED, LOW);
// "http://www.brewbubbles.com/firmware/spiffs.bin"
WiFiClient client;
Log.verbose(F("Pulling Filesystem from: %s" CR), F(LITTLEFSURL));
t_httpUpdate_return ret = ESPhttpUpdate.updateFS(client, F(LITTLEFSURL), "");

switch (ret)
Expand All @@ -115,11 +122,11 @@ void execspiffs()
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = true;
saveConfig(); // This not only saves the flags, it (re)saves the whole config after File System wipes it
_delay(1000);
config.nodrd = true;
Log.notice(F("HTTP File System OTA Update complete, restarting." CR));
saveConfig(); // This not only saves the flags, it (re)saves the whole config after File System wipes it
_delay(100);
ESP.restart();
_delay(1000);
break;
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/jsonconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ SOFTWARE. */
const char *filename = "/config.json";
Config config;

extern const size_t capacitySerial = 3 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 3 * JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(11);
extern const size_t capacityDeserial = capacitySerial + 810;
extern const size_t capacitySerial = 1536;
extern const size_t capacityDeserial = 768;

bool deleteConfigFile()
{
Expand Down Expand Up @@ -478,6 +478,15 @@ void Config::load(JsonObjectConst obj)
{
didupdate = obj["didupdate"];
}

if (obj["nodrd"].isNull())
{
nodrd = false;
}
else
{
nodrd = obj["nodrd"];
}
}

void Config::save(JsonObject obj) const
Expand All @@ -504,4 +513,6 @@ void Config::save(JsonObject obj) const
obj["dospiffs2"] = dospiffs2;
// Add dospiffs1 object
obj["didupdate"] = didupdate;
// Add nodrd object
obj["nodrd"] = nodrd;
}
1 change: 1 addition & 0 deletions src/jsonconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct Config
bool dospiffs1;
bool dospiffs2;
bool didupdate;
bool nodrd;

void load(JsonObjectConst);
void save(JsonObject) const;
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ void setup()
Log.notice(F("%s low, presenting portal." CR), stringify(RESETWIFI));
doWiFi(true);
}
else if (drd->detectDoubleReset())
else if (!config.nodrd && drd->detectDoubleReset())
{
Log.notice(F("DRD: Triggered, presenting portal." CR));
doWiFi(true);
}
else
{
Log.verbose(F("DRD: Normal boot." CR));
config.nodrd = false;
doWiFi();
}

Expand Down
2 changes: 1 addition & 1 deletion src/thingSpeakWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ int createChannel(
channelJSON = http.getString();

#ifdef PRINT_DEBUG_MESSAGES
Serial.println(F("DEBUG: Create ThingSpeak Channel results:"));
Serial.println(F("Create ThingSpeak Channel results:"));
Serial.println(httpCode); //Print HTTP return code
Serial.println(channelJSON); //Print request response payload
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ void resetController()
Log.notice(F("Reboot request - rebooting system." CR));
_delay(5000);
saveBpm();
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
}

Expand Down Expand Up @@ -137,6 +140,9 @@ void maintenanceLoop()
// The ms clock will rollover after ~49 days. To be on the safe side,
// restart the ESP after about 42 days to reset the ms clock.
Log.warning(F("Maintenance: Six week routine restart."));
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
}
if (lastNTPUpdate > NTPRESET)
Expand Down
7 changes: 6 additions & 1 deletion src/wifihandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ void doWiFi(bool dontUseStoredCreds)
_delay(3000);
digitalWrite(LED, HIGH);
Log.warning(F("Restarting." CR));
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
_delay(1000); // Just a hack to give it time to reset
}
else
{
Expand Down Expand Up @@ -178,6 +180,9 @@ void resetWifi()
blinker.detach(); // Turn off blinker
digitalWrite(LED, LOW); // Turn on LED
Log.notice(F("Restarting after clearing WiFi settings." CR));
config.nodrd = true;
saveConfig();
_delay(100);
ESP.restart();
}

Expand Down

0 comments on commit bedbc99

Please sign in to comment.