From 6f5c5536ccbd529b677aaa1b47647cd532f44e99 Mon Sep 17 00:00:00 2001 From: Ollo Date: Mon, 21 Dec 2020 17:07:15 +0100 Subject: [PATCH] Removed repeating code --- esp32/include/HomieConfiguration.h | 7 ++++++ esp32/src/main.cpp | 34 +++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index c2ec8ee..a364c4b 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -24,6 +24,13 @@ * @{ **/ +#define NUMBER_TYPE "number" +#define TEMPERATUR_SENSOR_LIPO "lipo" +#define TEMPERATUR_SENSOR_WATER "water" +#define TEMPERATUR_SENSOR_OUTSIDE "temp" +#define TEMPERATURE_NAME "Temperature" +#define TEMPERATURE_UNIT "°C" + HomieNode plant0("plant0", "Plant 0", "Plant"); /**< dynamic Homie information for first plant */ HomieNode plant1("plant1", "Plant 1", "Plant"); /**< dynamic Homie information for second plant */ HomieNode plant2("plant2", "Plant 2", "Plant"); /**< dynamic Homie information for first plant */ diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 86757e4..9804629 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -303,12 +303,12 @@ void mode2MQTT() float lipoTempCurrent = lipoTempSensor.getMedian(); if (lipoTempCurrent != NAN) { - sensorTemp.setProperty("lipo").send(String(lipoTempCurrent)); + sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent)); } float t2 = waterTempSensor.getMedian(); if (t2 != NAN) { - sensorTemp.setProperty("water").send(String(t2)); + sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2)); } //give mqtt time, use via publish callback instead? @@ -724,36 +724,36 @@ void systemInit() { mPlants[i].advertise(); } - sensorTemp.advertise("lipo") - .setName("Temperature") - .setDatatype("number") - .setUnit("°C"); - sensorTemp.advertise("water") - .setName("Temperature") - .setDatatype("number") - .setUnit("°C"); + sensorTemp.advertise(TEMPERATUR_SENSOR_LIPO) + .setName(TEMPERATURE_NAME) + .setDatatype(NUMBER_TYPE) + .setUnit(TEMPERATURE_UNIT); + sensorTemp.advertise(TEMPERATUR_SENSOR_WATER) + .setName(TEMPERATURE_NAME) + .setDatatype(NUMBER_TYPE) + .setUnit(TEMPERATURE_UNIT); sensorLipo.advertise("percent") .setName("Percent") - .setDatatype("number") + .setDatatype(NUMBER_TYPE) .setUnit("%"); sensorLipo.advertise("volt") .setName("Volt") - .setDatatype("number") + .setDatatype(NUMBER_TYPE) .setUnit("V"); sensorSolar.advertise("percent") .setName("Percent") - .setDatatype("number") + .setDatatype(NUMBER_TYPE) .setUnit("%"); sensorSolar.advertise("volt") .setName("Volt") - .setDatatype("number") + .setDatatype(NUMBER_TYPE) .setUnit("V"); - sensorWater.advertise("remaining").setDatatype("number").setUnit("%"); - startupReason.advertise("startupReason").setDatatype("number").setUnit("Enum"); + sensorWater.advertise("remaining").setDatatype(NUMBER_TYPE).setUnit("%"); + startupReason.advertise("startupReason").setDatatype(NUMBER_TYPE).setUnit("Enum"); } - stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler); + stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler); } bool mode1()