Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #NOTE:
- # This sensor is on port 0x04, so not compatible with grovepi unless you load an alternate firmware
- # This is work in progress, would need logic analyzer and arduino to get working
- # Error:
- # Traceback (most recent call last):
- # File "multichannel_gas_sensor.py", line 67, in <module>
- # m= MutichannelGasSensor()
- # File "multichannel_gas_sensor.py", line 21, in __init__
- # if self.readR0() >= 0:
- # File "multichannel_gas_sensor.py", line 27, in readR0
- # rtnData = self.readData(0x11)
- # File "multichannel_gas_sensor.py", line 52, in readData
- # buffer=bus.read_i2c_block_data(self.address, cmd, 4)
- #IOError: [Errno 5] Input/output error
- #
- # LINKS
- # http://www.seeedstudio.com/wiki/Grove_-_Multichannel_Gas_Sensor
- # https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor
- import time,sys
- from Adafruit_I2C import Adafruit_I2C
- import smbus
- # use the bus that matches your raspi version
- #rev = GPIO.RPI_REVISION
- # if True:
- # bus = smbus.SMBus(1)
- # else:
- # bus = smbus.SMBus(0)
- class MutichannelGasSensor:
- address = None
- is_connected = 0
- res=[0]*3
- def __init__(self, mode=1, address=0x04, i2c=None, **kwargs):
- i2c = Adafruit_I2C(0x04, busnum=2, debug=True)
- bus = smbus.SMBus(2)
- self._device = i2c
- self.address=0x04
- self.adcValueR0_NH3_Buf = 0;
- self.adcValueR0_CO_Buf = 0;
- self.adcValueR0_NO2_Buf = 0;
- print "Led on"
- #dta_test = [11, 1]
- #self._device.writeList(address, dta_test);
- bus.write_word_data(address, 11, 1)
- bus.write_word_data(address, 10, 1)
- #return
- #BEGIN
- version = self.get_addr_dta(6, 0)
- print version
- #POWER ON
- dta_test = [11,1]
- self._device.writeList(self.address, dta_test)
- time.sleep(1)
- #self.calcGas()
- def calcGas(self):
- # how to calc ratio/123
- #ledON
- dta_test = [10, 1]
- self._device.writeList(self.address, dta_test)
- time.sleep(1)
- print "CalcGas"
- #A0_0 = self.get_addr_dta(6, 8);
- time.sleep(1)
- #A0_1 = self.get_addr_dta(6, 10);
- time.sleep(1)
- A0_2 = self.get_addr_dta(6, 12);
- #print "A0_0: " + str(A0_0);
- #print "A0_1: " + str(A0_1);
- print "A0_2: " + str(A0_2);
- # An_0 = get_addr_dta(CH_VALUE_NH3);
- # An_1 = get_addr_dta(CH_VALUE_CO);
- # An_2 = get_addr_dta(CH_VALUE_NO2);
- # ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
- # ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
- # ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
- def get_addr_dta(self, addr_reg, __dta):
- self._device.write8(0x04, addr_reg)
- time.sleep(1)
- self._device.write8(0x04, __dta)
- time.sleep(1)
- testArray = self._device.readList(self.address, 2)
- dta=0
- raw=[None] * 10
- cnt = 0
- if raw[0] == 0:
- print "ERRORE GET ADDR DATA"
- return
- dta = testArray[0]
- dta <<= 8
- dta += testArray[1]
- if addr_reg == 8: #CH_VALUE_NH3
- self.adcValueR0_NH3_Buf = dta;
- elif addr_reg == 10: #CH_VALUE_CO
- self.adcValueR0_CO_Buf = dta;
- elif addr_reg == 12: #CH_VALUE_NO2
- self.adcValueR0_NO2_Buf = dta;
- print dta
- return dta
- def sendI2C(self,cmd):
- bus.write_byte(self.address, cmd)
- if __name__ == "__main__":
- m = MutichannelGasSensor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement