From e7fb357544dae42793b57457d08518f386bf8e7c Mon Sep 17 00:00:00 2001 From: Matthias Prinke <83612361+matthias-bs@users.noreply.github.com> Date: Wed, 15 May 2024 21:05:52 +0200 Subject: [PATCH] Added 1-Wire temperature sensor function (#31) --- src/AppLayer.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/AppLayer.h | 28 +++++++++++++++++----------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/AppLayer.cpp b/src/AppLayer.cpp index 18bfb83..f482679 100644 --- a/src/AppLayer.cpp +++ b/src/AppLayer.cpp @@ -50,6 +50,7 @@ // 20240427 Added BLE configuration/status via LoRaWAN // 20240507 Added configuration of max_sensors/rx_flags via LoRaWAN // 20240508 Added configuration of en_decoders via LoRaWAN +// 20240515 Added getOneWireTemperature() // // // ToDo: @@ -59,6 +60,46 @@ #include "AppLayer.h" +#ifdef ONEWIRE_EN + // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) + static OneWire oneWire(PIN_ONEWIRE_BUS); //!< OneWire bus + + // Pass our oneWire reference to Dallas Temperature. + static DallasTemperature owTempSensors(&oneWire); //!< Dallas temperature sensors connected to OneWire bus +#endif + +#ifdef ONEWIRE_EN + /*! + * \brief Get temperature from Maxim OneWire Sensor + * + * \param index sensor index + * + * \returns temperature in degrees Celsius or DEVICE_DISCONNECTED_C + */ + float + AppLayer::getOneWireTemperature(uint8_t index) + { + // Call sensors.requestTemperatures() to issue a global temperature + // request to all devices on the bus + owTempSensors.requestTemperatures(); + + // Get temperature by index + float tempC = owTempSensors.getTempCByIndex(index); + + // Check if reading was successful + if (tempC != DEVICE_DISCONNECTED_C) + { + log_d("Temperature = %.2f°C", tempC); + } + else + { + log_d("Error: Could not read temperature data"); + } + + return tempC; + }; +#endif + uint8_t AppLayer::decodeDownlink(uint8_t port, uint8_t *payload, size_t size) { @@ -395,6 +436,8 @@ void AppLayer::getPayloadStage1(uint8_t port, LoraEncoder &encoder) #endif #ifdef ONEWIRE_EN + float water_temp_c = getOneWireTemperature(); + // Debug output for auxiliary sensors/voltages if (water_temp_c != DEVICE_DISCONNECTED_C) { @@ -732,4 +775,4 @@ std::vector AppLayer::getBleAddr(void) return bleAddr; } -#endif \ No newline at end of file +#endif diff --git a/src/AppLayer.h b/src/AppLayer.h index 5c8855e..e0c6bec 100644 --- a/src/AppLayer.h +++ b/src/AppLayer.h @@ -38,6 +38,7 @@ // 20240424 Fixed BLE address initialization from Preferences, added begin() // 20240426 Moved bleAddrInit() out of begin() // 20240504 Added BresserWeatherSensorLWCmd.h +// 20240515 Added getOneWireTemperature() // // ToDo: // - @@ -121,14 +122,6 @@ class AppLayer Lightning lightningProc; #endif -#ifdef ONEWIRE_EN - // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) - OneWire oneWire(PIN_ONEWIRE_BUS); //!< OneWire bus - - // Pass our oneWire reference to Dallas Temperature. - DallasTemperature temp_sensors(&oneWire); //!< Dallas temperature sensors connected to OneWire bus -#endif - #ifdef DISTANCESENSOR_EN #if defined(ESP32) /// Ultrasonic distance sensor @@ -176,7 +169,7 @@ class AppLayer * * If available, addresses from Preferences are used, otherwise defaults from * BresserWeatherSensorLWCfg.h. - * + * * BleSensors() requires Preferences, which uses the Flash FS, * which is not available before the sketches' begin() is called - * thus the following cannot be handled by the constructor! @@ -191,11 +184,13 @@ class AppLayer // No addresses stored in Preferences, use default knownBLEAddresses = knownBLEAddressesDef; log_d("Using BLE addresses from BresserWeatherSensorLWCfg.h:"); - } else { + } + else + { log_d("Using BLE addresses from Preferences:"); } bleSensors = BleSensors(knownBLEAddresses); - + for (const std::string &s : knownBLEAddresses) { (void)s; @@ -204,6 +199,17 @@ class AppLayer #endif }; +#ifdef ONEWIRE_EN + /*! + * \brief Get temperature from Maxim OneWire Sensor + * + * \param index sensor index + * + * \returns temperature in degrees Celsius or DEVICE_DISCONNECTED_C + */ + float + getOneWireTemperature(uint8_t index = 0); +#endif /*! * \brief Decode app layer specific downlink messages