feat: Use MAC address for unique device name
This commit is contained in:
parent
ee3a392d5a
commit
7d77c5b051
|
@ -11,6 +11,8 @@ WiFiClient wc;
|
|||
MqttClient mqttClient(wc);
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
String deviceName;
|
||||
|
||||
#define OUTPUT_PIN 4
|
||||
|
||||
|
||||
|
@ -31,9 +33,17 @@ void setup() {
|
|||
Serial.println("");
|
||||
Serial.println("===== BOOT UP =====");
|
||||
|
||||
// Generate device name from MAC address
|
||||
String mac = WiFi.macAddress();
|
||||
mac.replace(":", ""); // Remove colons from MAC address
|
||||
String shortMac = mac.substring(0, 4);
|
||||
deviceName = "mega_" + shortMac;
|
||||
Serial.print("[SYSTEM] Generated device name: ");
|
||||
Serial.println(deviceName);
|
||||
|
||||
static int error_count = 0;
|
||||
|
||||
WiFi.hostname("airmega");
|
||||
WiFi.hostname(deviceName.c_str());
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SECRET_SSID, SECRET_PASS);
|
||||
|
||||
|
@ -84,7 +94,7 @@ void setup() {
|
|||
server.begin();
|
||||
Serial.println("[HTTP] server started");
|
||||
|
||||
mqttClient.setId("airmega");
|
||||
mqttClient.setId(deviceName.c_str());
|
||||
|
||||
Serial.print("[MQTT] Attempting to connect to the MQTT broker: ");
|
||||
Serial.println(broker);
|
||||
|
|
Loading…
Reference in New Issue
Block a user