Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wifi Reconnect #2207 #2208

Merged
merged 20 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion interface/src/app/main/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const Dashboard = () => {
{me.admin &&
di.dv?.c &&
!hasMask(di.dv.id, DeviceEntityMask.DV_READONLY) && (
<IconButton onClick={() => editDashboardValue(di)}>
<IconButton size="small" onClick={() => editDashboardValue(di)}>
<EditIcon
color="primary"
sx={{ fontSize: 16 }}
Expand Down
10 changes: 5 additions & 5 deletions interface/src/app/status/NetworkStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ const NetworkStatus = () => {

const networkStatus = ({ status }: NetworkStatusType) => {
switch (status) {
case NetworkConnectionStatus.ETHERNET_STATUS_CONNECTED:
return LL.CONNECTED(0) + ' (Ethernet)';
case NetworkConnectionStatus.WIFI_STATUS_NO_SHIELD:
return LL.INACTIVE(1);
case NetworkConnectionStatus.WIFI_STATUS_IDLE:
return LL.IDLE();
case NetworkConnectionStatus.WIFI_STATUS_NO_SSID_AVAIL:
return 'No SSID Available';
case NetworkConnectionStatus.WIFI_STATUS_CONNECTED:
return LL.CONNECTED(0) + ' (WiFi)';
case NetworkConnectionStatus.ETHERNET_STATUS_CONNECTED:
return LL.CONNECTED(0) + ' (Ethernet)';
return LL.CONNECTED(0) + ' (WiFi) (' + data.connect_count + ')';
case NetworkConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
return LL.CONNECTED(1) + ' ' + LL.FAILED(0);
return LL.CONNECTED(1) + ' ' + LL.FAILED(0) + ' (' + data.connect_count + ')';
case NetworkConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return LL.CONNECTED(1) + ' ' + LL.LOST();
return LL.CONNECTED(1) + ' ' + LL.LOST() + ' (' + data.connect_count + ')';
case NetworkConnectionStatus.WIFI_STATUS_DISCONNECTED:
return LL.DISCONNECTED();
default:
Expand Down
1 change: 1 addition & 0 deletions interface/src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface NetworkStatusType {
dns_ip_1: string;
dns_ip_2: string;
hostname: string;
connect_count: number;
}

export interface NetworkSettingsType {
Expand Down
4 changes: 4 additions & 0 deletions lib/framework/ESP8266React.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class ESP8266React {
return _apSettingsService.getAPNetworkStatus() == APNetworkStatus::ACTIVE;
}

uint16_t getWifiConnects() {
return _networkSettingsService.getWifiConnects();
}

private:
SecuritySettingsService _securitySettingsService;
NetworkSettingsService _networkSettingsService;
Expand Down
6 changes: 5 additions & 1 deletion lib/framework/NetworkSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,14 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
break;

case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
emsesp::EMSESP::logger().warning("WiFi disconnected. Reason: %s (%d)",
connectcount_++; // count the number of reconnects
emsesp::EMSESP::logger().warning("WiFi disconnected (#%d). Reason: %s (%d)",
connectcount_,
disconnectReason(info.wifi_sta_disconnected.reason),
info.wifi_sta_disconnected.reason); // IDF 4.0
emsesp::EMSESP::system_.has_ipv6(false);


break;

case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Expand Down
7 changes: 6 additions & 1 deletion lib/framework/NetworkSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,26 @@ class NetworkSettings {
static StateUpdateResult update(JsonObject root, NetworkSettings & settings);
};


class NetworkSettingsService : public StatefulService<NetworkSettings> {
public:
NetworkSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);

void begin();
void loop();

uint16_t getWifiConnects() const {
return connectcount_;
}

private:
HttpEndpoint<NetworkSettings> _httpEndpoint;
FSPersistence<NetworkSettings> _fsPersistence;

unsigned long _lastConnectionAttempt;
bool _stopping;

uint16_t connectcount_ = 0; // number of wifi reconnects

void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
void mDNS_start() const;
const char * disconnectReason(uint8_t code);
Expand Down
13 changes: 7 additions & 6 deletions lib/framework/NetworkStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) {
#else
root["local_ipv6"] = WiFi.linkLocalIPv6().toString();
#endif
root["mac_address"] = WiFi.macAddress();
root["rssi"] = WiFi.RSSI();
root["ssid"] = WiFi.SSID();
root["bssid"] = WiFi.BSSIDstr();
root["channel"] = WiFi.channel();
root["subnet_mask"] = WiFi.subnetMask().toString();
root["mac_address"] = WiFi.macAddress();
root["rssi"] = WiFi.RSSI();
root["ssid"] = WiFi.SSID();
root["bssid"] = WiFi.BSSIDstr();
root["channel"] = WiFi.channel();
root["connect_count"] = emsesp::EMSESP::esp8266React.getWifiConnects();
root["subnet_mask"] = WiFi.subnetMask().toString();

if (WiFi.gatewayIP() != INADDR_NONE) {
root["gateway_ip"] = WiFi.gatewayIP().toString();
Expand Down
3 changes: 2 additions & 1 deletion mock-api/rest_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ const network_status = {
gateway_ip: '10.10.10.1',
dns_ip_1: '10.10.10.1',
dns_ip_2: '0.0.0.0',
hostname: 'ems-esp'
hostname: 'ems-esp',
connect_count: 1
};
const list_networks = {
networks: [
Expand Down
2 changes: 1 addition & 1 deletion src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,10 +1776,10 @@ void EMSESP::loop() {
void EMSESP::start_serial_console() {
shell_ = std::make_shared<EMSESPConsole>(*this, serial_console_, true);
shell_->maximum_log_messages(100);
shell_->add_flags(CommandFlags::ADMIN); // always start in su/admin mode when running tests
shell_->start();
#if defined(EMSESP_DEBUG)
shell_->log_level(uuid::log::Level::DEBUG);
shell_->add_flags(CommandFlags::ADMIN); // always start in su/admin mode when compiled with debug
#else
shell_->log_level(uuid::log::Level::TRACE);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ void Mqtt::ha_status() {
publish_system_ha_sensor_config(DeviceValueType::INT8, "Tx reads", "txreads", DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT8, "Tx writes", "txwrites", DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT8, "Tx fails", "txfails", DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT16, "WiFi reconnects", "wificonnects", DeviceValueUOM::NONE);

// This comes from the info MQTT topic
publish_system_ha_sensor_config(DeviceValueType::STRING, "Version", "version", DeviceValueUOM::NONE);
Expand Down
19 changes: 7 additions & 12 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,7 @@ void System::heartbeat_json(JsonObject output) {

output["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3);
output["uptime_sec"] = uuid::get_uptime_sec();
bool value_b = ntp_connected();
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
output["ntp_status"] = value_b;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
output["ntp_status"] = value_b ? 1 : 0;
} else {
char s[12];
output["ntp_status"] = Helpers::render_boolean(s, value_b);
}

output["rxreceived"] = EMSESP::rxservice_.telegram_count();
output["rxfails"] = EMSESP::rxservice_.telegram_error_count();
output["txreads"] = EMSESP::txservice_.telegram_read_count();
Expand Down Expand Up @@ -724,6 +716,7 @@ void System::heartbeat_json(JsonObject output) {
int8_t rssi = WiFi.RSSI();
output["rssi"] = rssi;
output["wifistrength"] = wifi_quality(rssi);
output["wificonnects"] = EMSESP::esp8266React.getWifiConnects();
}
#endif
}
Expand Down Expand Up @@ -1504,9 +1497,10 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
// node["IPv6 address"] = uuid::printable_to_string(ETH.localIPv6());
// }
} else if (WiFi.status() == WL_CONNECTED) {
node["network"] = "WiFi";
node["hostname"] = WiFi.getHostname();
node["RSSI"] = WiFi.RSSI();
node["network"] = "WiFi";
node["hostname"] = WiFi.getHostname();
node["RSSI"] = WiFi.RSSI();
node["WIFIConnects"] = EMSESP::esp8266React.getWifiConnects();
// node["MAC"] = WiFi.macAddress();
// node["IPv4 address"] = uuid::printable_to_string(WiFi.localIP()) + "/" + uuid::printable_to_string(WiFi.subnetMask());
// node["IPv4 gateway"] = uuid::printable_to_string(WiFi.gatewayIP());
Expand Down Expand Up @@ -1535,6 +1529,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
node["CORSOrigin"] = settings.CORSOrigin;
}
});

#ifndef EMSESP_STANDALONE
EMSESP::esp8266React.getAPSettingsService()->read([&](const APSettings & settings) {
const char * pM[] = {"always", "disconnected", "never"};
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.7.1-dev.7"
#define EMSESP_APP_VERSION "3.7.1-dev.8"