#include <Wire.h>

// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire

void i2cmux(uint8_t bus){
  Wire.beginTransmission(0x70);  // TCA9548A address
  Wire.write(1 << bus);          // send byte to select bus
  Wire.endTransmission();
  //Serial1.print(bus);
}

void setup() {
  WIRE.begin();

  Serial1.begin(115200);
  while (!Serial1)
     delay(10);
  Serial1.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial1.println("Scanning...");

  for(int i=0;i<8;i++){
    i2cmux(i);
    nDevices = 0;
    for(address = 1; address < 127; address++ )
    {
      // The i2c_scanner uses the return value of
      // the Write.endTransmisstion to see if
      // a device did acknowledge to the address.
      WIRE.beginTransmission(address);
      error = WIRE.endTransmission();

      if (error == 0)
      {
        if(address!=0x70){
          Serial1.print(i);
          Serial1.print(":");
          Serial1.print("I2C device found at address 0x");
          if (address<16)
            Serial1.print("0");
          Serial1.print(address,HEX);
          Serial1.println("  !");
        }

        nDevices++;
      }
      else if (error==4)
      {
        Serial1.print("Unknown error at address 0x");
        if (address<16)
          Serial1.print("0");
        Serial1.println(address,HEX);
      }
    }

  }
  if (nDevices == 0)
    Serial1.println("No I2C devices found\n");
  else
    Serial1.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}