Skip to content

Commit

Permalink
馃悰 Make hardware device IDs actually unique
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Nov 19, 2016
1 parent a0dc390 commit ca6de81
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/Homie/Boot/BootConfig.cpp
Expand Up @@ -25,9 +25,7 @@ void BootConfig::setup() {
digitalWrite(Interface::get().led.pin, Interface::get().led.on);
}

const char* deviceId = DeviceId::get();

Interface::get().getLogger() << F("Device ID is ") << deviceId << endl;
Interface::get().getLogger() << F("Device ID is ") << DeviceId::get() << endl;

WiFi.mode(WIFI_AP_STA);

Expand All @@ -37,7 +35,7 @@ void BootConfig::setup() {
strcat(apName, DeviceId::get());

WiFi.softAPConfig(ACCESS_POINT_IP, ACCESS_POINT_IP, IPAddress(255, 255, 255, 0));
WiFi.softAP(apName, deviceId);
WiFi.softAP(apName, DeviceId::get());

Interface::get().getLogger() << F("AP started as ") << apName << endl;
_dns.setTTL(30);
Expand Down
2 changes: 1 addition & 1 deletion src/Homie/Boot/BootNormal.cpp
Expand Up @@ -154,7 +154,7 @@ void BootNormal::_onMqttConnected() {

Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$homie")), 1, true, HOMIE_VERSION);
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$implementation")), 1, true, "esp8266");
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$mac")), 1, true, DeviceId::get());
Interface::get().getMqttClient().publish(_prefixMqttTopic(PSTR("/$mac")), 1, true, WiFi.macAddress().c_str());

for (HomieNode* iNode : HomieNode::nodes) {
std::unique_ptr<char[]> subtopic = std::unique_ptr<char[]>(new char[1 + strlen(iNode->getId()) + 12 + 1]); // /id/$properties
Expand Down
31 changes: 15 additions & 16 deletions src/Homie/Utils/DeviceId.cpp
@@ -1,16 +1,15 @@
#include "DeviceId.hpp"

using namespace HomieInternals;

char DeviceId::_deviceId[] = ""; // need to define the static variable

void DeviceId::generate() {
char flashChipId[6 + 1];
sprintf(flashChipId, "%06x", ESP.getFlashChipId());

sprintf(DeviceId::_deviceId, "%06x%s", ESP.getChipId(), flashChipId + strlen(flashChipId) - 2);
}

const char* DeviceId::get() {
return DeviceId::_deviceId;
}
#include "DeviceId.hpp"

using namespace HomieInternals;

char DeviceId::_deviceId[] = ""; // need to define the static variable

void DeviceId::generate() {
uint8_t mac[6];
WiFi.macAddress(mac);
sprintf(DeviceId::_deviceId, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

const char* DeviceId::get() {
return DeviceId::_deviceId;
}
6 changes: 4 additions & 2 deletions src/Homie/Utils/DeviceId.hpp
@@ -1,6 +1,8 @@
#pragma once

#include "Arduino.h"
#include "Arduino.h"

#include <ESP8266WiFi.h>

namespace HomieInternals {
class DeviceId {
Expand All @@ -9,6 +11,6 @@ class DeviceId {
static const char* get();

private:
static char _deviceId[8 + 1];
static char _deviceId[12 + 1];
};
} // namespace HomieInternals

0 comments on commit ca6de81

Please sign in to comment.