Repository navigation
Expand file tree
/
Copy pathlabnet.cpp
More file actions
57 lines (47 loc) · 1.57 KB
/
Copy pathlabnet.cpp
File metadata and controls
57 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <Arduino.h>
#if __has_include(<Network.h>)
#include <Network.h> // Core 3.x: WiFi.h needs NetworkInterface/network_event_handle_t first
#endif
#include <WiFi.h>
#include <time.h>
#include "config.h"
#include "secrets.h"
#include "labnet.h"
static uint32_t lastWifiCheck = 0;
// getLocalTime() with an explicit 0 timeout: one shot, no 5s default wait.
// It reports false until SNTP has moved the clock past 2016.
static bool localNow(struct tm &timeinfo) {
return getLocalTime(&timeinfo, 0);
}
static void formatNow(char *buffer, size_t maxLen, const char *fmt, const char *fallback) {
if (!buffer || maxLen == 0) return;
struct tm timeinfo;
if (!localNow(timeinfo) || strftime(buffer, maxLen, fmt, &timeinfo) == 0) {
snprintf(buffer, maxLen, "%s", fallback);
}
}
void initNetwork() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// Association finishes in the background; the SNTP client fires once DHCP is up.
configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER_1, NTP_SERVER_2);
}
void updateNetwork() {
uint32_t now = millis();
if (now - lastWifiCheck < WIFI_RECONNECT_INTERVAL) return;
lastWifiCheck = now;
if (WiFi.status() != WL_CONNECTED) WiFi.reconnect();
}
bool isWifiConnected() {
return WiFi.status() == WL_CONNECTED;
}
bool isTimeSynced() {
struct tm timeinfo;
return localNow(timeinfo);
}
void getFormattedTime(char *buffer, size_t maxLen) {
formatNow(buffer, maxLen, "%I:%M:%S %p", "--:--:-- --");
}
void getFormattedDate(char *buffer, size_t maxLen) {
formatNow(buffer, maxLen, "%d/%m/%Y", "--/--/----");
}