Commit bd3970 sdb_dht22 semi rewrite

1 file Authored and Committed by Richard Marko 2 years ago
sdb_dht22 semi rewrite

    
sdb_dht22 +21 -29
  1 @@ -5,6 +5,8 @@
  2   import atexit
  3   import pigpio
  4   
  5 + DIAG = True
  6 + 
  7   
  8   class DHT(object):
  9   
 10 @@ -63,10 +65,10 @@
 11   
 12           atexit.register(self.cancel)
 13   
 14 -         self.bad_CS = 0  # Bad checksum count.
 15 -         self.bad_SM = 0  # Short message count.
 16 -         self.bad_MM = 0  # Missing message count.
 17 -         self.bad_SR = 0  # Sensor reset count.
 18 +         self.bad_checksum = 0
 19 +         self.short_message = 0
 20 +         self.missing_message = 0
 21 +         self.sensor_resets = 0
 22   
 23           # Power cycle if timeout > MAX_TIMEOUTS.
 24           self.no_response = 0
 25 @@ -139,7 +141,7 @@
 26   
 27                       else:
 28   
 29 -                         self.bad_CS += 1
 30 +                         self.bad_checksum += 1
 31   
 32               elif self.bit >= 24:  # in temp low byte
 33                   self.tL = (self.tL << 1) + val
 34 @@ -171,15 +173,14 @@
 35           else:  # level == pigpio.TIMEOUT:
 36               self.pi.set_watchdog(self.gpio, 0)
 37               if self.bit < 8:       # Too few data bits received.
 38 -                 self.bad_MM += 1    # Bump missing message count.
 39 +                 self.missing_message += 1
 40                   self.no_response += 1
 41                   if self.no_response > self.MAX_NO_RESPONSE:
 42                       self.no_response = 0
 43 -                     self.bad_SR += 1  # Bump sensor reset count.
 44 -                     self.reset_sensor()
 45 +                     self.reset()
 46   
 47               elif self.bit < 39:    # Short message receieved.
 48 -                 self.bad_SM += 1    # Bump short message count.
 49 +                 self.short_message += 1
 50                   self.no_response = 0
 51   
 52               else:                  # Full message received.
 53 @@ -200,35 +201,20 @@
 54           else:
 55               return -999
 56   
 57 -     def bad_checksum(self):
 58 -         """Return count of messages received with bad checksums."""
 59 -         return self.bad_CS
 60 - 
 61 -     def short_message(self):
 62 -         """Return count of short messages."""
 63 -         return self.bad_SM
 64 - 
 65 -     def missing_message(self):
 66 -         """Return count of missing messages."""
 67 -         return self.bad_MM
 68 - 
 69 -     def reset(self, delay=2):
 70 +      reset(self, delay=4):
 71           """
 72           Reset sensor
 73           """
 74   
 75           if self.power is not None:
 76               print("Resetting sensor")
 77 +             self.sensor_resets += 1
 78               self.powered = False
 79               self.pi.write(self.power, 0)
 80               time.sleep(delay)
 81               self.pi.write(self.power, 1)
 82               self.powered = True
 83   
 84 -     def sensor_resets(self):
 85 -         """Return count of power cycles because of sensor hangs."""
 86 -         return self.bad_SR
 87 - 
 88       def trigger(self):
 89           """Trigger a new relative humidity and temperature reading."""
 90           if self.powered:
 91 @@ -275,11 +261,17 @@
 92           time.sleep(0.2)
 93           t = time.time()
 94   
 95 -         for at in ["humidity", "temperature", "staleness", "bad_checksum",
 96 -                    "short_message", "missing_message", "sensor_resets"]:
 97 - 
 98 +         for at in ["humidity", "temperature", "staleness"]:
 99               print("{}.{} {:.2f} {}".format(source, at, getattr(s, at)(), t))
100   
101 + 
102 +         if DIAG:
103 +             for at in ["bad_checksum", "short_message",
104 +                        "missing_message", "sensor_resets",
105 +                        "no_response"]:
106 + 
107 +                 print("{}.{} {:.2f} {}".format(source, at, getattr(s, at), t))
108 + 
109           sys.stdout.flush()
110   
111           next_reading += INTERVAL