mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-10 18:24:27 +05:00
fix: conflicts with sdk 3.x.x for esp32 fixed
This commit is contained in:
@@ -1,35 +1,35 @@
|
|||||||
#include "NetworkConnection.h"
|
#include "NetworkConnection.h"
|
||||||
using namespace Network;
|
using namespace NetworkUtils;
|
||||||
|
|
||||||
void Connection::setup(bool useDhcp) {
|
void NetworkConnection::setup(bool useDhcp) {
|
||||||
setUseDhcp(useDhcp);
|
setUseDhcp(useDhcp);
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
wifi_set_event_handler_cb(Connection::onEvent);
|
wifi_set_event_handler_cb(NetworkConnection::onEvent);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
WiFi.onEvent(Connection::onEvent);
|
WiFi.onEvent(NetworkConnection::onEvent);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::reset() {
|
void NetworkConnection::reset() {
|
||||||
status = Status::NONE;
|
status = Status::NONE;
|
||||||
disconnectReason = DisconnectReason::NONE;
|
disconnectReason = DisconnectReason::NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::setUseDhcp(bool value) {
|
void NetworkConnection::setUseDhcp(bool value) {
|
||||||
useDhcp = value;
|
useDhcp = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::Status Connection::getStatus() {
|
NetworkConnection::Status NetworkConnection::getStatus() {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection::DisconnectReason Connection::getDisconnectReason() {
|
NetworkConnection::DisconnectReason NetworkConnection::getDisconnectReason() {
|
||||||
return disconnectReason;
|
return disconnectReason;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
void Connection::onEvent(System_Event_t *event) {
|
void NetworkConnection::onEvent(System_Event_t *event) {
|
||||||
switch (event->event) {
|
switch (event->event) {
|
||||||
case EVENT_STAMODE_CONNECTED:
|
case EVENT_STAMODE_CONNECTED:
|
||||||
status = useDhcp ? Status::CONNECTING : Status::CONNECTED;
|
status = useDhcp ? Status::CONNECTING : Status::CONNECTED;
|
||||||
@@ -75,7 +75,7 @@ void Connection::onEvent(System_Event_t *event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
void Connection::onEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
void NetworkConnection::onEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
status = useDhcp ? Status::CONNECTING : Status::CONNECTED;
|
status = useDhcp ? Status::CONNECTING : Status::CONNECTED;
|
||||||
@@ -106,7 +106,7 @@ void Connection::onEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Connection::DisconnectReason Connection::convertDisconnectReason(uint8_t reason) {
|
NetworkConnection::DisconnectReason NetworkConnection::convertDisconnectReason(uint8_t reason) {
|
||||||
switch (reason) {
|
switch (reason) {
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
case REASON_BEACON_TIMEOUT:
|
case REASON_BEACON_TIMEOUT:
|
||||||
@@ -145,6 +145,6 @@ Connection::DisconnectReason Connection::convertDisconnectReason(uint8_t reason)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Connection::useDhcp = false;
|
bool NetworkConnection::useDhcp = false;
|
||||||
Connection::Status Connection::status = Status::NONE;
|
NetworkConnection::Status NetworkConnection::status = Status::NONE;
|
||||||
Connection::DisconnectReason Connection::disconnectReason = DisconnectReason::NONE;
|
NetworkConnection::DisconnectReason NetworkConnection::disconnectReason = DisconnectReason::NONE;
|
||||||
@@ -5,8 +5,8 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Network {
|
namespace NetworkUtils {
|
||||||
struct Connection {
|
struct NetworkConnection {
|
||||||
enum class Status {
|
enum class Status {
|
||||||
CONNECTED,
|
CONNECTED,
|
||||||
CONNECTING,
|
CONNECTING,
|
||||||
@@ -7,35 +7,35 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <NetworkConnection.h>
|
#include <NetworkConnection.h>
|
||||||
|
|
||||||
namespace Network {
|
namespace NetworkUtils {
|
||||||
class Manager {
|
class NetworkMgr {
|
||||||
public:
|
public:
|
||||||
typedef std::function<void()> YieldCallback;
|
typedef std::function<void()> YieldCallback;
|
||||||
typedef std::function<void(unsigned int)> DelayCallback;
|
typedef std::function<void(unsigned int)> DelayCallback;
|
||||||
|
|
||||||
Manager() {
|
NetworkMgr() {
|
||||||
Connection::setup(this->useDhcp);
|
NetworkConnection::setup(this->useDhcp);
|
||||||
this->resetWifi();
|
this->resetWifi();
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setYieldCallback(YieldCallback callback = nullptr) {
|
NetworkMgr* setYieldCallback(YieldCallback callback = nullptr) {
|
||||||
this->yieldCallback = callback;
|
this->yieldCallback = callback;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setDelayCallback(DelayCallback callback = nullptr) {
|
NetworkMgr* setDelayCallback(DelayCallback callback = nullptr) {
|
||||||
this->delayCallback = callback;
|
this->delayCallback = callback;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setHostname(const char* value) {
|
NetworkMgr* setHostname(const char* value) {
|
||||||
this->hostname = value;
|
this->hostname = value;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setApCredentials(const char* ssid, const char* password = nullptr, byte channel = 0) {
|
NetworkMgr* setApCredentials(const char* ssid, const char* password = nullptr, byte channel = 0) {
|
||||||
this->apName = ssid;
|
this->apName = ssid;
|
||||||
this->apPassword = password;
|
this->apPassword = password;
|
||||||
this->apChannel = channel;
|
this->apChannel = channel;
|
||||||
@@ -43,7 +43,7 @@ namespace Network {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setStaCredentials(const char* ssid = nullptr, const char* password = nullptr, byte channel = 0) {
|
NetworkMgr* setStaCredentials(const char* ssid = nullptr, const char* password = nullptr, byte channel = 0) {
|
||||||
this->staSsid = ssid;
|
this->staSsid = ssid;
|
||||||
this->staPassword = password;
|
this->staPassword = password;
|
||||||
this->staChannel = channel;
|
this->staChannel = channel;
|
||||||
@@ -51,14 +51,14 @@ namespace Network {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setUseDhcp(bool value) {
|
NetworkMgr* setUseDhcp(bool value) {
|
||||||
this->useDhcp = value;
|
this->useDhcp = value;
|
||||||
Connection::setup(this->useDhcp);
|
NetworkConnection::setup(this->useDhcp);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setStaticConfig(const char* ip, const char* gateway, const char* subnet, const char* dns) {
|
NetworkMgr* setStaticConfig(const char* ip, const char* gateway, const char* subnet, const char* dns) {
|
||||||
this->staticIp.fromString(ip);
|
this->staticIp.fromString(ip);
|
||||||
this->staticGateway.fromString(gateway);
|
this->staticGateway.fromString(gateway);
|
||||||
this->staticSubnet.fromString(subnet);
|
this->staticSubnet.fromString(subnet);
|
||||||
@@ -67,7 +67,7 @@ namespace Network {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Manager* setStaticConfig(IPAddress& ip, IPAddress& gateway, IPAddress& subnet, IPAddress& dns) {
|
NetworkMgr* setStaticConfig(IPAddress& ip, IPAddress& gateway, IPAddress& subnet, IPAddress& dns) {
|
||||||
this->staticIp = ip;
|
this->staticIp = ip;
|
||||||
this->staticGateway = gateway;
|
this->staticGateway = gateway;
|
||||||
this->staticSubnet = subnet;
|
this->staticSubnet = subnet;
|
||||||
@@ -81,11 +81,11 @@ namespace Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isConnected() {
|
bool isConnected() {
|
||||||
return this->isStaEnabled() && Connection::getStatus() == Connection::Status::CONNECTED;
|
return this->isStaEnabled() && NetworkConnection::getStatus() == NetworkConnection::Status::CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isConnecting() {
|
bool isConnecting() {
|
||||||
return this->isStaEnabled() && Connection::getStatus() == Connection::Status::CONNECTING;
|
return this->isStaEnabled() && NetworkConnection::getStatus() == NetworkConnection::Status::CONNECTING;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStaEnabled() {
|
bool isStaEnabled() {
|
||||||
@@ -156,7 +156,9 @@ namespace Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
|
#if !defined(ESP_ARDUINO_VERSION_MAJOR) || ESP_ARDUINO_VERSION_MAJOR < 3
|
||||||
WiFi.setAutoConnect(false);
|
WiFi.setAutoConnect(false);
|
||||||
|
#endif
|
||||||
WiFi.setAutoReconnect(false);
|
WiFi.setAutoReconnect(false);
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
@@ -253,9 +255,9 @@ namespace Network {
|
|||||||
while (millis() - beginConnectionTime < timeout) {
|
while (millis() - beginConnectionTime < timeout) {
|
||||||
this->delayCallback(100);
|
this->delayCallback(100);
|
||||||
|
|
||||||
Connection::Status status = Connection::getStatus();
|
NetworkConnection::Status status = NetworkConnection::getStatus();
|
||||||
if (status != Connection::Status::CONNECTING && status != Connection::Status::NONE) {
|
if (status != NetworkConnection::Status::CONNECTING && status != NetworkConnection::Status::NONE) {
|
||||||
return status == Connection::Status::CONNECTED;
|
return status == NetworkConnection::Status::CONNECTED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +268,7 @@ namespace Network {
|
|||||||
if (this->isConnected() && !this->hasStaCredentials()) {
|
if (this->isConnected() && !this->hasStaCredentials()) {
|
||||||
Log.sinfoln(FPSTR(L_NETWORK), F("Reset"));
|
Log.sinfoln(FPSTR(L_NETWORK), F("Reset"));
|
||||||
this->resetWifi();
|
this->resetWifi();
|
||||||
Connection::reset();
|
NetworkConnection::reset();
|
||||||
this->delayCallback(200);
|
this->delayCallback(200);
|
||||||
|
|
||||||
} else if (this->isConnected() && !this->reconnectFlag) {
|
} else if (this->isConnected() && !this->reconnectFlag) {
|
||||||
@@ -305,7 +307,7 @@ namespace Network {
|
|||||||
Log.sinfoln(
|
Log.sinfoln(
|
||||||
FPSTR(L_NETWORK),
|
FPSTR(L_NETWORK),
|
||||||
F("Disconnected, reason: %d, uptime: %lu s."),
|
F("Disconnected, reason: %d, uptime: %lu s."),
|
||||||
Connection::getDisconnectReason(),
|
NetworkConnection::getDisconnectReason(),
|
||||||
(millis() - this->connectedTime) / 1000
|
(millis() - this->connectedTime) / 1000
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -327,16 +329,16 @@ namespace Network {
|
|||||||
} else if (this->isConnecting() && millis() - this->prevReconnectingTime > this->resetConnectionTimeout) {
|
} else if (this->isConnecting() && millis() - this->prevReconnectingTime > this->resetConnectionTimeout) {
|
||||||
Log.swarningln(FPSTR(L_NETWORK), F("Connection timeout, reset wifi..."));
|
Log.swarningln(FPSTR(L_NETWORK), F("Connection timeout, reset wifi..."));
|
||||||
this->resetWifi();
|
this->resetWifi();
|
||||||
Connection::reset();
|
NetworkConnection::reset();
|
||||||
this->delayCallback(200);
|
this->delayCallback(200);
|
||||||
|
|
||||||
} else if (!this->isConnecting() && this->hasStaCredentials() && (!this->prevReconnectingTime || millis() - this->prevReconnectingTime > this->reconnectInterval)) {
|
} else if (!this->isConnecting() && this->hasStaCredentials() && (!this->prevReconnectingTime || millis() - this->prevReconnectingTime > this->reconnectInterval)) {
|
||||||
Log.sinfoln(FPSTR(L_NETWORK), F("Try connect..."));
|
Log.sinfoln(FPSTR(L_NETWORK), F("Try connect..."));
|
||||||
|
|
||||||
this->reconnectFlag = false;
|
this->reconnectFlag = false;
|
||||||
Connection::reset();
|
NetworkConnection::reset();
|
||||||
if (!this->connect(true, this->connectionTimeout)) {
|
if (!this->connect(true, this->connectionTimeout)) {
|
||||||
Log.straceln(FPSTR(L_NETWORK), F("Connection failed. Status: %d, reason: %d"), Connection::getStatus(), Connection::getDisconnectReason());
|
Log.straceln(FPSTR(L_NETWORK), F("Connection failed. Status: %d, reason: %d"), NetworkConnection::getStatus(), NetworkConnection::getDisconnectReason());
|
||||||
}
|
}
|
||||||
|
|
||||||
this->prevReconnectingTime = millis();
|
this->prevReconnectingTime = millis();
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
#include <Blinker.h>
|
#include <Blinker.h>
|
||||||
|
|
||||||
extern Network::Manager* network;
|
using namespace NetworkUtils;
|
||||||
|
|
||||||
|
extern NetworkMgr* network;
|
||||||
extern MqttTask* tMqtt;
|
extern MqttTask* tMqtt;
|
||||||
extern OpenThermTask* tOt;
|
extern OpenThermTask* tOt;
|
||||||
extern FileData fsSettings, fsNetworkSettings;
|
extern FileData fsSettings, fsNetworkSettings;
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ using WebServer = ESP8266WebServer;
|
|||||||
#include <UpgradeHandler.h>
|
#include <UpgradeHandler.h>
|
||||||
#include <DNSServer.h>
|
#include <DNSServer.h>
|
||||||
|
|
||||||
extern Network::Manager* network;
|
using namespace NetworkUtils;
|
||||||
|
|
||||||
|
extern NetworkMgr* network;
|
||||||
extern FileData fsSettings, fsNetworkSettings;
|
extern FileData fsSettings, fsNetworkSettings;
|
||||||
extern MqttTask* tMqtt;
|
extern MqttTask* tMqtt;
|
||||||
|
|
||||||
@@ -359,7 +361,7 @@ protected:
|
|||||||
for (short int i = 0; i < apCount; i++) {
|
for (short int i = 0; i < apCount; i++) {
|
||||||
String ssid = WiFi.SSID(i);
|
String ssid = WiFi.SSID(i);
|
||||||
doc[i]["ssid"] = ssid;
|
doc[i]["ssid"] = ssid;
|
||||||
doc[i]["signalQuality"] = Network::Manager::rssiToSignalQuality(WiFi.RSSI(i));
|
doc[i]["signalQuality"] = NetworkMgr::rssiToSignalQuality(WiFi.RSSI(i));
|
||||||
doc[i]["channel"] = WiFi.channel(i);
|
doc[i]["channel"] = WiFi.channel(i);
|
||||||
doc[i]["hidden"] = !ssid.length();
|
doc[i]["hidden"] = !ssid.length();
|
||||||
doc[i]["encryptionType"] = WiFi.encryptionType(i);
|
doc[i]["encryptionType"] = WiFi.encryptionType(i);
|
||||||
@@ -496,7 +498,7 @@ protected:
|
|||||||
doc["network"]["mac"] = network->getStaMac();
|
doc["network"]["mac"] = network->getStaMac();
|
||||||
doc["network"]["connected"] = isConnected;
|
doc["network"]["connected"] = isConnected;
|
||||||
doc["network"]["ssid"] = network->getStaSsid();
|
doc["network"]["ssid"] = network->getStaSsid();
|
||||||
doc["network"]["signalQuality"] = isConnected ? Network::Manager::rssiToSignalQuality(network->getRssi()) : 0;
|
doc["network"]["signalQuality"] = isConnected ? NetworkMgr::rssiToSignalQuality(network->getRssi()) : 0;
|
||||||
doc["network"]["channel"] = isConnected ? network->getStaChannel() : 0;
|
doc["network"]["channel"] = isConnected ? network->getStaChannel() : 0;
|
||||||
doc["network"]["ip"] = isConnected ? network->getStaIp().toString() : "";
|
doc["network"]["ip"] = isConnected ? network->getStaIp().toString() : "";
|
||||||
doc["network"]["subnet"] = isConnected ? network->getStaSubnet().toString() : "";
|
doc["network"]["subnet"] = isConnected ? network->getStaSubnet().toString() : "";
|
||||||
|
|||||||
12
src/main.cpp
12
src/main.cpp
@@ -4,11 +4,11 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <FileData.h>
|
#include <FileData.h>
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
#include "ESPTelnetStream.h"
|
#include <ESPTelnetStream.h>
|
||||||
#include <TinyLogger.h>
|
#include <TinyLogger.h>
|
||||||
#include <NetworkManager.h>
|
#include <NetworkMgr.h>
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include <utils.h>
|
#include "utils.h"
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
#include <ESP32Scheduler.h>
|
#include <ESP32Scheduler.h>
|
||||||
@@ -27,11 +27,13 @@
|
|||||||
#include "PortalTask.h"
|
#include "PortalTask.h"
|
||||||
#include "MainTask.h"
|
#include "MainTask.h"
|
||||||
|
|
||||||
|
using namespace NetworkUtils;
|
||||||
|
|
||||||
// Vars
|
// Vars
|
||||||
FileData fsNetworkSettings(&LittleFS, "/network.conf", 'n', &networkSettings, sizeof(networkSettings), 1000);
|
FileData fsNetworkSettings(&LittleFS, "/network.conf", 'n', &networkSettings, sizeof(networkSettings), 1000);
|
||||||
FileData fsSettings(&LittleFS, "/settings.conf", 's', &settings, sizeof(settings), 60000);
|
FileData fsSettings(&LittleFS, "/settings.conf", 's', &settings, sizeof(settings), 60000);
|
||||||
ESPTelnetStream* telnetStream = nullptr;
|
ESPTelnetStream* telnetStream = nullptr;
|
||||||
Network::Manager* network = nullptr;
|
NetworkMgr* network = nullptr;
|
||||||
|
|
||||||
// Tasks
|
// Tasks
|
||||||
MqttTask* tMqtt;
|
MqttTask* tMqtt;
|
||||||
@@ -130,7 +132,7 @@ void setup() {
|
|||||||
Log.setLevel(settings.system.debug ? TinyLogger::Level::VERBOSE : TinyLogger::Level::INFO);
|
Log.setLevel(settings.system.debug ? TinyLogger::Level::VERBOSE : TinyLogger::Level::INFO);
|
||||||
|
|
||||||
// network
|
// network
|
||||||
network = (new Network::Manager)
|
network = (new NetworkMgr)
|
||||||
->setHostname(networkSettings.hostname)
|
->setHostname(networkSettings.hostname)
|
||||||
->setStaCredentials(
|
->setStaCredentials(
|
||||||
#ifdef WOKWI
|
#ifdef WOKWI
|
||||||
|
|||||||
Reference in New Issue
Block a user