Skip to content

Commit

Permalink
Merge pull request #294 from kivancsikert/mqtt/init-once
Browse files Browse the repository at this point in the history
Init WiFi and MQTT only once
  • Loading branch information
lptr authored Dec 15, 2024
2 parents e015def + 526065f commit bf24916
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 233 deletions.
14 changes: 13 additions & 1 deletion data-templates/device-config-wokwi.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{
"id": "wokwi",
"instance": "wokwi",
"location": "local",
"location": "bumblebee",
"peripherals": [
{
"name": "flow-control-a",
"type": "flow-control",
"params": {
"valve": {
"motor": "a"
},
"flow-meter": {
"pin": "A1"
}
}
}
],
"sleepWhenIdle": true
}
12 changes: 6 additions & 6 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FILE(GLOB_RECURSE app_sources main.cpp **/*.cpp)

idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
EMBED_TXTFILES "${CMAKE_SOURCE_DIR}/partitions.csv"
)

if(NOT DEFINED WOKWI)
set(WOKWI "$ENV{WOKWI}")
endif()
Expand All @@ -11,12 +17,6 @@ if(WOKWI)
component_compile_definitions(WOKWI)
endif()

idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
EMBED_TXTFILES "${CMAKE_SOURCE_DIR}/partitions.csv"
)

component_compile_definitions("${UD_GEN}")
component_compile_definitions(FARMHUB_REPORT_MEMORY)

Expand Down
2 changes: 1 addition & 1 deletion main/kernel/BootClock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct boot_clock {
return time_point(duration(esp_timer_get_time()));
}

static time_point boot_time() noexcept {
static time_point zero() noexcept {
return time_point(duration(0));
}
};
Expand Down
6 changes: 6 additions & 0 deletions main/kernel/Console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class ConsoleProvider {
case Level::Info:
count += printf(FARMHUB_LOG_COLOR(FARMHUB_LOG_COLOR_GREEN));
break;
case Level::Debug:
count += printf(FARMHUB_LOG_COLOR(FARMHUB_LOG_COLOR_CYAN));
break;
case Level::Verbose:
count += printf(FARMHUB_LOG_COLOR(FARMHUB_LOG_COLOR_BLUE));
break;
default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions main/kernel/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void convertFromJson(JsonVariantConst src, Level& dst) {
}

static void initLogging() {
#ifdef FARMHUB_DEBUG
// Reset ANSI colors
printf("\033[0m");
#endif

const char* logTags[] = {
"farmhub",
"farmhub:mdns",
Expand Down
46 changes: 14 additions & 32 deletions main/kernel/drivers/WiFiDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class WiFiDriver {
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &WiFiDriver::onEvent, this));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &WiFiDriver::onEvent, this));

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));

Task::run("wifi-driver", 4096, [this](Task&) {
runLoop();
});
Expand Down Expand Up @@ -191,7 +195,7 @@ class WiFiDriver {
}
}

inline void runLoop() {
void runLoop() {
int clients = 0;
bool connected = false;
std::optional<time_point<boot_clock>> connectingSince;
Expand All @@ -202,8 +206,8 @@ class WiFiDriver {
if (networkRequested.isSet() && !configPortalRunning.isSet()) {
esp_err_t err = esp_wifi_connect();
if (err != ESP_OK) {
LOGTD("wifi", "Failed to start connecting: %s", esp_err_to_name(err));
ensureWifiDeinitialized();
LOGTD("wifi", "Failed to start connecting: %s, stopping", esp_err_to_name(err));
ensureWifiStopped();
}
}
break;
Expand Down Expand Up @@ -239,7 +243,7 @@ class WiFiDriver {

LOGTI("wifi", "Connection timed out, retrying");
networkConnecting.clear();
ensureWifiDeinitialized();
ensureWifiStopped();
}
LOGTV("wifi", "Connecting for first client");
connectingSince = boot_clock::now();
Expand All @@ -256,7 +260,6 @@ class WiFiDriver {

void connect() {
networkConnecting.set();
ensureWifiInitialized();

#ifdef WOKWI
LOGTD("wifi", "Skipping provisioning on Wokwi");
Expand Down Expand Up @@ -288,35 +291,12 @@ class WiFiDriver {
void disconnect() {
if (powerSaveMode) {
LOGTV("wifi", "No more clients, shutting down radio to conserve power");
ensureWifiDeinitialized();
ensureWifiStopped();
} else {
LOGTV("wifi", "No more clients, but staying online because not saving power");
}
}

void ensureWifiInitialized() {
if (!wifiInitialized) {
LOGTD("wifi", "Initializing WiFi");
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
wifiUpSince = boot_clock::now();
wifiInitialized = true;
}
}

void ensureWifiDeinitialized() {
if (wifiInitialized) {
ensureWifiStopped();
auto currentUptime = currentWifiUptime();
wifiUptimeBefore += currentUptime;
wifiUpSince.reset();
LOGTD("wifi", "De-initializing WiFi (spent %lld ms initialized)", currentUptime.count());
ESP_ERROR_CHECK(esp_wifi_deinit());
wifiInitialized = false;
}
}

milliseconds currentWifiUptime() {
if (!wifiUpSince.has_value()) {
return milliseconds::zero();
Expand All @@ -325,7 +305,7 @@ class WiFiDriver {
}

void ensureWifiStationStarted(wifi_config_t& config) {
ensureWifiInitialized();
wifiUpSince = boot_clock::now();
if (!stationStarted.isSet()) {
if (powerSaveMode) {
auto listenInterval = 50;
Expand All @@ -351,6 +331,10 @@ class WiFiDriver {
LOGTD("wifi", "Stopping");
ESP_ERROR_CHECK(esp_wifi_stop());
}
auto currentUptime = currentWifiUptime();
wifiUptimeBefore += currentUptime;
wifiUpSince.reset();
LOGTD("wifi", "Stopping WiFi (was up %lld ms)", currentUptime.count());
}

void ensureWifiDisconnected() {
Expand All @@ -367,8 +351,6 @@ class WiFiDriver {
}

void startProvisioning() {
ensureWifiInitialized();

// Initialize provisioning manager
wifi_prov_mgr_config_t config = {
.scheme = wifi_prov_scheme_softap,
Expand Down
Loading

0 comments on commit bf24916

Please sign in to comment.