.coverage
.pytest_cache
.tox
+.vscode/
--- /dev/null
+"""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
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']
)
+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