Commit a8715b dht: separate reset sensor, reset on start

1 file Authored and Committed by Richard Marko 2 years ago
dht: separate reset sensor, reset on start

    
 1 @@ -6,7 +6,7 @@
 2   import pigpio
 3   
 4   
 5 - class sensor:
 6 + class DHT(object):
 7   
 8       """
 9       A class to read relative humidity and temperature from the
10 @@ -176,13 +176,8 @@
11                   if self.no_response > self.MAX_NO_RESPONSE:
12                       self.no_response = 0
13                       self.bad_SR += 1  # Bump sensor reset count.
14 -                     if self.power is not None:
15 -                         self.powered = False
16 -                         self.pi.write(self.power, 0)
17 -                         time.sleep(2)
18 -                         self.pi.write(self.power, 1)
19 -                         time.sleep(2)
20 -                         self.powered = True
21 +                     self.reset_sensor()
22 + 
23               elif self.bit < 39:    # Short message receieved.
24                   self.bad_SM += 1    # Bump short message count.
25                   self.no_response = 0
26 @@ -217,6 +212,19 @@
27           """Return count of missing messages."""
28           return self.bad_MM
29   
30 +     def reset(self, delay=2):
31 +         """
32 +         Reset sensor
33 +         """
34 + 
35 +         if self.power is not None:
36 +             print("Resetting sensor")
37 +             self.powered = False
38 +             self.pi.write(self.power, 0)
39 +             time.sleep(delay)
40 +             self.pi.write(self.power, 1)
41 +             self.powered = True
42 + 
43       def sensor_resets(self):
44           """Return count of power cycles because of sensor hangs."""
45           return self.bad_SR
46 @@ -248,7 +256,8 @@
47   
48       pi = pigpio.pi()
49   
50 -     s = sensor(pi, 22, LED=24, power=27)
51 +     s = DHT(pi, 22, LED=24, power=27)
52 +     s.reset()
53   
54       r = 0
55