Skip to content

Commit

Permalink
Added 1-Wire temperature sensor function (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed May 15, 2024
1 parent 54a13e1 commit e7fb357
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
45 changes: 44 additions & 1 deletion src/AppLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -732,4 +775,4 @@ std::vector<std::string> AppLayer::getBleAddr(void)

return bleAddr;
}
#endif
#endif
28 changes: 17 additions & 11 deletions src/AppLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// -
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit e7fb357

Please sign in to comment.