mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 09:19:51 +03:00
add Ethernet
This commit is contained in:
@@ -22,39 +22,81 @@ namespace emsesp {
|
||||
|
||||
WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
|
||||
// rest endpoint for web page
|
||||
server->on(EMSESP_STATUS_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1),
|
||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
|
||||
// trigger on wifi connects/disconnects
|
||||
#ifdef ESP32
|
||||
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
||||
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
||||
#elif defined(ESP8266)
|
||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
||||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
|
||||
#endif
|
||||
server->on(EMSESP_STATUS_SERVICE_PATH, HTTP_GET, securityManager->wrapRequest(std::bind(&WebStatusService::webStatusService, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
WiFi.onEvent(std::bind(&WebStatusService::WiFiEvent, this, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
void WebStatusService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
|
||||
}
|
||||
void WebStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
||||
EMSESP::system_.init_wifi(); // send out heartbeat MQTT as soon as we have a connection
|
||||
}
|
||||
#elif defined(ESP8266)
|
||||
void WebStatusService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
|
||||
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), event.reason);
|
||||
}
|
||||
void WebStatusService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
|
||||
EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), event.ip.toString().c_str(), WiFi.hostname().c_str());
|
||||
EMSESP::system_.init_wifi(); // send out heartbeat MQTT as soon as we have a connection
|
||||
}
|
||||
// handles both WiFI and Ethernet
|
||||
void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
|
||||
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
#ifndef EMSESP_STANDALONE
|
||||
EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
||||
#endif
|
||||
|
||||
// Lower WiFi tx power to free up current for bus-powered
|
||||
/*
|
||||
WIFI_POWER_19_5dBm = 78,// 19.5dBm <-- default
|
||||
WIFI_POWER_19dBm = 76,// 19dBm
|
||||
WIFI_POWER_18_5dBm = 74,// 18.5dBm
|
||||
WIFI_POWER_17dBm = 68,// 17dBm
|
||||
WIFI_POWER_15dBm = 60,// 15dBm
|
||||
WIFI_POWER_13dBm = 52,// 13dBm
|
||||
WIFI_POWER_11dBm = 44,// 11dBm
|
||||
WIFI_POWER_8_5dBm = 34,// 8.5dBm
|
||||
WIFI_POWER_7dBm = 28,// 7dBm
|
||||
WIFI_POWER_5dBm = 20,// 5dBm
|
||||
WIFI_POWER_2dBm = 8,// 2dBm
|
||||
WIFI_POWER_MINUS_1dBm = -4// -1dBm
|
||||
*/
|
||||
|
||||
/*
|
||||
wifi_power_t a1 = WiFi.getTxPower();
|
||||
// bool ok = WiFi.setTxPower(WIFI_POWER_17dBm); // lower power
|
||||
bool ok = WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
||||
wifi_power_t a2 = WiFi.getTxPower();
|
||||
LOG_INFO("Adjusting Wifi Tx power from %d to %d (%s)", a1, a2, ok ? "ok" : "failed");
|
||||
*/
|
||||
|
||||
EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_ETH_START:
|
||||
EMSESP::logger().info(F("Ethernet Started"));
|
||||
ETH.setHostname(emsesp::System::hostname().c_str());
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_ETH_GOT_IP:
|
||||
// prevent double calls
|
||||
if (!connected_) {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed());
|
||||
#endif
|
||||
EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection
|
||||
connected_ = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_ETH_DISCONNECTED:
|
||||
EMSESP::logger().info(F("Ethernet Disconnected"));
|
||||
connected_ = false;
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_ETH_STOP:
|
||||
EMSESP::logger().info(F("Ethernet Stopped"));
|
||||
connected_ = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
|
||||
AsyncJsonResponse * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_MEDIUM_DYN);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
@@ -34,18 +34,9 @@ class WebStatusService {
|
||||
WebStatusService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
private:
|
||||
bool connected_ = false;
|
||||
void webStatusService(AsyncWebServerRequest * request);
|
||||
|
||||
#ifdef ESP32
|
||||
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
|
||||
#elif defined(ESP8266)
|
||||
WiFiEventHandler _onStationModeDisconnectedHandler;
|
||||
WiFiEventHandler _onStationModeGotIPHandler;
|
||||
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
|
||||
static void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
|
||||
#endif
|
||||
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -74,7 +74,7 @@ void EMSESPShell::display_banner() {
|
||||
println();
|
||||
|
||||
// set console name
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { console_hostname_ = wifiSettings.hostname.c_str(); });
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { console_hostname_ = networkSettings.hostname.c_str(); });
|
||||
|
||||
if (console_hostname_.empty()) {
|
||||
console_hostname_.resize(16, '\0');
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
* Lightweight queue & array
|
||||
* Based ideas from https://github.com/muwerk/ustd
|
||||
* Limits to max 255 entries
|
||||
* Was used in testing with ESP8266 to overcome heap memory issues - no longer used
|
||||
*/
|
||||
|
||||
#ifndef EMSESP_CONTAINERS_H
|
||||
|
||||
@@ -31,9 +31,6 @@ AsyncWebServer webServer(80);
|
||||
#if defined(ESP32)
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &SPIFFS);
|
||||
WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &SPIFFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#elif defined(ESP8266)
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
|
||||
WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#elif defined(EMSESP_STANDALONE)
|
||||
FS dummyFS;
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &dummyFS);
|
||||
@@ -1027,11 +1024,9 @@ void EMSESP::send_raw_telegram(const char * data) {
|
||||
// start all the core services
|
||||
// the services must be loaded in the correct order
|
||||
void EMSESP::start() {
|
||||
// start the file system. We use LittleFS for ESP8266.
|
||||
#ifdef ESP32
|
||||
// start the file system
|
||||
#ifndef EMSESP_STANDALONE
|
||||
SPIFFS.begin(true);
|
||||
#elif defined(ESP8266)
|
||||
LittleFS.begin();
|
||||
#endif
|
||||
|
||||
esp8266React.begin(); // loads system settings (wifi, mqtt, etc)
|
||||
@@ -1039,25 +1034,11 @@ void EMSESP::start() {
|
||||
|
||||
system_.check_upgrade(); // do any upgrades
|
||||
|
||||
#if defined(EMSESP_DEBUG)
|
||||
#ifndef EMSESP_STANDALONE
|
||||
uint32_t tbefore = ESP.getFreeHeap();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Load our library of known devices into stack mem. Names are stored in Flash memory
|
||||
// Still it takes up about 960bytes
|
||||
device_library_.reserve(80);
|
||||
// Load our library of known devices into stack mem. Names are stored in Flash memory (take about 960bytes)
|
||||
device_library_ = {
|
||||
#include "device_library.h"
|
||||
};
|
||||
|
||||
#if defined(EMSESP_DEBUG)
|
||||
#ifndef EMSESP_STANDALONE
|
||||
uint32_t tafter = ESP.getFreeHeap();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
console_.start(); // telnet and serial console
|
||||
mqtt_.start(); // mqtt init
|
||||
system_.start(heap_start); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
|
||||
@@ -1069,13 +1050,6 @@ void EMSESP::start() {
|
||||
|
||||
LOG_INFO(F("EMS Device library loaded with %d records"), device_library_.size());
|
||||
|
||||
#if defined(EMSESP_DEBUG)
|
||||
#ifndef EMSESP_STANDALONE
|
||||
LOG_INFO(F("Used %d mem for devices"), tbefore - tafter);
|
||||
System::show_mem("after start()");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(EMSESP_STANDALONE)
|
||||
Mqtt::on_connect(); // simulate an MQTT connection
|
||||
#endif
|
||||
|
||||
@@ -40,7 +40,6 @@
|
||||
#include "WebSettingsService.h"
|
||||
#include "WebAPIService.h"
|
||||
|
||||
// #include "containers.h"
|
||||
#include "emsdevice.h"
|
||||
#include "emsfactory.h"
|
||||
#include "telegram.h"
|
||||
|
||||
@@ -96,7 +96,7 @@ MAKE_PSTR_WORD(unknown)
|
||||
MAKE_PSTR(EMSESP, "EMS-ESP")
|
||||
MAKE_PSTR(master_thermostat_fmt, "Master Thermostat Device ID = %s")
|
||||
MAKE_PSTR(host_fmt, "Host = %s")
|
||||
MAKE_PSTR(hostname_fmt, "WiFi Hostname = %s")
|
||||
MAKE_PSTR(hostname_fmt, "Hostname = %s")
|
||||
MAKE_PSTR(mark_interval_fmt, "Mark interval = %lus")
|
||||
MAKE_PSTR(wifi_ssid_fmt, "WiFi SSID = %s")
|
||||
MAKE_PSTR(wifi_password_fmt, "WiFi Password = %S")
|
||||
|
||||
@@ -340,7 +340,7 @@ void Mqtt::start() {
|
||||
mqttClient_ = EMSESP::esp8266React.getMqttClient();
|
||||
|
||||
// get the hostname, which we'll use to prefix to all topics
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { hostname_ = wifiSettings.hostname.c_str(); });
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { hostname_ = networkSettings.hostname.c_str(); });
|
||||
|
||||
// fetch MQTT settings, to see if MQTT is enabled
|
||||
EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & mqttSettings) {
|
||||
|
||||
165
src/system.cpp
165
src/system.cpp
@@ -107,9 +107,9 @@ void System::restart() {
|
||||
void System::wifi_reconnect() {
|
||||
LOG_INFO(F("Wifi reconnecting..."));
|
||||
Shell::loop_all();
|
||||
delay(1000); // wait a second
|
||||
EMSESP::webSettingsService.save(); // local settings
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password
|
||||
delay(1000); // wait a second
|
||||
EMSESP::webSettingsService.save(); // local settings
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->callUpdateHandlers("local"); // in case we've changed ssid or password
|
||||
}
|
||||
|
||||
// format fs
|
||||
@@ -164,7 +164,7 @@ void System::syslog_init() {
|
||||
syslog_.log_level((uuid::log::Level)syslog_level_);
|
||||
syslog_.mark_interval(syslog_mark_interval_);
|
||||
syslog_.destination(addr);
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { syslog_.hostname(wifiSettings.hostname.c_str()); });
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { syslog_.hostname(networkSettings.hostname.c_str()); });
|
||||
|
||||
EMSESP::logger().info(F("Syslog started"));
|
||||
#endif
|
||||
@@ -182,7 +182,7 @@ void System::start(uint32_t heap_start) {
|
||||
#endif
|
||||
|
||||
// print boot message
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), wifiSettings.hostname.c_str(), EMSESP_APP_VERSION); });
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); });
|
||||
|
||||
// these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""}
|
||||
EMSESP::webSettingsService.read([&](WebSettings & settings) {
|
||||
@@ -198,6 +198,42 @@ void System::start(uint32_t heap_start) {
|
||||
#endif
|
||||
});
|
||||
|
||||
// check ethernet profile, if we're using exclusive Ethernet then disabled wifi and AP/captive portal
|
||||
uint8_t ethernet_profile;
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { ethernet_profile = settings.ethernet_profile; });
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
uint8_t phy_addr = 0; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
|
||||
int power = -1; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source)
|
||||
int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY
|
||||
int mdio = 18; // Pin# of the I²C IO signal for the Ethernet PHY
|
||||
eth_phy_type_t type = ETH_PHY_LAN8720; // Type of the Ethernet PHY (LAN8720 or TLK110)
|
||||
eth_clock_mode_t clock_mode = ETH_CLOCK_GPIO0_IN; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock
|
||||
|
||||
// see if we can start it using default settings
|
||||
if (!ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) {
|
||||
// it failed. Now try again based on profile. 0 is the same as 1. 2 is for TLK110
|
||||
if (ethernet_profile == 2) {
|
||||
if (ETH.begin(31, power, mdc, mdio, ETH_PHY_TLK110, clock_mode)) {
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->update(
|
||||
[&](NetworkSettings & settings) {
|
||||
settings.ssid == ""; // remove SSID
|
||||
return StateUpdateResult::CHANGED;
|
||||
},
|
||||
"local");
|
||||
|
||||
EMSESP::esp8266React.getAPSettingsService()->update(
|
||||
[&](APSettings & settings) {
|
||||
settings.provisionMode = AP_MODE_NEVER;
|
||||
return StateUpdateResult::CHANGED;
|
||||
},
|
||||
"local");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// continue with init'ing services
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -217,9 +253,9 @@ void System::init() {
|
||||
|
||||
syslog_init(); // init SysLog
|
||||
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) { hostname(settings.hostname.c_str()); });
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { hostname(settings.hostname.c_str()); });
|
||||
|
||||
#if defined(ESP32)
|
||||
#ifndef EMSESP_STANDALONE
|
||||
// setCpuFrequencyMhz(160); // default is 240
|
||||
|
||||
// disable bluetooth
|
||||
@@ -288,7 +324,7 @@ void System::loop() {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#if defined(EMSESP_DEBUG)
|
||||
static uint32_t last_memcheck_ = 0;
|
||||
if (currentMillis - last_memcheck_ > 5000) { // 5 seconds
|
||||
if (currentMillis - last_memcheck_ > 10000) { // 10 seconds
|
||||
last_memcheck_ = currentMillis;
|
||||
show_mem("core");
|
||||
}
|
||||
@@ -391,32 +427,9 @@ void System::set_led_speed(uint32_t speed) {
|
||||
led_monitor();
|
||||
}
|
||||
|
||||
void System::init_wifi() {
|
||||
void System::init_network() {
|
||||
last_system_check_ = 0; // force the LED to go from fast flash to pulse
|
||||
send_heartbeat();
|
||||
|
||||
#if defined(ESP32)
|
||||
// TODO wifi tx power
|
||||
/*
|
||||
WIFI_POWER_19_5dBm = 78,// 19.5dBm
|
||||
WIFI_POWER_19dBm = 76,// 19dBm
|
||||
WIFI_POWER_18_5dBm = 74,// 18.5dBm
|
||||
WIFI_POWER_17dBm = 68,// 17dBm
|
||||
WIFI_POWER_15dBm = 60,// 15dBm
|
||||
WIFI_POWER_13dBm = 52,// 13dBm
|
||||
WIFI_POWER_11dBm = 44,// 11dBm
|
||||
WIFI_POWER_8_5dBm = 34,// 8.5dBm
|
||||
WIFI_POWER_7dBm = 28,// 7dBm
|
||||
WIFI_POWER_5dBm = 20,// 5dBm
|
||||
WIFI_POWER_2dBm = 8,// 2dBm
|
||||
WIFI_POWER_MINUS_1dBm = -4// -1dBm
|
||||
*/
|
||||
// wifi_power_t a1 = WiFi.getTxPower();
|
||||
// // bool ok = WiFi.setTxPower(WIFI_POWER_17dBm);
|
||||
// bool ok = true;
|
||||
// wifi_power_t a2 = WiFi.getTxPower();
|
||||
// LOG_INFO("Wifi Tx power was %d, is now %d, ok=%s", a1, a2, ok ? "ok" : "failed");
|
||||
#endif
|
||||
}
|
||||
|
||||
// check health of system, done every few seconds
|
||||
@@ -512,28 +525,9 @@ void System::show_system(uuid::console::Shell & shell) {
|
||||
shell.printfln(F("Uptime: %s"), uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3).c_str());
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#if defined(ESP8266)
|
||||
shell.printfln(F("Chip ID: 0x%08x"), ESP.getChipId());
|
||||
shell.printfln(F("SDK version: %s"), ESP.getSdkVersion());
|
||||
shell.printfln(F("Core version: %s"), ESP.getCoreVersion().c_str());
|
||||
shell.printfln(F("Full version: %s"), ESP.getFullVersion().c_str());
|
||||
shell.printfln(F("Boot version: %u"), ESP.getBootVersion());
|
||||
shell.printfln(F("Boot mode: %u"), ESP.getBootMode());
|
||||
shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz());
|
||||
shell.printfln(F("Flash chip: 0x%08X (%u bytes)"), ESP.getFlashChipId(), ESP.getFlashChipRealSize());
|
||||
shell.printfln(F("Reset reason: %s"), ESP.getResetReason().c_str());
|
||||
shell.printfln(F("Reset info: %s"), ESP.getResetInfo().c_str());
|
||||
#elif defined(ESP32)
|
||||
shell.printfln(F("SDK version: %s"), ESP.getSdkVersion());
|
||||
shell.printfln(F("CPU frequency: %u MHz"), ESP.getCpuFreqMHz());
|
||||
#endif
|
||||
shell.printfln(F("Sketch size: %u bytes (%u bytes free)"), ESP.getSketchSize(), ESP.getFreeSketchSpace());
|
||||
shell.printfln(F("Free heap: %lu bytes"), (uint32_t)ESP.getFreeHeap());
|
||||
#if defined(ESP8266)
|
||||
shell.printfln(F("Heap fragmentation: %u %%"), ESP.getHeapFragmentation());
|
||||
shell.printfln(F("Maximum free block size: %lu bytes"), (uint32_t)ESP.getMaxFreeBlockSize());
|
||||
shell.printfln(F("Free continuations stack: %lu bytes"), (uint32_t)ESP.getFreeContStack());
|
||||
#endif
|
||||
shell.println();
|
||||
|
||||
switch (WiFi.status()) {
|
||||
@@ -555,12 +549,7 @@ void System::show_system(uuid::console::Shell & shell) {
|
||||
shell.printfln(F("BSSID: %s"), WiFi.BSSIDstr().c_str());
|
||||
shell.printfln(F("RSSI: %d dBm (%d %%)"), WiFi.RSSI(), wifi_quality());
|
||||
shell.printfln(F("MAC address: %s"), WiFi.macAddress().c_str());
|
||||
|
||||
#if defined(ESP8266)
|
||||
shell.printfln(F("Hostname: %s"), WiFi.hostname().c_str());
|
||||
#elif defined(ESP32)
|
||||
shell.printfln(F("Hostname: %s"), WiFi.getHostname());
|
||||
#endif
|
||||
shell.printfln(F("IPv4 address: %s/%s"), uuid::printable_to_string(WiFi.localIP()).c_str(), uuid::printable_to_string(WiFi.subnetMask()).c_str());
|
||||
shell.printfln(F("IPv4 gateway: %s"), uuid::printable_to_string(WiFi.gatewayIP()).c_str());
|
||||
shell.printfln(F("IPv4 nameserver: %s"), uuid::printable_to_string(WiFi.dnsIP()).c_str());
|
||||
@@ -584,6 +573,20 @@ void System::show_system(uuid::console::Shell & shell) {
|
||||
break;
|
||||
}
|
||||
|
||||
shell.println();
|
||||
|
||||
// show Ethernet
|
||||
if (ETH.linkUp()) {
|
||||
shell.printfln(F("Ethernet: Connected"));
|
||||
shell.printfln(F("MAC address: %s"), ETH.macAddress().c_str());
|
||||
shell.printfln(F("Hostname: %s"), ETH.getHostname());
|
||||
shell.printfln(F("IPv4 address: %s/%s"), uuid::printable_to_string(ETH.localIP()).c_str(), uuid::printable_to_string(ETH.subnetMask()).c_str());
|
||||
shell.printfln(F("IPv4 gateway: %s"), uuid::printable_to_string(ETH.gatewayIP()).c_str());
|
||||
shell.printfln(F("IPv4 nameserver: %s"), uuid::printable_to_string(ETH.dnsIP()).c_str());
|
||||
} else {
|
||||
shell.printfln(F("Ethernet: disconnected"));
|
||||
}
|
||||
|
||||
EMSESP::webSettingsService.read([&](WebSettings & settings) {
|
||||
shell.println();
|
||||
|
||||
@@ -658,15 +661,15 @@ void System::console_commands(Shell & shell, unsigned int context) {
|
||||
|
||||
EMSESPShell::commands->add_command(ShellContext::SYSTEM,
|
||||
CommandFlags::ADMIN,
|
||||
flash_string_vector{F_(set), F_(wifi), F_(hostname)},
|
||||
flash_string_vector{F_(set), F_(hostname)},
|
||||
flash_string_vector{F_(name_mandatory)},
|
||||
[](Shell & shell __attribute__((unused)), const std::vector<std::string> & arguments) {
|
||||
shell.println("The wifi connection will be reset...");
|
||||
shell.println("The network connection will be reset...");
|
||||
Shell::loop_all();
|
||||
delay(1000); // wait a second
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->update(
|
||||
[&](WiFiSettings & wifiSettings) {
|
||||
wifiSettings.hostname = arguments.front().c_str();
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->update(
|
||||
[&](NetworkSettings & networkSettings) {
|
||||
networkSettings.hostname = arguments.front().c_str();
|
||||
return StateUpdateResult::CHANGED;
|
||||
},
|
||||
"local");
|
||||
@@ -677,8 +680,8 @@ void System::console_commands(Shell & shell, unsigned int context) {
|
||||
flash_string_vector{F_(set), F_(wifi), F_(ssid)},
|
||||
flash_string_vector{F_(name_mandatory)},
|
||||
[](Shell & shell, const std::vector<std::string> & arguments) {
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) {
|
||||
wifiSettings.ssid = arguments.front().c_str();
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) {
|
||||
networkSettings.ssid = arguments.front().c_str();
|
||||
return StateUpdateResult::CHANGED;
|
||||
});
|
||||
shell.println("Use `wifi reconnect` to apply the new settings");
|
||||
@@ -690,8 +693,8 @@ void System::console_commands(Shell & shell, unsigned int context) {
|
||||
shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) {
|
||||
if (completed) {
|
||||
if (password1 == password2) {
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->updateWithoutPropagation([&](WiFiSettings & wifiSettings) {
|
||||
wifiSettings.password = password2.c_str();
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->updateWithoutPropagation([&](NetworkSettings & networkSettings) {
|
||||
networkSettings.password = password2.c_str();
|
||||
return StateUpdateResult::CHANGED;
|
||||
});
|
||||
shell.println("Use `wifi reconnect` to apply the new settings");
|
||||
@@ -705,16 +708,16 @@ void System::console_commands(Shell & shell, unsigned int context) {
|
||||
});
|
||||
|
||||
EMSESPShell::commands->add_command(ShellContext::SYSTEM, CommandFlags::USER, flash_string_vector{F_(set)}, [](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) {
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
|
||||
shell.print(F(" "));
|
||||
shell.printfln(F_(hostname_fmt), wifiSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.hostname.c_str());
|
||||
shell.printfln(F_(hostname_fmt), networkSettings.hostname.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.hostname.c_str());
|
||||
});
|
||||
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & wifiSettings) {
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
|
||||
shell.print(F(" "));
|
||||
shell.printfln(F_(wifi_ssid_fmt), wifiSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : wifiSettings.ssid.c_str());
|
||||
shell.printfln(F_(wifi_ssid_fmt), networkSettings.ssid.isEmpty() ? uuid::read_flash_string(F_(unset)).c_str() : networkSettings.ssid.c_str());
|
||||
shell.print(F(" "));
|
||||
shell.printfln(F_(wifi_password_fmt), wifiSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks));
|
||||
shell.printfln(F_(wifi_password_fmt), networkSettings.ssid.isEmpty() ? F_(unset) : F_(asterisks));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -802,17 +805,17 @@ bool System::check_upgrade() {
|
||||
EMSESP::esp8266React.begin();
|
||||
EMSESP::webSettingsService.begin();
|
||||
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->update(
|
||||
[&](WiFiSettings & wifiSettings) {
|
||||
wifiSettings.hostname = general["hostname"] | FACTORY_WIFI_HOSTNAME;
|
||||
wifiSettings.ssid = network["ssid"] | FACTORY_WIFI_SSID;
|
||||
wifiSettings.password = network["password"] | FACTORY_WIFI_PASSWORD;
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->update(
|
||||
[&](NetworkSettings & networkSettings) {
|
||||
networkSettings.hostname = general["hostname"] | FACTORY_WIFI_HOSTNAME;
|
||||
networkSettings.ssid = network["ssid"] | FACTORY_WIFI_SSID;
|
||||
networkSettings.password = network["password"] | FACTORY_WIFI_PASSWORD;
|
||||
|
||||
wifiSettings.staticIPConfig = false;
|
||||
JsonUtils::readIP(network, "staticip", wifiSettings.localIP);
|
||||
JsonUtils::readIP(network, "dnsip", wifiSettings.dnsIP1);
|
||||
JsonUtils::readIP(network, "gatewayip", wifiSettings.gatewayIP);
|
||||
JsonUtils::readIP(network, "nmask", wifiSettings.subnetMask);
|
||||
networkSettings.staticIPConfig = false;
|
||||
JsonUtils::readIP(network, "staticip", networkSettings.localIP);
|
||||
JsonUtils::readIP(network, "dnsip", networkSettings.dnsIP1);
|
||||
JsonUtils::readIP(network, "gatewayip", networkSettings.gatewayIP);
|
||||
JsonUtils::readIP(network, "nmask", networkSettings.subnetMask);
|
||||
|
||||
return StateUpdateResult::CHANGED;
|
||||
},
|
||||
@@ -922,7 +925,7 @@ bool System::check_upgrade() {
|
||||
// e.g. http://ems-esp/api?device=system&cmd=settings
|
||||
// value and id are ignored
|
||||
bool System::command_settings(const char * value, const int8_t id, JsonObject & json) {
|
||||
EMSESP::esp8266React.getWiFiSettingsService()->read([&](WiFiSettings & settings) {
|
||||
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
|
||||
JsonObject node = json.createNestedObject("WIFI");
|
||||
node["ssid"] = settings.ssid;
|
||||
// node["password"] = settings.password;
|
||||
|
||||
@@ -68,7 +68,7 @@ class System {
|
||||
static void upload_status(bool in_progress);
|
||||
static bool upload_status();
|
||||
static void show_mem(const char * note);
|
||||
void init_wifi();
|
||||
void init_network();
|
||||
static void init();
|
||||
static void led_init();
|
||||
static void syslog_init();
|
||||
|
||||
Reference in New Issue
Block a user