Browse Source

send values MQTT

ir
Hendrik Langer 5 years ago
parent
commit
052216699a
  1. 13
      src/main.cpp
  2. 2
      src/network/XD0MQTT.cpp
  3. 2
      src/network/XD0MQTT.h
  4. 2
      src/network/XD0OTA.h

13
src/main.cpp

@ -36,6 +36,7 @@ Adafruit_BME280 bme; // I2C (also available: hardware SPI
//HardwareSerial Serial2(2); //HardwareSerial Serial2(2);
SdsDustSensor sds(Serial2); SdsDustSensor sds(Serial2);
XD0OTA ota("esp32-weatherstation");
XD0MQTT mqtt; XD0MQTT mqtt;
uint32_t lastDisplayUpdate = 0; uint32_t lastDisplayUpdate = 0;
@ -153,7 +154,6 @@ void setup()
} }
ESP_LOGD(TAG, "trying to fetch over-the-air update"); ESP_LOGD(TAG, "trying to fetch over-the-air update");
XD0OTA ota("esp32-weatherstation");
ota.update(); ota.update();
ESP_LOGD(TAG, "connecting to MQTT"); ESP_LOGD(TAG, "connecting to MQTT");
@ -197,6 +197,17 @@ void loop()
} }
while (display.nextPage()); while (display.nextPage());
display.powerOff(); 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(); unsigned long endTime = bme.beginReading();

2
src/network/XD0MQTT.cpp

@ -109,7 +109,7 @@ bool XD0MQTT::begin(void) {
return true; 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); int msg_id = esp_mqtt_client_publish(client, topic, data, len, qos, retain);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
} }

2
src/network/XD0MQTT.h

@ -19,7 +19,7 @@ class XD0MQTT {
public: public:
XD0MQTT(void); XD0MQTT(void);
bool begin(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: private:
// openssl s_client -showcerts -connect home.xd0.de:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem // openssl s_client -showcerts -connect home.xd0.de:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem
const char* rootCACertificate = \ const char* rootCACertificate = \

2
src/network/XD0OTA.h

@ -18,6 +18,7 @@ class XD0OTA {
XD0OTA(String deviceName); XD0OTA(String deviceName);
void update(void); void update(void);
int checkForUpdates(String url); int checkForUpdates(String url);
String getMAC(void);
private: private:
String deviceName; String deviceName;
const char* fwUrlBase = "https://fwupdate.xd0.de:444/fota/"; const char* fwUrlBase = "https://fwupdate.xd0.de:444/fota/";
@ -47,7 +48,6 @@ class XD0OTA {
"D5Npv9M5363apnO1VlVR+OuO1NEJusRK1aWk9RLZsTPxzwOWwdkifXxUEJ+f8mGn\n" \ "D5Npv9M5363apnO1VlVR+OuO1NEJusRK1aWk9RLZsTPxzwOWwdkifXxUEJ+f8mGn\n" \
"o+6SCw==\n" \ "o+6SCw==\n" \
"-----END CERTIFICATE-----\n"; "-----END CERTIFICATE-----\n";
String getMAC(void);
String getUpdateURL(String file, String extension); String getUpdateURL(String file, String extension);
void setClock(void); void setClock(void);
}; };

Loading…
Cancel
Save