Purpose:
A small, modular, non-blocking WiFi Station (STA) manager for ESP32 devices (tested on ESP32-C6 with Arduino IDE 2.3.6).
Focus: single responsibility — WiFi STA only. No AP / OTA / MQTT included. All user-visible strings live in PROGMEM
for RAM saving and easy localization.
/YourSketchFolder/
main.ino
/wifi_sta/
secrets.h
wifi_config.h
strings.h
logger.h
logger.cpp
wifi_module.h
wifi_module.cpp
wifi_sta_sources.h
Important:
secrets.h
must remain private (do not commit). See.gitignore
example below.
- Copy the
wifi_sta
directory into the same folder where yourmain.ino
sits. - Edit
wifi_sta/secrets.h
and fill inSECRETS_WIFI_SSID
andSECRETS_WIFI_PASSWORD
. - Add
/wifi_sta/secrets.h
to.gitignore
. - In
main.ino
, include headers in this order (header then wrapper):
#include <Arduino.h>
#include "wifi_sta/wifi_module.h"
// Force compilation of .cpp sources inside wifi_sta subfolder
#include "wifi_sta/wifi_sta_sources.h"
void setup() {
Serial.begin(115200);
delay(50); // give Serial time to spin up
// Optional runtime overrides
WifiModule::setLogLevel(LOG_LEVEL_DEBUG);
WifiModule::setLanguage(LANG_EN);
// Initialize module (register events). This does NOT enable the radio.
WifiModule::begin();
// Enable radio and start non-blocking connection attempts
WifiModule::enable();
}
void loop() {
WifiModule::loop();
// your non-blocking code...
delay(10);
}
Order matters: include
wifi_module.h
beforewifi_sta_sources.h
. The wrapper forces Arduino IDE to compile.cpp
files located in the subfolder.
WifiModule::begin()
— Initialize module (register event handlers). Radio remains OFF.WifiModule::enable()
— Turn radio ON and start non-blocking connect attempts.WifiModule::disable()
— Power radio OFF and forget network (non-blocking).WifiModule::loop()
— Must be called frequently from yourloop()
; runs timers and reconnect logic.WifiModule::isEnabled()
/WifiModule::isConnected()
— status queries.WifiModule::getIP()
/WifiModule::getRSSI()
— diagnostics.WifiModule::requestReconnectNow()
/WifiModule::forceDisconnect()
— manual controls.WifiModule::setLanguage(Language lang)
— setLANG_EN
/LANG_TR
.WifiModule::setLogLevel(LogLevel level)
— set log verbosity (TRACE
..FATAL
).
- All user-facing messages live in
strings.h
and are stored in PROGMEM. - Logger reads templates from PROGMEM, copies to RAM, runs
vsnprintf()
and then writes to Serial. - English and Turkish translations are included. Add more languages by following the
strings.h
pattern.
- Levels:
TRACE
,DEBUG
,INFO
,WARN
,ERROR
,FATAL
(most verbose → least). - Example:
WifiModule::setLogLevel(LOG_LEVEL_DEBUG);
- Output format:
[LEVEL] message
wifi_sta/secrets.h
(example — DO NOT commit):
#ifndef WIFI_SECRETS_H
#define WIFI_SECRETS_H
#define SECRETS_WIFI_SSID "your-ssid"
#define SECRETS_WIFI_PASSWORD "your-password"
#endif // WIFI_SECRETS_H
Add to .gitignore
:
/wifi_sta/secrets.h
- Tested with Arduino IDE 2.3.6 and ESP32-C6 support.
- Event registration uses
WiFi.onEvent([](auto ev, auto info){...});
for cross-core compatibility. - Arduino IDE sometimes ignores
.cpp
files in subfolders —wifi_sta_sources.h
includes the module.cpp
files to ensure they get compiled without making an Arduino library. This keeps your layout simple and avoids library packaging if you prefer that.
-
undefined reference to 'WifiModule::...'
- Ensure
#include "wifi_sta/wifi_sta_sources.h"
is present afterwifi_module.h
inmain.ino
. - Ensure all
.cpp
files are present insidewifi_sta
folder and spelled correctly.
- Ensure
-
PROGMEM weirdness / garbled strings
- Confirm
strings.h
usespgmspace.h
and thatfetchMessageTemplate()
is used by the logger to copy templates into RAM before formatting.
- Confirm
- Use a GitHub repository. This project contains multiple files, a README, and usage examples — repo gives versioning, issues, PRs, releases, and easier future conversion to a real Arduino Library.
- Gist is fine only for single-file snippets or tiny examples.
- MIT is recommended for permissive reuse. Add
LICENSE
to the repo if you choose MIT.
- Convert to a proper Arduino library (
library.properties
) if you want to install via the IDE. - Add additional languages and unit tests for
strings.h
/ PROGMEM fetch. - Optional non-blocking health check (async HTTP/TCP) as a separate module that uses this WiFi STA module.