forked from mrmcwethy/Adafruit_CircuitPython_DHT
-
Notifications
You must be signed in to change notification settings - Fork 62
Addition of test timing script #68
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dec04e6
Merge pull request #1 from adafruit/master
yeyeto2788 d001638
Create dht_time_calibration.py
yeyeto2788 4de55f4
Update dht_time_calibration.py
yeyeto2788 e62a47f
Update dht_time_calibration.py
yeyeto2788 27909fa
Update dht_time_calibration.py
yeyeto2788 378d6c2
Update dht_time_calibration.py
yeyeto2788 c1276db
Update dht_time_calibration.py
yeyeto2788 6c9561b
Add better description to advace script
yeyeto2788 153f2b1
Update examples.rst
yeyeto2788 7afac54
Update examples.rst
yeyeto2788 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ bundles | |
.eggs | ||
dist | ||
**/*.egg-info | ||
venv | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# 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 | ||
|
||
import board | ||
|
||
import adafruit_dht | ||
|
||
# Change the pin used below | ||
pin_to_use = "PG6" | ||
|
||
# Maximum number of tries per timing | ||
max_retries_per_time = 10 | ||
# 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 | ||
|
||
# Variable to store all reads on a try | ||
reads = {} | ||
|
||
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. | ||
dhtDevice = adafruit_dht.DHT11(pin=getattr(board, pin_to_use)) | ||
# Change the default wait time for triggering the read. | ||
# pylint: disable=protected-access | ||
dhtDevice._trig_wait = milliseconds | ||
|
||
# pylint: disable=protected-access | ||
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} 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. | ||
print_all = False | ||
if print_all: | ||
print(json.dumps(reads)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.