Example #1
0
    def read_values(self):
        """
        читаем данные с контроллера
        """
        if self.state == OFF:
            return

        try:
            values = i2c.read(settings.I2C_READ_BYTES_COUNT)
        except IOError:
            self.set_state(OFF)
            return
        else:
            self.set_state(ON)

        if values[:2] != [97, 116]:
            return

        return [
            # время актуальности
            int(time()),
            # батарея
            values[2] * 256 + values[3],
            # температура
            values[4] * 256 + values[5],
            # фото
            values[6] * 256 + values[7]
        ]
Example #2
0
 def publish(self):
     data = i2c.read(2)
     topic = 'pressure/'+self.name
     data = int.from_bytes(data,byteorder='big', signed=True)
     print(str(topic)+ " : " +str(data) + " : " + self.hostname)
     infot = publish.single(topic, data, qos=2, hostname =  self.hostname)
     
     return infot
Example #3
0
def ADS1115_GetVal():
    i2c.open(ADS1115_ADDRESS)
    i2c.write([0x00])
    (MSB, LSB) = i2c.read(2)
    i2c.close()
    Vel = (MSB << 8) + LSB
    if (Vel > 32767):
        Vel = 0
    return Vel
Example #4
0
def readBmp180Id(addr=DEVICE):
  #Slave Device adress
  i2c.open(addr)

  # Register Address
  REG_ID=0xD0

  #Set address at 0xD0 register
  i2c.write( [REG_ID] )
  ( chip_id, chip_version ) = i2c.read(2)

  i2c.close()
  return (chip_id, chip_version)
Example #5
0
    def ReadBlock(self, address, key, value):
        """Reads a block from olimex mod-io.

        Args:
            key: interger, an address where to read data from.
            length: integer, how much data to read.
        """
        try:
            i2c.open(address)
            i2c.write(key)
            value = i2c.read(value)
            i2c.close()
            return value
        except IOError:
            raise DeviceNotFoundException("Could not communicate with device")
Example #6
0
def ReadADC(Address, channel, ref):

    config = 0b1000000111000011
    config |= (channel << 12)
    config |= (ref << 9)

    lectura = 0
    aux = 0x80

    configH = (config >> 8)
    configL = config & 0x00FF

    i2c.init("/dev/i2c-0")

    i2c.open(Address)  #Open ADC I2C
    i2c.write([0x01, configH, configL])  #Write to Adddress 1 (config register)
    i2c.close()

    while (lectura != aux):

        i2c.open(Address)
        high = i2c.read(2)
        i2c.close()

        lectura = high[0] & aux

    i2c.open(Address)
    i2c.write([0x00])
    i2c.close()

    i2c.open(Address)
    read = i2c.read(2)
    i2c.close()

    value = (read[0] << 8) + read[1]
    return value
Example #7
0
    def ReadBlock(self, address, key, value):
        """Reads a block from olimex mod-io.

        Args:
            key: interger, an address where to read data from.
            length: integer, how much data to read.
        """
        try:
            i2c.open(address)
            i2c.write(key)
            value = i2c.read(value)
            i2c.close()
            return value
        except IOError:
            raise DeviceNotFoundException("Could not communicate with device")
Example #8
0
def readBmp180(addr=DEVICE):
  #Slave Device adress
  i2c.open(addr)

  # Register Addresses
  REG_CALIB  = 0xAA
  REG_MEAS   = 0xF4
  REG_MSB    = 0xF6
  REG_LSB    = 0xF7

  # Control Register Address
  CRV_TEMP   = 0x2E
  CRV_PRES   = 0x34

  # Oversample setting
  OVERSAMPLE = 0   # 0 - 3

  # Read calibration data
  # Read calibration data from EEPROM
  i2c.write([REG_CALIB])
  cal = i2c.read(22)

  # Convert byte data to word values
  AC1 = getShort(cal, 0)
  AC2 = getShort(cal, 2)
  AC3 = getShort(cal, 4)
  AC4 = getUshort(cal, 6)
  AC5 = getUshort(cal, 8)
  AC6 = getUshort(cal, 10)
  B1  = getShort(cal, 12)
  B2  = getShort(cal, 14)
  MB  = getShort(cal, 16)
  MC  = getShort(cal, 18)
  MD  = getShort(cal, 20)

  print("AC1:",AC1)
  print("AC2:",AC2)
  print("AC3:",AC3)
  print("AC4:",AC4)
  print("AC5:",AC5)
  print("AC6:",AC6)
  print("B1:",B1)
  print("B2:",B2)
  print("MB:",MB)
  print("MC:",MC)
  print("MD",MD)

  # Read temperature
  i2c.write( [REG_MEAS, CRV_TEMP] )
  time.sleep(0.015)

  i2c.write([0xF6])
  ( msb, lsb ) = i2c.read( 2 )

  UT = (msb << 8) + lsb
  print("msb,lsb",msb,lsb)
  print("UT",UT)

  # Read pressure
  i2c.write([REG_MEAS, CRV_PRES + (OVERSAMPLE << 6)])
  time.sleep(0.14)
  i2c.write([REG_MSB])
  (msb, lsb, xsb) = i2c.read(3)
  i2c.close()
  print("msb,lsb,xsb",msb,lsb,xsb)
  UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)
  print("UP",UP)

  # Refine temperature
  X1 = ((UT - AC6) * AC5) >> 15
  X2 = (MC << 11) / (X1 + MD)
  B5 = X1 + X2
  temperature = ( B5 + 8 ) >> 4

  # Refine pressure
  B6  = B5 - 4000
  B62 = B6 * B6 >> 12
  X1  = (B2 * B62) >> 11
  X2  = AC2 * B6 >> 11
  X3  = X1 + X2
  B3  = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2

  X1 = AC3 * B6 >> 13
  X2 = (B1 * B62) >> 16
  X3 = ((X1 + X2) + 2) >> 2
  B4 = (AC4 * (X3 + 32768)) >> 15
  B7 = (UP - B3) * (50000 >> OVERSAMPLE)

  P = (B7 * 2) / B4

  X1 = ( P >> 8 ) * ( P >> 8 )
  X1 = ( X1 * 3038 ) >> 16
  X2 = ( -7357 * P ) >> 16
  pressure = P + ( (X1 + X2 + 3791) >> 4 )

  return ( temperature / 10.0, pressure/ 100.0 )
Example #9
0
 def read_byte_data(self, adr):
     i2c.open(self.address)
     i2c.write([adr])
     res = i2c.read(1)[0]
     i2c.close()
     return res
Example #10
0
i2c.write([0x00])

print "Dump eeprom:"
print "="*24

print "    ",
for i in xrange(16):
    print " %x" % i,

print "\t",
for i in xrange(16):
    print "%x" % i,
print ""

"""Print data"""
for i in xrange(128):
    page = i2c.read(16)
    print "%03x:" % (i*0x10),
    for j in xrange(0, 16):
        print "%02x" % page[j],

    """Print characters"""
    print "\t",
    for j in xrange(16):
        if page[j] <= 126 and page[j] >= 32:
            print chr(page[j]),
        else:
            print '.',
    print ""

i2c.close()
Example #11
0
  # Control Register Address
  CRV_TEMP   = 0x2E
  CRV_PRES   = 0x34

  # Oversample setting
  OVERSAMPLE = 0   # 0 - 3

  # Read calibration data
  # Read calibration data from EEPROM
<<<<<<< HEAD
 
  i2c.open(addr) #Slave Device adress
  time.sleep(0.05)

  i2c.write([REG_CALIB])
  cal = i2c.read(22)
  time.sleep(0.05)

  #cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
=======
  i2c.write([REG_CALIB])
  cal = i2c.read(22)
>>>>>>> 2066dfbee270812bf435b6e815ff896003e6ccf1

  # Convert byte data to word values
  AC1 = getShort(cal, 0)
  AC2 = getShort(cal, 2)
  AC3 = getShort(cal, 4)
  AC4 = getUshort(cal, 6)
  AC5 = getUshort(cal, 8)
  AC6 = getUshort(cal, 10)
i2c.open(eeprom_address)
"""Set address pointer to the first"""
i2c.write([0x00])

print "Dump eeprom:"
print "=" * 24

print "    ",
for i in xrange(16):
    print " %x" % i,

print "\t",
for i in xrange(16):
    print "%x" % i,
print ""
"""Print data"""
for i in xrange(128):
    page = i2c.read(16)
    print "%03x:" % (i * 0x10),
    for j in xrange(0, 16):
        print "%02x" % page[j],
    """Print characters"""
    print "\t",
    for j in xrange(16):
        if page[j] <= 126 and page[j] >= 32:
            print chr(page[j]),
        else:
            print '.',
    print ""

i2c.close()
Example #13
0
#!/usr/bin/env python

from pyA20 import i2c

i2c.init("/dev/i2c-0") #Initialize module to use /dev/i2c-0
res = i2c.open(0x20) #The slave device address is 0x2A


i2c.write([0x21]) #Set address at 0x21 ADCL register
value = i2c.read(1) #Read 1 byte
valueL=[hex(x) for x in value]

i2c.write([0x20]) #Set address at 0x20 ADCH register
value = i2c.read(1) #Read 1 
valueH=[int(x) for x in value]

total=int(valueL[0],16)+(valueH[0]<<8)
voltage=(total+3.737611)/76.250833
percentage=round((100*(voltage-10.4))/2.2)
percentage=100 if percentage>100 else percentage
percentage=0 if percentage<0 else percentage
print str(percentage)


i2c.close() #End communication with slave device