Closed HeatfanJohn closed 9 years ago
Leave the class as it originally was but remove the import atexit and the atexit call.
Change the main to look like the following.
import pigpio
import DHT22
pi = pigpio.pi()
s = DHT22.sensor(pi, 4)
s.trigger()
time.sleep(0.2)
print("{} {} {:3.2f} {} {} {} {}".format(
s.humidity(), s.temperature(), s.staleness(),
s.bad_checksum(), s.short_message(), s.missing_message(),
s.sensor_resets()))
s.cancel()
pi.stop()
The reading will be good provided bad_checksum(), short_message(), missing_message(), and sensor_resets() are all zero.
That resolved the problem. Thank you!
Is the atexit code a safety mechanism to ensure that cancel is always performed?
What would happen if the program exited without calling cancel?
Also, I have noticed that on my gen 1 Pi model B that pigpiod always uses around 8% CPU as reported by TOP.
Is that normal?
On Tue, Jul 28, 2015 at 5:30 PM, joan2937 notifications@github.com wrote:
Leave the class as it originally was but remove the import atexit and the atexit call.
Change the main to look like the following.
import pigpio
import DHT22
pi = pigpio.pi()
s = DHT22.sensor(pi, 4)
s.trigger()
time.sleep(0.2)
print("{} {} {:3.2f} {} {} {} {}".format( s.humidity(), s.temperature(), s.staleness(), s.bad_checksum(), s.short_message(), s.missing_message(), s.sensor_resets()))
s.cancel()
pi.stop()
The reading will be good provided bad_checksum(), short_message(), missing_message(), and sensor_resets() are all zero.
— Reply to this email directly or view it on GitHub https://github.com/joan2937/pigpio/issues/16#issuecomment-125759028.
The atexit in the class was really to make sure the watchdog was switched off if the main program terminated (the expectation being that most often the class would be used in a forever loop). If the watchdog was left switched on it might confuse a later script which just expected to get level changes.
8% CPU usage is about right on a non-Pi2. That's the base level for the overhead of the 200 thousand gpio reads per second made by the daemon. However that's a fixed overhead regardless of how many gpios are used for PWM or servo pulses and is very quickly more efficient than trying to service anywhere near that number of gpio interrupts.
I'm trying to use your DHT22.py sample code to return a single temperature and humidity reading from an AM2302 sensor. When I modified your code to only return one reading and then exit I received the following errors:
Here is my
main
which only makes one sensor reading.I made the following change to the
cancel
method of your DHT22sensor
class. I added anif
to ensure thatsl.s
wasn'tNone
before callset_watchdog
. This appears to work, but I'm sure that this violates all the principles of object-oriented programming.Is there a better way to do this?
Also, what actions should I take to ensure that I got a valid reading?