Can you find out why the wifi sometime shows red when it get disconnectedit does not reconnect. Can you explain/display the logic around the wifistatus?
Assistant
Let me find the wifi status logic in the firmware code.
Assistant
Why the WiFi icon goes red and stays red
The icon itself is fine — it's a live poll (gfx_screen.cpp:446-452), not a stuck flag:
Red just means WiFi.isConnected()==false right now, honestly reported. The bug is that nothing ever tries to reconnect.
The reconnect path is starved
Reconnection only happens inside ApiConnector::handle_produced_work() (api_connector.cpp:63):
cpp
if (!WiFi.isConnected() && !activate(true)) {
M5_LOGD("Unable to send, lost connection");
return e_api_reporter_error_not_connected;
}
But that line is only reached after earlier gates pass: log_aggregator->is_fresh(), log_data.valid() (needs both GPS and GM data valid), and time_to_send(). If any of those fail — e.g. GPS momentarily invalid — the function returns early and activate() is never called. No sensor data flowing → no reconnect attempt, ever.
connect_wifi() (wifi_connection.cpp:17-86) does WiFi.disconnect(true) + WiFi.reconnect() + a single delay(100), then returns — no retry loop, no re-check after the radio actually settles. On CoreS3 it skips the disconnect/clear step entirely.
No event-driven detection at all
There's no WiFi.onEvent()/ARDUINO_EVENT_WIFI_STA_DISCONNECTED handler anywhere in the firmware — disconnects are never proactively caught, only inferred if/when the polling path above happens to run.
The Activatable state machine also doesn't help
Handler/Activatable only re-calls activate() automatically when state == e_state_activating_failed. Once WiFi connects successfully once, state latches to e_state_active forever — a later silent drop never flips it back to failed, so that framework retry path never fires either.
Net effect: a real disconnect leaves the icon correctly red, but the only code capable of calling WiFi.reconnect() is gated behind fresh GPS+GM sensor data and a 10s backoff — if sensor data isn't flowing, or the reconnect happens to fail its one-shot attempt, the device just stays disconnected indefinitely with no further attempts.
Fix options (didn't implement yet, since this was a diagnosis request):
Add a periodic independent reconnect check (e.g. in the main loop, not gated on sensor validity) that retries every N seconds regardless of handle_produced_work gating.
Add WiFi.onEvent() for ARDUINO_EVENT_WIFI_STA_DISCONNECTED to trigger immediate reconnect attempts.
Make connect_wifi() actually loop/verify connection instead of single delay(100) + return.
Want me to implement one of these?
User
can you try all three?
Assistant
Implementing all three fixes: (1) event-driven disconnect detection, (2) periodic reconnect independent of sensor-data gating, (3) a real verify-loop in connect_wifi() instead of single delay+return.
Assistant
Now let's implement the changes.
Assistant
Now update api_connector.cpp: move the static last_try into the member so it can be reset by the disconnect event, and add maintain_connection().
Assistant
Now add the disconnect-event tracking and event registration to WiFiWrapper.
Assistant
Now the .cpp: constructor init, event handler + registration, consume_disconnect_event, and a real verify-loop in connect_wifi().
Assistant
Need to declare flag_disconnect_event in the header (used by the static event callback via WiFiWrapper_i).
Assistant
Now replace the single delay(100) reconnect attempts in connect_wifi() with the bounded verify-loop:
Assistant
Looks correct. Now let's try compiling to catch any issues.
Assistant
CoreS3 builds cleanly. Let's also verify Core2 compiles (different code path in connect_wifi).
Assistant
Both boards build successfully.
Assistant
All three fixes are implemented and both Core2/CoreS3 builds succeed.
1. Event-driven detection — WiFi.onEvent() registered in setup() (main.cpp); on ARDUINO_EVENT_WIFI_STA_DISCONNECTED it sets a flag (wifi_connection.cpp) that immediately clears the retry backoff.
2. Periodic reconnect independent of sensor gating — ApiConnector::maintain_connection() (api_connector.cpp) is called every loop() iteration (main.cpp), bypassing the GPS/GM-freshness gates that previously starved reconnection. It skips only when there's no device ID configured or the user is on the AP config screen.
3. Real verify-loop in connect_wifi() — replaced the single delay(100) + give-up with wait_for_connection(3000), which polls WiFi.status() for up to 3s so a reconnect attempt actually gets a chance to settle before being judged failed.
Net effect: a dropped connection is now detected immediately via the event handler, retried on a real bounded verification window, and retried periodically regardless of whether sensor data is flowing — so the red icon should now reflect a transient state, not a permanent one.
User
Please make a short doc of the changes and commit and push..
User
[Request interrupted by user]
User
Make a branch first..
Assistant
Now let me write a short doc of the changes.
Assistant
Good, only the wifi-related files are staged — the pre-existing docx/kicad changes are left untouched.
Assistant
Done. Pushed branch fix/wifi-reconnect-logic to both GitHub and Codeberg with a single commit containing the code fix plus bgeigiezen_firmware/docs/wifi_reconnect_fix.md.
Note: the pre-existing unrelated modifications (docx, KiCad files) on development were left untouched/unstaged — only the WiFi-fix files were committed.
User
What is current version?
Assistant
Current firmware version is 3.4.1 (defined in platformio.ini:24-26, assembled as VERSION_STRING = "bGeigieZen 3.4.1" in user_config.h).
User
Can you bumpup to version 3.4.2?
Assistant
Version bumped to 3.4.2 (diagnostics shown are just spellcheck noise, unrelated). Since this build flag applies globally to [env], it affects all four board environments.
Want me to commit this on the same fix/wifi-reconnect-logic branch, or separately?
User
Than commit and push..
User
[Request interrupted by user]
User
PR and merge it..
Assistant
Caught a stray edit that corrupted the file — line 50 reads ${env.lib_deThen cimmitps} instead of ${env.lib_deps} (looks like accidental keystrokes landed in the file). Fixing that before committing.
Assistant
Clean now. Let's build to confirm, then commit, push, PR, and merge.
PR #89 merged into development on GitHub. Now syncing local development and mirroring the merge to Codeberg (since this repo pushes to both remotes).
Assistant
That's expected — checking out development reverted these files to the pre-merge state locally. Pulling the merged PR now.
Assistant
Local development fast-forwarded to the merged state. Now pushing the same to Codeberg.
Assistant
All done:
Fixed the corrupted line in platformio.ini that had crept in.
Committed the version bump (3.4.2) and pushed to both remotes.
Opened PR #89 on GitHub and merged it into development.
Fast-forwarded local development and pushed the merge to Codeberg as well, so both remotes are in sync.
The pre-existing unrelated dirty files (docx, KiCad) are still sitting untouched in your working tree on development, as before.
User
please add those as a Kicad commit/push..
Assistant
These are large binary/generated hardware files (docx is unrelated). I'll commit the KiCad hardware files as their own commit, and the docx separately, since they're unrelated to each other.
Assistant
Committed and pushed to both GitHub and Codeberg as a separate KiCad-only commit (d192f25e). Left the .docx file uncommitted since it's unrelated — let me know if you want that pushed too.
User
push that too..
Assistant
Pushed to both GitHub and Codeberg (141793b0). Working tree is now clean (aside from the untracked .omc/ directory).
User
can we delete the other non track files or are they needed?
User
[Request interrupted by user]
User
Ahhhh just remove all that .omc data from the computer. No need for that any more
Assistant
This is local tool-state (some other AI/agent tool's scanned project memory), not tracked by git and not referenced anywhere in the repo — safe to remove.
Assistant
Deleted .omc/ entirely. git status is now clean.
User
AOn Core 2 after the update to V3.4.2 the touching of the buttons on the menu does not work any more. Can you fix that?
User
[Request interrupted by user]
User
Core 3 works fine
Assistant
Found it. My earlier fix (wait_for_connection(3000)) was fine for the WiFi settings screen (user-initiated, blocking is expected), but I also wired it into the backgroundmaintain_connection() that now runs every loop() iteration. On Core2, connect_wifi() has extra esp_wifi_stop()/start() delays on top of that 3s wait — so whenever WiFi fails to associate (e.g. real device ID but no/wrong SSID configured), the main loop freezes for ~3.3–3.8s every 10 seconds, and M5.update() (which reads touch) doesn't run during that freeze. That's why buttons feel dead on Core2 but not CoreS3 (CoreS3 has no extra delays and usually associates faster).
Fix: make the background reconnect path non-blocking — kick off WiFi.begin()/reconnect() and return immediately, without the verify-wait. Keep the verify-wait only for the WiFi settings screen where blocking briefly for user feedback is fine.
Assistant
Now update the implementation:
Assistant
Now update ApiConnector::activate() (called from the background maintain_connection() loop) to pass wait_for_result=false.
Assistant
Now let's build both environments to confirm this compiles.
Assistant
Both build clean. Now let's bump the patch version and commit/push this fix.
Assistant
Now let's also update the doc, then create a branch, commit, PR, and merge.
Assistant
Fixed, built, merged (PR #90), and synced to both remotes as v3.4.3.
Root cause: the v3.4.2 background WiFi-reconnect fix ran on every loop() iteration and used the new bounded verify-wait (wait_for_connection(3000)). On Core2, connect_wifi() also does extra esp_wifi_stop()/start() housekeeping with hard delays — so a failed reconnect attempt froze loop() for ~3.3–3.8s, and M5.update() (which polls touch) never ran during that freeze. CoreS3 has no equivalent Core2-only delays, which is why it wasn't affected.
Fix:connect_wifi() now takes a wait_for_result flag. The automatic background maintenance path fires the connection attempt and returns immediately without blocking loop(); the WiFi settings screen (user-initiated) still blocks briefly for immediate feedback, since that's expected there.
I can't test on physical Core2 hardware, so please verify menu touch stays responsive on your device when WiFi is disconnected/reconnecting.
User
Work fine now
Assistant
Glad it's confirmed working — thanks for testing on the hardware. v3.4.3 is merged and pushed to both remotes, so you're all set.
User
On core If it swiches (through the menu) to RT mode, the screen is balnk for about 3 seconds. Can yuo fix that?
Assistant
Both build clean. Bumping the version and committing.
Root cause: switching to Real-Time mode clears the screen, then FixedModeScreen::enter_screen() calls connect_wifi() with the blocking default. On Core2 that triggers the same class of issue as before — the 3s connect-verify wait plus extra esp_wifi_stop()/start() housekeeping — so the screen sat blank until it timed out or connected.
Fix: pass wait_for_result=false there too, same pattern as the earlier background-reconnect fix. Since I can't test on your hardware, want me to merge this now, or would you like to try it on-device first?
User
Still doing it.. One think I noticed that selecting drive mode after realtime mode, the Zen resets?
Assistant
Two separate things to chase — the screen still blanking (my earlier fix wasn't the whole story) and a reset when going RT → Drive mode. Let me investigate both.
Assistant
activate() is called both from set_handler_active() (screen transitions — must not block) and from the framework's own auto-retry inside try_handle_work() (also on the main loop). Neither context should block. Making it non-blocking unconditionally:
Assistant
Now let's build both boards.
Assistant
Both build clean. Bumping version and updating the doc.
Assistant
Still on the open PR #91 branch — adding these follow-up fixes to the same branch/PR since they complete the same fix.
Assistant
Pushed to PR #91 (now v3.4.5), both boards build clean.
Blank screen (still happening): the 3.4.4 fix only patched the direct connect_wifi() call in enter_screen(). Right after that, set_handler_active(true) triggers ApiConnector::activate(), which also called connect_wifi() — but without the wait_for_result=false flag, so the blocking 3s verify-loop still fired. Now activate() always connects non-blocking.
Reset switching RT → Drive: leaving RT mode deactivates the API handler, which called vTaskDelete() on the async send task. If that task was mid-HTTPClient::POST (inside the WiFi/lwIP stack) when killed, it can corrupt heap/lock state and crash — matching a reset right after leaving RT mode. Now it waits up to 5s for the task to finish naturally before force-killing it.
Please retest both on your Core2: RT mode entry (screen should no longer blank) and RT → Drive mode switching (should no longer reset). Let me know and I'll merge #91.
User
Please undo the revisions.. keep this version at 3.4.2
Assistant
Now reverting the merged PR #90 (Core2 touch-freeze fix) to get back to the exact v3.4.2 state, keeping the unrelated KiCad/docx commits intact.
Assistant
Version is back to 3.4.2, and the touch-freeze fix files match the merged-PR#89 state. Let's build both boards to confirm the revert is clean.
Assistant
Both build clean. Pushing the revert and cleaning up the abandoned PR/branches.
Assistant
Now closing the still-open, unmerged PR #91 (RT-mode fixes) without merging it.
Assistant
Reverted and pushed to both GitHub and Codeberg (fde6f27d). Summary:
Reverted the merged PR #90 (Core2 touch-freeze fix, 3.4.3) via git revert -m 1, restoring wifi_connection.h/.cpp, api_connector.cpp, and the doc to their exact state at v3.4.2.
platformio.ini is back to PATCH_VERSION=2 (v3.4.2).
Closed PR #91 (the unmerged RT-mode fixes) without merging.
Kept the unrelated KiCad/docx commits untouched.
Both Core2 and CoreS3 builds verified clean at this reverted state.
The feature branches (fix/wifi-reconnect-logic-2, fix/rt-mode-blank-screen) are still sitting on the remotes if you want to reference them later, but development no longer includes their changes. Let me know if you'd like those branches deleted too.