|
| 1 | +# SPDX-FileCopyrightText: 2021 yeyeto2788 for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +This script let's you check the best timing for you sensor as other people have face timing issues |
| 6 | +as seen on issue https://github.com/adafruit/Adafruit_CircuitPython_DHT/issues/66. |
| 7 | +
|
| 8 | +By changing the variables values below you will be able to check the best timing for you sensor, |
| 9 | +take into account that by most datasheets the timing for the sensor are 0.001 DHT22 and |
| 10 | +0.018 for DHT11 which are the default values of the library. |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import time |
| 15 | + |
| 16 | +import board |
| 17 | + |
| 18 | +import adafruit_dht |
| 19 | + |
| 20 | +# Change the pin used below |
| 21 | +pin_to_use = "PG6" |
| 22 | + |
| 23 | +# Maximum number of tries per timing |
| 24 | +max_retries_per_time = 10 |
| 25 | +# Minimum wait time from where to start testing |
| 26 | +min_time = 1500 |
| 27 | +# Maximum wait time on where to stop testing |
| 28 | +max_time = 2000 |
| 29 | +# Increment on time |
| 30 | +time_increment = 100 |
| 31 | + |
| 32 | +# Variable to store all reads on a try |
| 33 | +reads = {} |
| 34 | + |
| 35 | +initial_msg = f""" |
| 36 | +\nInitializing test with the following parameters: |
| 37 | +
|
| 38 | +- Maximum retries per waiting time: {max_retries_per_time} |
| 39 | +- Start time (ms): {min_time} |
| 40 | +- End time (ms): {max_time} |
| 41 | +- Increment time (ms): {time_increment} |
| 42 | +
|
| 43 | +This execution will try to read the sensor {max_retries_per_time} times |
| 44 | +for {len(range(min_time, max_time, time_increment))} different wait times values. |
| 45 | +
|
| 46 | +""" |
| 47 | +# Print initial message on the console. |
| 48 | +print(initial_msg) |
| 49 | + |
| 50 | +for milliseconds in range(min_time, max_time, time_increment): |
| 51 | + # Instantiate the DHT11 object. |
| 52 | + dhtDevice = adafruit_dht.DHT11(pin=getattr(board, pin_to_use)) |
| 53 | + # Change the default wait time for triggering the read. |
| 54 | + # pylint: disable=protected-access |
| 55 | + dhtDevice._trig_wait = milliseconds |
| 56 | + |
| 57 | + # pylint: disable=protected-access |
| 58 | + print(f"Using 'trig_wait' of {dhtDevice._trig_wait}") |
| 59 | + # Reset the read count for next loop |
| 60 | + reads_count = 0 |
| 61 | + |
| 62 | + # Create the key on the reads dictionary with the milliseconds used on |
| 63 | + # this try. |
| 64 | + if milliseconds not in reads: |
| 65 | + reads[milliseconds] = {"total_reads": 0} |
| 66 | + |
| 67 | + for try_number in range(0, max_retries_per_time): |
| 68 | + try: |
| 69 | + # Read temperature and humidity |
| 70 | + temperature = dhtDevice.temperature |
| 71 | + humidity = dhtDevice.humidity |
| 72 | + read_values = {"temperature": temperature, "humidity": humidity} |
| 73 | + |
| 74 | + if try_number not in reads[milliseconds]: |
| 75 | + reads[milliseconds][try_number] = read_values |
| 76 | + |
| 77 | + reads_count += 1 |
| 78 | + except RuntimeError as e: |
| 79 | + time.sleep(2) |
| 80 | + else: |
| 81 | + time.sleep(1) |
| 82 | + |
| 83 | + reads[milliseconds]["total_reads"] = reads_count |
| 84 | + |
| 85 | + print(f"Total read(s): {reads[milliseconds]['total_reads']}\n") |
| 86 | + dhtDevice.exit() |
| 87 | + |
| 88 | +# Gather the highest read numbers from all reads done. |
| 89 | +best_result = max([reads[milliseconds]["total_reads"] for milliseconds in reads]) |
| 90 | + |
| 91 | +# Gather best time(s) in milliseconds where we got more reads |
| 92 | +best_times = [ |
| 93 | + milliseconds |
| 94 | + for milliseconds in reads |
| 95 | + if reads[milliseconds]["total_reads"] == best_result |
| 96 | +] |
| 97 | +print( |
| 98 | + f"Maximum reads: {best_result} out of {max_retries_per_time} with the " |
| 99 | + f"following times: {', '.join([str(t) for t in best_times])}" |
| 100 | +) |
| 101 | + |
| 102 | +# change the value on the line below to see all reads performed. |
| 103 | +print_all = False |
| 104 | +if print_all: |
| 105 | + print(json.dumps(reads)) |
0 commit comments