From: Phil Howard Date: Thu, 6 Jun 2019 23:22:14 +0000 (+0100) Subject: Start gas and tests X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/commitdiff_plain/4f8716dc13aa14bf586dd78002d6bf4d331bc656?ds=inline;hp=--cc Start gas and tests --- 4f8716dc13aa14bf586dd78002d6bf4d331bc656 diff --git a/.gitignore b/.gitignore index fa45562..5824813 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ library/debian/ .coverage .pytest_cache .tox +.vscode/ diff --git a/library/envirophatplus/__init__.py b/library/envirophatplus/__init__.py index 2192205..b8023d8 100644 --- a/library/envirophatplus/__init__.py +++ b/library/envirophatplus/__init__.py @@ -1,2 +1 @@ - __version__ = '0.0.1' diff --git a/library/envirophatplus/gas.py b/library/envirophatplus/gas.py new file mode 100644 index 0000000..9b213ed --- /dev/null +++ b/library/envirophatplus/gas.py @@ -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 index 0000000..e69de29 diff --git a/library/setup.py b/library/setup.py index 3fdea5a..9d0a024 100755 --- a/library/setup.py +++ b/library/setup.py @@ -50,5 +50,5 @@ setup( 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'] ) diff --git a/library/tests/test_setup.py b/library/tests/test_setup.py index e69de29..af3ed82 100644 --- a/library/tests/test_setup.py +++ b/library/tests/test_setup.py @@ -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