Skip to content

Instantly share code, notes, and snippets.

@jrleeman
Last active September 5, 2015 17:01
Show Gist options
  • Save jrleeman/952a4d678619196ae757 to your computer and use it in GitHub Desktop.
Save jrleeman/952a4d678619196ae757 to your computer and use it in GitHub Desktop.
#include <pca9547.h>
#include <BME280_MOD-1022.h>
#include <Wire.h>
/// constants
const uint8_t NUM_DEVICES = 4;
const int NUM_AVG = 5000;
const int WX_AVG = 5;
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
/// initialize a PCA9547 instance on the default address.
PCA9547 i2c_bus_splitter;
void setup()
{
Serial.begin(9600);
Wire.begin();
// Setup each magnetometer
for (uint8_t i=0; i<NUM_DEVICES; i++)
{
i2c_bus_splitter.setBusChannel(i);
delay(1);
config();
}
i2c_bus_splitter.setBusChannel(NUM_DEVICES);
delay(1);
// Setup BME280
BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms
BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16
BME280.writeOversamplingPressure(os16x); // pressure x16
BME280.writeOversamplingTemperature(os2x); // temperature x2
BME280.writeOversamplingHumidity(os1x); // humidity x1
BME280.writeMode(smNormal);
}
void loop()
{
// Read each magnetometer
for (uint8_t i=0; i<NUM_DEVICES; i++)
{
i2c_bus_splitter.setBusChannel(i);
delay(1);
log_mag(i);
}
// Read the weather parameters
i2c_bus_splitter.setBusChannel(NUM_DEVICES);
delay(1);
log_wx(NUM_DEVICES);
}
void log_wx(int bus_num)
{
float temperature = 0;
float humidity = 0;
float pressure = 0;
for(int i = 0; i<WX_AVG; i++){
BME280.readCompensationParams();
while (BME280.isMeasuring()) {}
BME280.readMeasurements();
temperature += BME280.getTemperatureMostAccurate();
humidity += BME280.getHumidityMostAccurate();
pressure += BME280.getPressureMostAccurate();
}
temperature = temperature/WX_AVG;
humidity = humidity/WX_AVG;
pressure = pressure/WX_AVG;
Serial.print(bus_num+1);
Serial.print(",");
Serial.print(millis());
Serial.print(",");
Serial.print(temperature);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.println(pressure);
}
void log_mag(int bus_num)
{
float mag_x = 0;
float mag_y = 0;
float mag_z = 0;
for (int i=0; i<NUM_AVG; i++)
{
mag_x += read_x();
mag_y += read_y();
mag_z += read_z();
}
mag_x /= NUM_AVG;
mag_y /= NUM_AVG;
mag_z /= NUM_AVG;
Serial.print(bus_num+1);
Serial.print(",");
Serial.print(millis());
Serial.print(",");
Serial.print(mag_x);
Serial.print(",");
Serial.print(mag_y);
Serial.print(",");
Serial.println(mag_z);
}
int read_x(void)
{
int xl, xh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x01); // x MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x02); // x LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xl = Wire.read(); // receive the byte
}
int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
return xout;
}
int read_y(void)
{
int yl, yh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x03); // y MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x04); // y LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yl = Wire.read(); // receive the byte
}
int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
return yout;
}
int read_z(void)
{
int zl, zh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x05); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x06); // z LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zl = Wire.read(); // receive the byte
}
int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
return zout;
}
void config(void)
{
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // cntrl register2
Wire.write(0x80); // send 0x80, enable auto resets
Wire.endTransmission(); // stop transmitting
delay(15);
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // cntrl register1
Wire.write(1); // send 0x01, active mode
Wire.endTransmission(); // stop transmitting
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment