diff --git a/NDIR_RasPi_Python/NDIR.py b/NDIR_RasPi_Python/NDIR.py index bd9ea8a..e837f14 100644 --- a/NDIR_RasPi_Python/NDIR.py +++ b/NDIR_RasPi_Python/NDIR.py @@ -5,6 +5,8 @@ class Sensor(): cmd_measure = [0xFF,0x01,0x9C,0x00,0x00,0x00,0x00,0x00,0x63] ppm = 0 + IODIR = 0X0A << 3 + IOSTATE = 0X0B << 3 IOCONTROL = 0X0E << 3 FCR = 0X02 << 3 LCR = 0X03 << 3 @@ -46,7 +48,7 @@ def measure(self): try: self.write_register(self.FCR, 0x07) self.send(self.cmd_measure) - time.sleep(0.01) + time.sleep(0.02) self.parse(self.receive()) return True except IOError: @@ -96,3 +98,15 @@ def receive(self): break return buf + + def power_on(self): + state = self.read_register(self.IOSTATE) + state |= 1 + self.write_register(self.IOSTATE, state) + + def power_off(self): + self.write_register(self.IODIR, 0x03) + state = self.read_register(self.IOSTATE) + state &= ~1 + self.write_register(self.IOSTATE, state) + diff --git a/NDIR_RasPi_Python/example.py b/NDIR_RasPi_Python/example.py index 18cb72c..7236619 100644 --- a/NDIR_RasPi_Python/example.py +++ b/NDIR_RasPi_Python/example.py @@ -6,11 +6,19 @@ if sensor.begin() == False: print("Adaptor initialization FAILED!") exit() - -while True: +sensor.power_on() +print("Power On") +i = 0 +while i < 50: + if i == 25: + sensor.power_off() + print("Power Off") + sensor.ppm = -1 if sensor.measure(): print("CO2 Concentration: " + str(sensor.ppm) + "ppm") else: print("Sensor communication ERROR.") time.sleep(1) + i += 1 +