mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-25 07:43:04 +05:00
feat: Implement W5500 SPI ethernet support (#286)
* feat: implement feature gate & w5500 SPI ethernet * feat: eth conn restart handling, wifi-ap connection type, BLE sensor gate * fix: bring eth down when disabled * fix: disable WiFi scan when ethernet is active * feat: refactor uplink types into separate classes * fix: compare enum, not string * chore: remove dead code * fix: remove out of scope persistNow(), move w5500 to networksettings, add base for future ethernet implementations * fix: keep wifi armed while reconnecting, reset IP when moving from static to DHCP, refactor wifi loop to use return * fix: clear out connectionType string for enum
This commit is contained in:
1 parent
dfbd297f90
commit
f9a814ca11
25 files changed
+1801
-450
No files matched your search
+10
-1
@@ -25,6 +25,12 @@ public:
|
||||
}
|
||||
|
||||
bool publishDynamicSensor(Sensors::Settings& sSensor, Sensors::ValueType vType = Sensors::ValueType::PRIMARY, UnitSystem unit = UnitSystem::METRIC, bool enabledByDefault = true) {
|
||||
#if !USE_BLE
|
||||
if (sSensor.type == Sensors::Type::BLUETOOTH) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
// set device class & unit of measurement
|
||||
@@ -1178,7 +1184,10 @@ public:
|
||||
|
||||
bool publishNetworkRssi(bool enabledByDefault = true) {
|
||||
JsonDocument doc;
|
||||
doc[FPSTR(HA_AVAILABILITY)][FPSTR(HA_TOPIC)] = this->statusTopic.c_str();
|
||||
doc[FPSTR(HA_AVAILABILITY)][0][FPSTR(HA_TOPIC)] = this->statusTopic.c_str();
|
||||
doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_TOPIC)] = this->stateTopic.c_str();
|
||||
doc[FPSTR(HA_AVAILABILITY)][1][FPSTR(HA_VALUE_TEMPLATE)] = F("{{ iif(value_json.master.network.connectionType == 'wifi', 'online', 'offline') }}");
|
||||
doc[FPSTR(HA_AVAILABILITY_MODE)] = F("all");
|
||||
doc[FPSTR(HA_ENABLED_BY_DEFAULT)] = enabledByDefault;
|
||||
doc[FPSTR(HA_UNIQUE_ID)] = this->getUniqueIdWithPrefix(FPSTR(S_RSSI));
|
||||
doc[FPSTR(HA_DEFAULT_ENTITY_ID)] = this->getEntityIdWithPrefix(FPSTR(HA_ENTITY_SENSOR), FPSTR(S_RSSI));
|
||||
|
||||
+2
-1
@@ -102,7 +102,8 @@ protected:
|
||||
|
||||
vars.mqtt.connected = tMqtt->isConnected();
|
||||
vars.network.connected = network->isConnected();
|
||||
vars.network.rssi = network->isConnected() ? WiFi.RSSI() : 0;
|
||||
vars.network.rssi = network->getRssi();
|
||||
vars.network.connectionType = network->getUplink();
|
||||
|
||||
if (settings.system.logLevel >= TinyLogger::Level::SILENT && settings.system.logLevel <= TinyLogger::Level::VERBOSE) {
|
||||
if (Log.getLevel() != settings.system.logLevel) {
|
||||
|
||||
+170
-54
@@ -29,6 +29,7 @@ public:
|
||||
this->webServer = new WebServer(80);
|
||||
this->bufferedWebServer = new BufferedWebServer(this->webServer, 32u);
|
||||
this->dnsServer = new DNSServer();
|
||||
this->mDnsRetryTime = millis() - this->mDnsRetryInterval;
|
||||
}
|
||||
|
||||
~PortalTask() {
|
||||
@@ -56,6 +57,11 @@ protected:
|
||||
bool dnsServerEnabled = false;
|
||||
unsigned long webServerChangeState = 0;
|
||||
bool mDnsState = false;
|
||||
#if USE_ETHERNET
|
||||
NetworkMgr::Uplink mdnsConnectionType = NetworkMgr::Uplink::None;
|
||||
#endif
|
||||
unsigned long mDnsRetryTime = 0;
|
||||
const unsigned long mDnsRetryInterval = 5000;
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
const char* getTaskName() override {
|
||||
@@ -296,12 +302,14 @@ protected:
|
||||
networkSettings.staticConfig.gateway,
|
||||
networkSettings.staticConfig.subnet,
|
||||
networkSettings.staticConfig.dns
|
||||
);
|
||||
)
|
||||
->setEthernet(networkSettings.ethernet.enabled);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (!doc[FPSTR(S_SETTINGS)].isNull() && jsonToSettings(doc[FPSTR(S_SETTINGS)], settings)) {
|
||||
fsSettings.update();
|
||||
applyEthernetSettings();
|
||||
changed = true;
|
||||
}
|
||||
|
||||
@@ -315,14 +323,14 @@ protected:
|
||||
if (sensorId < 0 || sensorId > 255 || !Sensors::isValidSensorId(sensorId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (jsonToSensorSettings(sensorId, sensor.value(), Sensors::settings[sensorId])) {
|
||||
fsSensorsSettings.update();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
doc.clear();
|
||||
doc.shrinkToFit();
|
||||
|
||||
@@ -341,6 +349,7 @@ protected:
|
||||
|
||||
JsonDocument doc;
|
||||
networkSettingsToJson(networkSettings, doc);
|
||||
this->ethernetToJson(doc);
|
||||
doc.shrinkToFit();
|
||||
|
||||
this->bufferedWebServer->send(200, F("application/json"), doc);
|
||||
@@ -350,7 +359,7 @@ protected:
|
||||
if (this->isAuthRequired() && !this->isValidCredentials()) {
|
||||
return this->webServer->send(401);
|
||||
}
|
||||
|
||||
|
||||
if (vars.states.restarting) {
|
||||
return this->webServer->send(503);
|
||||
}
|
||||
@@ -376,12 +385,20 @@ protected:
|
||||
}
|
||||
|
||||
bool changed = jsonToNetworkSettings(doc, networkSettings);
|
||||
|
||||
// ethernet is only usable with a complete W5500 pin set
|
||||
if (!this->isEthernetConfigured() && networkSettings.ethernet.enabled) {
|
||||
networkSettings.ethernet.enabled = false;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
doc.clear();
|
||||
doc.shrinkToFit();
|
||||
|
||||
networkSettingsToJson(networkSettings, doc);
|
||||
this->ethernetToJson(doc);
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
||||
this->bufferedWebServer->send(changed ? 201 : 200, F("application/json"), doc);
|
||||
|
||||
if (changed) {
|
||||
@@ -399,7 +416,10 @@ protected:
|
||||
networkSettings.staticConfig.subnet,
|
||||
networkSettings.staticConfig.dns
|
||||
)
|
||||
->setEthernet(networkSettings.ethernet.enabled)
|
||||
->reconnect();
|
||||
|
||||
this->applyEthernetSettings();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -408,6 +428,12 @@ protected:
|
||||
return this->webServer->send(401);
|
||||
}
|
||||
|
||||
// scanning needs WiFi, which is not the uplink while ethernet carries the traffic
|
||||
if (network->getUplink() == NetworkMgr::Uplink::Ethernet) {
|
||||
this->webServer->send(409);
|
||||
return;
|
||||
}
|
||||
|
||||
auto apCount = WiFi.scanComplete();
|
||||
if (apCount <= 0) {
|
||||
if (apCount != WIFI_SCAN_RUNNING) {
|
||||
@@ -417,11 +443,11 @@ protected:
|
||||
WiFi.scanNetworks(true, true, true);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
this->webServer->send(404);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
JsonDocument doc;
|
||||
for (short int i = 0; i < apCount; i++) {
|
||||
const String& ssid = WiFi.SSID(i);
|
||||
@@ -454,7 +480,7 @@ protected:
|
||||
JsonDocument doc;
|
||||
settingsToJson(settings, doc);
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
||||
this->bufferedWebServer->send(200, F("application/json"), doc);
|
||||
});
|
||||
|
||||
@@ -462,7 +488,7 @@ protected:
|
||||
if (this->isAuthRequired() && !this->isValidCredentials()) {
|
||||
return this->webServer->send(401);
|
||||
}
|
||||
|
||||
|
||||
if (vars.states.restarting) {
|
||||
return this->webServer->send(503);
|
||||
}
|
||||
@@ -501,6 +527,7 @@ protected:
|
||||
doc.shrinkToFit();
|
||||
|
||||
fsSettings.update();
|
||||
applyEthernetSettings();
|
||||
tMqtt->resetPublishedSettingsTime();
|
||||
}
|
||||
});
|
||||
@@ -516,7 +543,7 @@ protected:
|
||||
if (this->webServer->hasArg(F("detailed"))) {
|
||||
detailed = this->webServer->arg(F("detailed")).toInt() > 0;
|
||||
}
|
||||
|
||||
|
||||
JsonDocument doc;
|
||||
for (uint8_t sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
|
||||
if (detailed) {
|
||||
@@ -530,7 +557,7 @@ protected:
|
||||
doc[sensorId] = Sensors::settings[sensorId].name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
doc.shrinkToFit();
|
||||
this->bufferedWebServer->send(200, F("application/json"), doc);
|
||||
});
|
||||
@@ -540,7 +567,7 @@ protected:
|
||||
if (this->isAuthRequired() && !this->isValidCredentials()) {
|
||||
return this->webServer->send(401);
|
||||
}
|
||||
|
||||
|
||||
if (!this->webServer->hasArg(F("id"))) {
|
||||
return this->webServer->send(400);
|
||||
}
|
||||
@@ -561,12 +588,12 @@ protected:
|
||||
doc.shrinkToFit();
|
||||
this->bufferedWebServer->send(200, F("application/json"), doc);
|
||||
});
|
||||
|
||||
|
||||
this->webServer->on(F("/api/sensor"), HTTP_POST, [this]() {
|
||||
if (this->isAuthRequired() && !this->isValidCredentials()) {
|
||||
return this->webServer->send(401);
|
||||
}
|
||||
|
||||
|
||||
if (vars.states.restarting) {
|
||||
return this->webServer->send(503);
|
||||
}
|
||||
@@ -623,7 +650,7 @@ protected:
|
||||
auto& sSettings = Sensors::settings[sensorId];
|
||||
sensorSettingsToJson(sensorId, sSettings, doc);
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
||||
this->bufferedWebServer->send(changed ? 201 : 200, F("application/json"), doc);
|
||||
}
|
||||
|
||||
@@ -674,13 +701,13 @@ protected:
|
||||
|
||||
varsToJson(vars, doc);
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
||||
this->bufferedWebServer->send(changed ? 201 : 200, F("application/json"), doc);
|
||||
|
||||
if (changed) {
|
||||
doc.clear();
|
||||
doc.shrinkToFit();
|
||||
|
||||
|
||||
tMqtt->resetPublishedVarsTime();
|
||||
}
|
||||
});
|
||||
@@ -690,6 +717,16 @@ protected:
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
// capabilities: only the flags this build actually has; the UI shows features on an explicit true
|
||||
auto docFeatures = doc[FPSTR(S_FEATURES)].to<JsonObject>();
|
||||
#if USE_ETHERNET
|
||||
docFeatures[FPSTR(S_ETHERNET)] = true;
|
||||
#endif
|
||||
|
||||
#if USE_BLE
|
||||
docFeatures[FPSTR(S_BLE)] = true;
|
||||
#endif
|
||||
|
||||
auto docSystem = doc[FPSTR(S_SYSTEM)].to<JsonObject>();
|
||||
docSystem[FPSTR(S_RESET_REASON)] = getResetReason();
|
||||
docSystem[FPSTR(S_UPTIME)] = millis() / 1000;
|
||||
@@ -698,9 +735,15 @@ protected:
|
||||
docNetwork[FPSTR(S_HOSTNAME)] = networkSettings.hostname;
|
||||
docNetwork[FPSTR(S_MAC)] = network->getStaMac();
|
||||
docNetwork[FPSTR(S_CONNECTED)] = isConnected;
|
||||
docNetwork[FPSTR(S_SSID)] = network->getStaSsid();
|
||||
docNetwork[FPSTR(S_SIGNAL_QUALITY)] = isConnected ? NetworkMgr::rssiToSignalQuality(network->getRssi()) : 0;
|
||||
docNetwork[FPSTR(S_CHANNEL)] = isConnected ? network->getStaChannel() : 0;
|
||||
docNetwork[FPSTR(S_CONNECTION_TYPE)] = network->getConnectionType();
|
||||
// SSID, signal and channel describe the WiFi uplink only: with ethernet up the radio is
|
||||
// parked and those readings are stale, so report them as not applicable rather than 0 dBm
|
||||
const bool wifiUplink = network->getUplink() == NetworkMgr::Uplink::Wifi;
|
||||
const char* staSsid = network->getStaSsid();
|
||||
|
||||
docNetwork[FPSTR(S_SSID)] = (wifiUplink && staSsid) ? staSsid : "";
|
||||
docNetwork[FPSTR(S_SIGNAL_QUALITY)] = network->getSignalQuality();
|
||||
docNetwork[FPSTR(S_CHANNEL)] = wifiUplink ? network->getStaChannel() : 0;
|
||||
docNetwork[FPSTR(S_IP)] = isConnected ? network->getStaIp().toString() : "";
|
||||
docNetwork[FPSTR(S_SUBNET)] = isConnected ? network->getStaSubnet().toString() : "";
|
||||
docNetwork[FPSTR(S_GATEWAY)] = isConnected ? network->getStaGateway().toString() : "";
|
||||
@@ -727,7 +770,7 @@ protected:
|
||||
docHeap[FPSTR(S_MIN_FREE)] = getFreeHeap(true);
|
||||
docHeap[FPSTR(S_MAX_FREE_BLOCK)] = getMaxFreeBlockHeap();
|
||||
docHeap[FPSTR(S_MIN_MAX_FREE_BLOCK)] = getMaxFreeBlockHeap(true);
|
||||
|
||||
|
||||
auto docChip = doc[FPSTR(S_CHIP)].to<JsonObject>();
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
docChip[FPSTR(S_MODEL)] = esp_is_8285() ? F("ESP8285") : F("ESP8266");
|
||||
@@ -766,6 +809,9 @@ protected:
|
||||
this->webServer->on(F("/api/debug"), HTTP_GET, [this]() {
|
||||
JsonDocument doc;
|
||||
|
||||
auto docNetwork = doc[FPSTR(S_NETWORK)].to<JsonObject>();
|
||||
docNetwork[FPSTR(S_CONNECTION_TYPE)] = network->getConnectionType();
|
||||
|
||||
auto docBuild = doc[FPSTR(S_BUILD)].to<JsonObject>();
|
||||
docBuild[FPSTR(S_VERSION)] = BUILD_VERSION;
|
||||
docBuild[FPSTR(S_DATE)] = __DATE__ " " __TIME__;
|
||||
@@ -787,7 +833,7 @@ protected:
|
||||
docHeap[FPSTR(S_MIN_FREE)] = getFreeHeap(true);
|
||||
docHeap[FPSTR(S_MAX_FREE_BLOCK)] = getMaxFreeBlockHeap();
|
||||
docHeap[FPSTR(S_MIN_MAX_FREE_BLOCK)] = getMaxFreeBlockHeap(true);
|
||||
|
||||
|
||||
auto docChip = doc[FPSTR(S_CHIP)].to<JsonObject>();
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
docChip[FPSTR(S_MODEL)] = esp_is_8285() ? F("ESP8285") : F("ESP8266");
|
||||
@@ -846,7 +892,7 @@ protected:
|
||||
docCrash[FPSTR(S_EPC)] = epcStr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
char filename[64];
|
||||
@@ -892,54 +938,31 @@ protected:
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
this->delay(250);
|
||||
#endif
|
||||
|
||||
|
||||
this->startWebServer();
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Started: AP up or STA connected"));
|
||||
|
||||
// Enabling mDNS
|
||||
if (!this->mDnsState && settings.portal.mdns) {
|
||||
if (MDNS.begin(networkSettings.hostname)) {
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
this->mDnsState = true;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS enabled and service added"));
|
||||
}
|
||||
}
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Started: AP up or network connected"));
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::optimistic_yield(1000);
|
||||
#endif
|
||||
|
||||
} else if (this->stateWebServer() && !network->isApEnabled() && !network->isStaEnabled()) {
|
||||
} else if (this->stateWebServer() && !network->isApEnabled() && !network->isConnected()) {
|
||||
this->stopWebServer();
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Stopped: AP and STA down"));
|
||||
|
||||
// Disabling mDNS
|
||||
if (this->mDnsState) {
|
||||
MDNS.end();
|
||||
this->mDnsState = false;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS disabled"));
|
||||
}
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Stopped: no network"));
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::optimistic_yield(1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Disabling mDNS if disabled in settings
|
||||
if (this->mDnsState && !settings.portal.mdns) {
|
||||
MDNS.end();
|
||||
this->mDnsState = false;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS disabled"));
|
||||
}
|
||||
// mDNS
|
||||
this->loopMdns();
|
||||
|
||||
// dns server
|
||||
if (!this->stateDnsServer() && !network->isConnected() && network->isApEnabled() && this->stateWebServer()) {
|
||||
this->startDnsServer();
|
||||
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Started: AP up"));
|
||||
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::optimistic_yield(1000);
|
||||
#endif
|
||||
@@ -982,6 +1005,99 @@ protected:
|
||||
return !network->isApEnabled() && settings.portal.auth && strlen(settings.portal.password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Keep mDNS in sync with the active uplink
|
||||
* @details The responder binds its records to the interface that is up when it starts, so when
|
||||
* switching between interfaces, mDNS must be restarted
|
||||
*/
|
||||
void loopMdns() {
|
||||
#if USE_ETHERNET
|
||||
const NetworkMgr::Uplink connectionType = network->getUplink();
|
||||
|
||||
if (connectionType != this->mdnsConnectionType) {
|
||||
this->mdnsConnectionType = connectionType;
|
||||
|
||||
if (this->mDnsState) {
|
||||
MDNS.end();
|
||||
this->mDnsState = false;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS restarting: connection is now %s"), NetworkMgr::uplinkName(connectionType));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!settings.portal.mdns || !this->stateWebServer()) {
|
||||
if (this->mDnsState) {
|
||||
MDNS.end();
|
||||
this->mDnsState = false;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS disabled"));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->mDnsState || millis() - this->mDnsRetryTime < this->mDnsRetryInterval) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->mDnsRetryTime = millis();
|
||||
|
||||
if (MDNS.begin(networkSettings.hostname)) {
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
this->mDnsState = true;
|
||||
|
||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("mDNS enabled and service added"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add the computed W5500 state to a network settings document
|
||||
* @details The key is absent entirely on builds without ethernet, rather than an empty object:
|
||||
* presence of the section is what tells a client the target can do it. The stored fields
|
||||
* (enabled flag, pinout) come from networkSettingsToJson, this adds only what is derived.
|
||||
*/
|
||||
void ethernetToJson(JsonDocument& dst) {
|
||||
#if USE_ETHERNET
|
||||
dst[FPSTR(S_ETHERNET)][FPSTR(S_CONFIGURED)] = this->isEthernetConfigured();
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Ethernet can only be used when all six W5500 pins are configured
|
||||
*/
|
||||
bool isEthernetConfigured() {
|
||||
#if USE_ETHERNET
|
||||
return networkSettings.ethernet.w5500.clk != GPIO_IS_NOT_CONFIGURED
|
||||
&& networkSettings.ethernet.w5500.mosi != GPIO_IS_NOT_CONFIGURED
|
||||
&& networkSettings.ethernet.w5500.miso != GPIO_IS_NOT_CONFIGURED
|
||||
&& networkSettings.ethernet.w5500.cs != GPIO_IS_NOT_CONFIGURED
|
||||
&& networkSettings.ethernet.w5500.irq != GPIO_IS_NOT_CONFIGURED
|
||||
&& networkSettings.ethernet.w5500.rst != GPIO_IS_NOT_CONFIGURED;
|
||||
#else
|
||||
// nothing to configure: the W5500 driver is not compiled into this target
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Push the W5500 wiring from the settings into the network manager
|
||||
*/
|
||||
void applyEthernetSettings() {
|
||||
#if USE_ETHERNET
|
||||
network
|
||||
->setEthernet(networkSettings.ethernet.enabled)
|
||||
->setEthernetPins(
|
||||
networkSettings.ethernet.w5500.clk,
|
||||
networkSettings.ethernet.w5500.mosi,
|
||||
networkSettings.ethernet.w5500.miso,
|
||||
networkSettings.ethernet.w5500.cs,
|
||||
networkSettings.ethernet.w5500.irq,
|
||||
networkSettings.ethernet.w5500.rst
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isValidCredentials() {
|
||||
return this->webServer->authenticate(settings.portal.login, settings.portal.password);
|
||||
}
|
||||
@@ -1075,4 +1191,4 @@ protected:
|
||||
strftime(localNowValue, sizeof(localNowValue), PSTR("%Y-%m-%d-%H-%M-%S"), localNow);
|
||||
snprintf_P(filename, maxSizeFilename, PSTR("%s_%s_%s.json"), networkSettings.hostname, localNowValue, type);
|
||||
}
|
||||
};
|
||||
};
|
||||
+21
-2
@@ -1,3 +1,5 @@
|
||||
// NetworkSettings struct is append-only. The file is persisted across updates,
|
||||
// and removing/modifying existing keys will lead to file corruption.
|
||||
struct NetworkSettings {
|
||||
char hostname[25] = DEFAULT_HOSTNAME;
|
||||
bool useDhcp = true;
|
||||
@@ -20,6 +22,22 @@ struct NetworkSettings {
|
||||
char password[65] = DEFAULT_STA_PASSWORD;
|
||||
uint8_t channel = 0;
|
||||
} sta;
|
||||
|
||||
struct {
|
||||
bool enabled = DEFAULT_ETHERNET_ENABLED;
|
||||
|
||||
// nothing reads this yet: the W5500 is the only implementation the firmware can drive
|
||||
EthernetImplementation implementation = EthernetImplementation::W5500;
|
||||
|
||||
struct {
|
||||
uint8_t clk = DEFAULT_ETH_CLK_GPIO;
|
||||
uint8_t mosi = DEFAULT_ETH_MOSI_GPIO;
|
||||
uint8_t miso = DEFAULT_ETH_MISO_GPIO;
|
||||
uint8_t cs = DEFAULT_ETH_CS_GPIO;
|
||||
uint8_t irq = DEFAULT_ETH_IRQ_GPIO;
|
||||
uint8_t rst = DEFAULT_ETH_RST_GPIO;
|
||||
} w5500;
|
||||
} ethernet;
|
||||
} networkSettings;
|
||||
|
||||
struct Settings {
|
||||
@@ -132,7 +150,7 @@ struct Settings {
|
||||
uint8_t minTemp = DEFAULT_DHW_MIN_TEMP;
|
||||
uint8_t maxTemp = DEFAULT_DHW_MAX_TEMP;
|
||||
uint8_t maxModulation = 100;
|
||||
|
||||
|
||||
struct {
|
||||
uint8_t highTemp = 95;
|
||||
uint8_t lowTemp = 90;
|
||||
@@ -272,6 +290,7 @@ struct Variables {
|
||||
struct {
|
||||
bool connected = false;
|
||||
int8_t rssi = 0;
|
||||
NetworkUtils::NetworkMgr::Uplink connectionType = NetworkUtils::NetworkMgr::Uplink::None;
|
||||
} network;
|
||||
|
||||
struct {
|
||||
@@ -440,4 +459,4 @@ struct Variables {
|
||||
bool restarting = false;
|
||||
bool upgrading = false;
|
||||
} states;
|
||||
} vars;
|
||||
} vars;
|
||||
+40
-2
@@ -54,6 +54,11 @@
|
||||
#define USE_BLE false
|
||||
#endif
|
||||
|
||||
// ethernet (W5500 over SPI) is only validated on the esp32_c6 target: off everywhere else
|
||||
#ifndef USE_ETHERNET
|
||||
#define USE_ETHERNET false
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_HOSTNAME
|
||||
#define DEFAULT_HOSTNAME ""
|
||||
#endif
|
||||
@@ -82,6 +87,34 @@
|
||||
#define DEFAULT_STATUS_LED_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETHERNET_ENABLED
|
||||
#define DEFAULT_ETHERNET_ENABLED false
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_CLK_GPIO
|
||||
#define DEFAULT_ETH_CLK_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_MOSI_GPIO
|
||||
#define DEFAULT_ETH_MOSI_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_MISO_GPIO
|
||||
#define DEFAULT_ETH_MISO_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_CS_GPIO
|
||||
#define DEFAULT_ETH_CS_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_IRQ_GPIO
|
||||
#define DEFAULT_ETH_IRQ_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_ETH_RST_GPIO
|
||||
#define DEFAULT_ETH_RST_GPIO GPIO_IS_NOT_CONFIGURED
|
||||
#endif
|
||||
|
||||
#ifndef DEFAULT_PORTAL_LOGIN
|
||||
#define DEFAULT_PORTAL_LOGIN ""
|
||||
#endif
|
||||
@@ -147,7 +180,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef PROGMEM
|
||||
#define PROGMEM
|
||||
#define PROGMEM
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
@@ -168,4 +201,9 @@ enum class HysteresisAction : uint8_t {
|
||||
SET_ZERO_TARGET = 1
|
||||
};
|
||||
|
||||
char buffer[255];
|
||||
enum class EthernetImplementation : uint8_t {
|
||||
NONE = 0,
|
||||
W5500 = 1
|
||||
};
|
||||
|
||||
char buffer[255];
|
||||
+16
-1
@@ -74,7 +74,7 @@ void setup() {
|
||||
|
||||
return tm{sec, min, hour};
|
||||
});
|
||||
|
||||
|
||||
Serial.begin(115200);
|
||||
#if ARDUINO_USB_MODE
|
||||
Serial.setTxBufferSize(512);
|
||||
@@ -84,6 +84,9 @@ void setup() {
|
||||
|
||||
//
|
||||
// Network settings
|
||||
// The network file has to survive updates, this ensures it doesn't get wiped when the structure is modified
|
||||
fsNetworkSettings.addWithoutWipe(true);
|
||||
|
||||
switch (fsNetworkSettings.read()) {
|
||||
case FD_FS_ERR:
|
||||
Log.swarningln(FPSTR(L_NETWORK_SETTINGS), F("Filesystem error, load default"));
|
||||
@@ -154,6 +157,18 @@ void setup() {
|
||||
break;
|
||||
}
|
||||
|
||||
// ethernet (W5500) is configured from the network settings, after they are loaded
|
||||
network
|
||||
->setEthernet(networkSettings.ethernet.enabled)
|
||||
->setEthernetPins(
|
||||
networkSettings.ethernet.w5500.clk,
|
||||
networkSettings.ethernet.w5500.mosi,
|
||||
networkSettings.ethernet.w5500.miso,
|
||||
networkSettings.ethernet.w5500.cs,
|
||||
networkSettings.ethernet.w5500.irq,
|
||||
networkSettings.ethernet.w5500.rst
|
||||
);
|
||||
|
||||
// generate mqtt prefix if it is empty
|
||||
if (!strlen(settings.mqtt.prefix)) {
|
||||
strcpy(settings.mqtt.prefix, getChipId("otgateway_").c_str());
|
||||
|
||||
@@ -48,6 +48,7 @@ const char S_AUTO_FAULT_RESET[] PROGMEM = "autoFaultReset";
|
||||
const char S_BACKTRACE[] PROGMEM = "backtrace";
|
||||
const char S_BATTERY[] PROGMEM = "battery";
|
||||
const char S_BAUDRATE[] PROGMEM = "baudrate";
|
||||
const char S_BLE[] PROGMEM = "ble";
|
||||
const char S_BLOCKING[] PROGMEM = "blocking";
|
||||
const char S_BSSID[] PROGMEM = "bssid";
|
||||
const char S_BUILD[] PROGMEM = "build";
|
||||
@@ -55,14 +56,18 @@ const char S_CASCADE_CONTROL[] PROGMEM = "cascadeControl";
|
||||
const char S_CHANNEL[] PROGMEM = "channel";
|
||||
const char S_CH2_ALWAYS_ENABLED[] PROGMEM = "ch2AlwaysEnabled";
|
||||
const char S_CHIP[] PROGMEM = "chip";
|
||||
const char S_CLK[] PROGMEM = "clk";
|
||||
const char S_CODE[] PROGMEM = "code";
|
||||
const char S_CONNECTED[] PROGMEM = "connected";
|
||||
const char S_CONFIGURED[] PROGMEM = "configured";
|
||||
const char S_CONNECTION_TYPE[] PROGMEM = "connectionType";
|
||||
const char S_CONTINUES[] PROGMEM = "continues";
|
||||
const char S_COOLING[] PROGMEM = "cooling";
|
||||
const char S_COOLING_SUPPORT[] PROGMEM = "coolingSupport";
|
||||
const char S_CORE[] PROGMEM = "core";
|
||||
const char S_CORES[] PROGMEM = "cores";
|
||||
const char S_CRASH[] PROGMEM = "crash";
|
||||
const char S_CS[] PROGMEM = "cs";
|
||||
const char S_CURRENT_TEMP[] PROGMEM = "currentTemp";
|
||||
const char S_DATA[] PROGMEM = "data";
|
||||
const char S_DATE[] PROGMEM = "date";
|
||||
@@ -82,10 +87,12 @@ const char S_ENABLED[] PROGMEM = "enabled";
|
||||
const char S_ENV[] PROGMEM = "env";
|
||||
const char S_EPC[] PROGMEM = "epc";
|
||||
const char S_EQUITHERM[] PROGMEM = "equitherm";
|
||||
const char S_ETHERNET[] PROGMEM = "ethernet";
|
||||
const char S_EXPONENT[] PROGMEM = "exponent";
|
||||
const char S_EXTERNAL_PUMP[] PROGMEM = "externalPump";
|
||||
const char S_FACTOR[] PROGMEM = "factor";
|
||||
const char S_FAULT[] PROGMEM = "fault";
|
||||
const char S_FEATURES[] PROGMEM = "features";
|
||||
const char S_FREEZE_PROTECTION[] PROGMEM = "freezeProtection";
|
||||
const char S_FREEZING[] PROGMEM = "freezing";
|
||||
const char S_FILTERING[] PROGMEM = "filtering";
|
||||
@@ -119,6 +126,7 @@ const char S_INPUT[] PROGMEM = "input";
|
||||
const char S_INTERVAL[] PROGMEM = "interval";
|
||||
const char S_INVERT_STATE[] PROGMEM = "invertState";
|
||||
const char S_IP[] PROGMEM = "ip";
|
||||
const char S_IRQ[] PROGMEM = "irq";
|
||||
const char S_I_FACTOR[] PROGMEM = "i_factor";
|
||||
const char S_I_MULTIPLIER[] PROGMEM = "i_multiplier";
|
||||
const char S_LOGIN[] PROGMEM = "login";
|
||||
@@ -139,7 +147,9 @@ const char S_MIN_FREE[] PROGMEM = "minFree";
|
||||
const char S_MIN_MAX_FREE_BLOCK[] PROGMEM = "minMaxFreeBlock";
|
||||
const char S_MIN_POWER[] PROGMEM = "minPower";
|
||||
const char S_MIN_TEMP[] PROGMEM = "minTemp";
|
||||
const char S_MISO[] PROGMEM = "miso";
|
||||
const char S_MODEL[] PROGMEM = "model";
|
||||
const char S_MOSI[] PROGMEM = "mosi";
|
||||
const char S_MODULATION[] PROGMEM = "modulation";
|
||||
const char S_MQTT[] PROGMEM = "mqtt";
|
||||
const char S_NAME[] PROGMEM = "name";
|
||||
@@ -160,6 +170,7 @@ const char S_OVERHEAT_PROTECTION[] PROGMEM = "overheatProtectio
|
||||
const char S_PASSWORD[] PROGMEM = "password";
|
||||
const char S_PID[] PROGMEM = "pid";
|
||||
const char S_PORT[] PROGMEM = "port";
|
||||
const char S_PINS[] PROGMEM = "pins";
|
||||
const char S_PORTAL[] PROGMEM = "portal";
|
||||
const char S_POST_CIRCULATION_TIME[] PROGMEM = "postCirculationTime";
|
||||
const char S_POWER[] PROGMEM = "power";
|
||||
@@ -177,6 +188,7 @@ const char S_RESTART[] PROGMEM = "restart";
|
||||
const char S_RETURN_TEMP[] PROGMEM = "returnTemp";
|
||||
const char S_REV[] PROGMEM = "rev";
|
||||
const char S_RSSI[] PROGMEM = "rssi";
|
||||
const char S_RST[] PROGMEM = "rst";
|
||||
const char S_RX_LED_GPIO[] PROGMEM = "rxLedGpio";
|
||||
const char S_SDK[] PROGMEM = "sdk";
|
||||
const char S_SENSORS[] PROGMEM = "sensors";
|
||||
@@ -190,6 +202,7 @@ const char S_SIZE[] PROGMEM = "size";
|
||||
const char S_SLAVE[] PROGMEM = "slave";
|
||||
const char S_SLOPE[] PROGMEM = "slope";
|
||||
const char S_SSID[] PROGMEM = "ssid";
|
||||
const char S_STARTED[] PROGMEM = "started";
|
||||
const char S_STA[] PROGMEM = "sta";
|
||||
const char S_STATE[] PROGMEM = "state";
|
||||
const char S_STATIC_CONFIG[] PROGMEM = "staticConfig";
|
||||
|
||||
+79
-1
@@ -296,8 +296,24 @@ void networkSettingsToJson(const NetworkSettings& src, JsonVariant dst) {
|
||||
dst[FPSTR(S_STA)][FPSTR(S_SSID)] = src.sta.ssid;
|
||||
dst[FPSTR(S_STA)][FPSTR(S_PASSWORD)] = src.sta.password;
|
||||
dst[FPSTR(S_STA)][FPSTR(S_CHANNEL)] = src.sta.channel;
|
||||
|
||||
#if USE_ETHERNET
|
||||
auto ethernet = dst[FPSTR(S_ETHERNET)].to<JsonObject>();
|
||||
|
||||
ethernet[FPSTR(S_ENABLED)] = src.ethernet.enabled;
|
||||
|
||||
auto pins = ethernet[FPSTR(S_PINS)].to<JsonObject>();
|
||||
pins[FPSTR(S_CLK)] = src.ethernet.w5500.clk;
|
||||
pins[FPSTR(S_MOSI)] = src.ethernet.w5500.mosi;
|
||||
pins[FPSTR(S_MISO)] = src.ethernet.w5500.miso;
|
||||
pins[FPSTR(S_CS)] = src.ethernet.w5500.cs;
|
||||
pins[FPSTR(S_IRQ)] = src.ethernet.w5500.irq;
|
||||
pins[FPSTR(S_RST)] = src.ethernet.w5500.rst;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool jsonToEthernetPins(const JsonVariantConst src, NetworkSettings& dst, bool& changed);
|
||||
|
||||
bool jsonToNetworkSettings(const JsonVariantConst src, NetworkSettings& dst) {
|
||||
bool changed = false;
|
||||
|
||||
@@ -413,6 +429,19 @@ bool jsonToNetworkSettings(const JsonVariantConst src, NetworkSettings& dst) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ethernet
|
||||
if (src[FPSTR(S_ETHERNET)][FPSTR(S_ENABLED)].is<bool>()) {
|
||||
bool value = src[FPSTR(S_ETHERNET)][FPSTR(S_ENABLED)].as<bool>();
|
||||
|
||||
if (value != dst.ethernet.enabled) {
|
||||
dst.ethernet.enabled = value;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
jsonToEthernetPins(src[FPSTR(S_ETHERNET)][FPSTR(S_PINS)], dst, changed);
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
@@ -573,6 +602,55 @@ inline void safeSettingsToJson(const Settings& src, JsonVariant dst) {
|
||||
settingsToJson(src, dst, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse a single GPIO value: an empty string means "not configured"
|
||||
*
|
||||
* @return true if the key existed in the source document
|
||||
*/
|
||||
bool jsonToGpioValue(const JsonVariantConst src, const __FlashStringHelper* key, uint8_t& dst, bool& changed) {
|
||||
if (src[key].isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (src[key].is<JsonString>() && src[key].as<JsonString>().size() == 0) {
|
||||
if (dst != GPIO_IS_NOT_CONFIGURED) {
|
||||
dst = GPIO_IS_NOT_CONFIGURED;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned char value = src[key].as<unsigned char>();
|
||||
|
||||
if (GPIO_IS_VALID(value) && value != dst) {
|
||||
dst = value;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse the W5500 pinout from an object holding clk/mosi/miso/cs/irq/rst
|
||||
*
|
||||
* @return true if the source object exists
|
||||
*/
|
||||
bool jsonToEthernetPins(const JsonVariantConst src, NetworkSettings& dst, bool& changed) {
|
||||
if (src.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
jsonToGpioValue(src, FPSTR(S_CLK), dst.ethernet.w5500.clk, changed);
|
||||
jsonToGpioValue(src, FPSTR(S_MOSI), dst.ethernet.w5500.mosi, changed);
|
||||
jsonToGpioValue(src, FPSTR(S_MISO), dst.ethernet.w5500.miso, changed);
|
||||
jsonToGpioValue(src, FPSTR(S_CS), dst.ethernet.w5500.cs, changed);
|
||||
jsonToGpioValue(src, FPSTR(S_IRQ), dst.ethernet.w5500.irq, changed);
|
||||
jsonToGpioValue(src, FPSTR(S_RST), dst.ethernet.w5500.rst, changed);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool jsonToSettings(const JsonVariantConst src, Settings& dst, bool safe = false) {
|
||||
bool changed = false;
|
||||
|
||||
@@ -697,7 +775,6 @@ bool jsonToSettings(const JsonVariantConst src, Settings& dst, bool safe = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// portal
|
||||
if (src[FPSTR(S_PORTAL)][FPSTR(S_AUTH)].is<bool>()) {
|
||||
bool value = src[FPSTR(S_PORTAL)][FPSTR(S_AUTH)].as<bool>();
|
||||
@@ -2194,6 +2271,7 @@ void varsToJson(const Variables& src, JsonVariant dst) {
|
||||
|
||||
master[FPSTR(S_NETWORK)][FPSTR(S_CONNECTED)] = src.network.connected;
|
||||
master[FPSTR(S_NETWORK)][FPSTR(S_RSSI)] = src.network.rssi;
|
||||
master[FPSTR(S_NETWORK)][FPSTR(S_CONNECTION_TYPE)] = NetworkUtils::NetworkMgr::uplinkName(src.network.connectionType);
|
||||
master[FPSTR(S_MQTT)][FPSTR(S_CONNECTED)] = src.mqtt.connected;
|
||||
master[FPSTR(S_EMERGENCY)][FPSTR(S_STATE)] = src.emergency.state;
|
||||
master[FPSTR(S_EXTERNAL_PUMP)][FPSTR(S_STATE)] = src.externalPump.state;
|
||||
|
||||
Reference in new issue
Block a user