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

Some network changes ( missing push event at IP change, Active scan, connect best signal AP ) #17

Merged
10 changes: 10 additions & 0 deletions src/app_config.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "emonesp.h"
#include "espal.h"
#include "divert.h"
#include "net_manager.h"
#include "mqtt.h"
#include "ocpp.h"
#include "tesla_client.h"
Expand Down Expand Up @@ -264,6 +265,12 @@ void config_changed(String name)

if(name == "time_zone") {
config_set_timezone(time_zone);
} else if(name == "hostname") {
// restart network if not in ap mode
if (!net.isWifiModeApOnly()) {
net.end();
net.begin();
}
} else if(name == "flags") {
divert.setMode((config_divert_enabled() && 1 == config_charge_mode()) ? DivertMode::Eco : DivertMode::Normal);
if(mqtt_connected() != config_mqtt_enabled()) {
Expand Down Expand Up @@ -486,3 +493,6 @@ config_reset() {
LittleFS.format();
config_load_settings();
}



23 changes: 20 additions & 3 deletions src/net_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "lcd.h"
#include "espal.h"
#include "time_man.h"
#include "event.h"

#include "LedManagerTask.h"

Expand Down Expand Up @@ -73,6 +74,14 @@ void NetManagerTask::begin()
}
}

void NetManagerTask::end()
{
if(NULL != _instance)
{
MicroTask.stopTask(_instance);
KipK marked this conversation as resolved.
Show resolved Hide resolved
}
}

// -------------------------------------------------------------------
// Start Access Point
// Access point is used for wifi network selection
Expand All @@ -90,7 +99,6 @@ void NetManagerTask::wifiStartAccessPoint()

WiFi.enableAP(true);
WiFi.enableSTA(true); // Needed for scanning

WiFi.softAPConfig(_apIP, _apIP, _apNetMask);

// Create Unique SSID e.g "emonESP_XXXXXX"
Expand Down Expand Up @@ -156,6 +164,8 @@ void NetManagerTask::wifiClientConnect()

WiFi.hostname(esp_hostname.c_str());
WiFi.setSleep(WIFI_PS_NONE);
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
WiFi.begin(esid.c_str(), epass.c_str());

_clientRetryTime = millis() + WIFI_CLIENT_RETRY_TIMEOUT;
Expand All @@ -164,7 +174,7 @@ void NetManagerTask::wifiClientConnect()
void NetManagerTask::wifiScanNetworks(WiFiScanCompleteCallback callback)
{
if(WiFi.scanComplete() != WIFI_SCAN_RUNNING) {
WiFi.scanNetworks(true, false, true);
WiFi.scanNetworks(true, false, false);
}
_scanCompleteCallbacks.push_back(callback);
}
Expand Down Expand Up @@ -206,6 +216,12 @@ void NetManagerTask::wifiOnStationModeConnected(const WiFiEventStationModeConnec
void NetManagerTask::wifiOnStationModeGotIP(const WiFiEventStationModeGotIP &event)
{
haveNetworkConnection(WiFi.localIP());
StaticJsonDocument<128> doc;
doc["wifi_client_connected"] = (int)net.isWifiClientConnected();
doc["eth_connected"] = (int)net.isWiredConnected();
doc["net_connected"] = (int)net.isWifiClientConnected();
doc["ipaddress"] = net.getIp();
event_send(doc);

// Clear any error state
_clientDisconnects = 0;
Expand Down Expand Up @@ -514,6 +530,7 @@ void NetManagerTask::setup()
MDNS.addServiceTxt("openevse", "tcp", "type", buildenv.c_str());
MDNS.addServiceTxt("openevse", "tcp", "version", currentfirmware.c_str());
MDNS.addServiceTxt("openevse", "tcp", "id", ESPAL.getLongId());

}
}

Expand Down Expand Up @@ -789,4 +806,4 @@ bool NetManagerTask::isWiredConnected()
#else
return false;
#endif
}
}
5 changes: 3 additions & 2 deletions src/net_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class NetManagerTask : public MicroTasks::Task
NetManagerTask(LcdTask &lcd, LedManagerTask &led, TimeManager &time);

void begin();

void end();

void wifiScan();

void wifiStart();
Expand All @@ -183,7 +184,7 @@ class NetManagerTask : public MicroTasks::Task
void wifiTurnOnAp();

void wifiScanNetworks(WiFiScanCompleteCallback callback);

bool isConnected();
bool isWifiClientConnected();
bool isWiredConnected();
Expand Down