From 1ec888759884a10ce4a5c73f4b211fcf2884d761 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 12 Nov 2017 09:05:36 -0800 Subject: [PATCH 1/2] add power control --- NDIR_RasPi_Python/NDIR.py | 14 ++++++++++++++ NDIR_RasPi_Python/example.py | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/NDIR_RasPi_Python/NDIR.py b/NDIR_RasPi_Python/NDIR.py index bd9ea8a..69dfc6c 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 @@ -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 + From 3b98e3e665e6258ae8b8848879e33f9a9595b3c7 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 12 Nov 2017 10:12:41 -0800 Subject: [PATCH 2/2] improve sensor communication --- NDIR_RasPi_Python/NDIR.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NDIR_RasPi_Python/NDIR.py b/NDIR_RasPi_Python/NDIR.py index 69dfc6c..e837f14 100644 --- a/NDIR_RasPi_Python/NDIR.py +++ b/NDIR_RasPi_Python/NDIR.py @@ -48,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: