dynamic creation of MQTT topic

This commit is contained in:
Ronald Schaten 2016-02-24 08:43:44 +01:00
parent bf61e4c937
commit 8e71160516

View File

@ -70,6 +70,14 @@ bool isEqual(float a, float b, float epsilon=0.001) {
return fabs(a - b) <= epsilon * fabs(a); return fabs(a - b) <= epsilon * fabs(a);
} }
char* topic(const char* this_topic) {
static char topic[34];
strcpy(topic, mqtt_topic);
strcat(topic, "/");
strcat(topic, this_topic);
return topic;
}
void read_sensor() { void read_sensor() {
// wait at least 2 seconds seconds between measurements // wait at least 2 seconds seconds between measurements
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
@ -96,21 +104,14 @@ void read_sensor() {
dtostrf(temperature, 1, 2, str_temperature); dtostrf(temperature, 1, 2, str_temperature);
dtostrf(heatindex, 1, 2, str_heatindex); dtostrf(heatindex, 1, 2, str_heatindex);
char pub_topic[34];
if (!isEqual(humidity, previousHumidity)) { if (!isEqual(humidity, previousHumidity)) {
strcpy(pub_topic,mqtt_topic); client.publish(topic("humidity"), str_humidity);
strcat(pub_topic,"/humidity");
client.publish(pub_topic, str_humidity);
} }
if (!isEqual(temperature, previousTemperature)) { if (!isEqual(temperature, previousTemperature)) {
strcpy(pub_topic,mqtt_topic); client.publish(topic("temperature"), str_temperature);
strcat(pub_topic,"/temperature");
client.publish(pub_topic, str_temperature);
} }
if (!isEqual(heatindex, previousHeatindex)) { if (!isEqual(heatindex, previousHeatindex)) {
strcpy(pub_topic,mqtt_topic); client.publish(topic("heatindex"), str_heatindex);
strcat(pub_topic,"/heatindex");
client.publish(pub_topic, str_heatindex);
} }
Serial.print("Humidity: "); Serial.print("Humidity: ");
@ -151,13 +152,10 @@ void reconnect() {
while (!client.connected()) { while (!client.connected()) {
Serial.print("Attempting MQTT connection..."); Serial.print("Attempting MQTT connection...");
// Attempt to connect // Attempt to connect
char pub_topic[34]; if (client.connect(wifi_station_get_hostname(), topic("online"), MQTTQOS1, true, "0")) {
strcpy(pub_topic,mqtt_topic);
strcat(pub_topic,"/online");
if (client.connect(wifi_station_get_hostname(), pub_topic, MQTTQOS1, true, "0")) {
Serial.println("connected"); Serial.println("connected");
// Once connected, publish an announcement... // Once connected, publish an announcement...
client.publish(pub_topic, "1"); client.publish(topic("online"), "1");
// ... and resubscribe: // ... and resubscribe:
client.subscribe("inTopic"); client.subscribe("inTopic");
} else { } else {
@ -300,16 +298,9 @@ void setup() {
// Initial read // Initial read
read_sensor(); read_sensor();
char pub_topic[34]; client.publish(topic("humidity"), str_humidity);
strcpy(pub_topic,mqtt_topic); client.publish(topic("temperature"), str_temperature);
strcat(pub_topic,"/humidity"); client.publish(topic("heatindex"), str_heatindex);
client.publish(pub_topic, str_humidity);
strcpy(pub_topic,mqtt_topic);
strcat(pub_topic,"/temperature");
client.publish(pub_topic, str_temperature);
strcpy(pub_topic,mqtt_topic);
strcat(pub_topic,"/heatindex");
client.publish(pub_topic, str_heatindex);
// Handle http requests // Handle http requests
server.on("/", [](){ server.on("/", [](){