-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reading of hardware/deployment specific configuration node_conf…
…ig.json from LittleFS
- Loading branch information
1 parent
e59c7fd
commit ff42d2d
Showing
4 changed files
with
225 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"timezone": "CET-1CEST,M3.5.0,M10.5.0/3", | ||
"battery_weak": 3301, | ||
"battery_low": 3401, | ||
"battery_discharge_lim": 3201, | ||
"battery_charge_lim": 4201, | ||
"battery_capacity": 2201 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// LoadNodeCfg.cpp | ||
// | ||
// Load LoRaWAN node configuration 'node_config.json' from LittleFS, if available | ||
// | ||
// This configuration file is intended for hardware/deployment environment | ||
// specific settings (e.g. battery voltage levels, timezone) | ||
// | ||
// created: 07/2024 | ||
// | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Matthias Prinke | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
// | ||
// History: | ||
// | ||
// 20240725 Created | ||
// | ||
// ToDo: | ||
// - | ||
// | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "LoadNodeCfg.h" | ||
|
||
// Load LoRaWAN node configuration 'node_config.json' from LittleFS, if available | ||
void loadNodeCfg( | ||
String &tzinfo, | ||
uint16_t &batt_weak, | ||
uint16_t &batt_low, | ||
uint16_t &batt_discharge_lim, | ||
uint16_t &batt_charge_lim, | ||
uint16_t &batt_capacity) | ||
{ | ||
uint16_t foo = 42; | ||
|
||
if (!LittleFS.begin( | ||
#if defined(ESP32) | ||
// Format the LittleFS partition on error; parameter only available for ESP32 | ||
true | ||
#endif | ||
)) | ||
{ | ||
log_d("Could not initialize LittleFS."); | ||
} | ||
else | ||
{ | ||
File file = LittleFS.open("/node_config.json", "r"); | ||
|
||
if (!file) | ||
{ | ||
log_i("File 'node_config.json' not found."); | ||
} | ||
else | ||
{ | ||
log_d("Reading 'node_config.json'"); | ||
JsonDocument doc; | ||
|
||
// Deserialize the JSON document | ||
DeserializationError error = deserializeJson(doc, file); | ||
if (error) | ||
{ | ||
log_d("Failed to read JSON file, using defaults."); | ||
} | ||
else | ||
{ | ||
if (doc.containsKey("timezone")) | ||
tzinfo = doc["timezone"].as<String>(); | ||
if (doc.containsKey("battery_weak")) | ||
batt_weak = doc["battery_weak"]; | ||
if (doc.containsKey("battery_low")) | ||
batt_low = doc["battery_low"]; | ||
if (doc.containsKey("battery_discharge_lim")) | ||
batt_discharge_lim = doc["battery_discharge_lim"]; | ||
if (doc.containsKey("battery_charge_lim")) | ||
batt_charge_lim = doc["battery_charge_lim"]; | ||
if (doc.containsKey("battery_capacity")) | ||
batt_capacity = doc["battery_capacity"]; | ||
} // deserializeJson o.k. | ||
} // file read o.k. | ||
file.close(); | ||
} // LittleFS o.k. | ||
|
||
log_d("Timezone: %s", tzinfo.c_str()); | ||
log_d("Battery weak: %4d mV", batt_weak); | ||
log_d("Battery low: %4d mV", batt_low); | ||
log_d("Battery discharge limit: %4d mV", batt_discharge_lim); | ||
log_d("Battery charge limit: %4d mV", batt_charge_lim); | ||
log_d("Battery capacity: %4d mAh", batt_capacity); | ||
} // loadNodeCfg() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// LoadNodeCfg.cpp | ||
// | ||
// Load LoRaWAN node configuration 'node_config.json' from LittleFS, if available | ||
// | ||
// This configuration file is intended for hardware/deployment environment | ||
// specific settings (e.g. battery voltage levels, timezone) | ||
// | ||
// created: 07/2024 | ||
// | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2024 Matthias Prinke | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
// | ||
// History: | ||
// | ||
// 20240725 Created | ||
// | ||
// ToDo: | ||
// - | ||
// | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include <Arduino.h> | ||
#include <LittleFS.h> | ||
#include <ArduinoJson.h> | ||
#include "logging.h" | ||
|
||
/*! | ||
* \brief Load LoRaWAN node configuration 'node_config.json' from LittleFS, if available | ||
* | ||
* Returns all values by reference. Keeps the original value(s) if file not found, | ||
* cannot be parsed or any value is missing. | ||
* | ||
* JSON file format: | ||
* { | ||
* "timezone": "CET-1CEST,M3.5.0,M10.5.0/3", | ||
* "battery_weak": 3300, | ||
* "battery_low": 3400, | ||
* "battery_discharge_lim": 3200, | ||
* "battery_charge_lim": 4200, | ||
* "battery_capacity": 2200 | ||
* } | ||
*/ | ||
void loadNodeCfg( | ||
String &tzinfo, | ||
uint16_t &batt_weak, | ||
uint16_t &batt_low, | ||
uint16_t &batt_discharge_lim, | ||
uint16_t &batt_charge_lim, | ||
uint16_t &batt_capacity); |