Advertisement
Guest User

Untitled

a guest
May 7th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #NOTE:
  3. # This sensor is on port 0x04, so not compatible with grovepi unless you load an alternate firmware
  4. # This is work in progress, would need logic analyzer and arduino to get working
  5. # Error:
  6. #   Traceback (most recent call last):
  7. #  File "multichannel_gas_sensor.py", line 67, in <module>
  8. #    m= MutichannelGasSensor()
  9. #  File "multichannel_gas_sensor.py", line 21, in __init__
  10. #    if self.readR0() >= 0:
  11. #  File "multichannel_gas_sensor.py", line 27, in readR0
  12. #    rtnData = self.readData(0x11)
  13. #  File "multichannel_gas_sensor.py", line 52, in readData
  14. #    buffer=bus.read_i2c_block_data(self.address, cmd, 4)
  15. #IOError: [Errno 5] Input/output error
  16. #
  17. # LINKS
  18. # http://www.seeedstudio.com/wiki/Grove_-_Multichannel_Gas_Sensor
  19. # https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor
  20. import time,sys
  21. from Adafruit_I2C import Adafruit_I2C
  22.  
  23. import smbus
  24.  
  25. # use the bus that matches your raspi version
  26. #rev = GPIO.RPI_REVISION
  27. # if True:
  28. #     bus = smbus.SMBus(1)
  29. # else:
  30. #     bus = smbus.SMBus(0)
  31.  
  32. class MutichannelGasSensor:
  33.     address = None
  34.     is_connected = 0
  35.     res=[0]*3
  36.  
  37.     def __init__(self, mode=1, address=0x04, i2c=None, **kwargs):
  38.         i2c = Adafruit_I2C(0x04, busnum=2, debug=True)
  39.         bus = smbus.SMBus(2)
  40.  
  41.         self._device = i2c
  42.         self.address=0x04
  43.         self.adcValueR0_NH3_Buf = 0;
  44.         self.adcValueR0_CO_Buf = 0;
  45.         self.adcValueR0_NO2_Buf = 0;
  46.  
  47.  
  48.         print "Led on"
  49.         #dta_test = [11, 1]
  50.         #self._device.writeList(address, dta_test);
  51.         bus.write_word_data(address, 11, 1)
  52.         bus.write_word_data(address, 10, 1)
  53.  
  54.     #return
  55.  
  56.         #BEGIN
  57.         version = self.get_addr_dta(6, 0)
  58.         print version
  59.  
  60.         #POWER ON
  61.         dta_test = [11,1]
  62.         self._device.writeList(self.address, dta_test)
  63.         time.sleep(1)
  64.         #self.calcGas()
  65.  
  66.        
  67.     def calcGas(self):
  68.         # how to calc ratio/123
  69.  
  70.         #ledON
  71.         dta_test = [10, 1]
  72.         self._device.writeList(self.address, dta_test)
  73.         time.sleep(1)
  74.  
  75.         print "CalcGas"
  76.         #A0_0 = self.get_addr_dta(6, 8);
  77.         time.sleep(1)
  78.         #A0_1 = self.get_addr_dta(6, 10);
  79.         time.sleep(1)
  80.         A0_2 = self.get_addr_dta(6, 12);
  81.  
  82.         #print "A0_0: " + str(A0_0);
  83.         #print "A0_1: " + str(A0_1);
  84.         print "A0_2: " + str(A0_2);
  85.        
  86.         # An_0 = get_addr_dta(CH_VALUE_NH3);
  87.         # An_1 = get_addr_dta(CH_VALUE_CO);
  88.         # An_2 = get_addr_dta(CH_VALUE_NO2);
  89.        
  90.         # ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
  91.         # ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
  92.         # ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
  93.  
  94.  
  95.     def get_addr_dta(self, addr_reg, __dta):
  96.         self._device.write8(0x04, addr_reg)  
  97.         time.sleep(1)
  98.         self._device.write8(0x04, __dta)
  99.         time.sleep(1)
  100.  
  101.         testArray = self._device.readList(self.address, 2)
  102.  
  103.         dta=0
  104.         raw=[None] * 10
  105.         cnt = 0
  106.  
  107.         if raw[0] == 0:
  108.             print "ERRORE GET ADDR DATA"
  109.             return
  110.         dta = testArray[0]
  111.         dta <<= 8
  112.         dta += testArray[1]
  113.  
  114.         if addr_reg == 8: #CH_VALUE_NH3
  115.             self.adcValueR0_NH3_Buf = dta;
  116.         elif addr_reg == 10: #CH_VALUE_CO
  117.             self.adcValueR0_CO_Buf = dta;
  118.         elif addr_reg == 12: #CH_VALUE_NO2
  119.             self.adcValueR0_NO2_Buf = dta;
  120.  
  121.         print dta
  122.         return dta
  123.    
  124.     def sendI2C(self,cmd):
  125.         bus.write_byte(self.address, cmd)
  126.  
  127.  
  128. if __name__ == "__main__"
  129.     m = MutichannelGasSensor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement