Skip to content

Commit

Permalink
Merge pull request #5173 from tonhuisman/feature/P105-add-alternative…
Browse files Browse the repository at this point in the history
…-aht10-initialization

[P105] Add Alternative initialization option for AHT10 clones
  • Loading branch information
TD-er authored Dec 9, 2024
2 parents a5f3682 + 4bfbf79 commit 9806d0b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
14 changes: 11 additions & 3 deletions docs/source/Plugin/P105.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^
Expand All @@ -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.
Binary file added docs/source/Plugin/P105_AHT10_alt_init.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 17 additions & 13 deletions src/_P105_AHT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -146,6 +145,10 @@ boolean Plugin_105(uint8_t function, struct EventStruct *event, String& string)
static_cast<int>(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<int>(AHTx_device_type::AHT10_DEVICE) == PCONFIG(1)) {
addFormCheckBox(F("AHT10 Alternative initialization"), F("altinit"), PCONFIG(2));
}
}

success = true;
Expand All @@ -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;
Expand All @@ -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<AHTx_device_type>(PCONFIG(1))));
new (std::nothrow) P105_data_struct(PCONFIG(0), static_cast<AHTx_device_type>(PCONFIG(1)), 1 == PCONFIG(2)));
break;
}

Expand Down
14 changes: 9 additions & 5 deletions src/src/PluginStructs/P105_data_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {}
Expand Down
7 changes: 5 additions & 2 deletions src/src/PluginStructs/P105_data_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 9806d0b

Please sign in to comment.