diff --git a/docs/source/Plugin/P105.rst b/docs/source/Plugin/P105.rst index 49c21d21a8..c28fc651d0 100644 --- a/docs/source/Plugin/P105.rst +++ b/docs/source/Plugin/P105.rst @@ -67,11 +67,17 @@ Available options: .. image:: P105_SensorModelOptions.png -**AHT1x** AHT10/AHT15. These sensor models should better be avoided, as it doesn't always work with other devices on the same I2C bus. +* **AHT1x** AHT10/AHT15. These sensor models should better be avoided, as it doesn't always work with other devices on the same I2C bus. -**AHT20** An more modern version of the sensor. Use this option also when connecting a DHT20 or AM2301B sensor. +* **AHT20** An more modern version of the sensor. Use this option also when connecting a DHT20 or AM2301B sensor. -**AHT21** An more modern version of the sensor, very similar to the AHT20, in a more compact chip package. +* **AHT21** An more modern version of the sensor, very similar to the AHT20, in a more compact chip package. + +When selecting the **AHT1x** Sensor model, an extra option is made available: + +.. image:: P105_AHT10_alt_init.png + +* **AHT10 Alternative initialization**: Some AHT10 clone sensors do not seem to like the regular AHT10/AHT15 initialization sequence. They do however accept a soft reset command. By enabling this checkbox, only the soft reset is sent to the device. Only available for AHT1x devices. Data Acquisition ^^^^^^^^^^^^^^^^ @@ -92,4 +98,6 @@ Change log .. versionchanged:: 2.0 ... + |changed| 2024-12-03 Add alternative initialization option + |added| 2021-08-01 Moved from ESPEasy PluginPlayground to the main repository. diff --git a/docs/source/Plugin/P105_AHT10_alt_init.png b/docs/source/Plugin/P105_AHT10_alt_init.png new file mode 100644 index 0000000000..f74ee299b4 Binary files /dev/null and b/docs/source/Plugin/P105_AHT10_alt_init.png differ diff --git a/src/_P105_AHT.ino b/src/_P105_AHT.ino index f0de4315b8..b11240f814 100644 --- a/src/_P105_AHT.ino +++ b/src/_P105_AHT.ino @@ -27,6 +27,8 @@ */ /** History: + * 2024-12-03 tonhuisman: Add alternative initialization for AHT10 (clone), see https://github.com/letscontrolit/ESPEasy/issues/5172 + * Small code optimization. * 2024-04-28 tonhuisman: Update plugin name and documentation as DHT20 and AM2301B actually contain an AHT20! * DHT20: https://www.adafruit.com/product/5183 (Description) * AM2301B: https://www.adafruit.com/product/5181 (Description) @@ -54,18 +56,15 @@ boolean Plugin_105(uint8_t function, struct EventStruct *event, String& string) { case PLUGIN_DEVICE_ADD: { - Device[++deviceCount].Number = PLUGIN_ID_105; - Device[deviceCount].Type = DEVICE_TYPE_I2C; - Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_TEMP_HUM; - Device[deviceCount].Ports = 0; - Device[deviceCount].PullUpOption = false; - Device[deviceCount].InverseLogicOption = false; - Device[deviceCount].FormulaOption = true; - Device[deviceCount].ValueCount = 2; - Device[deviceCount].SendDataOption = true; - Device[deviceCount].TimerOption = true; - Device[deviceCount].GlobalSyncOption = true; - Device[deviceCount].PluginStats = true; + Device[++deviceCount].Number = PLUGIN_ID_105; + Device[deviceCount].Type = DEVICE_TYPE_I2C; + Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_TEMP_HUM; + Device[deviceCount].Ports = 0; + Device[deviceCount].FormulaOption = true; + Device[deviceCount].ValueCount = 2; + Device[deviceCount].SendDataOption = true; + Device[deviceCount].TimerOption = true; + Device[deviceCount].PluginStats = true; break; } @@ -146,6 +145,10 @@ boolean Plugin_105(uint8_t function, struct EventStruct *event, String& string) static_cast(AHTx_device_type::AHT21_DEVICE) }; addFormSelector(F("Sensor model"), F("ahttype"), 3, options, indices, PCONFIG(1), true); addFormNote(F("Changing Sensor model will reload the page.")); + + if (static_cast(AHTx_device_type::AHT10_DEVICE) == PCONFIG(1)) { + addFormCheckBox(F("AHT10 Alternative initialization"), F("altinit"), PCONFIG(2)); + } } success = true; @@ -160,6 +163,7 @@ boolean Plugin_105(uint8_t function, struct EventStruct *event, String& string) PCONFIG(0) = 0x38; // AHT20/AHT21 only support a single I2C address. } else { PCONFIG(0) = getFormItemInt(F("i2c_addr")); + PCONFIG(2) = isFormItemChecked(F("altinit")) ? 1 : 0; } success = true; break; @@ -169,7 +173,7 @@ boolean Plugin_105(uint8_t function, struct EventStruct *event, String& string) { success = initPluginTaskData( event->TaskIndex, - new (std::nothrow) P105_data_struct(PCONFIG(0), static_cast(PCONFIG(1)))); + new (std::nothrow) P105_data_struct(PCONFIG(0), static_cast(PCONFIG(1)), 1 == PCONFIG(2))); break; } diff --git a/src/src/PluginStructs/P105_data_struct.cpp b/src/src/PluginStructs/P105_data_struct.cpp index 04f653108f..b653ea4d11 100644 --- a/src/src/PluginStructs/P105_data_struct.cpp +++ b/src/src/PluginStructs/P105_data_struct.cpp @@ -22,11 +22,12 @@ struct AHTx_Status { const uint8_t status; }; -AHTx_Device::AHTx_Device(uint8_t addr, AHTx_device_type type) : +AHTx_Device::AHTx_Device(uint8_t addr, AHTx_device_type type, bool altInit) : i2cAddress(addr), device_type(type), last_hum_val(0.0f), - last_temp_val(0.0f) {} + last_temp_val(0.0f), + alt_init(altInit) {} const __FlashStringHelper * AHTx_Device::getDeviceName() const { switch (device_type) { @@ -38,8 +39,11 @@ const __FlashStringHelper * AHTx_Device::getDeviceName() const { } bool AHTx_Device::initialize() { - const uint8_t cmd_init = (device_type == AHTx_device_type::AHT10_DEVICE) ? 0xE1 : 0xBE; + const uint8_t cmd_init = (AHTx_device_type::AHT10_DEVICE == device_type) ? 0xE1 : 0xBE; + if ((AHTx_device_type::AHT10_DEVICE == device_type) && alt_init) { + return I2C_write8(i2cAddress, 0xBA); // Soft reset only + } return I2C_write16_reg(i2cAddress, cmd_init, 0x0800); } @@ -92,8 +96,8 @@ bool AHTx_Device::readData() { return true; } -P105_data_struct::P105_data_struct(uint8_t addr, AHTx_device_type dev) : - device(addr, dev), +P105_data_struct::P105_data_struct(uint8_t addr, AHTx_device_type dev, bool altInit) : + device(addr, dev, altInit), state(AHTx_state::AHTx_Uninitialized), last_measurement(0), trigger_time(0) {} diff --git a/src/src/PluginStructs/P105_data_struct.h b/src/src/PluginStructs/P105_data_struct.h index d358ee61e7..ba3ebbd0fd 100644 --- a/src/src/PluginStructs/P105_data_struct.h +++ b/src/src/PluginStructs/P105_data_struct.h @@ -23,7 +23,8 @@ class AHTx_Device { public: AHTx_Device(uint8_t addr, - AHTx_device_type type); + AHTx_device_type type, + bool altInit); AHTx_Device() = delete; const __FlashStringHelper* getDeviceName() const; @@ -48,11 +49,13 @@ class AHTx_Device { const AHTx_device_type device_type; float last_hum_val = 0.0f; float last_temp_val = 0.0f; + bool alt_init = false; }; struct P105_data_struct : public PluginTaskData_base { P105_data_struct(uint8_t addr, - AHTx_device_type dev); + AHTx_device_type dev, + bool altInit); P105_data_struct() = delete; virtual ~P105_data_struct() = default;