From 052216699abda746258455279c4f32dc0b51e44f Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 24 Jul 2019 15:39:05 +0200 Subject: [PATCH] send values MQTT --- src/main.cpp | 13 ++++++++++++- src/network/XD0MQTT.cpp | 2 +- src/network/XD0MQTT.h | 2 +- src/network/XD0OTA.h | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index aceadb0..262c4ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,7 @@ Adafruit_BME280 bme; // I2C (also available: hardware SPI //HardwareSerial Serial2(2); SdsDustSensor sds(Serial2); +XD0OTA ota("esp32-weatherstation"); XD0MQTT mqtt; uint32_t lastDisplayUpdate = 0; @@ -153,7 +154,6 @@ void setup() } ESP_LOGD(TAG, "trying to fetch over-the-air update"); - XD0OTA ota("esp32-weatherstation"); ota.update(); ESP_LOGD(TAG, "connecting to MQTT"); @@ -197,6 +197,17 @@ void loop() } while (display.nextPage()); display.powerOff(); + + /* send values MQTT */ + 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", bme.readTemperature()); + char humidity[7]; sprintf(humidity, "%.2f", bme.readHumidity()); + char pressure[8]; sprintf(pressure, "%.2f", bme.readPressure() / 100.0F); + mqtt.publish(topic_temperature.c_str(), temperature, sizeof(temperature)); + mqtt.publish(topic_humidity.c_str(), humidity, sizeof(humidity)); + mqtt.publish(topic_pressure.c_str(), pressure, sizeof(pressure)); } /* unsigned long endTime = bme.beginReading(); diff --git a/src/network/XD0MQTT.cpp b/src/network/XD0MQTT.cpp index 7c64c62..91289f6 100644 --- a/src/network/XD0MQTT.cpp +++ b/src/network/XD0MQTT.cpp @@ -109,7 +109,7 @@ bool XD0MQTT::begin(void) { return true; } -bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos=1, int retain=0) { +bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos, int retain) { int msg_id = esp_mqtt_client_publish(client, topic, data, len, qos, retain); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); } diff --git a/src/network/XD0MQTT.h b/src/network/XD0MQTT.h index 83be9f5..8950770 100644 --- a/src/network/XD0MQTT.h +++ b/src/network/XD0MQTT.h @@ -19,7 +19,7 @@ class XD0MQTT { public: XD0MQTT(void); bool begin(void); - bool publish(const char* topic, const char* data, int len, int qos, int retain); + bool publish(const char* topic, const char* data, int len, int qos=1, int retain=0); private: // openssl s_client -showcerts -connect home.xd0.de:8883 /dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem const char* rootCACertificate = \ diff --git a/src/network/XD0OTA.h b/src/network/XD0OTA.h index b0554af..7436561 100644 --- a/src/network/XD0OTA.h +++ b/src/network/XD0OTA.h @@ -18,6 +18,7 @@ class XD0OTA { XD0OTA(String deviceName); void update(void); int checkForUpdates(String url); + String getMAC(void); private: String deviceName; const char* fwUrlBase = "https://fwupdate.xd0.de:444/fota/"; @@ -47,7 +48,6 @@ class XD0OTA { "D5Npv9M5363apnO1VlVR+OuO1NEJusRK1aWk9RLZsTPxzwOWwdkifXxUEJ+f8mGn\n" \ "o+6SCw==\n" \ "-----END CERTIFICATE-----\n"; - String getMAC(void); String getUpdateURL(String file, String extension); void setClock(void); };