1 """Read the MICS6814 via an ads1015 ADC"""
6 import RPi
.GPIO
as GPIO
8 MICS6814_HEATER_PIN
= 24
11 ads1015
.I2C_ADDRESS_DEFAULT
= ads1015
.I2C_ADDRESS_ALTERNATE
17 class Mics6814Reading(object):
18 __slots__
= 'oxidising', 'reducing', 'nh3', 'adc'
20 def __init__(self
, ox
, red
, nh3
, adc
=None):
27 fmt
= """Oxidising: {ox:05.02f} Ohms
28 Reducing: {red:05.02f} Ohms
29 NH3: {nh3:05.02f} Ohms"""
30 if self
.adc
is not None:
32 ADC: {adc:05.02f} Volts
49 adc
= ads1015
.ADS1015(i2c_addr
=0x49)
50 adc
.set_mode('single')
51 adc
.set_programmable_gain(MICS6814_GAIN
)
52 adc
.set_sample_rate(1600)
54 GPIO
.setwarnings(False)
55 GPIO
.setmode(GPIO
.BCM
)
56 GPIO
.setup(MICS6814_HEATER_PIN
, GPIO
.OUT
)
57 GPIO
.output(MICS6814_HEATER_PIN
, 1)
58 atexit
.register(cleanup
)
61 def enable_adc(value
=True):
62 """Enable reading from the additional ADC pin."""
67 def set_adc_gain(value
):
68 """Set gain value for the additional ADC pin."""
74 GPIO
.output(MICS6814_HEATER_PIN
, 0)
78 """Return gas resistence for oxidising, reducing and NH3"""
80 ox
= adc
.get_voltage('in0/gnd')
81 red
= adc
.get_voltage('in1/gnd')
82 nh3
= adc
.get_voltage('in2/gnd')
85 ox
= (ox
* 56000) / (3.3 - ox
)
86 except ZeroDivisionError:
90 red
= (red
* 56000) / (3.3 - red
)
91 except ZeroDivisionError:
95 nh3
= (nh3
* 56000) / (3.3 - nh3
)
96 except ZeroDivisionError:
102 if _adc_gain
== MICS6814_GAIN
:
103 analog
= adc
.get_voltage('ref/gnd')
105 adc
.set_programmable_gain(_adc_gain
)
107 analog
= adc
.get_voltage('ref/gnd')
108 adc
.set_programmable_gain(MICS6814_GAIN
)
110 return Mics6814Reading(ox
, red
, nh3
, analog
)
113 def read_oxidising():
114 """Return gas resistance for oxidising gases.
116 Eg chlorine, nitrous oxide
119 return read_all().oxidising
123 """Return gas resistance for reducing gases.
125 Eg hydrogen, carbon monoxide
128 return read_all().reducing
132 """Return gas resistance for nh3/ammonia"""
134 return read_all().nh3