diff --git a/src/AppLayer.cpp b/src/AppLayer.cpp index 8f2a754..869cb59 100644 --- a/src/AppLayer.cpp +++ b/src/AppLayer.cpp @@ -64,6 +64,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) { @@ -464,4 +504,5 @@ void AppLayer::setAppPayloadCfg(uint8_t *bytes, uint8_t size) appPrefs.putBytes("payloadcfg", bytes, size); appPrefs.end(); memcpy(appPayloadCfg, bytes, size); -} \ No newline at end of file +} +