Start gas and tests
authorPhil Howard <phil@gadgetoid.com>
Thu, 6 Jun 2019 23:22:14 +0000 (00:22 +0100)
committerPhil Howard <phil@gadgetoid.com>
Thu, 6 Jun 2019 23:22:14 +0000 (00:22 +0100)
.gitignore
library/envirophatplus/__init__.py
library/envirophatplus/gas.py [new file with mode: 0644]
library/envirophatplus/noise.py [new file with mode: 0644]
library/setup.py
library/tests/test_setup.py

index fa45562340a0e31d0d8f9885ce1989ec826835ea..5824813f2d753534e42a4d6f55b4e52171a837f3 100644 (file)
@@ -18,3 +18,4 @@ library/debian/
 .coverage
 .pytest_cache
 .tox
 .coverage
 .pytest_cache
 .tox
+.vscode/
index 219220542ea00abf2a362b079a16ff42c172e51f..b8023d8bc0ca413584a729e5106f86ba8fe65400 100644 (file)
@@ -1,2 +1 @@
-
 __version__ = '0.0.1'
 __version__ = '0.0.1'
diff --git a/library/envirophatplus/gas.py b/library/envirophatplus/gas.py
new file mode 100644 (file)
index 0000000..9b213ed
--- /dev/null
@@ -0,0 +1,80 @@
+"""Read the MICS6812 via an ads1015 ADC"""
+
+import ads1015
+import RPi.GPIO as GPIO
+
+MICS6812_EN_PIN = 24
+
+
+ads1015.I2C_ADDRESS_DEFAULT = ads1015.I2C_ADDRESS_ALTERNATE
+_is_setup = False
+
+
+class Mics6812Reading(object):
+    __slots__ = 'oxidising', 'reducing', 'nh3'
+
+    def __init__(self, ox, red, nh3):
+        self.oxidising = ox
+        self.reducing = red
+        self.nh3 = nh3
+
+    def __repr__(self):
+        return """Oxidising: {:05.02f}
+Reducing: {:05.02f}
+NH3: {:05.02f}
+""".format(self.oxidising, self.reducing, self.nh3)
+
+    __str__ = __repr__
+
+
+def setup():
+    global adc, _is_setup
+    if _is_setup:
+        return
+    _is_setup = True
+
+    adc = ads1015.ADS1015(i2c_addr=0x49)
+    adc.set_mode('single')
+    adc.set_programmable_gain(6.148)
+    adc.set_sample_rate(1600)
+
+    GPIO.setwarnings(False)
+    GPIO.setmode(GPIO.BCM)
+    GPIO.setup(MICS6812_EN_PIN, GPIO.OUT)
+    GPIO.output(MICS6812_EN_PIN, 1)
+
+
+def read_all():
+    ox = adc.get_voltage('in0/gnd')
+    red = adc.get_voltage('in1/gnd')
+    nh3 = adc.get_voltage('in2/gnd')
+
+    ox = (ox * 56000) / (3.3 - ox)
+    red = (red * 56000) / (3.3 - red)
+    nh3 = (nh3 * 56000) / (3.3 - nh3)
+
+    return Mics6812Reading(ox, red, nh3)
+
+
+def read_oxidising():
+    """Return gas resistance for oxidising gases.
+
+    Eg chlorine, nitrous oxide
+    """
+    setup()
+    return read_all().oxidising
+
+
+def read_reducing():
+    """Return gas resistance for reducing gases.
+
+    Eg hydrogen, carbon monoxide
+    """
+    setup()
+    return read_all().reducing
+
+
+def read_nh3():
+    """Return gas resistance for nh3/ammonia"""
+    setup()
+    return read_all().nh3
diff --git a/library/envirophatplus/noise.py b/library/envirophatplus/noise.py
new file mode 100644 (file)
index 0000000..e69de29
index 3fdea5ad67e255ac3cd57261e8d4db0cb18428fd..9d0a0244bb42b499aebd805e3944a0333fb2347d 100755 (executable)
@@ -50,5 +50,5 @@ setup(
     project_urls={'GitHub': 'https://www.github.com/pimoroni/envirophatplus-python'},
     classifiers=classifiers,
     packages=['envirophatplus'],
     project_urls={'GitHub': 'https://www.github.com/pimoroni/envirophatplus-python'},
     classifiers=classifiers,
     packages=['envirophatplus'],
-    install_requires=['pms5003', 'ltr559', 'st7735']
+    install_requires=['pimoroni-bme280', 'pms5003', 'ltr559', 'st7735', 'ads1015']
 )
 )
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..af3ed82d2694956ebacf5b3aa6bf82ad5152c2c5 100644 (file)
@@ -0,0 +1,54 @@
+import sys
+import mock
+from i2cdevice import MockSMBus
+
+
+class SMBusFakeDevice(MockSMBus):
+    def __init__(self, i2c_bus):
+        MockSMBus.__init__(self, i2c_bus)
+        self.regs[0x00:0x01] = 0x0f, 0x00
+
+
+def test_gas_setup():
+    sys.modules['RPi'] = mock.Mock()
+    sys.modules['RPi.GPIO'] = mock.Mock()
+    smbus = mock.Mock()
+    smbus.SMBus = SMBusFakeDevice
+    sys.modules['smbus'] = smbus
+    from envirophatplus import gas
+    gas.setup()
+    gas.setup()
+
+
+def test_gas_read_all():
+    sys.modules['RPi'] = mock.Mock()
+    sys.modules['RPi.GPIO'] = mock.Mock()
+    smbus = mock.Mock()
+    smbus.SMBus = SMBusFakeDevice
+    sys.modules['smbus'] = smbus
+    from envirophatplus import gas
+    result = gas.read_all()
+
+    assert type(result.oxidising) == float
+    assert int(result.oxidising) == 16641
+
+    assert type(result.reducing) == float
+    assert int(result.reducing) == 16727
+
+    assert type(result.nh3) == float
+    assert int(result.nh3) == 16813
+
+    assert "Oxidising" in str(result)
+
+
+def test_gas_read_each():
+    sys.modules['RPi'] = mock.Mock()
+    sys.modules['RPi.GPIO'] = mock.Mock()
+    smbus = mock.Mock()
+    smbus.SMBus = SMBusFakeDevice
+    sys.modules['smbus'] = smbus
+    from envirophatplus import gas
+
+    assert int(gas.read_oxidising()) == 16641
+    assert int(gas.read_reducing()) == 16727
+    assert int(gas.read_nh3()) == 16813