Start gas and tests
[EVA-2020-02-2.git] / library / envirophatplus / gas.py
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