Closed tvStatic closed 5 years ago
Hi, thanks for the code, but I do have some issues with it. So I looked around on the internet, and found a slight easier to read code: https://github.com/contactless/wb-mqtt-am2320/blob/master/am2320.py
So I added support for this sensor. Just enter the I2C address (default is 5c). Could you test this?
Just do a git pull and restart. Clear browser cache and you should be able to add AM2320 sensors. Both temperature and humidity is supported.
Thanks for your efforts!
Getting the following when I added this sensor:
load_raw_data humid:
global name 'PARAM_AM2320_READ' is not defined
2019-06-12 12:53:56,752 - WARNING - terrariumSensor - Measured value NoneC from am2320 sensor 'Test Temp' is outside valid range 0.00C - 40.00C in 0.00985 seconds.
I moved the two declarations in terrariumI2CSensor.py:
PARAM_AM2320_READ = 0x03
REG_AM2320_HUMIDITY_MSB = 0x00
To line 622, then got this error:
load_raw_data humid:
local variable 'buf' referenced before assignment
I added the following at the start of get_raw_data:
buf = ""
Then got the following error:
load_raw_data temp:
[Errno 121] Remote I/O error
load_raw_data humid:
global name 'unpack' is not defined
Added:
from struct import unpack
To the top of the file. And finally ended up with this:
load_raw_data temp:
[Errno 121] Remote I/O error
load_raw_data humid:
unpack requires a string argument of length 2
My own scripts are continuing to work at address 5c, so it's not a sensor problem. To verify: Hardware is AM2320, Address is 5c.
Hmm, I made some updates. So could you see if it improves?
Else. could you run the script https://github.com/contactless/wb-mqtt-am2320/blob/master/am2320.py and see if that works for you? If that script is not working, then my code will also not work.
I was able to figure out the issue based on running the script you specified.
The issue is related to this part of the code that I provided, for which there is no equivalent in the mqtt version:
# wake AM2320 up, goes to sleep to not warm up and affect the humidity sensor
# This write will fail as AM2320 won't ACK this write
try:
posix.write(fd, b'\0x00')
except:
pass
Fix was to replace line 602 in terrariumI2CSensor.py with the following:
# wake AM2320 up, goes to sleep to not warm up and affect the humidity sensor
# This write will fail as AM2320 won't ACK this write
try:
self.i2c_bus.write_i2c_block_data(int('0x' + gpio_pins[0],16), 0x00, [])
except:
pass
With this code fix, the sensor is operated as expected. Thanks!
Ah, yes now I see it. I have updated my code. It should now work nicely. Could you update once more and test it? And thanks for the testing... it helps a lot and saves me some money not buying all kind of sensors. :P
I can verify that the latest commit is working correctly. Thanks again.
AM2320 is a I2C temperature/humidity sensor.
The sensor is a little finicky in that it sleeps after 3 seconds of inactivity, so it is necessary to send a packet to it to wake it up before reading from it. It can be run with a one-wire configuration like a DHT22, but I was unable to make it work with this configuration.
Code to get the temperature and humidity is as follows (from here but the code is untabbed):
When I have time, I can prepare a pull request if preferred.