-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Additional DHT sensor support, including new I2C versions #2290
Comments
Datasheets shared here: https://www.dropbox.com/sh/500lbdxn7hlk4nk/AAAm312r9ESImG0aqd91IXAqa?dl=0 |
One approach is to pass a constructor argument which may either be a Pin or an I2C instance. The address could default to None, so the caller either passes a Pin or an I2C and an address. The constructor then does something along these lines: self.i2c = None
self.pin = None
if type(arg) is pyb.I2C:
if type(address) is not int:
raise ValueError('Must pass I2C address')
else:
self.i2c = arg
self.address = address
elif type(arg) is pyb.Pin:
self.pin = arg
else:
raise ValueError('Must supply a Pin or I2C instance') |
It's much better to have an optional keyword argument that specifiAM2320es what it is, something like:
This way you can still pass anything that had the right methods and attributes, and it will work, no matter what its type is. This is especially useful for injecting a debugging object, or for writing unit tests where you don't want it to use real hardware. |
Another approach is to have the sensor's logic in the base class, and have two classes, say Then you can still have a factory function that instantiates the right class based on the arguments passed, as above. |
My AM2320s arrived today. They have an I2C interface that works differently to the DHT12 described above. We may need separate libraries after all. Here is a AM2320 version working on my WeMos D1 mini:
I am seeing a lot of |
AM2320 and DHT12 libraries added here: My dream of a universal DHT driver is fading. With lots of subtle differences between the devices, perhaps it's best to have separate drivers. |
…itions Add pin reset and never reset to common-hal
I would like to improve the DHT driver and add support for an additional 14 sensors.
I have been experimenting with a number of DHT sensors (Aosong AM23xx series) and have discovered there are 4 main types:
Type 1: low-resolution digital (unique to DHT11)
Sensors: DHT11
Type 2: digital (AM230x)
Sensors: AM2301, DHT22 (AM2302), AM2302 (Wired), DHT33 (RHT04), AM2303, AM2305 (DHT44), AM2306
Type 3: digital and I2C (AM232x)
Sensors: DHT12, AM2320, AM2321, AM2322, AM2325
Type 4: I2C (AM231x)
Sensors: AM2311, AM2312, AM2313, AM2315
With the exception of the DHT11 sensor, all of them share the same custom 1-wire protocol (unrelated to Dallas).
All of the newer sensors which have an I2C interface, have the same device and register addresses.
They are all interchangeable, with range, accuracy, stability, current draw, speed and cost being the only differences.
All of the dual digital + I2C sensors can be put in digital mode by grounding the 4th pin (SCL).
I have compiled specifications for each sensor from various English and Chinese datasheets:
https://docs.google.com/spreadsheets/d/1jbSpU-rLXuba9aubcYI15_6hI2wM7-vmQ4iPcAV7MI4/edit
I would like to update the MicroPython ESP8266 DHT driver to support all of these sensors.
I have prototyped the I2C version and it works with my DHT12.
I can test the rest of the I2C sensors once they arrive in the mail.
I added this to /scripts/dht.py and flashed to my device:
It works, but it can be done better.
Here is it working with a DHT12 and a WeMos D1 Mini.
Connections:
I am not sure of the most pythonic way to structure this as each new dual interface sensor can potentially have 2x base classes, 1-wire and I2C.
They are constructed with either a single digital pin, or an I2C object + device address.
Constructor overloading?
There is also 16 DHT sensors, with probably more on the way.
They all fall into the type categories I have detailed above.
Should we have a class per type of sensor? eg. DHT11, DHT_OneWire, DHT_I2C?
A class for each of the product ranges? eg. DHT11, DHT12, AM230x, AM231x, AM232x?
Hoping someone with a bit more Python experience can help with the structure.
The text was updated successfully, but these errors were encountered: