forked from pimoroni/enviroplus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.py
executable file
·69 lines (52 loc) · 1.54 KB
/
gas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import time
import atexit
import ads1015
import RPi.GPIO as GPIO
MICS6814_HEATER_PIN = 24
MICS6814_GAIN = 6.144
ads1015.I2C_ADDRESS_DEFAULT = ads1015.I2C_ADDRESS_ALTERNATE
_is_setup = False
_adc_enabled = False
_adc_gain = 6.148
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(MICS6814_GAIN)
adc.set_sample_rate(1600)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(MICS6814_HEATER_PIN, GPIO.OUT)
GPIO.output(MICS6814_HEATER_PIN, 1)
atexit.register(cleanup)
def enable_adc(value=True):
"""Enable reading from the additional ADC pin."""
global _adc_enabled
_adc_enabled = value
def set_adc_gain(value):
"""Set gain value for the additional ADC pin."""
global _adc_gain
_adc_gain = value
def cleanup():
GPIO.output(MICS6814_HEATER_PIN, 0)
def read_all():
"""Return gas resistence for oxidising, reducing and NH3"""
setup()
CH_OX = adc.get_voltage('in0/gnd') / 3.3
CH_RED = adc.get_voltage('in1/gnd') / 3.3
CH_NH3 = adc.get_voltage('in2/gnd') / 3.3
print 'CO: ', (pow(CH_RED, -1.179) * 4.385)
print 'NO2: ', (pow(CH_OX, 1.007) / 6.855)
print 'NH3: ', (pow(CH_NH3, -1.67) / 1.47)
print 'C3H8: ', (pow(CH_NH3, -2.518) * 570.164)
print 'C4H10: ', (pow(CH_NH3, -2.138) * 398.107)
try:
while True:
read_all()
time.sleep(1.0)
except KeyboardInterrupt:
pass