diff --git a/ampel-firmware/config.public.h b/ampel-firmware/config.public.h index b90cae5828f01be2443e20e5aaecc0a3c52bb2f5..06987601c9004e57b6b9de3a3b5db0625f27e9d4 100644 --- a/ampel-firmware/config.public.h +++ b/ampel-firmware/config.public.h @@ -41,8 +41,8 @@ // AMPEL_CONFIG_VERSION should be defined, and have exactly 3 characters. // If you modify this string, every parameter saved on the Ampel will be replaced by the ones in config.h. // AMPEL_CONFIG_VERSION should also be updated if the configuration structure is modified. -// The structure of the Ampel configuration has been modified 10 times, so it's called "a10" for now. -# define AMPEL_CONFIG_VERSION "a10" +// The structure of the Ampel configuration has been modified 11 times, so it's called "a11" for now. +# define AMPEL_CONFIG_VERSION "a11" /** * SERVICES @@ -150,6 +150,7 @@ # define MQTT_ENCRYPTED true // Set to false for unencrypted MQTT (e.g. with port 1883). # define MQTT_USER "" # define MQTT_PASSWORD "" +# define MQTT_TOPIC_PREFIX "CO2sensors/" // ESPxxxxxx will be added to the prefix, so complete topic will be "CO2sensors/ESPxxxxxx" /** * LoRaWAN diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp index f6d2853f08da3a977ee251e0d7b8104ab8e2486f..ddb10848f6a13d8638bdffaa8c4a2c8c2e95accb 100644 --- a/ampel-firmware/mqtt.cpp +++ b/ampel-firmware/mqtt.cpp @@ -33,13 +33,13 @@ namespace mqtt { unsigned long last_failed_at = 0; bool connected = false; - char publish_topic[21]; // e.g. "CO2sensors/ESPxxxxxx\0" + char publish_topic[42]; // "MQTT_TOPIC_PREFIX/ESPxxxxxx\0", e.g. "CO2sensors/ESPxxxxxx\0" const char *json_sensor_format; char last_successful_publish[23] = ""; void initialize(const char *sensorId) { json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}"); - snprintf(publish_topic, sizeof(publish_topic), "CO2sensors/%s", sensorId); + snprintf(publish_topic, sizeof(publish_topic), "%s%s", config::mqtt_topic_prefix, sensorId); if (config::mqtt_encryption) { // The sensor doesn't check the fingerprint of the MQTT broker, because otherwise this fingerprint should be updated @@ -62,11 +62,13 @@ namespace mqtt { void publish(const char *timestamp, int16_t co2, float temperature, float humidity) { if (wifi::connected() && mqttClient.connected()) { led_effects::onBoardLEDOn(); - Serial.print(F("MQTT - Publishing message ... ")); + Serial.print(F("MQTT - Publishing message to '")); + Serial.print(publish_topic); + Serial.print(F("' ... ")); char payload[75]; // Should be enough for json... snprintf(payload, sizeof(payload), json_sensor_format, timestamp, co2, temperature, humidity); - // Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da' + // Topic is 'MQTT_TOPIC_PREFIX/ESP123456' if (mqttClient.publish(publish_topic, payload)) { Serial.println(F("OK")); ntp::getLocalTime(last_successful_publish); @@ -130,7 +132,7 @@ namespace mqtt { if (connected) { if (config::allow_mqtt_commands) { - char control_topic[60]; // Should be enough for "CO2sensors/ESPd03cc5/control" + char control_topic[50]; // Should be enough for "MQTT_TOPIC_PREFIX/ESPd03cc5/control\0" snprintf(control_topic, sizeof(control_topic), "%s/control", publish_topic); mqttClient.subscribe(control_topic); mqttClient.setCallback(controlSensorCallback); @@ -184,7 +186,7 @@ namespace mqtt { // If the sensor can be reach by MQTT, it can answer with info about local_ip and ssid. // The sensor will send the info to "CO2sensors/ESP123456/info". void sendInfoAboutLocalNetwork() { - char info_topic[60]; // Should be enough for "CO2sensors/ESP123456/info" + char info_topic[50]; // Should be enough for "MQTT_TOPIC_PREFIX/ESP123456/info" snprintf(info_topic, sizeof(info_topic), "%s/info", publish_topic); char payload[75]; // Should be enough for info json... diff --git a/ampel-firmware/web_config.cpp b/ampel-firmware/web_config.cpp index b67506b35928bcae9dfa9510e881643adfcd6278..d7d1ea83fbd639ecde7f69d99dc2d46bb78c93b1 100644 --- a/ampel-firmware/web_config.cpp +++ b/ampel-firmware/web_config.cpp @@ -6,7 +6,7 @@ #define STRINGIFY(x) STRING(x) // Allows to call STRINGIFY(SOME_MACRO) and convert the value of SOME_MACRO to string #include "config.h" -#ifndef AMPEL_PASSWORD +#ifndef MQTT_TOPIC_PREFIX # error Missing config.h file. Please copy config.public.h to config.h. # warning config.h has a new structure (e.g. AMPEL_WIFI should be set to true or false) #endif @@ -118,6 +118,8 @@ namespace web_config { PasswordTParameter<STRING_LEN> mqttPasswordParam = Builder<PasswordTParameter<STRING_LEN>>("mqtt_password").label( "MQTT password").defaultValue(MQTT_PASSWORD).build(); + TextTParameter<STRING_LEN> mqttTopicPrefixParam = + Builder<TextTParameter<STRING_LEN>>("mqtt_topic").label("MQTT Topic prefix").defaultValue(MQTT_TOPIC_PREFIX).build(); /** * NTP Time */ @@ -204,6 +206,7 @@ namespace web_config { mqttParams.addItem(&mqttPortParam); mqttParams.addItem(&mqttUserParam); mqttParams.addItem(&mqttPasswordParam); + mqttParams.addItem(&mqttTopicPrefixParam); mqttParams.addItem(&mqttEncryptionParam); mqttParams.addItem(&mqttCommandsParam); @@ -352,6 +355,7 @@ namespace config { char *mqtt_server = web_config::mqttServerParam.value(); char *mqtt_user = web_config::mqttUserParam.value(); char *mqtt_password = web_config::mqttPasswordParam.value(); + char *mqtt_topic_prefix = web_config::mqttTopicPrefixParam.value(); uint16_t &mqtt_port = web_config::mqttPortParam.value(); uint16_t &mqtt_sending_interval = web_config::mqttIntervalParam.value(); bool &mqtt_encryption = web_config::mqttEncryptionParam.value(); diff --git a/ampel-firmware/web_config.h b/ampel-firmware/web_config.h index c9b23af91bb82c49fa26dfd141b388600d10ff7c..f0ab8d92be12dee72be7bc994d12595b8c1f289f 100644 --- a/ampel-firmware/web_config.h +++ b/ampel-firmware/web_config.h @@ -43,6 +43,7 @@ namespace config { extern char *mqtt_server; extern char *mqtt_user; extern char *mqtt_password; + extern char *mqtt_topic_prefix; extern uint16_t &mqtt_port; extern uint16_t &mqtt_sending_interval; // [s] extern bool &mqtt_encryption; // [true / false]