After more than 5 years, I still get an occasional Email from Makers who ask questions about the Aosong AM2315 temperature/humidity sensor. I got one this week asking why my AM2315 python code did not work with Python3.
This nudged me to take a look at my code and figure out what it would take to get it to work with Python3. Next thing you know, I decided to remove the underlying dependency on any C code libraries and ensure my sample Python code works in both Python2.7 and Python3 without modifications.
To do this, I rely on two Python modules. The first is the great code written by our friends over at Adafruit. They have created a Rapsberry Pi GPIO code module written in pure Python. The second module is from the innovative folks over at SwitchDoc Labs.
I have updated the AM2315 Implementation Guide with the new changes. I also modified the SwitchDoc code so that it will work with Python3.
Now it is time for a rant. If you are a Python programmer, it is time for you to get serious about Python3 if you are still clinging to your Python2.7 worldview. All new code you write should be written in Python3. Python2.7 has an End Of Life date on January 1, 2020. There is a lot of 2.7 code out there that will work in Python3 with just a few changes. In fact, the only changes I made to get the SwitchDoc code to work was putting parenthesis around the print statements. EOR. (End of Rant)
You can download the updated implementation document with a code sample here. Thanks to Adafruit and SwitchDoc Labs for being a part of the Open Source community.
Pingback: AM2315 Update | Electronic Adventures with Sopwith
Hi Sopwith,
I have been trying to get my am2315 working via every one of the many drivers for quite some time with no success and was just now very happy to see your update. i2cdetect -y 1 works fine, but after following the steps in your most excellent how-to, I think I’m having some python version issues?
here is the error message with python3 from your script:
pi@b ~ $ python3 aosong-am2315.py
Traceback (most recent call last):
File “aosong-am2315.py”, line 196, in
c = am2315.read_temperature()
File “aosong-am2315.py”, line 176, in read_temperature
self._read_data()
File “aosong-am2315.py”, line 97, in _read_data
self.humidity = ((tmp[2] << 8) | tmp[3]) / 10.0
TypeError: 'NoneType' object is not subscriptable
And here is the python (2.7) error:
pi@b ~/SDL_Pi_AM2315 $ python testAM2315.py
T
Traceback (most recent call last):
File "testAM2315.py", line 7, in
print “T “, thsen.read_temperature()
File “/home/pi/SDL_Pi_AM2315/AM2315.py”, line 115, in read_temperature
self._read_data()
File “/home/pi/SDL_Pi_AM2315/AM2315.py”, line 88, in _read_data
self.humidity = ((tmp[2] << 8) | tmp[3]) / 10.0
TypeError: 'NoneType' object has no attribute '__getitem__'
I am quite new at python but have google both TypeErrors, and am quite stuck.
Thanks for any help.