def scan_devices(bus: smbus.SMBus):
    connected = []

    for device in range(20, 120):
        try:
            bus.write_quick(device)
            connected.append(device)
        except Exception:
            pass

    return connected
Exemplo n.º 2
0
class Scan(Base):
    def __init__(self):
        self.found = []

    def setup(self):
        self.bus = SMBus(1)
        return self

    def run(self):
        for i in range(1, 127):
            # rez = self.bus.read_byte(i)
            try:
                rez = self.bus.write_quick(i)
                self.found.append(i)
                print "%s -> %s" % (i, rez)
            except IOError:
                pass

    def command(self, line):
        if re.search('^rescan$', line):
            self.run()
            if len(self.found) > 0:
                self.commander.default_address = self.found[0]
            return True
        elif re.search('^list$', line):
            for a in self.found:
                print a
            return True

    def help(self):
        return "rescan # rescan for i2c"
Exemplo n.º 3
0
class Scan(Base):
    def __init__(self):
        self.found = []

    def setup(self):
        self.bus = SMBus(1)
        return self

    def run(self):
        for i in range(1,127):
            # rez = self.bus.read_byte(i)
            try:
                rez = self.bus.write_quick(i)
                self.found.append(i)
                print "%s -> %s" % (i,rez) 
            except IOError:
                pass
    def command(self,line):
        if re.search('^rescan$', line):
            self.run()
            if len(self.found) > 0:
                self.commander.default_address = self.found[0]
            return True
        elif re.search('^list$', line):
            for a in self.found:
                print a
            return True

    def help(self):
        return "rescan # rescan for i2c"
Exemplo n.º 4
0
#!/usr/bin/python3

import time
import smbus

from smbus import SMBus

HIH6130 = SMBus(1)

while(True):
	var = [0, 0, 0, 0]

	HIH6130.write_quick(0x27)
	time.sleep(0.050)
	var = HIH6130.read_i2c_block_data(0x27, 0)
	status =(var[0] & 0xc0) >> 6
	humidity = (((var[0] & 0x3f) << 8) + var[1]) * 100.0 / 16382.0
	tempC = ((var[2] << 6) +((var[3] & 0xfc) >> 2)) * 165.0 / 16382.0 - 40.0
	
	print 'Feuchtigkeit: %.2f RH' % humidity
	print 'Temperatur  : %.2f C' % tempC
	time.sleep(1)
Exemplo n.º 5
0
    try:
        # write block data
        i2c.write_i2c_block_data(addr, index, seq)
    except OSError as e:
        print(e)

    sleep(delay)

    for i in range(index, index + count):
        data[i] = seq[i - index]

exit(0)

try:
    # zero byte write
    i2c.write_quick(addr)
except OSError as e:
    error[0] = e

sleep(delay)

try:
    # single byte read
    read[0] = i2c.read_byte(addr)
except OSError as e:
    error[1] = e

sleep(delay)

try:
    # single byte write
Exemplo n.º 6
0
class WeatherStation:
  def __init__(self, dataFile='/dev/null'):
    self.dataFile = dataFile
    self.fetched = False

    # Initialise the BMP085 and use STANDARD mode (default value)
    # self.bmp = BMP085(0x77, debug=True)
    self.bmp = BMP085(0x77)
    
    # To specify a different operating mode, uncomment one of the following:
    # self.bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
    # self.bmp = BMP085(0x77, 1)  # STANDARD Mode
    # self.bmp = BMP085(0x77, 2)  # HIRES Mode
    # self.bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

    # Initilize HIH-6130.
    bus = GFDITools.guessBus()
    self.HIH6130 = SMBus(bus=bus)

    # Temporary storage array for HIH6130 data
    self.blockData = [0, 0, 0, 0]


  def fetchData(self):
    # Fetch temp & pressure data from BMP
    self.temp = self.bmp.readTemperature()
    self.pressure = self.bmp.readPressure() / 100.0
    # Altitude seems buggy, so we're skipping it for now.
    # altitude = self.bmp.readAltitude()

    # Tell HIH-6130 we want temperature and humidity data.
    self.HIH6130.write_quick(0x27)
    # Wait for it.
    time.sleep(0.050)
    # Read the data we requested.
    blockData = self.HIH6130.read_i2c_block_data(0x27, 0)
    # Process the data.
    self.status = (blockData[0] & 0xc0) >> 6
    self.humidity = (((blockData[0] & 0x3f) << 8) + blockData[1]) * 100.0 / 16383.0
    self.tempC = ((blockData[2] << 6) + ((blockData[3] & 0xfc) >> 2)) * 165.0 / 16383.0 - 40.0
    # tempF = tempC*9.0/5.0 + 32.0

    # Make a note that there is now data to be had.
    self.fetched = True


  def printData(self):
    # print data to screen
    # print "Data:       ", "%02x "*len(d)%tuple(d)
    # print "Status:     ", status
    if self.fetched:
      print "Humidity:   %.2f" % self.humidity, "%RH"
      print "Temperature:    %.2f C" % self.tempC
      print "Barometric Pressure:    %.2f hPa" % self.pressure
      print "Temperature:    %.2f C" % self.temp
    else:
      print "No data has been fetched."
      return -1


  def recordData(self):
    if self.fetched:
      if not os.path.exists(self.dataFile):
        with open(self.dataFile, 'w') as csvfile:
          datawriter = csv.writer(
            csvfile,
            quotechar=',',
            quoting=csv.QUOTE_MINIMAL
          )
          datawriter.writerow([
            'Date',
            'Temp(C)',
            'Pressure(hPa)',
            'Humidity(%RH)'
          ])

      with open(self.dataFile, 'a+') as csvfile:
        datawriter = csv.writer(csvfile, quotechar=',', quoting=csv.QUOTE_MINIMAL)
        datawriter.writerow([datetime.datetime.now(), self.tempC, self.pressure, self.humidity])
    else:
      print "No data has been fetched."
      return -1