Skip to content
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

add raspberry pi and other fast-gpio device support when no pulseio #14

Closed
wants to merge 2 commits into from
Closed

Conversation

ladyada
Copy link
Member

@ladyada ladyada commented Aug 26, 2018

tested on Pi 3

@ladyada ladyada requested a review from a team August 26, 2018 05:14
Copy link

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran this on a Raspberry Pi 3B+
It works - sometimes ...
I get bad readings quite often (every 3 or 4) but there is an occasional crash -- see example:

>>> import dht_simpletest
Temp: 85.1 F / 29.5 C    Humidity: 68.3% 
A full buffer was not returned.  Try again.
Temp: 85.1 F / 29.5 C    Humidity: 68.7% 
Checksum did not validate. Try again.
Temp: 84.9 F / 29.4 C    Humidity: 68.9% 
Temp: 84.9 F / 29.4 C    Humidity: 68.9% 
Temp: 84.9 F / 29.4 C    Humidity: 68.9% 
A full buffer was not returned.  Try again.
Temp: 84.9 F / 29.4 C    Humidity: 69.0% 
Checksum did not validate. Try again.
Temp: 84.7 F / 29.3 C    Humidity: 69.0% 
Checksum did not validate. Try again.
Temp: 84.7 F / 29.3 C    Humidity: 69.0% 
Temp: 84.7 F / 29.3 C    Humidity: 69.0% 
Temp: 84.7 F / 29.3 C    Humidity: 68.9% 
Checksum did not validate. Try again.
Temp: 84.7 F / 29.3 C    Humidity: 68.9% 
Temp: 84.7 F / 29.3 C    Humidity: 68.7% 
Temp: 84.7 F / 29.3 C    Humidity: 68.6% 
A full buffer was not returned.  Try again.
Temp: 84.6 F / 29.2 C    Humidity: 68.5% 
Temp: 84.6 F / 29.2 C    Humidity: 68.5% 
A full buffer was not returned.  Try again.
Temp: 84.6 F / 29.2 C    Humidity: 68.4% 
A full buffer was not returned.  Try again.
Temp: 84.6 F / 29.2 C    Humidity: 68.4% 
Temp: 84.4 F / 29.1 C    Humidity: 68.4% 
Temp: 84.4 F / 29.1 C    Humidity: 68.5% 
Temp: 84.4 F / 29.1 C    Humidity: 68.5% 
Temp: 84.4 F / 29.1 C    Humidity: 68.6% 
Temp: 84.4 F / 29.1 C    Humidity: 68.6% 
Temp: 84.4 F / 29.1 C    Humidity: 68.6% 
Temp: 84.4 F / 29.1 C    Humidity: 68.6% 
Temp: 84.4 F / 29.1 C    Humidity: 68.6% 
A full buffer was not returned.  Try again.
Checksum did not validate. Try again.
Checksum did not validate. Try again.
Temp: 84.2 F / 29.0 C    Humidity: 68.5% 
Temp: 84.2 F / 29.0 C    Humidity: 68.6% 
Temp: 84.2 F / 29.0 C    Humidity: 68.6% 
Temp: 84.2 F / 29.0 C    Humidity: 68.6% 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/projects/blinka/dht/dht_simpletest.py", line 11, in <module>
    temperature_c = dhtDevice.temperature
  File "/home/pi/projects/blinka/dht/adafruit_dht.py", line 213, in temperature
    self.measure()
  File "/home/pi/projects/blinka/dht/adafruit_dht.py", line 164, in measure
    pulses = self._get_pulses()
  File "/home/pi/projects/blinka/dht/adafruit_dht.py", line 149, in _get_pulses
    pulses.append(int(1000000 * (transitions[i] - transitions[i-1])))
OverflowError: unsigned short is greater than maximum
>>> 

@jerryneedell
Copy link

I made this change and it seems to run OK: Lots of invalid checksums and "Full buffer not returned " errors, but it keeps running without the integer overflows.

diff --git a/adafruit_dht.py b/adafruit_dht.py
index f795f4d..928c50f 100644
--- a/adafruit_dht.py
+++ b/adafruit_dht.py
@@ -146,7 +146,11 @@ class DHTBase:
                         transitions.append(time.monotonic()) # save the timestamp
                 # convert transtions to microsecond delta pulses:
                 for i in range(1, len(transitions)):
-                    pulses.append(int(1000000 * (transitions[i] - transitions[i-1])))
+                    pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i-1]))
+                    if pulses_micro_sec > 65535:
+                        pulse.append(65535)
+                    else:
+                        pulses.append(pulses_micro_sec)
         return pulses
 
     def measure(self):

@ladyada
Copy link
Member Author

ladyada commented Aug 26, 2018

awesome, added!

@tannewt
Copy link
Member

tannewt commented Aug 28, 2018

How much more memory does this take up in a SAMD? (You can measure with gc.mem_free() with a gc.collect before it.) Thanks!

@jerryneedell
Copy link

Is there a reason why this has not been merged yet? It just came up via the forum:
https://forums.adafruit.com/viewtopic.php?f=60&t=142347

@ladyada
Copy link
Member Author

ladyada commented Oct 16, 2018

umm i didnt get around to calculating the gc.free on an m0 before and after
@tannewt do ya still want that?

@brennen
Copy link

brennen commented Oct 16, 2018

i can check mem usage here in a minute.

@tannewt
Copy link
Member

tannewt commented Oct 16, 2018

@brennen let us know what you find.

@brennen
Copy link

brennen commented Oct 16, 2018

ok, so i was running the following test on a feather m0 express:

import time
from board import D6
import gc

gc.collect()
print("before import:", gc.mem_free())
import adafruit_dht
print("after import:", gc.mem_free())

gc.collect()
print("before init:", gc.mem_free())
#initial the dht device
dhtDevice = adafruit_dht.DHT22(D6)
print("after init:", gc.mem_free())

while True:
    gc.collect()
    print("before sensor read:", gc.mem_free())
    try:
        time.sleep(.5)
        # show the values to the serial port
        temperature = dhtDevice.temperature * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print("Temp: {:.1f} F Humidity: {}% ".format(temperature, humidity))
        print("after sensor read:", gc.mem_free())
    except (RuntimeError, TypeError) as error:
        print(error.args)

on master, i get:

before import: 18912
after import: 11024
before init: 16464
after init: 16400
before sensor read: 16400
Temp: 69.6 F Humidity: 42.0% 
after sensor read: 15808
before sensor read: 16384

all well and good. with changes in this PR, i get:

main.py output:
before import: 18912
after import: 10320
before init: 15984
after init: 15920
before sensor read: 15920
('DHT sensor not found, check wiring',)
before sensor read: 15808
("unsupported types for __mul__: 'NoneType', 'float'",)
before sensor read: 15760
("unsupported types for __mul__: 'NoneType', 'float'",)
before sensor read: 15760
("unsupported types for __mul__: 'NoneType', 'float'",)
before sensor read: 15760
('DHT sensor not found, check wiring',)

increasing time.sleep() call to 2 seconds gets rid of the TypeError - i assume temperature was just returning None, which may be correct behavior that the simpletest just needs to be tweaked to account for, but it still seems like i can't get a reading at all with this code. here's the simpletest output:

main.py output:
DHT sensor not found, check wiring
DHT sensor not found, check wiring
DHT sensor not found, check wiring
DHT sensor not found, check wiring
DHT sensor not found, check wiring

@brennen
Copy link

brennen commented Oct 16, 2018

...aaaaand i see i probably got my wires crossed with another merge to master. @jerryneedell lemme know if you want a memory check once conflicts are resolved.

@jerryneedell
Copy link

Hmm, I don’t the sensor not found error is not the same as the None issues we saw. I’ll try to reproduce this tomorrow.

@jerryneedell
Copy link

hmm -- I reproduced the errors reported by @brennen on a Trinket M0
I also found that there was a slight change made to the get_pulses() function in the PR.
If I revert it back to the previous code, I no longer get errors and the DHt sensor reads.
here is the change:

jerryneedell@Ubuntu-Macmini:~/projects/trinket_m0$ diff adafruit_dht.py ~/projects/test_DHT/Adafruit_CircuitPython_DHT/adafruit_dht.py
122,124c122,123
<                 while True:
<                     if time.monotonic() - tmono > 0.25:
<                         break # time out after 1/4 seconds
---
>                 while time.monotonic() - tmono > 0.25:
>                     pass # time out after 1/4 seconds

In the PR code , while loop will immediately end - the test is inverted - it should wait at least .25 sec.
this change also works and just fixes the relational test

jerryneedell@Ubuntu-Macmini:~/projects/trinket_m0$ diff adafruit_dht.py ~/projects/test_DHT/Adafruit_CircuitPython_DHT/adafruit_dht.py
122,123c122,123
<                 while time.monotonic() - tmono < 0.25:
<                         pass # time out after 1/4 seconds
---
>                 while time.monotonic() - tmono > 0.25:
>                     pass # time out after 1/4 seconds
jerryneedell@Ubuntu-Macmini:~/projects/trinket_m0$ 

Here is the output of @brennen test code on the trinket M0:

Adafruit CircuitPython 4.0.0-alpha.1-99-g293345119 on 2018-10-12; Adafruit Trinket M0 with samd21e18
>>> import dht_brennen
before import: 19472
after import: 16784
before init: 16784
after init: 16704
before sensor read: 16704
Temp: 73.2 F Humidity: 59.5%
after sensor read: 16112
before sensor read: 16688
Temp: 73.2 F Humidity: 59.5%
after sensor read: 16608
before sensor read: 16688
Temp: 73.2 F Humidity: 59.5%
after sensor read: 16608
before sensor read: 16688
Temp: 73.2 F Humidity: 59.5%
after sensor read: 16608
before sensor read: 16688
Temp: 73.2 F Humidity: 59.2%
after sensor read: 16112
before sensor read: 16688
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dht_brennen.py", line 27, in <module>
  File "dht_brennen.py", line 20, in <module>
KeyboardInterrupt:

I'm not sure how to try to make this change to the PR and resolve the conflicts with the current master.
Can I do it with the Web Editor? @ladyada - would you prefer to just do the changes yourself.
Let me know how you want to proceed.

@ladyada
Copy link
Member Author

ladyada commented Oct 17, 2018

sorry i have no idea how to fix and im not going to have time this week to learn, the changes arent that large.

i can just close this, y'all cna start a new PR?

@jerryneedell
Copy link

@ladyada I'll work on w new PR - hopefully tomorrow to fix this and reconcile with the master. Once it is in, you can close this one if you approve.

@ladyada
Copy link
Member Author

ladyada commented Oct 17, 2018

yay thanks!

@jerryneedell
Copy link

@ladyada since #18 has been merged should we go ahead and close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants