Add ChipID to hostname

* Address https://github.com/toblum/McLighting/issues/149
#define ENABLE_MQTT_HOSTNAME_CHIPID
#define ENABLE_HA_HOSTNAME_CHIPID
This commit is contained in:
debsahu 2018-04-23 15:55:47 -04:00
parent ba5146d3d7
commit a27611b813
3 changed files with 25 additions and 10 deletions

View file

@ -413,24 +413,24 @@ void setup() {
// Configure MQTT
// ***************************************************************************
#ifdef ENABLE_MQTT
if (mqtt_host != "" && String(mqtt_port).toInt() > 0) {
if (mqtt_host != "" && atoi(mqtt_port) > 0) {
snprintf(mqtt_intopic, sizeof mqtt_intopic, "%s/in", HOSTNAME);
snprintf(mqtt_outtopic, sizeof mqtt_outtopic, "%s/out", HOSTNAME);
DBG_OUTPUT_PORT.printf("MQTT active: %s:%d\n", mqtt_host, String(mqtt_port).toInt());
mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt());
mqtt_client.setServer(mqtt_host, atoi(mqtt_port));
mqtt_client.setCallback(mqtt_callback);
}
#endif
#ifdef ENABLE_AMQTT
if (mqtt_host != "" && String(mqtt_port).toInt() > 0) {
if (mqtt_host != "" && atoi(mqtt_port) > 0) {
amqttClient.onConnect(onMqttConnect);
amqttClient.onDisconnect(onMqttDisconnect);
amqttClient.onMessage(onMqttMessage);
amqttClient.setServer(mqtt_host, String(mqtt_port).toInt());
amqttClient.setCredentials(mqtt_user, mqtt_pass);
amqttClient.setServer(mqtt_host, atoi(mqtt_port));
if (mqtt_user != "" or mqtt_pass != "") amqttClient.setCredentials(mqtt_user, mqtt_pass);
amqttClient.setClientId(mqtt_clientid);
connectToMqtt();
@ -969,4 +969,4 @@ void loop() {
EEPROM.commit();
}
#endif
}
}

View file

@ -58,6 +58,7 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
#endif
#ifdef ENABLE_HOMEASSISTANT
//#define ENABLE_HA_HOSTNAME_CHIPID // Uncomment/comment to add ESPChipID to end of HA hostname
String mqtt_ha = "home/" + String(HOSTNAME) + "_ha/";
String mqtt_ha_state_in = mqtt_ha + "state/in";
String mqtt_ha_state_out = mqtt_ha + "state/out";
@ -70,7 +71,13 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
uint16_t color_temp = 327; // min is 154 and max is 500
#endif
const char mqtt_clientid[] = "NeoPixelsStrip"; // MQTT ClientID
#define ENABLE_MQTT_HOSTNAME_CHIPID // Uncomment/comment to add ESPChipID to end of MQTT hostname
#ifdef ENABLE_MQTT_HOSTNAME_CHIPID
const char* mqtt_clientid = String(String(HOSTNAME) + "-" + String(ESP.getChipId())).c_str(); // MQTT ClientID
#else
const char* mqtt_clientid = HOSTNAME; // MQTT ClientID
#endif
char mqtt_host[64] = "";
char mqtt_port[6] = "";
@ -140,4 +147,4 @@ LEDState main_color = { 255, 0, 0 }; // Store the "main color" of the strip use
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
boolean buttonState = false;
#endif
#endif

View file

@ -1044,7 +1044,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject();
json["name"] = HOSTNAME;
#ifdef ENABLE_HA_HOSTNAME_CHIPID
json["name"] = String(String(HOSTNAME) + "-" + String(ESP.getChipId()));
#else
json["name"] = HOSTNAME;
#endif
json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out;
json["command_topic"] = mqtt_ha_state_in;
@ -1124,7 +1128,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject();
json["name"] = HOSTNAME;
#ifdef ENABLE_HA_HOSTNAME_CHIPID
json["name"] = String(String(HOSTNAME) + "-" + String(ESP.getChipId()));
#else
json["name"] = HOSTNAME;
#endif
json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out;
json["command_topic"] = mqtt_ha_state_in;