letscontrolit / ESPEasy

Easy MultiSensor device based on ESP8266/ESP32
http://www.espeasy.com
Other
3.28k stars 2.21k forks source link

AM2320 not shown in i2c-scanner (SDA=GPIO1, SCL=GPIO3) #3781

Closed Micha-he closed 1 year ago

Micha-he commented 3 years ago

All tests are with image from 02.08. or the source from that date.

If I disabled the serial-port on a ESP and connect a AM2320 at the changed i2c-bus (SDA=GPIO1, SCL=GPIO3), the scanner didn't show the device, but it works flawlessly.

devs i2cscan

A test on a other ESP (firmware ESP_Easy_mega_20210802_normal_ESP8266_1M) with same serial-/i2c-settings, but with a MCP23017 as test-device, is displayed correctly in the i2c-scanner, even if the i2c-bus is configured on GPIO1 & GPIO3.

Maybe a problem with the AM2320-address ?

Unfortunately, I cannot test the AM2320 on another ESP because I only have one and it is connected firmly. The ESP in this Lingan SWA1 device, has only the RX/TX-pins available for i2c communication.

tonhuisman commented 3 years ago

The I2C scanner will show all device-addresses where a response is received. Possibly it won't show the device-name or, if it is configured, the plugin name, depending on the build used (that part is left out f.e. from the test builds), but it should be able to list the address.

tonhuisman commented 3 years ago

According to the AM2320 datasheet, the max. SCL clock frequency is 100 kHz, have you configured the I2C 'Slow device Clock Speed' at 100000 Hz (or lower)? As that setting is used by the I2C scanner. Your task should probably also have that option enabled.

NB: The AM2320 is recognized by the scanner as such, assuming that code is compiled in 😉 and uses address 0x5C.

Micha-he commented 3 years ago

Yes, I've set the i2c-speed to slow in the device config. Otherwise the AM2320 doesn't provide any data!

tonhuisman commented 3 years ago

It may be a peculiarity of that sensor, as the library used by ESPEasy has this code in the read() method:

  Wire.beginTransmission(AM2320_address);
  Wire.endTransmission();
  // запрос 4 байт (температуры и влажности)
  Wire.beginTransmission(AM2320_address);
  Wire.write(0x03);// запрос

So in fact it starts and stops an I2C transmission sequence, only to then send the actual read command.

TD-er commented 3 years ago

I think this may have been fixed by code I wrote yesterday. There was an issue related to the frequency switching and initialization of I2C.

On the other hand, the code shown by @tonhuisman may also be the true cause. The first Wire.begin... and immediate Wire.end... is also referred to as waking up the I2C device. So perhaps if you scan twice in short intervals, you may also see it?

Micha-he commented 3 years ago

I'll do a test today evening...

Micha-he commented 3 years ago

Yes, when I fast reload the i2c-scanner page, then the device(-address) is shown.

TD-er commented 3 years ago

OK, I will see what I can do. Not sure if we need to scan it twice on each scan, but maybe a flag can help here, or maybe check when the last scan was performed and if that was less than N seconds ago, we can scan each address twice.

tonhuisman commented 1 year ago

This is most likely fixed, so issue can be closed.

Micha-he commented 1 year ago

Nice, that you are closed this issue. But the problem still exists !

TD-er commented 1 year ago

Thanks for getting back on this.

The main purpose for closing those 200+ issues was to only keep those still relevant issues. Apparently this one is still relevant, thus let's keep it open.

TD-er commented 1 year ago

Maybe the I2C scan should be done at 100 kHz?

tonhuisman commented 1 year ago

I2C scan is already run at 100 kHz (Low I2C speed).

We might indeed need to wake I2C devices twice, but it is a particularity of this device that it won't respond like 99.9% of other I2C sensors. I'm not sure how other devices might like that, as it seems to be in the grey area of the specifications 🤔

Micha-he commented 1 year ago

The following was noticed during the test with a current custom-firmware:

The AM2320-device isn't read anything and is disabled after some minutes. I'll do another test with disabled "Check I2C devices when enabled" option tody evening.

But the i2c-scanner problem is the same, works after 2-5 quick reloads.

TD-er commented 1 year ago

The I2C check does actually use a similar check, so that may explain why it is being disabled after a while.

So we need to exclude this device from the I2C device check procedure.

Micha-he commented 1 year ago

Testresult from today: If I disable "Check I2C devices when enabled", the AM2320-device remains active !

If I re-enable it and activate a Debug-Log, then I can only see one line related to this device: I2C : Device doesn't respond for task: 3, then it is disabled again.

tonhuisman commented 1 year ago

@Micha-he I've started a fix for these issues in #4788 and a build is brewing in this GH Actions run. Can you please test to see if this solves the issues listed above, and report your findings here?

Micha-he commented 1 year ago

True, but I need a 2M256 build with the AM2320 plugin!

tonhuisman commented 1 year ago

Any other required plugins in that build?

tonhuisman commented 1 year ago

This is the Custom 2M256 build, with added P051, P037, P105 and P144 added: (I had that available, and just added P051 😉) ESP_Easy_mega_20230905_custom_ESP8266_2M256.zip

Micha-he commented 1 year ago

And P001? I'll flash it tomorrow and report here.

tonhuisman commented 1 year ago

The default Custom set is included, as defined in pre_custom_esp82xx.py, so P001 is included by default 😉

Micha-he commented 1 year ago

The changes #4788 at the AM2320-plugin and the i2c-scanner works fabulously ! I didn't see any problems.