From ffdd6d05c0553265b9cb65ca84e3e94b67c7abf7 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 18 Jul 2024 17:42:03 +0100 Subject: [PATCH 1/8] Add DS2484 hosting DS18b20 temp sensor --- platformio.ini | 1 + src/components/i2c/WipperSnapper_I2C.cpp | 11 ++ src/components/i2c/WipperSnapper_I2C.h | 2 + .../drivers/WipperSnapper_I2C_Driver_DS2484.h | 167 ++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h diff --git a/platformio.ini b/platformio.ini index 18c993abf..cde57656f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ lib_deps = adafruit/Adafruit BMP280 Library adafruit/Adafruit BMP3XX Library adafruit/Adafruit DPS310 + adafruit/Adafruit DS248x adafruit/Adafruit INA219 adafruit/Adafruit HTS221 adafruit/Adafruit HTU21DF Library diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 3a25088fa..a89fc182b 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -296,6 +296,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice( _dps310->configureDriver(msgDeviceInitReq); drivers.push_back(_dps310); WS_DEBUG_PRINTLN("DPS310 Initialized Successfully!"); + } else if (strcmp("ds2484", msgDeviceInitReq->i2c_device_name) == 0) { + _ds2484 = new WipperSnapper_I2C_Driver_DS2484(this->_i2c, i2cAddress); + if (!_ds2484->begin()) { + WS_DEBUG_PRINTLN("ERROR: Failed to initialize DS2484!"); + _busStatusResponse = + wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; + return false; + } + _ds2484->configureDriver(msgDeviceInitReq); + drivers.push_back(_ds2484); + WS_DEBUG_PRINTLN("DS2484 Initialized Successfully!"); } else if (strcmp("ens160", msgDeviceInitReq->i2c_device_name) == 0) { _ens160 = new WipperSnapper_I2C_Driver_ENS160(this->_i2c, i2cAddress); if (!_ens160->begin()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index d9b1a2fa5..008397258 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -28,6 +28,7 @@ #include "drivers/WipperSnapper_I2C_Driver_BMP280.h" #include "drivers/WipperSnapper_I2C_Driver_BMP3XX.h" #include "drivers/WipperSnapper_I2C_Driver_DPS310.h" +#include "drivers/WipperSnapper_I2C_Driver_DS2484.h" #include "drivers/WipperSnapper_I2C_Driver_ENS160.h" #include "drivers/WipperSnapper_I2C_Driver_HTS221.h" #include "drivers/WipperSnapper_I2C_Driver_HTU21D.h" @@ -132,6 +133,7 @@ class WipperSnapper_Component_I2C { // Sensor driver objects WipperSnapper_I2C_Driver_AHTX0 *_ahtx0 = nullptr; WipperSnapper_I2C_Driver_DPS310 *_dps310 = nullptr; + WipperSnapper_I2C_Driver_DS2484 *_ds2484 = nullptr; WipperSnapper_I2C_Driver_ENS160 *_ens160 = nullptr; WipperSnapper_I2C_Driver_SCD30 *_scd30 = nullptr; WipperSnapper_I2C_Driver_BH1750 *_bh1750 = nullptr; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h new file mode 100644 index 000000000..91ea2b334 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -0,0 +1,167 @@ +/*! + * @file WipperSnapper_I2C_Driver_DS2484.h + * + * Device driver the DS2484 I2C OneWire converter (hosting a DS18b20). + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Copyright (c) Tyeth Gundry 2024 for Adafruit Industries. + * + * MIT license, all text here must be included in any redistribution. + * + */ + +#ifndef WipperSnapper_I2C_Driver_DS2484_H +#define WipperSnapper_I2C_Driver_DS2484_H + +#define DS18B20_FAMILY_CODE 0x28 +#define DS18B20_CMD_CONVERT_T 0x44 +#define DS18B20_CMD_MATCH_ROM 0x55 +#define DS18B20_CMD_READ_SCRATCHPAD 0xBE + +#include "WipperSnapper_I2C_Driver.h" +#include + +/**************************************************************************/ +/*! + @brief Class that provides a sensor driver for the DS2484 I2C OneWire + converter hosting a DS18b20 temperature sensor. +*/ +/**************************************************************************/ +class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { + +public: + /*******************************************************************************/ + /*! + @brief Constructor for a DS2484 sensor. + @param i2c + The I2C interface. + @param sensorAddress + 7-bit device address. + */ + /*******************************************************************************/ + WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c, + uint16_t sensorAddress = _DS2484_ADDRESS) + : WipperSnapper_I2C_Driver(i2c, sensorAddress) { + _i2c = i2c; + _sensorAddress = sensorAddress; + } + + /*******************************************************************************/ + /*! + @brief Destructor for an DS2484 sensor. + */ + /*******************************************************************************/ + ~WipperSnapper_I2C_Driver_DS2484() { delete _ds2484; } + + /*******************************************************************************/ + /*! + @brief Initializes the DS2484 sensor and begins I2C. + @returns True if initialized successfully, False otherwise. + */ + /*******************************************************************************/ + bool begin() { + // initialize DS2484 + _ds2484 = new Adafruit_DS248x(); + if (!_ds2484->begin(_i2c, (uint8_t)_sensorAddress)) { + WS_DEBUG_PRINTLN("Could not find DS2484"); + return false; + } + + // check bus is okay + if (!_ds2484->OneWireReset()) { + WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset"); + if (_ds2484->shortDetected()) { + WS_DEBUG_PRINTLN("\tShort detected"); + } + if (!_ds2484->presencePulseDetected()) { + WS_DEBUG_PRINTLN("\tNo presense pulse"); + } + return false; + } + + // locate first DS18B20 + bool found_device = false; + _ds2484->OneWireReset(); + while (!found_device && _ds2484->OneWireSearch(_rom)) { + if (_rom[0] == DS18B20_FAMILY_CODE) { + found_device = true; + } + } + + if (!found_device) { + WS_DEBUG_PRINTLN("Could not find DS18B20 attached to DS2484"); + return false; + } + + WS_DEBUG_PRINTLN("DS2484 found"); + return true; + } + + /*******************************************************************************/ + /*! + @brief Processes a temperature event. + @param tempEvent + Pointer to an Adafruit_Sensor event. + @returns True if the temperature was obtained successfully, False + otherwise. + */ + bool processTemperatureEvent(sensors_event_t *tempEvent) { + if (!_ds2484->OneWireReset()) { + return false; + } + if (!_ds2484->presencePulseDetected()) { + tempEvent->temperature = NAN; + return true; + } + + _ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command + for (int i = 0; i < 8; i++) { + _ds2484->OneWireWriteByte(_rom[i]); + } + + // Start temperature conversion + _ds2484->OneWireWriteByte(DS18B20_CMD_CONVERT_T); // Convert T command + delay(750); // Wait for conversion (750ms for maximum precision) + + // Read scratchpad + _ds2484->OneWireReset(); + _ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command + for (int i = 0; i < 8; i++) { + _ds2484->OneWireWriteByte(_rom[i]); + } + _ds2484->OneWireWriteByte( + DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command + + uint8_t data[9]; + for (int i = 0; i < 9; i++) { + _ds2484->OneWireReadByte(&data[i]); + } + + // Calculate temperature + int16_t raw = (data[1] << 8) | data[0]; + tempEvent->temperature = (float)raw / 16.0; + return true; + } + + /*******************************************************************************/ + /*! + @brief Gets the DS2484's current temperature. + @param tempEvent + Pointer to an Adafruit_Sensor event. + @returns True if the temperature was obtained successfully, False + otherwise. + */ + /*******************************************************************************/ + bool getEventAmbientTemp(sensors_event_t *tempEvent) { + return processTemperatureEvent(tempEvent); + } + +protected: + Adafruit_DS248x *_ds2484; ///< DS2484 driver object + uint8_t _rom[8]; ///< DS18B20 ROM +}; + +#endif // WipperSnapper_I2C_Driver_DS2484 \ No newline at end of file From a6da532093e3c86eb177372aa4cddfcb7fa0d338 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 18 Jul 2024 17:43:33 +0100 Subject: [PATCH 2/8] Add DS2484 to library.properties requirements --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 843b3ab28..184961be5 100644 --- a/library.properties +++ b/library.properties @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper category=Communication url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino architectures=* -depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork +depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork From 7356c8901b269a9d4b8900310d89e60fd7ad2a10 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 18 Jul 2024 17:47:53 +0100 Subject: [PATCH 3/8] Remove default address for DS2484 --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index 91ea2b334..5bec10bbd 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -42,8 +42,7 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { 7-bit device address. */ /*******************************************************************************/ - WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c, - uint16_t sensorAddress = _DS2484_ADDRESS) + WipperSnapper_I2C_Driver_DS2484(TwoWire *i2c, uint16_t sensorAddress) : WipperSnapper_I2C_Driver(i2c, sensorAddress) { _i2c = i2c; _sensorAddress = sensorAddress; From 6c3b2267508d77add6aed30c8c820c7b429a1b25 Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 18 Jul 2024 18:02:54 +0100 Subject: [PATCH 4/8] Document DS18b20 defines for DS2484 --- .../i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index 5bec10bbd..9b3d4485d 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -16,10 +16,10 @@ #ifndef WipperSnapper_I2C_Driver_DS2484_H #define WipperSnapper_I2C_Driver_DS2484_H -#define DS18B20_FAMILY_CODE 0x28 -#define DS18B20_CMD_CONVERT_T 0x44 -#define DS18B20_CMD_MATCH_ROM 0x55 -#define DS18B20_CMD_READ_SCRATCHPAD 0xBE +#define DS18B20_FAMILY_CODE 0x28 ///< DS18B20 family code +#define DS18B20_CMD_CONVERT_T 0x44 ///< Convert T command +#define DS18B20_CMD_MATCH_ROM 0x55 ///< Match ROM command +#define DS18B20_CMD_READ_SCRATCHPAD 0xBE ///< Read Scratchpad command #include "WipperSnapper_I2C_Driver.h" #include From 38dfd1fd857fd1f94bcd2b034d7399290ad85034 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 19 Jul 2024 13:30:12 +0100 Subject: [PATCH 5/8] Add OneWireSearchReset in case device search is locked --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index 9b3d4485d..db44c6d58 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -84,6 +84,7 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { // locate first DS18B20 bool found_device = false; _ds2484->OneWireReset(); + _ds2484->OneWireSearchReset(); while (!found_device && _ds2484->OneWireSearch(_rom)) { if (_rom[0] == DS18B20_FAMILY_CODE) { found_device = true; From be8f2068e498d28331eb8d62f8419495745c891d Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 19 Jul 2024 13:59:29 +0100 Subject: [PATCH 6/8] Add onewire debug info if no sensor found on bus --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index db44c6d58..b1ff2f154 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -89,6 +89,11 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { if (_rom[0] == DS18B20_FAMILY_CODE) { found_device = true; } + else { + WS_DEBUG_PRINT("Found unwanted device with family code: 0x"); + WS_DEBUG_PRINTHEX(_rom[0]); + WS_DEBUG_PRINTLN(" expected 0x28"); // DS18B20_FAMILY_CODE + } } if (!found_device) { From c0acc944c5aa44da3f108c38e5f9b7fadf1307a2 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 19 Jul 2024 14:14:46 +0100 Subject: [PATCH 7/8] format document --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index b1ff2f154..146990391 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -88,8 +88,7 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { while (!found_device && _ds2484->OneWireSearch(_rom)) { if (_rom[0] == DS18B20_FAMILY_CODE) { found_device = true; - } - else { + } else { WS_DEBUG_PRINT("Found unwanted device with family code: 0x"); WS_DEBUG_PRINTHEX(_rom[0]); WS_DEBUG_PRINTLN(" expected 0x28"); // DS18B20_FAMILY_CODE From 80326ceafbe77107bd0850f36d0f80a8f47a91ab Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Tue, 23 Jul 2024 20:35:50 +0100 Subject: [PATCH 8/8] DS2484: resolve feedback + failure messages --- .../i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h index 146990391..ed276ab9d 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_DS2484.h @@ -114,6 +114,7 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { */ bool processTemperatureEvent(sensors_event_t *tempEvent) { if (!_ds2484->OneWireReset()) { + WS_DEBUG_PRINTLN("Failed to do a OneWire bus reset"); return false; } if (!_ds2484->presencePulseDetected()) { @@ -131,7 +132,11 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { delay(750); // Wait for conversion (750ms for maximum precision) // Read scratchpad - _ds2484->OneWireReset(); + if (!_ds2484->OneWireReset()) { + WS_DEBUG_PRINTLN( + "Failed to do a OneWire bus reset after starting temp conversion"); + return false; + } _ds2484->OneWireWriteByte(DS18B20_CMD_MATCH_ROM); // Match ROM command for (int i = 0; i < 8; i++) { _ds2484->OneWireWriteByte(_rom[i]); @@ -140,7 +145,7 @@ class WipperSnapper_I2C_Driver_DS2484 : public WipperSnapper_I2C_Driver { DS18B20_CMD_READ_SCRATCHPAD); // Read Scratchpad command uint8_t data[9]; - for (int i = 0; i < 9; i++) { + for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) { _ds2484->OneWireReadByte(&data[i]); }