mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-04-26 12:17:56 +00:00
Improved ethernet connection
This commit is contained in:
@@ -26,6 +26,10 @@ public:
|
|||||||
virtual bool isConnected();
|
virtual bool isConnected();
|
||||||
virtual bool isConfigChanged();
|
virtual bool isConfigChanged();
|
||||||
virtual void getCurrentConfig(NetworkConfig& networkConfig);
|
virtual void getCurrentConfig(NetworkConfig& networkConfig);
|
||||||
|
virtual IPAddress getIP();
|
||||||
|
virtual IPAddress getSubnetMask();
|
||||||
|
virtual IPAddress getGateway();
|
||||||
|
virtual IPAddress getDns(uint8_t idx);
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
virtual void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
virtual void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||||
#endif
|
#endif
|
||||||
@@ -22,6 +22,10 @@ public:
|
|||||||
bool isConnected();
|
bool isConnected();
|
||||||
bool isConfigChanged();
|
bool isConfigChanged();
|
||||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||||
|
IPAddress getIP();
|
||||||
|
IPAddress getSubnetMask();
|
||||||
|
IPAddress getGateway();
|
||||||
|
IPAddress getDns(uint8_t idx);
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||||
@@ -35,6 +39,10 @@ private:
|
|||||||
bool configChanged = false;
|
bool configChanged = false;
|
||||||
unsigned long timeout = CONNECTION_TIMEOUT;
|
unsigned long timeout = CONNECTION_TIMEOUT;
|
||||||
unsigned long lastRetry = 0;
|
unsigned long lastRetry = 0;
|
||||||
|
|
||||||
|
int8_t ethPowerPin = -1;
|
||||||
|
uint8_t ethEnablePin = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -21,6 +21,10 @@ public:
|
|||||||
bool isConnected();
|
bool isConnected();
|
||||||
bool isConfigChanged();
|
bool isConfigChanged();
|
||||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||||
|
IPAddress getIP();
|
||||||
|
IPAddress getSubnetMask();
|
||||||
|
IPAddress getGateway();
|
||||||
|
IPAddress getDns(uint8_t idx);
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||||
@@ -23,6 +23,10 @@ public:
|
|||||||
bool isConnected();
|
bool isConnected();
|
||||||
bool isConfigChanged();
|
bool isConfigChanged();
|
||||||
void getCurrentConfig(NetworkConfig& networkConfig);
|
void getCurrentConfig(NetworkConfig& networkConfig);
|
||||||
|
IPAddress getIP();
|
||||||
|
IPAddress getSubnetMask();
|
||||||
|
IPAddress getGateway();
|
||||||
|
IPAddress getDns(uint8_t idx);
|
||||||
|
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||||
@@ -27,8 +27,6 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
|
|||||||
eth_phy_type_t ethType = ETH_PHY_LAN8720;
|
eth_phy_type_t ethType = ETH_PHY_LAN8720;
|
||||||
eth_clock_mode_t ethClkMode = ETH_CLOCK_GPIO0_IN;
|
eth_clock_mode_t ethClkMode = ETH_CLOCK_GPIO0_IN;
|
||||||
uint8_t ethAddr = 0;
|
uint8_t ethAddr = 0;
|
||||||
int8_t ethPowerPin = -1;
|
|
||||||
uint8_t ethEnablePin = 0;
|
|
||||||
uint8_t ethMdc = 0;
|
uint8_t ethMdc = 0;
|
||||||
uint8_t ethMdio = 0;
|
uint8_t ethMdio = 0;
|
||||||
|
|
||||||
@@ -71,33 +69,33 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
|
|||||||
|
|
||||||
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to Ethernet\n"));
|
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to Ethernet\n"));
|
||||||
|
|
||||||
#if defined(ESP32)
|
if(ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
|
||||||
if(strlen(config.hostname) > 0) {
|
#if defined(ESP32)
|
||||||
ETH.setHostname(config.hostname);
|
if(strlen(config.hostname) > 0) {
|
||||||
}
|
ETH.setHostname(config.hostname);
|
||||||
#endif
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if(strlen(config.ip) > 0) {
|
if(strlen(config.ip) > 0) {
|
||||||
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
|
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
|
||||||
ip.fromString(config.ip);
|
ip.fromString(config.ip);
|
||||||
gw.fromString(config.gateway);
|
gw.fromString(config.gateway);
|
||||||
sn.fromString(config.subnet);
|
sn.fromString(config.subnet);
|
||||||
if(strlen(config.dns1) > 0) {
|
if(strlen(config.dns1) > 0) {
|
||||||
dns1.fromString(config.dns1);
|
dns1.fromString(config.dns1);
|
||||||
} else if(strlen(config.gateway) > 0) {
|
} else if(strlen(config.gateway) > 0) {
|
||||||
dns1.fromString(config.gateway); // If no DNS, set gateway by default
|
dns1.fromString(config.gateway); // If no DNS, set gateway by default
|
||||||
}
|
}
|
||||||
if(strlen(config.dns2) > 0) {
|
if(strlen(config.dns2) > 0) {
|
||||||
dns2.fromString(config.dns2);
|
dns2.fromString(config.dns2);
|
||||||
} else if(dns1.toString().isEmpty()) {
|
} else if(dns1.toString().isEmpty()) {
|
||||||
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
|
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
|
||||||
}
|
}
|
||||||
if(!ETH.config(ip, gw, sn, dns1, dns2)) {
|
if(!ETH.config(ip, gw, sn, dns1, dns2)) {
|
||||||
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
|
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (!ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
|
|
||||||
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start Ethernet\n"));
|
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start Ethernet\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,7 +104,7 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) {
|
void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) {
|
||||||
// TODO
|
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EthernetConnectionHandler::isConnected() {
|
bool EthernetConnectionHandler::isConnected() {
|
||||||
@@ -145,4 +143,20 @@ bool EthernetConnectionHandler::isConfigChanged() {
|
|||||||
|
|
||||||
void EthernetConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
void EthernetConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||||
networkConfig = this->config;
|
networkConfig = this->config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IPAddress EthernetConnectionHandler::getIP() {
|
||||||
|
return ETH.localIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress EthernetConnectionHandler::getSubnetMask() {
|
||||||
|
return ETH.subnetMask();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress EthernetConnectionHandler::getGateway() {
|
||||||
|
return ETH.gatewayIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress EthernetConnectionHandler::getDns(uint8_t idx) {
|
||||||
|
return ETH.dnsIP(idx);
|
||||||
|
}
|
||||||
@@ -69,4 +69,20 @@ bool WiFiAccessPointConnectionHandler::isConfigChanged() {
|
|||||||
|
|
||||||
void WiFiAccessPointConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
void WiFiAccessPointConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||||
networkConfig = this->config;
|
networkConfig = this->config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiAccessPointConnectionHandler::getIP() {
|
||||||
|
return WiFi.softAPIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiAccessPointConnectionHandler::getSubnetMask() {
|
||||||
|
return WiFi.softAPSubnetMask();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiAccessPointConnectionHandler::getGateway() {
|
||||||
|
return WiFi.softAPIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiAccessPointConnectionHandler::getDns(uint8_t idx) {
|
||||||
|
return WiFi.softAPIP();
|
||||||
|
}
|
||||||
@@ -221,4 +221,20 @@ bool WiFiClientConnectionHandler::isConfigChanged() {
|
|||||||
|
|
||||||
void WiFiClientConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
void WiFiClientConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
|
||||||
networkConfig = this->config;
|
networkConfig = this->config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiClientConnectionHandler::getIP() {
|
||||||
|
return WiFi.localIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiClientConnectionHandler::getSubnetMask() {
|
||||||
|
return WiFi.subnetMask();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiClientConnectionHandler::getGateway() {
|
||||||
|
return WiFi.gatewayIP();
|
||||||
|
}
|
||||||
|
|
||||||
|
IPAddress WiFiClientConnectionHandler::getDns(uint8_t idx) {
|
||||||
|
return WiFi.dnsIP(idx);
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
#include "RemoteDebug.h"
|
#include "RemoteDebug.h"
|
||||||
#include "PriceService.h"
|
#include "PriceService.h"
|
||||||
#include "RealtimePlot.h"
|
#include "RealtimePlot.h"
|
||||||
|
#include "ConnectionHandler.h"
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
@@ -56,6 +57,7 @@ public:
|
|||||||
void setPriceSettings(String region, String currency);
|
void setPriceSettings(String region, String currency);
|
||||||
void setMeterConfig(uint8_t distributionSystem, uint16_t mainFuse, uint16_t productionCapacity);
|
void setMeterConfig(uint8_t distributionSystem, uint16_t mainFuse, uint16_t productionCapacity);
|
||||||
void setMqttHandler(AmsMqttHandler* mqttHandler);
|
void setMqttHandler(AmsMqttHandler* mqttHandler);
|
||||||
|
void setConnectionHandler(ConnectionHandler* ch);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RemoteDebug* debugger;
|
RemoteDebug* debugger;
|
||||||
@@ -76,6 +78,7 @@ private:
|
|||||||
EnergyAccounting* ea = NULL;
|
EnergyAccounting* ea = NULL;
|
||||||
RealtimePlot* rtp = NULL;
|
RealtimePlot* rtp = NULL;
|
||||||
AmsMqttHandler* mqttHandler = NULL;
|
AmsMqttHandler* mqttHandler = NULL;
|
||||||
|
ConnectionHandler* ch = NULL;
|
||||||
bool uploading = false;
|
bool uploading = false;
|
||||||
File file;
|
File file;
|
||||||
bool performRestart = false;
|
bool performRestart = false;
|
||||||
|
|||||||
@@ -154,6 +154,10 @@ void AmsWebServer::setMqttHandler(AmsMqttHandler* mqttHandler) {
|
|||||||
this->mqttHandler = mqttHandler;
|
this->mqttHandler = mqttHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AmsWebServer::setConnectionHandler(ConnectionHandler* ch) {
|
||||||
|
this->ch = ch;
|
||||||
|
}
|
||||||
|
|
||||||
void AmsWebServer::setPriceService(PriceService* ps) {
|
void AmsWebServer::setPriceService(PriceService* ps) {
|
||||||
this->ps = ps;
|
this->ps = ps;
|
||||||
}
|
}
|
||||||
@@ -249,11 +253,11 @@ void AmsWebServer::sysinfoJson() {
|
|||||||
hostname = "ams-"+chipIdStr;
|
hostname = "ams-"+chipIdStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress localIp = WiFi.localIP();
|
IPAddress localIp = ch->getIP();
|
||||||
IPAddress subnet = WiFi.subnetMask();
|
IPAddress subnet = ch->getSubnetMask();
|
||||||
IPAddress gateway = WiFi.gatewayIP();
|
IPAddress gateway = ch->getGateway();
|
||||||
IPAddress dns1 = WiFi.dnsIP(0);
|
IPAddress dns1 = ch->getDns(0);
|
||||||
IPAddress dns2 = WiFi.dnsIP(1);
|
IPAddress dns2 = ch->getDns(1);
|
||||||
|
|
||||||
char macStr[18] = { 0 };
|
char macStr[18] = { 0 };
|
||||||
char apMacStr[18] = { 0 };
|
char apMacStr[18] = { 0 };
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
extra_configs = platformio-user.ini
|
extra_configs = platformio-user.ini
|
||||||
|
|
||||||
[common]
|
[common]
|
||||||
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, FirmwareVersion, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, PriceService, EnergyAccounting, AmsMqttHandler, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, RealtimePlot, SvelteUi
|
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, FirmwareVersion, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, PriceService, EnergyAccounting, AmsMqttHandler, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, RealtimePlot, ConnectionHandler, SvelteUi
|
||||||
lib_ignore = OneWire
|
lib_ignore = OneWire
|
||||||
extra_scripts =
|
extra_scripts =
|
||||||
pre:scripts/addversion.py
|
pre:scripts/addversion.py
|
||||||
|
|||||||
@@ -1019,6 +1019,7 @@ void connectToNetwork() {
|
|||||||
toggleSetupMode();
|
toggleSetupMode();
|
||||||
}
|
}
|
||||||
ch->connect(network, sysConfig);
|
ch->connect(network, sysConfig);
|
||||||
|
ws.setConnectionHandler(ch);
|
||||||
} else {
|
} else {
|
||||||
setupMode = false;
|
setupMode = false;
|
||||||
toggleSetupMode();
|
toggleSetupMode();
|
||||||
|
|||||||
Reference in New Issue
Block a user