From d00163889f1de5e38048c3ca27c6940a7ce90d1b Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 13:33:02 +0100 Subject: [PATCH 1/9] Create dht_time_calibration.py Addition of a script to test best timing for DHT sensor. --- examples/dht_time_calibration.py | 84 ++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 examples/dht_time_calibration.py diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py new file mode 100644 index 0000000..a0857c4 --- /dev/null +++ b/examples/dht_time_calibration.py @@ -0,0 +1,84 @@ +import json +import time + +import board + +import adafruit_dht + +pin_to_use = 'PG6' + +# Maximum number of tries per timing +max_retries_per_time = 10 +# Minimum time from where to start testing +min_time = 500 +# Maximum time on where to stop testing +max_time = 2000 +# Increment on time +time_increment = 100 + +# Variable to store all reads on a try +reads = {} + +print("\nInitializing test.\n") + +for milliseconds in range(min_time, max_time, time_increment): + # Instantiate the DHT11 object. + dhtDevice = adafruit_dht.DHT11(pin=getattr(board, pin_to_use)) + # Change the default wait time for triggering the read. + dhtDevice._trig_wait = milliseconds + + print(f"Using 'trig_wait' of {dhtDevice._trig_wait}") + # Reset the read count for next loop + reads_count = 0 + + # Create the key on the reads dictionary with the milliseconds used on + # this try. + if milliseconds not in reads: + reads[milliseconds] = { + "total_reads": 0 + } + + for try_number in range(0, max_retries_per_time): + try: + # Read temperature and humidity + temperature = dhtDevice.temperature + humidity = dhtDevice.humidity + read_values = { + "temperature": temperature, + "humidity": humidity + } + + if try_number not in reads[milliseconds]: + reads[milliseconds][try_number] = read_values + + reads_count += 1 + except RuntimeError as e: + time.sleep(2) + else: + time.sleep(1) + + reads[milliseconds]["total_reads"] = reads_count + + print(f"Total read(s): {reads[milliseconds]['total_reads']}\n") + dhtDevice.exit() + + +# Gather the highest read numbers from all reads done. +best_result = max( + [ + reads[milliseconds]["total_reads"] for milliseconds in reads + ] +) + +# Gather best time(s) in milliseconds where we got more reads +best_times = [ + milliseconds for milliseconds in reads if reads[milliseconds]['total_reads'] == best_result +] +print( + f"Maximum reads: {best_result} with the " + f"following times: {''.join([str(t) for t in best_times])}" +) + +# change the value on the line below to see all reads performed. +print_all = False +print(json.dumps(reads)) if print_all else None From 4de55f4bb2a7e2453fbb688444af779518198f02 Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 13:39:08 +0100 Subject: [PATCH 2/9] Update dht_time_calibration.py Fix minor issue on printing best timing --- examples/dht_time_calibration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py index a0857c4..93a6df6 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration.py @@ -76,7 +76,7 @@ ] print( f"Maximum reads: {best_result} with the " - f"following times: {''.join([str(t) for t in best_times])}" + f"following times: {' ,'.join([str(t) for t in best_times])}" ) # change the value on the line below to see all reads performed. From e62a47f410192fec0e8f2fac7ba07bfd1a1a41e2 Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 13:47:09 +0100 Subject: [PATCH 3/9] Update dht_time_calibration.py Add licensing so build does not fail. --- examples/dht_time_calibration.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py index 93a6df6..3899b2e 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2021 yeyeto2788 for Adafruit Industries +# SPDX-License-Identifier: MIT + import json import time From 27909fae69d484be9e54922203ad5e71d1c099bd Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 14:16:09 +0100 Subject: [PATCH 4/9] Update dht_time_calibration.py Add more details on console output like the number of tries per trig_wait --- examples/dht_time_calibration.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py index 3899b2e..cd6c598 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration.py @@ -22,7 +22,10 @@ # Variable to store all reads on a try reads = {} -print("\nInitializing test.\n") +print( + "\nInitializing test.\n", + f"Total tries per trig_wait {max_retries_per_time}" +) for milliseconds in range(min_time, max_time, time_increment): # Instantiate the DHT11 object. @@ -78,8 +81,8 @@ milliseconds for milliseconds in reads if reads[milliseconds]['total_reads'] == best_result ] print( - f"Maximum reads: {best_result} with the " - f"following times: {' ,'.join([str(t) for t in best_times])}" + f"Maximum reads: {best_result} out of {max_retries_per_time} with the " + f"following times: {', '.join([str(t) for t in best_times])}" ) # change the value on the line below to see all reads performed. From 378d6c219c05a46590fa48b2823234b2f0194634 Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 14:45:05 +0100 Subject: [PATCH 5/9] Update dht_time_calibration.py Fix issues on linting. --- examples/dht_time_calibration.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py index cd6c598..69540f5 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration.py @@ -8,7 +8,7 @@ import adafruit_dht -pin_to_use = 'PG6' +pin_to_use = "PG6" # Maximum number of tries per timing max_retries_per_time = 10 @@ -22,17 +22,16 @@ # Variable to store all reads on a try reads = {} -print( - "\nInitializing test.\n", - f"Total tries per trig_wait {max_retries_per_time}" -) +print("\nInitializing test.\n", f"Total tries per trig_wait {max_retries_per_time}") for milliseconds in range(min_time, max_time, time_increment): # Instantiate the DHT11 object. dhtDevice = adafruit_dht.DHT11(pin=getattr(board, pin_to_use)) # Change the default wait time for triggering the read. + # pylint: disable=W0212 dhtDevice._trig_wait = milliseconds + # pylint: disable=W0212 print(f"Using 'trig_wait' of {dhtDevice._trig_wait}") # Reset the read count for next loop reads_count = 0 @@ -40,19 +39,14 @@ # Create the key on the reads dictionary with the milliseconds used on # this try. if milliseconds not in reads: - reads[milliseconds] = { - "total_reads": 0 - } + reads[milliseconds] = {"total_reads": 0} for try_number in range(0, max_retries_per_time): try: # Read temperature and humidity temperature = dhtDevice.temperature humidity = dhtDevice.humidity - read_values = { - "temperature": temperature, - "humidity": humidity - } + read_values = {"temperature": temperature, "humidity": humidity} if try_number not in reads[milliseconds]: reads[milliseconds][try_number] = read_values @@ -68,17 +62,14 @@ print(f"Total read(s): {reads[milliseconds]['total_reads']}\n") dhtDevice.exit() - # Gather the highest read numbers from all reads done. -best_result = max( - [ - reads[milliseconds]["total_reads"] for milliseconds in reads - ] -) +best_result = max([reads[milliseconds]["total_reads"] for milliseconds in reads]) # Gather best time(s) in milliseconds where we got more reads best_times = [ - milliseconds for milliseconds in reads if reads[milliseconds]['total_reads'] == best_result + milliseconds + for milliseconds in reads + if reads[milliseconds]["total_reads"] == best_result ] print( f"Maximum reads: {best_result} out of {max_retries_per_time} with the " @@ -87,4 +78,5 @@ # change the value on the line below to see all reads performed. print_all = False -print(json.dumps(reads)) if print_all else None +if print_all: + print(json.dumps(reads)) From c1276dba59876b815686d147b4947d92e757dcbf Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Fri, 19 Mar 2021 15:24:36 +0100 Subject: [PATCH 6/9] Update dht_time_calibration.py Add more description and also what parameters were used on the test. --- examples/dht_time_calibration.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration.py index 69540f5..b603d65 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration.py @@ -12,9 +12,9 @@ # Maximum number of tries per timing max_retries_per_time = 10 -# Minimum time from where to start testing -min_time = 500 -# Maximum time on where to stop testing +# Minimum wait time from where to start testing +min_time = 1500 +# Maximum wait time on where to stop testing max_time = 2000 # Increment on time time_increment = 100 @@ -22,7 +22,20 @@ # Variable to store all reads on a try reads = {} -print("\nInitializing test.\n", f"Total tries per trig_wait {max_retries_per_time}") +initial_msg = f""" +\nInitializing test with the following parameters: + +- Maximum retries per waiting time: {max_retries_per_time} +- Start time (ms): {min_time} +- End time (ms): {max_time} +- Increment time (ms): {time_increment} + +This execution will try to read the sensor {max_retries_per_time} times +for {len(range(min_time, max_time, time_increment))} different wait times values. + +""" +# Print initial message on the console. +print(initial_msg) for milliseconds in range(min_time, max_time, time_increment): # Instantiate the DHT11 object. From 6c9561bfaedb1cd8b1e1a2bf73f79ed4be7d38a0 Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Tue, 23 Mar 2021 08:31:39 +0100 Subject: [PATCH 7/9] Add better description to advace script Add better description to the advance script. Add the `venv` folder to be ignore. Add the ussage of the script for the documentation. --- .gitignore | 1 + docs/examples.rst | 10 ++++++++++ ...ibration.py => dht_time_calibration_advance.py} | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) rename examples/{dht_time_calibration.py => dht_time_calibration_advance.py} (83%) diff --git a/.gitignore b/.gitignore index 9647e71..597d08d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ bundles .eggs dist **/*.egg-info +venv diff --git a/docs/examples.rst b/docs/examples.rst index 10e4456..96a0405 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,3 +6,13 @@ Ensure your device works with this simple test. .. literalinclude:: ../examples/dht_simpletest.py :caption: examples/dht_simpletest.py :linenos: + + +Time calibration advance test +------------ + +Check what is the best time your sensor. + +.. literalinclude:: ../examples/dht_time_calibration_advance.py + :caption: examples/dht_time_calibration_advance.py + :linenos: \ No newline at end of file diff --git a/examples/dht_time_calibration.py b/examples/dht_time_calibration_advance.py similarity index 83% rename from examples/dht_time_calibration.py rename to examples/dht_time_calibration_advance.py index b603d65..9879f6c 100644 --- a/examples/dht_time_calibration.py +++ b/examples/dht_time_calibration_advance.py @@ -1,6 +1,15 @@ # SPDX-FileCopyrightText: 2021 yeyeto2788 for Adafruit Industries # SPDX-License-Identifier: MIT +""" +This script let's you check the best timing for you sensor as other people have face timing issues +as seen on issue https://github.com/adafruit/Adafruit_CircuitPython_DHT/issues/66. + +By changing the variables values below you will be able to check the best timing for you sensor, +take into account that by most datasheets the timing for the sensor are 0.001 DHT22 and +0.018 for DHT11 which are the default values of the library. +""" + import json import time @@ -8,6 +17,7 @@ import adafruit_dht +# Change the pin used below pin_to_use = "PG6" # Maximum number of tries per timing @@ -41,10 +51,10 @@ # Instantiate the DHT11 object. dhtDevice = adafruit_dht.DHT11(pin=getattr(board, pin_to_use)) # Change the default wait time for triggering the read. - # pylint: disable=W0212 + # pylint: disable=protected-access dhtDevice._trig_wait = milliseconds - # pylint: disable=W0212 + # pylint: disable=protected-access print(f"Using 'trig_wait' of {dhtDevice._trig_wait}") # Reset the read count for next loop reads_count = 0 From 153f2b163a52c63703e0203921f180efb384311b Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Tue, 23 Mar 2021 10:00:06 +0100 Subject: [PATCH 8/9] Update examples.rst Add new line on file --- docs/examples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples.rst b/docs/examples.rst index 96a0405..187d8ec 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -15,4 +15,4 @@ Check what is the best time your sensor. .. literalinclude:: ../examples/dht_time_calibration_advance.py :caption: examples/dht_time_calibration_advance.py - :linenos: \ No newline at end of file + :linenos: From 7afac543a4dcbf63cc6878c8e0274b4cd0745667 Mon Sep 17 00:00:00 2001 From: yeyeto2788 Date: Tue, 23 Mar 2021 13:04:38 +0100 Subject: [PATCH 9/9] Update examples.rst Add longer heading underscore line. --- docs/examples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples.rst b/docs/examples.rst index 187d8ec..399b499 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -9,7 +9,7 @@ Ensure your device works with this simple test. Time calibration advance test ------------- +------------------------------ Check what is the best time your sensor.