Use a randomly generated suffix in MQTT client ID
This commit is contained in:
18
src/main.cpp
18
src/main.cpp
@@ -3,6 +3,8 @@
|
||||
#include <arduino-timer.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <RFPowerView.h>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include "Shade.h"
|
||||
#include "ShadeRepository.h"
|
||||
#include "HADiscovery.h"
|
||||
@@ -19,13 +21,27 @@ RFPowerView powerView(RF_CE_PIN, RF_CS_PIN, RF_IRQ_PIN, RF_ID);
|
||||
uint8_t lastRollingCode1 = 0x3D;
|
||||
uint8_t lastRollingCode2 = 0x96;
|
||||
|
||||
static std::mt19937 generator(std::chrono::system_clock::now().time_since_epoch().count());
|
||||
|
||||
std::string generate_client_id_suffix(int length) {
|
||||
std::uniform_int_distribution<char> distribution('0', '9');
|
||||
|
||||
std::string random_string;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
random_string += distribution(generator);
|
||||
}
|
||||
return random_string;
|
||||
}
|
||||
|
||||
std::string clientID = "hotdog_" + generate_client_id_suffix(6);
|
||||
|
||||
EspMQTTClient client(
|
||||
SECRET_WIFI_SSID, // Wifi SSID
|
||||
SECRET_WIFI_PASSWORD, // Wifi Password
|
||||
SECRET_MQTT_SERVER_IP, // MQTT Broker server IP
|
||||
SECRET_MQTT_USERNAME, // MQTT Username
|
||||
SECRET_MQTT_PASSWORD, // MQTT Password
|
||||
"hotdog", // MQTT Client ID
|
||||
clientID.c_str(), // MQTT Client ID
|
||||
1883
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user