diff --git a/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino b/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino index d1b12b21..a6e0c1e7 100644 --- a/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino +++ b/examples/BresserWeatherSensorMQTT/BresserWeatherSensorMQTT.ino @@ -86,6 +86,7 @@ // 20221227 Replaced DEBUG_PRINT/DEBUG_PRINTLN by Arduino logging functions // 20230114 Fixed rain gauge update // 20230124 Improved WiFi connection robustness +// 20230708 Changed MQTT payload and topic from char[] to String // // ToDo: // @@ -151,6 +152,7 @@ int const num_sensors = 1; #endif +#include #include #include #include @@ -159,7 +161,7 @@ int const num_sensors = 1; #include "WeatherUtils.h" #include "RainGauge.h" -const char sketch_id[] = "BresserWeatherSensorMQTT 20221024"; +const char sketch_id[] = "BresserWeatherSensorMQTT 20230709"; // Map sensor IDs to Names SensorMap sensor_map[NUM_SENSORS] = { @@ -408,9 +410,9 @@ void messageReceived(String &topic, String &payload) */ void publishWeatherdata(bool complete) { - char mqtt_payload[PAYLOAD_SIZE]; // sensor data - char mqtt_payload2[PAYLOAD_SIZE]; // calculated extra data - char mqtt_topic[TOPIC_SIZE+31]; // add space for ID/name + String mqtt_payload; // sensor data + String mqtt_payload2; // calculated extra data + String mqtt_topic; // MQTT topic including ID/name // ArduinoJson does not allow to set number of decimals for floating point data - // neither does MQTT Dashboard... @@ -418,8 +420,8 @@ void publishWeatherdata(bool complete) for (int i=0; i PAYLOAD_SIZE) { + log_e("mqtt_payload (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload.length(), PAYLOAD_SIZE); + } + if (mqtt_payload2.length() > PAYLOAD_SIZE) { + log_e("mqtt_payload2 (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload2.length(), PAYLOAD_SIZE); + } + // Try to map sensor ID to name to make MQTT topic explanatory for (int n=0; n 2) { - Serial.printf("%s: %s\n", mqttPubExtra, mqtt_payload2); - client.publish(mqttPubExtra, mqtt_payload2, false, 0); + #if CORE_DEBUG_LEVEL != ARDUHAL_LOG_LEVEL_NONE + snprintf(mqtt_payload_tmp, PAYLOAD_SIZE, mqtt_payload2.c_str()); + #endif + if (mqtt_payload2.length() > 2) { + log_i("%s: %s\n", mqttPubExtra, mqtt_payload_tmp); + client.publish(mqttPubExtra, mqtt_payload2.substring(0, PAYLOAD_SIZE-1), false, 0); } } // for (int i=0; i #endif - +#include #include #include #include @@ -157,7 +158,7 @@ #include "src/WeatherUtils.h" #include "src/RainGauge.h" -const char sketch_id[] = "BresserWeatherSensorMQTT 20221024"; +const char sketch_id[] = "BresserWeatherSensorMQTT 20230709"; // Map sensor IDs to Names SensorMap sensor_map[NUM_SENSORS] = { @@ -404,7 +405,6 @@ void messageReceived(String &topic, String &payload) */ - /*! \brief Publish weather data as MQTT message @@ -413,9 +413,9 @@ void messageReceived(String &topic, String &payload) */ void publishWeatherdata(bool complete) { - char mqtt_payload[PAYLOAD_SIZE]; // sensor data - char mqtt_payload2[PAYLOAD_SIZE]; // calculated extra data - char mqtt_topic[TOPIC_SIZE+31]; // add space for ID/name + String mqtt_payload; // sensor data + String mqtt_payload2; // calculated extra data + String mqtt_topic; // MQTT topic including ID/name // ArduinoJson does not allow to set number of decimals for floating point data - // neither does MQTT Dashboard... @@ -423,8 +423,8 @@ void publishWeatherdata(bool complete) for (int i=0; i PAYLOAD_SIZE) { + log_e("mqtt_payload (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload.length(), PAYLOAD_SIZE); + } + if (mqtt_payload2.length() > PAYLOAD_SIZE) { + log_e("mqtt_payload2 (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload2.length(), PAYLOAD_SIZE); + } + // Try to map sensor ID to name to make MQTT topic explanatory for (int n=0; n 2) { - Serial.printf("%s: %s\n", mqttPubExtra, mqtt_payload2); - client.publish(mqttPubExtra, mqtt_payload2, false, 0); + #if CORE_DEBUG_LEVEL != ARDUHAL_LOG_LEVEL_NONE + snprintf(mqtt_payload_tmp, PAYLOAD_SIZE, mqtt_payload2.c_str()); + #endif + if (mqtt_payload2.length() > 2) { + log_i("%s: %s\n", mqttPubExtra, mqtt_payload_tmp); + client.publish(mqttPubExtra, mqtt_payload2.substring(0, PAYLOAD_SIZE-1), false, 0); } } // for (int i=0; i #include #include #ifdef ESP32 @@ -570,9 +572,9 @@ void messageReceived(String &topic, String &payload) */ void publishWeatherdata(bool complete) { - char mqtt_payload[PAYLOAD_SIZE]; // sensor data - char mqtt_payload2[PAYLOAD_SIZE]; // calculated extra data - char mqtt_topic[TOPIC_SIZE+31]; // add space for ID/name + String mqtt_payload; // sensor data + String mqtt_payload2; // calculated extra data + String mqtt_topic; // MQTT topic including ID/name // ArduinoJson does not allow to set number of decimals for floating point data - // neither does MQTT Dashboard... @@ -580,8 +582,8 @@ void publishWeatherdata(bool complete) for (int i=0; i PAYLOAD_SIZE) { + log_e("mqtt_payload (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload.length(), PAYLOAD_SIZE); + } + if (mqtt_payload2.length() > PAYLOAD_SIZE) { + log_e("mqtt_payload2 (%d) > PAYLOAD_SIZE (%d). Payload will be truncated!", mqtt_payload2.length(), PAYLOAD_SIZE); + } + // Try to map sensor ID to name to make MQTT topic explanatory for (int n=0; n 2) { - Serial.printf("%s: %s\n", mqttPubExtra, mqtt_payload2); - client.publish(mqttPubExtra, mqtt_payload2, false, 0); + #if CORE_DEBUG_LEVEL != ARDUHAL_LOG_LEVEL_NONE + snprintf(mqtt_payload_tmp, PAYLOAD_SIZE, mqtt_payload2.c_str()); + #endif + if (mqtt_payload2.length() > 2) { + log_i("%s: %s\n", mqttPubExtra, mqtt_payload_tmp); + client.publish(mqttPubExtra, mqtt_payload2.substring(0, PAYLOAD_SIZE-1), false, 0); } } // for (int i=0; i