Browse Source

mqtt publish float values

ir
Hendrik Langer 5 years ago
parent
commit
4543981246
  1. 27
      src/main.cpp
  2. 7
      src/network/XD0MQTT.cpp
  3. 1
      src/network/XD0MQTT.h

27
src/main.cpp

@ -197,17 +197,13 @@ void sendValues() {
String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity");
String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure");
char temperature[8]; sprintf(temperature, "%.2f", sensor_readings.temperature);
char humidity[7]; sprintf(humidity, "%.2f", sensor_readings.humidity);
char pressure[8]; sprintf(pressure, "%.2f", sensor_readings.pressure / 100.0F);
mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature));
mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity));
mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure));
mqtt.publish(topic_temperature.c_str(), sensor_readings.temperature, "%.2f");
mqtt.publish(topic_humidity.c_str(), sensor_readings.humidity, "%.2f");
mqtt.publish(topic_pressure.c_str(), sensor_readings.pressure / 100.0F, "%.2f");
}
if (bme680_active) {
String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc");
char voc[12]; sprintf(voc, "%.2f", sensor_readings.voc / 1000.0F);
mqtt.publish(topic_voc.c_str(), voc, strlen(voc));
mqtt.publish(topic_voc.c_str(), sensor_readings.voc / 1000.0F, "%.2f");
}
if (!bme280_active && !bme680_active) {
String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
@ -220,21 +216,16 @@ void sendValues() {
String topic_uvi = String("thomas/sensor/") + ota.getMAC() + String("/uvi");
String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva");
String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb");
char uvi[10]; sprintf(uvi, "%.2f", sensor_readings.uvi);
char uva[10]; sprintf(uva, "%.2f", sensor_readings.uva);
char uvb[10]; sprintf(uvb, "%.2f", sensor_readings.uvb);
mqtt.publish(topic_uvi.c_str(), uvi, strlen(uvi));
mqtt.publish(topic_uva.c_str(), uva, strlen(uva));
mqtt.publish(topic_uvb.c_str(), uvb, strlen(uvb));
mqtt.publish(topic_uvi.c_str(), sensor_readings.uvi, "%.2f");
mqtt.publish(topic_uva.c_str(), sensor_readings.uva, "%.2f");
mqtt.publish(topic_uvb.c_str(), sensor_readings.uvb, "%.2f");
}
if (sds_active) {
String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10");
char pm10[10]; sprintf(pm10, "%.2f", sensor_readings.pm10);
String topic_pm25 = String("thomas/sensor/") + ota.getMAC() + String("/pm25");
char pm25[10]; sprintf(pm25, "%.2f", sensor_readings.pm25);
mqtt.publish(topic_pm10.c_str(), pm10, strlen(pm10));
mqtt.publish(topic_pm25.c_str(), pm25, strlen(pm25));
mqtt.publish(topic_pm10.c_str(), sensor_readings.pm10, "%.2f");
mqtt.publish(topic_pm25.c_str(), sensor_readings.pm25, "%.2f");
}
}

7
src/network/XD0MQTT.cpp

@ -115,6 +115,13 @@ bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos, int
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
}
bool XD0MQTT::publish(const char* topic, const float data, const char* format, int qos, int retain) {
char data_str[64];
sprintf(data_str, format, data);
int msg_id = esp_mqtt_client_publish(client, topic, data_str, strlen(data_str), qos, retain);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
}
bool XD0MQTT::subscribe(const char* topic, const cb_t &cb, int qos) {
subscription_t subscription = {topic, cb, qos};
subscriptions_.push_back(subscription);

1
src/network/XD0MQTT.h

@ -30,6 +30,7 @@ class XD0MQTT {
XD0MQTT(void);
bool begin(void);
bool publish(const char* topic, const char* data, int len, int qos=1, int retain=0);
bool publish(const char* topic, const float data, const char* format, int qos=1, int retain=0);
bool subscribe(const char* topic, const cb_t &cb, int qos=1);
bool unsubscribe(const char* topic);
esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event);

Loading…
Cancel
Save