From af0aae4b41b46f152e353e11e876ff358ab4194a Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sat, 3 Feb 2018 19:05:50 +0100 Subject: [PATCH] mqtt --- src/main.cpp | 31 ++++++++++++++++--------------- src/screen.cpp | 16 ++++++++++------ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e89e7ff..0339e24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,9 +48,7 @@ uint32_t lastTransmit = 0; //Variables for the mqtt packages and topics uint16_t statusPacketIdSub = 0; String commandTopic; -String temperatureTopic; -String humidityTopic; -String pressureTopic; +String bme280Topic; void setup() { // gpio configuration @@ -72,10 +70,8 @@ void setup() { bme280.printValues(); //Configure the MQTT topics - commandTopic = "esp32-node/cmd/" + iot.hostname + "/command"; - temperatureTopic = "esp32-node/stat/" + iot.hostname + "/temperature"; - humidityTopic = "esp32-node/stat/" + iot.hostname + "/humidity"; - pressureTopic = "esp32-node/stat/" + iot.hostname + "/pressure"; + commandTopic = "esp32-node/cmd/" + iot.hostname + "/play"; + bme280Topic = "esp32-node/stat/" + iot.hostname + "/bme280"; //Set up the Callbacks for the MQTT instance. Refer to the Async MQTT Client documentation // TODO: We should do this actually _before_ connecting the mqtt client... @@ -116,13 +112,15 @@ void onMqttConnect(bool sessionPresent) { void transmitStatus() { DEBUG_PRINTLN(__func__); - char sensorC[6]; - sprintf(sensorC, "%04i", bme280.readTemperature()); - statusPacketIdSub = iot.mqtt.publish(temperatureTopic.c_str(), 1, true, sensorC); - sprintf(sensorC, "%04i", bme280.readHumidity()); - statusPacketIdSub = iot.mqtt.publish(humidityTopic.c_str(), 1, true, sensorC); - sprintf(sensorC, "%04i", bme280.readPressure()); - statusPacketIdSub = iot.mqtt.publish(pressureTopic.c_str(), 1, true, sensorC); + StaticJsonBuffer<200> jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + root["temperature"] = bme280.readTemperature(); + root["humidity"] = bme280.readHumidity(); + root["pressure"] = bme280.readPressure(); + + char sensorBuf[root.measureLength()+1]; + root.printTo(sensorBuf, sizeof(sensorBuf)); + statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 1, true, sensorBuf); } @@ -131,7 +129,10 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties DEBUG_PRINTLN(__func__); //Since we only subscribed to one topic, we only have to compare the payload - if (strcmp(payload, "true") == 0) { + if (strcmp(payload, "ON") == 0) { + mp3.start(); + } else if (strcmp(payload, "OFF") == 0) { + mp3.stop(); } } diff --git a/src/screen.cpp b/src/screen.cpp index 00eda39..2c361d8 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -22,7 +22,7 @@ void SelectionList::draw() { u8g2.drawLine(0,header_height,u8g2.getDisplayWidth(),header_height); /* draw menu items */ - u8g2.setFont(u8g2_font_8x13B_tr); + u8g2.setFont(u8g2_font_8x13B_tf); int length = std::min(visible, (uint8_t)string_list.size()); for (int i=0; i