Skip to content

Commit 4f8716d

Browse files
committed
Start gas and tests
1 parent 3547bf1 commit 4f8716d

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ library/debian/
1818
.coverage
1919
.pytest_cache
2020
.tox
21+
.vscode/

library/envirophatplus/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
21
__version__ = '0.0.1'

library/envirophatplus/gas.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Read the MICS6812 via an ads1015 ADC"""
2+
3+
import ads1015
4+
import RPi.GPIO as GPIO
5+
6+
MICS6812_EN_PIN = 24
7+
8+
9+
ads1015.I2C_ADDRESS_DEFAULT = ads1015.I2C_ADDRESS_ALTERNATE
10+
_is_setup = False
11+
12+
13+
class Mics6812Reading(object):
14+
__slots__ = 'oxidising', 'reducing', 'nh3'
15+
16+
def __init__(self, ox, red, nh3):
17+
self.oxidising = ox
18+
self.reducing = red
19+
self.nh3 = nh3
20+
21+
def __repr__(self):
22+
return """Oxidising: {:05.02f}
23+
Reducing: {:05.02f}
24+
NH3: {:05.02f}
25+
""".format(self.oxidising, self.reducing, self.nh3)
26+
27+
__str__ = __repr__
28+
29+
30+
def setup():
31+
global adc, _is_setup
32+
if _is_setup:
33+
return
34+
_is_setup = True
35+
36+
adc = ads1015.ADS1015(i2c_addr=0x49)
37+
adc.set_mode('single')
38+
adc.set_programmable_gain(6.148)
39+
adc.set_sample_rate(1600)
40+
41+
GPIO.setwarnings(False)
42+
GPIO.setmode(GPIO.BCM)
43+
GPIO.setup(MICS6812_EN_PIN, GPIO.OUT)
44+
GPIO.output(MICS6812_EN_PIN, 1)
45+
46+
47+
def read_all():
48+
ox = adc.get_voltage('in0/gnd')
49+
red = adc.get_voltage('in1/gnd')
50+
nh3 = adc.get_voltage('in2/gnd')
51+
52+
ox = (ox * 56000) / (3.3 - ox)
53+
red = (red * 56000) / (3.3 - red)
54+
nh3 = (nh3 * 56000) / (3.3 - nh3)
55+
56+
return Mics6812Reading(ox, red, nh3)
57+
58+
59+
def read_oxidising():
60+
"""Return gas resistance for oxidising gases.
61+
62+
Eg chlorine, nitrous oxide
63+
"""
64+
setup()
65+
return read_all().oxidising
66+
67+
68+
def read_reducing():
69+
"""Return gas resistance for reducing gases.
70+
71+
Eg hydrogen, carbon monoxide
72+
"""
73+
setup()
74+
return read_all().reducing
75+
76+
77+
def read_nh3():
78+
"""Return gas resistance for nh3/ammonia"""
79+
setup()
80+
return read_all().nh3

library/envirophatplus/noise.py

Whitespace-only changes.

library/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
project_urls={'GitHub': 'https://www.github.com/pimoroni/envirophatplus-python'},
5151
classifiers=classifiers,
5252
packages=['envirophatplus'],
53-
install_requires=['pms5003', 'ltr559', 'st7735']
53+
install_requires=['pimoroni-bme280', 'pms5003', 'ltr559', 'st7735', 'ads1015']
5454
)

library/tests/test_setup.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
import mock
3+
from i2cdevice import MockSMBus
4+
5+
6+
class SMBusFakeDevice(MockSMBus):
7+
def __init__(self, i2c_bus):
8+
MockSMBus.__init__(self, i2c_bus)
9+
self.regs[0x00:0x01] = 0x0f, 0x00
10+
11+
12+
def test_gas_setup():
13+
sys.modules['RPi'] = mock.Mock()
14+
sys.modules['RPi.GPIO'] = mock.Mock()
15+
smbus = mock.Mock()
16+
smbus.SMBus = SMBusFakeDevice
17+
sys.modules['smbus'] = smbus
18+
from envirophatplus import gas
19+
gas.setup()
20+
gas.setup()
21+
22+
23+
def test_gas_read_all():
24+
sys.modules['RPi'] = mock.Mock()
25+
sys.modules['RPi.GPIO'] = mock.Mock()
26+
smbus = mock.Mock()
27+
smbus.SMBus = SMBusFakeDevice
28+
sys.modules['smbus'] = smbus
29+
from envirophatplus import gas
30+
result = gas.read_all()
31+
32+
assert type(result.oxidising) == float
33+
assert int(result.oxidising) == 16641
34+
35+
assert type(result.reducing) == float
36+
assert int(result.reducing) == 16727
37+
38+
assert type(result.nh3) == float
39+
assert int(result.nh3) == 16813
40+
41+
assert "Oxidising" in str(result)
42+
43+
44+
def test_gas_read_each():
45+
sys.modules['RPi'] = mock.Mock()
46+
sys.modules['RPi.GPIO'] = mock.Mock()
47+
smbus = mock.Mock()
48+
smbus.SMBus = SMBusFakeDevice
49+
sys.modules['smbus'] = smbus
50+
from envirophatplus import gas
51+
52+
assert int(gas.read_oxidising()) == 16641
53+
assert int(gas.read_reducing()) == 16727
54+
assert int(gas.read_nh3()) == 16813

0 commit comments

Comments
 (0)