garyns / pigpio-dht

DHT11 Temperature and Humidity Sensor using pigpio
GNU General Public License v3.0
14 stars 6 forks source link

example code has defect #2

Closed Blindfreddy closed 4 years ago

Blindfreddy commented 4 years ago

The code in README file doesn't work

As is:

# NOTE: The import has a _ not - in the module name.
from pigpio_dit import DHT11, DHT22

gpio = 21 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.read()
print(result)

but should be:

# NOTE: The import has a _ not - in the module name.
from pigpio_dht.DHT11 import DHT11
#from pigpio_dht.DHT22 import DHT22

gpio = 21 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.read()
print(result)
garyns commented 4 years ago

Thanks for pointing this out.

The root cause of the examples failing was "dht" was spelt "dit".

The README in both GitHub and PyPi has been fixed now.

So in full, the fixed example as you referenced is

# NOTE: The import has a _ not - in the module name.
from pigpio_dht import DHT11, DHT22

gpio = 21 # BCM Numbering

sensor = DHT11(gpio)
#sensor = DHT22(gpio)

result = sensor.read()
print(result)
Blindfreddy commented 4 years ago

Hi

Pretty sure that still doesn't work, because the import statement imports the module and not the objects with the same name.

>>> from pigpio_dht import DHT11, DHT22
>>> gpio = 21 # BCM Numbering
>>> sensor = DHT11(gpio)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

To fix this and to adhere to python naming conventions you should rename the modules DHT11.py and DHT22.py to lowercase dht11.py and dht22.py (and while you're at it DHTXX.py to dhtxx.py) and either import them, then in the line with sensor = it should be

sensor = dht11.DHT11(gpio) or sensor = dht22.DHT22(gpio)

or change the import to be

from pigpio_dht.dht11 import DHT11
from pigpio_dht.dht22 import DHT22

But that's still somewhat confusing, why not merge the classes into one module and import the desired class directly from it ? Then the readme would be correct and adhere to naming conventions.

garyns commented 4 years ago

Thanks. I've nailed the underlying issue down to the .gitignore (that was copied from another project) having an entry that excludes__init__.py, so it's not in GitHub or PyPi. At this time due to travel commitments, I can't test changes against a physical RaspberyPi until later next week, at which time I'll commit the fix. Thanks again for raising this issue (and your others). I appreciate a capable second set of eyes and review. Gary.

garyns commented 4 years ago

Resolved. init.py now in repo and pypi package. Closing.

garyns commented 4 years ago

Closing.