Here I collect functions that I am using in all my ESP32 projects. It is work in progress and will be updated over time.
Include myLib.h
to get access to the functions of the library
Following are needed if WiFiManager is used to setup WiFi AP and credentials via portal
WIFIMANAGER-ESP32
WebServer-esp32
DNSServer---esp32
These libraries can be installed in two ways:
Download the libraries and manually copy them to correct folders.
In platformio.ini add or uncomment the following lines:
lib_deps =
https://github.com/beegee-tokyo/ESP32-MyLib.git
https://github.com/zhouhan0126/WIFIMANAGER-ESP32.git
https://github.com/zhouhan0126/WebServer-esp32.git
https://github.com/zhouhan0126/DNSServer---esp32.git
On my ESP8266 I use tzapu's WiFiManager library. But right now this library is not working with ESP32 because of missing WebServer and DNSServer for ESP32.
For the time being I am using zhouhan0126 WIFIMANAGER-ESP32, 'https://github.com/zhouhan0126/WebServer-esp32' and 'https://github.com/zhouhan0126/DNSServer---esp32'.
WiFi connection functionalities
void createName(char *apName, int apIndex)
Generates an unique name for the module from a given prefix and the first and last 3 parts of the MAC address of the module.
apName
points to a character array that contains the prefix and placeholders for the unique id. I am using --xxxxxxxx. For my tests this isESP32-Test-xxxxxx
.apIndex
is the index of the first placeholder where the MAC address ID will be inserted.
bool connDirect(const char *ssid, const char *password, uint32_t timeout)
Tries to connect to a known AP within a given timeout.
ssid
is the SSID to connect topassword
is the password for the APtimeout
is the time in milli seconds to wait for the connectionreturn
flag if connection was successfull
bool connWiFiManager(const char *ssid, uint32_t connTimeout, uint32_t portalTimeout, bool connAuto)
Tries to connect to the last known AP. If this fails or if there is no AP credentials stored the module switches into AP mode and a configuration portal is created to setup the network credentials
ssid
SSID for the AP in case the connection failsconnTimeout
Time to wait for connection to succeedportalTimeout
Time to wait for input on configuration portalconnAuto
If true, the functions tries first to connect to an known AP
If false, the configuration portal is immediately openedreturn
flag if connection was successfull
bool connSmartConfig(uint32_t confTimeout, uint32_t connTimeout)
Connect with SmartConfig. First try auto connect, if it fails wait for SmartConfig to get WiFI credentials from an Android or IPhone app.
confTimeout
Time to wait to receive smart configurationconnTimeout
Time to wait for connection to WiFi after configuration was receivedreturn
flag if connection was successfull
ESP32 HW timer functionalities
4 HW timers are available and can be used with this library
hw_timer_t *startTimerMsec (uint64_t triggerTime, callback_t callback, bool repeat)
hw_timer_t *startTimerSec (uint64_t triggerTime, callback_t callback, bool repeat)
hw_timer_t *startTimer (uint64_t triggerTime, callback_t callback, bool repeat)
Start a timer for a single (repeat = false) or repeating (repeat = true) trigger.
startTimerSec for use with seconds,
startTimerMSec for use with milli seconds,
startTimer for use with micro seconds
triggerTime
time to trigger in seconds, milli seconds or micro secondscallback
the function to be called when the timer is triggeredrepeat
flag if the timer will be triggered repeatedly or only one timereturn
pointer to timer structure, needed to stop the timer
void restartTimer(hw_timer_t *timerToRestart)
Restarts a timer with the same repeat/alarm time as it was created
timerToRestart
timer to be restarted
void stopTimer(hw_timer_t *timerToStop)
Stop the timer associated with timerToStop timer structure
timerToStop
timer to be stopped
void stopAllTimers()
Stop all used timers, usefull to stop timer triggered repeating events when an OTA starts
NTP client wrapper for FreeRTOS SNTP functionality
bool initNTP()
Starts NTP client, sets timezone to Philippines (fix at the moment) and requests a first time update from the NTP server.
return
true if NTP update fails within 10 seconds
bool tryGetTime()
calls NTP client update() to check if an updated time stamp is available
return
true if update was available, false if error occured
String digitalClockDisplay()
Returns time and date as string in the format hh:mm dd.mm.yy
return
String with time and date readable
String digitalTimeDisplay()
Returns time as string in the format hh:mm
return
String with time readable
String digitalTimeDisplaySec()
Returns time as string in the format hh:mm:ss
return
String with time readable
String digitalDateDisplay()
Returns date as string in the format dd.mm.yy
return
String with time readable