refactor: remove ESP8266 support and legacy flash wrappers

This commit is contained in:
Yurii committed 2026-10-21 04:34:00 +03:00
1 parent 959a15c11f
commit b72bccb3d7
24 files changed
+2027 -2415

No files matched your search

-20
View File
@@ -14,17 +14,8 @@ public:
void send(int code, T contentType, const JsonVariantConst content, bool pretty = false) {
auto contentLength = pretty ? measureJsonPretty(content) : measureJson(content);
#ifdef ARDUINO_ARCH_ESP8266
if (!this->webServer->chunkedResponseModeStart(code, contentType)) {
this->webServer->send(505, F("text/html"), F("HTTP1.1 required"));
return;
}
this->webServer->setContentLength(contentLength);
#else
this->webServer->setContentLength(contentLength);
this->webServer->send(code, contentType, emptyString);
#endif
if (pretty) {
serializeJsonPretty(content, *this);
@@ -35,11 +26,7 @@ public:
this->flush();
#ifdef ARDUINO_ARCH_ESP8266
this->webServer->chunkedResponseFinalize();
#else
this->webServer->sendContent(emptyString);
#endif
}
size_t write(uint8_t c) {
@@ -78,10 +65,6 @@ public:
return;
}
#ifdef ARDUINO_ARCH_ESP8266
::optimistic_yield(1000);
#endif
auto& client = this->webServer->client();
if (client.connected()) {
this->webServer->sendContent((const char*)this->buffer, this->bufferPos);
@@ -89,9 +72,6 @@ public:
this->bufferPos = 0;
#ifdef ARDUINO_ARCH_ESP8266
::optimistic_yield(1000);
#endif
}
protected:
@@ -53,23 +53,23 @@ public:
return false;
}
doc[FPSTR(HA_DEVICE)][FPSTR(HA_IDENTIFIERS)][0] = this->devicePrefix;
doc[FPSTR(HA_DEVICE)][FPSTR(HA_SW_VERSION)] = this->deviceVersion;
doc[HA_DEVICE][HA_IDENTIFIERS][0] = this->devicePrefix;
doc[HA_DEVICE][HA_SW_VERSION] = this->deviceVersion;
if (this->deviceManufacturer != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MANUFACTURER)] = this->deviceManufacturer;
doc[HA_DEVICE][HA_MANUFACTURER] = this->deviceManufacturer;
}
if (this->deviceModel != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_MODEL)] = this->deviceModel;
doc[HA_DEVICE][HA_MODEL] = this->deviceModel;
}
if (this->deviceName != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_NAME)] = this->deviceName;
doc[HA_DEVICE][HA_NAME] = this->deviceName;
}
if (this->deviceConfigUrl != nullptr) {
doc[FPSTR(HA_DEVICE)][FPSTR(HA_CONF_URL)] = this->deviceConfigUrl;
doc[HA_DEVICE][HA_CONF_URL] = this->deviceConfigUrl;
}
bool result = this->writer->publish(topic, doc, true);
@@ -110,7 +110,7 @@ public:
topic.concat(this->devicePrefix);
topic.concat(nameSeparator);
topic.concat(name);
topic.concat(F("/config"));
topic.concat("/config");
return topic;
}
+71 -74
View File
@@ -1,78 +1,75 @@
#pragma once
#ifndef PROGMEM
#define PROGMEM // NOLINT
#endif
const char HA_ENTITY_BINARY_SENSOR[] PROGMEM = "binary_sensor";
const char HA_ENTITY_BUTTON[] PROGMEM = "button";
const char HA_ENTITY_FAN[] PROGMEM = "fan";
const char HA_ENTITY_CLIMATE[] PROGMEM = "climate";
const char HA_ENTITY_NUMBER[] PROGMEM = "number";
const char HA_ENTITY_SELECT[] PROGMEM = "select";
const char HA_ENTITY_SENSOR[] PROGMEM = "sensor";
const char HA_ENTITY_SWITCH[] PROGMEM = "switch";
const char HA_ENTITY_BINARY_SENSOR[] = "binary_sensor";
const char HA_ENTITY_BUTTON[] = "button";
const char HA_ENTITY_FAN[] = "fan";
const char HA_ENTITY_CLIMATE[] = "climate";
const char HA_ENTITY_NUMBER[] = "number";
const char HA_ENTITY_SELECT[] = "select";
const char HA_ENTITY_SENSOR[] = "sensor";
const char HA_ENTITY_SWITCH[] = "switch";
// https://www.home-assistant.io/integrations/mqtt/#supported-abbreviations-in-mqtt-discovery-messages
const char HA_DEFAULT_ENTITY_ID[] PROGMEM = "def_ent_id"; // "default_entity_id "
const char HA_DEVICE[] PROGMEM = "dev"; // "device"
const char HA_IDENTIFIERS[] PROGMEM = "ids"; // "identifiers"
const char HA_SW_VERSION[] PROGMEM = "sw"; // "sw_version"
const char HA_MANUFACTURER[] PROGMEM = "mf"; // "manufacturer"
const char HA_MODEL[] PROGMEM = "mdl"; // "model"
const char HA_NAME[] PROGMEM = "name";
const char HA_CONF_URL[] PROGMEM = "cu"; // "configuration_url"
const char HA_COMMAND_TOPIC[] PROGMEM = "cmd_t"; // "command_topic"
const char HA_COMMAND_TEMPLATE[] PROGMEM = "cmd_tpl"; // "command_template"
const char HA_ENABLED_BY_DEFAULT[] PROGMEM = "en"; // "enabled_by_default"
const char HA_UNIQUE_ID[] PROGMEM = "uniq_id"; // "unique_id"
const char HA_ENTITY_CATEGORY[] PROGMEM = "ent_cat"; // "entity_category"
const char HA_ENTITY_CATEGORY_DIAGNOSTIC[] PROGMEM = "diagnostic";
const char HA_ENTITY_CATEGORY_CONFIG[] PROGMEM = "config";
const char HA_STATE_TOPIC[] PROGMEM = "stat_t"; // "state_topic"
const char HA_VALUE_TEMPLATE[] PROGMEM = "val_tpl"; // "value_template"
const char HA_OPTIONS[] PROGMEM = "ops"; // "options"
const char HA_AVAILABILITY[] PROGMEM = "avty"; // "availability"
const char HA_AVAILABILITY_MODE[] PROGMEM = "avty_mode"; // "availability_mode"
const char HA_TOPIC[] PROGMEM = "t"; // "topic"
const char HA_DEVICE_CLASS[] PROGMEM = "dev_cla"; // "device_class"
const char HA_UNIT_OF_MEASUREMENT[] PROGMEM = "unit_of_meas"; // "unit_of_measurement"
const char HA_UNIT_OF_MEASUREMENT_C[] PROGMEM = "°C";
const char HA_UNIT_OF_MEASUREMENT_F[] PROGMEM = "°F";
const char HA_UNIT_OF_MEASUREMENT_PERCENT[] PROGMEM = "%";
const char HA_UNIT_OF_MEASUREMENT_L_MIN[] PROGMEM = "L/min";
const char HA_UNIT_OF_MEASUREMENT_GAL_MIN[] PROGMEM = "gal/min";
const char HA_ICON[] PROGMEM = "ic"; // "icon"
const char HA_MIN[] PROGMEM = "min";
const char HA_MAX[] PROGMEM = "max";
const char HA_STEP[] PROGMEM = "step";
const char HA_MODE[] PROGMEM = "mode";
const char HA_MODE_BOX[] PROGMEM = "box";
const char HA_STATE_ON[] PROGMEM = "stat_on"; // "state_on"
const char HA_STATE_OFF[] PROGMEM = "stat_off"; // "state_off"
const char HA_PAYLOAD_ON[] PROGMEM = "pl_on"; // "payload_on"
const char HA_PAYLOAD_OFF[] PROGMEM = "pl_off"; // "payload_off"
const char HA_STATE_CLASS[] PROGMEM = "stat_cla"; // "state_class"
const char HA_STATE_CLASS_MEASUREMENT[] PROGMEM = "measurement";
const char HA_EXPIRE_AFTER[] PROGMEM = "exp_aft"; // "expire_after"
const char HA_CURRENT_TEMPERATURE_TOPIC[] PROGMEM = "curr_temp_t"; // "current_temperature_topic"
const char HA_CURRENT_TEMPERATURE_TEMPLATE[] PROGMEM = "curr_temp_tpl"; // "current_temperature_template"
const char HA_TEMPERATURE_COMMAND_TOPIC[] PROGMEM = "temp_cmd_t"; // "temperature_command_topic"
const char HA_TEMPERATURE_COMMAND_TEMPLATE[] PROGMEM = "temp_cmd_tpl"; // "temperature_command_template"
const char HA_TEMPERATURE_STATE_TOPIC[] PROGMEM = "temp_stat_t"; // "temperature_state_topic"
const char HA_TEMPERATURE_STATE_TEMPLATE[] PROGMEM = "temp_stat_tpl"; // "temperature_state_template"
const char HA_TEMPERATURE_UNIT[] PROGMEM = "temp_unit"; // "temperature_unit"
const char HA_MODE_COMMAND_TOPIC[] PROGMEM = "mode_cmd_t"; // "mode_command_topic"
const char HA_MODE_COMMAND_TEMPLATE[] PROGMEM = "mode_cmd_tpl"; // "mode_command_template"
const char HA_MODE_STATE_TOPIC[] PROGMEM = "mode_stat_t"; // "mode_state_topic"
const char HA_MODE_STATE_TEMPLATE[] PROGMEM = "mode_stat_tpl"; // "mode_state_template"
const char HA_MODES[] PROGMEM = "modes";
const char HA_ACTION_TOPIC[] PROGMEM = "act_t"; // "action_topic"
const char HA_ACTION_TEMPLATE[] PROGMEM = "act_tpl"; // "action_template"
const char HA_MIN_TEMP[] PROGMEM = "min_temp";
const char HA_MAX_TEMP[] PROGMEM = "max_temp";
const char HA_TEMP_STEP[] PROGMEM = "temp_step";
const char HA_PRESET_MODE_COMMAND_TOPIC[] PROGMEM = "pr_mode_cmd_t"; // "preset_mode_command_topic"
const char HA_PRESET_MODE_COMMAND_TEMPLATE[] PROGMEM = "pr_mode_cmd_tpl"; // "preset_mode_command_template"
const char HA_PRESET_MODE_STATE_TOPIC[] PROGMEM = "pr_mode_stat_t"; // "preset_mode_state_topic"
const char HA_PRESET_MODE_VALUE_TEMPLATE[] PROGMEM = "pr_mode_val_tpl"; // "preset_mode_value_template"
const char HA_PRESET_MODES[] PROGMEM = "pr_modes"; // "preset_modes"
const char HA_DEFAULT_ENTITY_ID[] = "def_ent_id"; // "default_entity_id "
const char HA_DEVICE[] = "dev"; // "device"
const char HA_IDENTIFIERS[] = "ids"; // "identifiers"
const char HA_SW_VERSION[] = "sw"; // "sw_version"
const char HA_MANUFACTURER[] = "mf"; // "manufacturer"
const char HA_MODEL[] = "mdl"; // "model"
const char HA_NAME[] = "name";
const char HA_CONF_URL[] = "cu"; // "configuration_url"
const char HA_COMMAND_TOPIC[] = "cmd_t"; // "command_topic"
const char HA_COMMAND_TEMPLATE[] = "cmd_tpl"; // "command_template"
const char HA_ENABLED_BY_DEFAULT[] = "en"; // "enabled_by_default"
const char HA_UNIQUE_ID[] = "uniq_id"; // "unique_id"
const char HA_ENTITY_CATEGORY[] = "ent_cat"; // "entity_category"
const char HA_ENTITY_CATEGORY_DIAGNOSTIC[] = "diagnostic";
const char HA_ENTITY_CATEGORY_CONFIG[] = "config";
const char HA_STATE_TOPIC[] = "stat_t"; // "state_topic"
const char HA_VALUE_TEMPLATE[] = "val_tpl"; // "value_template"
const char HA_OPTIONS[] = "ops"; // "options"
const char HA_AVAILABILITY[] = "avty"; // "availability"
const char HA_AVAILABILITY_MODE[] = "avty_mode"; // "availability_mode"
const char HA_TOPIC[] = "t"; // "topic"
const char HA_DEVICE_CLASS[] = "dev_cla"; // "device_class"
const char HA_UNIT_OF_MEASUREMENT[] = "unit_of_meas"; // "unit_of_measurement"
const char HA_UNIT_OF_MEASUREMENT_C[] = "°C";
const char HA_UNIT_OF_MEASUREMENT_F[] = "°F";
const char HA_UNIT_OF_MEASUREMENT_PERCENT[] = "%";
const char HA_UNIT_OF_MEASUREMENT_L_MIN[] = "L/min";
const char HA_UNIT_OF_MEASUREMENT_GAL_MIN[] = "gal/min";
const char HA_ICON[] = "ic"; // "icon"
const char HA_MIN[] = "min";
const char HA_MAX[] = "max";
const char HA_STEP[] = "step";
const char HA_MODE[] = "mode";
const char HA_MODE_BOX[] = "box";
const char HA_STATE_ON[] = "stat_on"; // "state_on"
const char HA_STATE_OFF[] = "stat_off"; // "state_off"
const char HA_PAYLOAD_ON[] = "pl_on"; // "payload_on"
const char HA_PAYLOAD_OFF[] = "pl_off"; // "payload_off"
const char HA_STATE_CLASS[] = "stat_cla"; // "state_class"
const char HA_STATE_CLASS_MEASUREMENT[] = "measurement";
const char HA_EXPIRE_AFTER[] = "exp_aft"; // "expire_after"
const char HA_CURRENT_TEMPERATURE_TOPIC[] = "curr_temp_t"; // "current_temperature_topic"
const char HA_CURRENT_TEMPERATURE_TEMPLATE[] = "curr_temp_tpl"; // "current_temperature_template"
const char HA_TEMPERATURE_COMMAND_TOPIC[] = "temp_cmd_t"; // "temperature_command_topic"
const char HA_TEMPERATURE_COMMAND_TEMPLATE[] = "temp_cmd_tpl"; // "temperature_command_template"
const char HA_TEMPERATURE_STATE_TOPIC[] = "temp_stat_t"; // "temperature_state_topic"
const char HA_TEMPERATURE_STATE_TEMPLATE[] = "temp_stat_tpl"; // "temperature_state_template"
const char HA_TEMPERATURE_UNIT[] = "temp_unit"; // "temperature_unit"
const char HA_MODE_COMMAND_TOPIC[] = "mode_cmd_t"; // "mode_command_topic"
const char HA_MODE_COMMAND_TEMPLATE[] = "mode_cmd_tpl"; // "mode_command_template"
const char HA_MODE_STATE_TOPIC[] = "mode_stat_t"; // "mode_state_topic"
const char HA_MODE_STATE_TEMPLATE[] = "mode_stat_tpl"; // "mode_state_template"
const char HA_MODES[] = "modes";
const char HA_ACTION_TOPIC[] = "act_t"; // "action_topic"
const char HA_ACTION_TEMPLATE[] = "act_tpl"; // "action_template"
const char HA_MIN_TEMP[] = "min_temp";
const char HA_MAX_TEMP[] = "max_temp";
const char HA_TEMP_STEP[] = "temp_step";
const char HA_PRESET_MODE_COMMAND_TOPIC[] = "pr_mode_cmd_t"; // "preset_mode_command_topic"
const char HA_PRESET_MODE_COMMAND_TEMPLATE[] = "pr_mode_cmd_tpl"; // "preset_mode_command_template"
const char HA_PRESET_MODE_STATE_TOPIC[] = "pr_mode_stat_t"; // "preset_mode_state_topic"
const char HA_PRESET_MODE_VALUE_TEMPLATE[] = "pr_mode_val_tpl"; // "preset_mode_value_template"
const char HA_PRESET_MODES[] = "pr_modes"; // "preset_modes"
-24
View File
@@ -1,24 +0,0 @@
#include <WiFiClient.h>
class MqttWiFiClient : public WiFiClient {
public:
#ifdef ARDUINO_ARCH_ESP8266
void flush() override {
if (this->connected()) {
WiFiClient::flush(0);
}
}
void stop() override {
this->abort();
}
#endif
#ifdef ARDUINO_ARCH_ESP32
void setSync(bool) {}
bool getSync() {
return false;
}
#endif
};
-16
View File
@@ -1,9 +1,7 @@
#pragma once
#include <Arduino.h>
#include <MqttClient.h>
#ifdef ARDUINO_ARCH_ESP32
#include <mutex>
#endif
class MqttWriter {
@@ -17,17 +15,13 @@ public:
this->bufferSize = bufferSize;
this->buffer = static_cast<uint8_t*>(malloc(bufferSize * sizeof(*this->buffer)));
#ifdef ARDUINO_ARCH_ESP32
this->mutex = new std::mutex();
#endif
}
~MqttWriter() {
free(this->buffer);
#ifdef ARDUINO_ARCH_ESP32
delete this->mutex;
#endif
}
MqttWriter* setYieldCallback(YieldCallback callback = nullptr) {
@@ -49,15 +43,9 @@ public:
}
bool lock() {
#ifdef ARDUINO_ARCH_ESP32
if (!this->mutex->try_lock()) {
return false;
}
#else
if (this->isLocked()) {
return false;
}
#endif
this->locked = true;
this->writeAfterLock = 0;
@@ -72,9 +60,7 @@ public:
void unlock() {
this->locked = false;
#if defined(ARDUINO_ARCH_ESP32)
this->mutex->unlock();
#endif
}
bool publish(const char* topic, const JsonVariantConst doc, bool retained = false) {
@@ -212,9 +198,7 @@ protected:
size_t bufferSize = 64;
size_t bufferPos = 0;
bool locked = false;
#ifdef ARDUINO_ARCH_ESP32
mutable std::mutex* mutex;
#endif
unsigned long lockedTime = 0;
size_t writeAfterLock = 0;
};
-73
View File
@@ -4,11 +4,7 @@ using namespace NetworkUtils;
void NetworkConnection::setup(bool useDhcp) {
setUseDhcp(useDhcp);
#if defined(ARDUINO_ARCH_ESP8266)
wifi_set_event_handler_cb(NetworkConnection::onEvent);
#elif defined(ARDUINO_ARCH_ESP32)
WiFi.onEvent(NetworkConnection::onEvent);
#endif
}
void NetworkConnection::reset() {
@@ -29,57 +25,6 @@ NetworkConnection::DisconnectReason NetworkConnection::getDisconnectReason() {
return disconnectReason;
}
#if defined(ARDUINO_ARCH_ESP8266)
void NetworkConnection::onEvent(System_Event_t *event) {
switch (event->event) {
case EVENT_STAMODE_CONNECTED:
status = useDhcp ? Status::CONNECTING : Status::CONNECTED;
rawDisconnectReason = 0;
disconnectReason = DisconnectReason::NONE;
break;
case EVENT_STAMODE_GOT_IP:
status = Status::CONNECTED;
rawDisconnectReason = 0;
disconnectReason = DisconnectReason::NONE;
break;
case EVENT_STAMODE_DHCP_TIMEOUT:
status = Status::DISCONNECTED;
rawDisconnectReason = 0;
disconnectReason = DisconnectReason::DHCP_TIMEOUT;
break;
case EVENT_STAMODE_DISCONNECTED:
status = Status::DISCONNECTED;
rawDisconnectReason = event->event_info.disconnected.reason;
disconnectReason = convertDisconnectReason(event->event_info.disconnected.reason);
// https://github.com/esp8266/Arduino/blob/d5eb265f78bff9deb7063d10030a02d021c8c66c/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp#L231
if ((wifi_station_get_connect_status() == STATION_GOT_IP) && !wifi_station_get_reconnect_policy()) {
wifi_station_disconnect();
}
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
// https://github.com/esp8266/Arduino/blob/d5eb265f78bff9deb7063d10030a02d021c8c66c/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp#L241
{
auto& src = event->event_info.auth_change;
if ((src.old_mode != AUTH_OPEN) && (src.new_mode == AUTH_OPEN)) {
status = Status::DISCONNECTED;
rawDisconnectReason = 0;
disconnectReason = DisconnectReason::OTHER;
wifi_station_disconnect();
}
}
break;
default:
break;
}
}
#elif defined(ARDUINO_ARCH_ESP32)
void NetworkConnection::onEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
@@ -111,26 +56,9 @@ void NetworkConnection::onEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
break;
}
}
#endif
NetworkConnection::DisconnectReason NetworkConnection::convertDisconnectReason(uint8_t reason) {
switch (reason) {
#if defined(ARDUINO_ARCH_ESP8266)
case REASON_BEACON_TIMEOUT:
return DisconnectReason::BEACON_TIMEOUT;
case REASON_NO_AP_FOUND:
return DisconnectReason::NO_AP_FOUND;
case REASON_AUTH_FAIL:
return DisconnectReason::AUTH_FAIL;
case REASON_ASSOC_FAIL:
return DisconnectReason::ASSOC_FAIL;
case REASON_HANDSHAKE_TIMEOUT:
return DisconnectReason::HANDSHAKE_TIMEOUT;
#elif defined(ARDUINO_ARCH_ESP32)
case WIFI_REASON_BEACON_TIMEOUT:
return DisconnectReason::BEACON_TIMEOUT;
@@ -145,7 +73,6 @@ NetworkConnection::DisconnectReason NetworkConnection::convertDisconnectReason(u
case WIFI_REASON_HANDSHAKE_TIMEOUT:
return DisconnectReason::HANDSHAKE_TIMEOUT;
#endif
default:
return DisconnectReason::OTHER;
+1 -10
View File
@@ -1,9 +1,4 @@
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include "lwip/etharp.h"
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif
#include <WiFi.h>
namespace NetworkUtils {
struct NetworkConnection {
@@ -34,11 +29,7 @@ namespace NetworkUtils {
static void reset();
static Status getStatus();
static DisconnectReason getDisconnectReason();
#if defined(ARDUINO_ARCH_ESP8266)
static void onEvent(System_Event_t *evt);
#elif defined(ARDUINO_ARCH_ESP32)
static void onEvent(WiFiEvent_t event, WiFiEventInfo_t info);
#endif
protected:
static DisconnectReason convertDisconnectReason(uint8_t reason);
+16 -95
View File
@@ -1,10 +1,5 @@
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include "lwip/etharp.h"
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#include <NetworkConnection.h>
namespace NetworkUtils {
@@ -147,44 +142,23 @@ namespace NetworkUtils {
bool resetWifi() {
// set policy manual for work 13 ch
{
#ifdef ARDUINO_ARCH_ESP8266
wifi_country_t country = {"JP", 1, 14, WIFI_COUNTRY_POLICY_MANUAL};
wifi_set_country(&country);
#elif defined(ARDUINO_ARCH_ESP32)
const wifi_country_t country = {"JP", 1, 14, CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER, WIFI_COUNTRY_POLICY_MANUAL};
esp_wifi_set_country(&country);
#endif
}
WiFi.persistent(false);
#if !defined(ESP_ARDUINO_VERSION_MAJOR) || ESP_ARDUINO_VERSION_MAJOR < 3
#if ESP_ARDUINO_VERSION_MAJOR < 3
WiFi.setAutoConnect(false);
#endif
WiFi.setAutoReconnect(false);
#ifdef ARDUINO_ARCH_ESP8266
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#elif defined(ARDUINO_ARCH_ESP32)
WiFi.setSleep(USE_BLE ? WIFI_PS_MIN_MODEM : WIFI_PS_NONE);
#endif
WiFi.softAPdisconnect();
#ifdef ARDUINO_ARCH_ESP8266
/*if (wifi_softap_dhcps_status() == DHCP_STARTED) {
wifi_softap_dhcps_stop();
}*/
#endif
WiFi.disconnect(false, true);
#ifdef ARDUINO_ARCH_ESP8266
/*if (wifi_station_dhcpc_status() == DHCP_STARTED) {
wifi_station_dhcpc_stop();
}*/
wifi_station_dhcpc_set_maxtry(5);
#endif
#if defined(ARDUINO_ARCH_ESP32) && ESP_ARDUINO_VERSION_MAJOR < 3
#if ESP_ARDUINO_VERSION_MAJOR < 3
// Nothing. Because memory leaks when turn off WiFi on ESP32 SDK < 3.0.0
return true;
#else
@@ -206,12 +180,6 @@ namespace NetworkUtils {
NetworkConnection::reset();
} else {
/*#ifdef ARDUINO_ARCH_ESP8266
if (wifi_station_dhcpc_status() == DHCP_STARTED) {
wifi_station_dhcpc_stop();
}
#endif*/
this->disconnect();
}
@@ -221,14 +189,12 @@ namespace NetworkUtils {
this->delayCallback(250);
#ifdef ARDUINO_ARCH_ESP32
if (this->setWifiHostname(this->hostname)) {
Log.straceln(FPSTR(L_NETWORK), F("Set hostname '%s': success"), this->hostname);
Log.straceln(L_NETWORK, "Set hostname '%s': success", this->hostname);
} else {
Log.serrorln(FPSTR(L_NETWORK), F("Set hostname '%s': fail"), this->hostname);
Log.serrorln(L_NETWORK, "Set hostname '%s': fail", this->hostname);
}
#endif
if (!WiFi.mode((WiFiMode_t)(WiFi.getMode() | WIFI_STA))) {
return false;
@@ -236,21 +202,8 @@ namespace NetworkUtils {
this->delayCallback(250);
#ifdef ARDUINO_ARCH_ESP8266
if (this->setWifiHostname(this->hostname)) {
Log.straceln(FPSTR(L_NETWORK), F("Set hostname '%s': success"), this->hostname);
} else {
Log.serrorln(FPSTR(L_NETWORK), F("Set hostname '%s': fail"), this->hostname);
}
this->delayCallback(250);
#endif
#ifdef ARDUINO_ARCH_ESP32
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
#endif
if (!this->useDhcp) {
WiFi.begin(this->staSsid, this->staPassword, this->staChannel, nullptr, false);
@@ -275,30 +228,26 @@ namespace NetworkUtils {
}
void disconnect() {
#ifdef ARDUINO_ARCH_ESP32
WiFi.disconnectAsync(false, true);
const unsigned long start = millis();
while (WiFi.isConnected() && (millis() - start) < 5000) {
this->delayCallback(100);
}
#else
WiFi.disconnect(false, true);
#endif
}
void loop() {
if (this->reconnectFlag) {
this->delayCallback(5000);
Log.sinfoln(FPSTR(L_NETWORK), F("Reconnecting..."));
Log.sinfoln(L_NETWORK, "Reconnecting...");
this->reconnectFlag = false;
this->disconnect();
NetworkConnection::reset();
this->delayCallback(1000);
} else if (this->isConnected() && !this->hasStaCredentials()) {
Log.sinfoln(FPSTR(L_NETWORK), F("Reset"));
Log.sinfoln(L_NETWORK, "Reset");
this->resetWifi();
NetworkConnection::reset();
this->delayCallback(1000);
@@ -309,8 +258,8 @@ namespace NetworkUtils {
this->connected = true;
Log.sinfoln(
FPSTR(L_NETWORK),
F("Connected, downtime: %lu s., IP: %s, RSSI: %hhd"),
L_NETWORK,
"Connected, downtime: %lu s., IP: %s, RSSI: %hhd",
(millis() - this->disconnectedTime) / 1000,
WiFi.localIP().toString().c_str(),
WiFi.RSSI()
@@ -318,58 +267,51 @@ namespace NetworkUtils {
}
if (this->isApEnabled() && millis() - this->connectedTime > this->reconnectInterval && !this->hasApClients()) {
Log.sinfoln(FPSTR(L_NETWORK), F("Stop AP because STA connected"));
Log.sinfoln(L_NETWORK, "Stop AP because STA connected");
WiFi.mode(WIFI_STA);
return;
}
#ifdef ARDUINO_ARCH_ESP8266
if (millis() - this->prevArpGratuitous > 60000) {
this->stationKeepAliveNow();
this->prevArpGratuitous = millis();
}
#endif
} else {
if (this->connected) {
this->disconnectedTime = millis();
this->connected = false;
Log.sinfoln(
FPSTR(L_NETWORK),
F("Disconnected, reason: %d, uptime: %lu s."),
L_NETWORK,
"Disconnected, reason: %d, uptime: %lu s.",
NetworkConnection::getDisconnectReason(),
(millis() - this->connectedTime) / 1000
);
}
if (!this->hasStaCredentials() && !this->isApEnabled()) {
Log.sinfoln(FPSTR(L_NETWORK), F("No STA credentials, start AP"));
Log.sinfoln(L_NETWORK, "No STA credentials, start AP");
WiFi.mode(WIFI_AP_STA);
this->delayCallback(250);
WiFi.softAP(this->apName, this->apPassword, this->apChannel);
} else if (!this->isApEnabled() && millis() - this->disconnectedTime > this->failedConnectTimeout) {
Log.sinfoln(FPSTR(L_NETWORK), F("Disconnected for a long time, start AP"));
Log.sinfoln(L_NETWORK, "Disconnected for a long time, start AP");
WiFi.mode(WIFI_AP_STA);
this->delayCallback(250);
WiFi.softAP(this->apName, this->apPassword, this->apChannel);
} else if (this->isConnecting() && millis() - this->prevReconnectingTime > this->resetConnectionTimeout) {
Log.swarningln(FPSTR(L_NETWORK), F("Connection timeout, reset wifi..."));
Log.swarningln(L_NETWORK, "Connection timeout, reset wifi...");
this->resetWifi();
NetworkConnection::reset();
this->delayCallback(250);
} else if (!this->isConnecting() && this->hasStaCredentials() && (!this->prevReconnectingTime || millis() - this->prevReconnectingTime > this->reconnectInterval)) {
Log.sinfoln(FPSTR(L_NETWORK), F("Try connect..."));
Log.sinfoln(L_NETWORK, "Try connect...");
NetworkConnection::reset();
if (!this->connect(true, this->connectionTimeout)) {
Log.straceln(FPSTR(L_NETWORK), F("Connection failed. Status: %d, reason: %d, raw reason: %d"), NetworkConnection::getStatus(), NetworkConnection::getDisconnectReason(), NetworkConnection::rawDisconnectReason);
Log.straceln(L_NETWORK, "Connection failed. Status: %d, reason: %d, raw reason: %d", NetworkConnection::getStatus(), NetworkConnection::getDisconnectReason(), NetworkConnection::rawDisconnectReason);
}
this->prevReconnectingTime = millis();
@@ -451,7 +393,6 @@ namespace NetworkUtils {
bool connected = false;
bool reconnectFlag = false;
unsigned long prevArpGratuitous = 0;
unsigned long prevReconnectingTime = 0;
unsigned long connectedTime = 0;
unsigned long disconnectedTime = 0;
@@ -469,25 +410,5 @@ namespace NetworkUtils {
return WiFi.setHostname(hostname);
}
#ifdef ARDUINO_ARCH_ESP8266
/**
* @brief
* https://github.com/arendst/Tasmota/blob/e6515883f0ee5451931b6280ff847b117de5a231/tasmota/tasmota_support/support_wifi.ino#L1196
*/
static void stationKeepAliveNow(void) {
for (netif* interface = netif_list; interface != nullptr; interface = interface->next) {
if (
(interface->flags & NETIF_FLAG_LINK_UP)
&& (interface->flags & NETIF_FLAG_UP)
&& interface->num == STATION_IF
&& (!ip4_addr_isany_val(*netif_ip4_addr(interface)))
) {
etharp_gratuitous(interface);
break;
}
}
}
#endif
};
}
+7 -13
View File
@@ -28,11 +28,9 @@ public:
return this;
}
#if defined(ARDUINO_ARCH_ESP32)
bool canHandle(WebServer &server, HTTPMethod method, const String &uri) override {
return this->canHandle(method, uri);
}
#endif
bool canHandle(HTTPMethod method, const String& uri) override {
return method == HTTP_GET && uri.equals(this->uri) && (!this->canHandleCallback || this->canHandleCallback(method, uri));
@@ -54,17 +52,17 @@ public:
}
}
if (!this->eTag.isEmpty() && server.header(F("If-None-Match")).equals(this->eTag.c_str())) {
if (!this->eTag.isEmpty() && server.header("If-None-Match").equals(this->eTag.c_str())) {
server.send(304);
return true;
}
}
if (!this->path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !this->fs->exists(path)) {
String pathWithGz = this->path + FPSTR(mimeTable[gz].endsWith);
if (!this->path.endsWith(mimeTable[gz].endsWith) && !this->fs->exists(path)) {
String pathWithGz = this->path + mimeTable[gz].endsWith;
if (this->fs->exists(pathWithGz)) {
this->path += FPSTR(mimeTable[gz].endsWith);
this->path += mimeTable[gz].endsWith;
}
}
@@ -78,18 +76,14 @@ public:
}
if (this->cacheHeader != nullptr) {
server.sendHeader(F("Cache-Control"), this->cacheHeader);
server.sendHeader("Cache-Control", this->cacheHeader);
}
if (server._eTagEnabled && !this->eTag.isEmpty()) {
server.sendHeader(F("ETag"), this->eTag);
server.sendHeader("ETag", this->eTag);
}
#if defined(ARDUINO_ARCH_ESP8266)
server.streamFile(file, F("text/html"), method);
#else
server.streamFile(file, F("text/html"), 200);
#endif
server.streamFile(file, "text/html", 200);
return true;
}
+7 -7
View File
@@ -132,11 +132,11 @@ public:
result->status = UpgradeStatus::ERROR_ON_START;
result->error = Update.errorString();
Log.serrorln(FPSTR(L_PORTAL_OTA), "File '%s', on start: %s", fileName.c_str(), result->error.c_str());
Log.serrorln(L_PORTAL_OTA, "File '%s', on start: %s", fileName.c_str(), result->error.c_str());
return;
}
Log.sinfoln(FPSTR(L_PORTAL_OTA), "File '%s', started", fileName.c_str());
Log.sinfoln(L_PORTAL_OTA, "File '%s', started", fileName.c_str());
}
if (dataLength) {
@@ -146,7 +146,7 @@ public:
result->error = Update.errorString();
Log.serrorln(
FPSTR(L_PORTAL_OTA), "File '%s', on write %d bytes, %d of %d bytes",
L_PORTAL_OTA, "File '%s', on write %d bytes, %d of %d bytes",
fileName.c_str(),
dataLength,
result->progress + dataLength,
@@ -157,7 +157,7 @@ public:
result->progress += dataLength;
Log.sinfoln(
FPSTR(L_PORTAL_OTA), "File '%s', write %d bytes, %d of %d bytes",
L_PORTAL_OTA, "File '%s', write %d bytes, %d of %d bytes",
fileName.c_str(),
dataLength,
result->progress,
@@ -171,7 +171,7 @@ public:
result->status = UpgradeStatus::SIZE_MISMATCH;
Log.serrorln(
FPSTR(L_PORTAL_OTA), "File '%s', size mismatch: %d of %d bytes",
L_PORTAL_OTA, "File '%s', size mismatch: %d of %d bytes",
fileName.c_str(),
result->progress,
result->size
@@ -185,12 +185,12 @@ public:
result->status = UpgradeStatus::ERROR_ON_FINISH;
result->error = Update.errorString();
Log.serrorln(FPSTR(L_PORTAL_OTA), "File '%s', on finish: %s", fileName.c_str(), result->error);
Log.serrorln(L_PORTAL_OTA, "File '%s', on finish: %s", fileName.c_str(), result->error.c_str());
return;
}
result->status = UpgradeStatus::SUCCESS;
Log.sinfoln(FPSTR(L_PORTAL_OTA), "File '%s': finish", fileName.c_str());
Log.sinfoln(L_PORTAL_OTA, "File '%s': finish", fileName.c_str());
}
}