basic functionality
This commit is contained in:
54
src/NetworkManager.cpp
Normal file
54
src/NetworkManager.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "NetworkManager.h"
|
||||
|
||||
const char* STA_SSID = "shroud";
|
||||
const char* STA_PASS = "th3r31sn0sp00n";
|
||||
|
||||
NetworkManager::NetworkManager(ClusterContext& ctx) : ctx(ctx) {}
|
||||
|
||||
void NetworkManager::setHostnameFromMac() {
|
||||
uint8_t mac[6];
|
||||
WiFi.macAddress(mac);
|
||||
char buf[32];
|
||||
sprintf(buf, "esp-%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
WiFi.hostname(buf);
|
||||
ctx.hostname = String(buf);
|
||||
}
|
||||
|
||||
void NetworkManager::setupWiFi() {
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(STA_SSID, STA_PASS);
|
||||
Serial.println("[WiFi] Connecting to AP...");
|
||||
unsigned long startAttemptTime = millis();
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 15000) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println();
|
||||
Serial.print("[WiFi] Connected to AP, IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
} else {
|
||||
Serial.println();
|
||||
Serial.println("[WiFi] Failed to connect to AP. Creating AP...");
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(STA_SSID, STA_PASS);
|
||||
Serial.print("[WiFi] AP created, IP: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
}
|
||||
setHostnameFromMac();
|
||||
ctx.udp->begin(4210);
|
||||
ctx.localIP = WiFi.localIP();
|
||||
ctx.hostname = WiFi.hostname();
|
||||
Serial.print("[WiFi] Hostname set to: ");
|
||||
Serial.println(ctx.hostname);
|
||||
Serial.print("[WiFi] UDP listening on port 4210\n");
|
||||
|
||||
// Register this node in the memberlist via event system
|
||||
NodeInfo self;
|
||||
self.hostname = ctx.hostname;
|
||||
self.ip = WiFi.localIP();
|
||||
self.lastSeen = millis();
|
||||
self.status = NodeInfo::ACTIVE;
|
||||
ctx.triggerEvent("node_discovered", &self);
|
||||
}
|
||||
Reference in New Issue
Block a user