While at it, I’d also remove the String(...)
constructs and also unify as many publishes into a single event.
e.g.
char msg[128];
...
snprintf(msg, sizeof(msg), "x:%4.1f, y:%4.1f, z:%4.1f", Magx, Magy, Magz);
Particle.publish("Mag", msg);
BTW, I’d also suggest some code improvements
e.g. use arrays to avoid having to write the same block of code multiple times
const int NUMSENSORS = 5;
Adafruit_MLX90393 sensor[NUMSENSORS];
...
void setup() {
...
for (int s=0; s < NUMSENSORS, s++) {
tcaselect(s);
delay(100);
if (!sensor[s].begin_I2C(0x0C)) { // hardware I2C mode, can pass in address & alt Wire
Serial.printlnf("Sensor %d not found ... checked your wiring?", s+1);
}
else{
//Serial.printlnf(" S%d ok", s+1);
}
sensor[s].setGain(MLX90393_GAIN_1X);
sensor[s].setResolution(MLX90393_X, MLX90393_RES_16);
sensor[s].setResolution(MLX90393_Y, MLX90393_RES_16);
sensor[s].setResolution(MLX90393_Z, MLX90393_RES_16);
sensor[s].setOversampling(MLX90393_OSR_0);
sensor[s].setFilter(MLX90393_FILTER_5);
}
}
void loop() {
char msg[128];
float x, y, z;
...
for (int s=0; s < NUMSENSORS, s++) {
tcaselect(s);
if (!sensor[s].begin_I2C(0x0C)) { // hardware I2C mode, can pass in address & alt Wire
Serial.println("Sensor %d not found ... check your wiring!", s+1);
continue; // when sensor isn't found, move on to next one
}
if (sensor[s].readData(&x, &y, &z)) {
snprintf(msg, sizeof(msg), "%d ( %4.1f / %4.1f / 4.1f )", s+1, x, y, z);
Serial.printlnf("OK - Sensor%s", msg);
Particle.publish("Sensor", msg);
delay(1000);
}
else {
Serial.printlnf("Unable to read XYZ data from the sensor %d.", s+1);
}
}
}
You could even get away with a single publish like this (which would save time and data operations)
imu:Vector<3> mag;
sensors_event_t event;
void loop() {
static uint32_t msLastPublish = 0;
char msg[256];
float x, y, z;
if (millis() - msLastPublish < 1000) return; // a better way to keep track of the publishing rate limit
bno.getEvent(&event);
if(!bno.begin()) { // There was a problem detecting the BNO055 ... check your connections
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
snprintf(msg, sizeof(msg), "Mag ( --.- / --.- / --.- )\r\n");
}
else {
Serial.println(" IMU ok");
mag = bno.getVector(Adafruit:BNO055:VECTOR_MAGNETOMETER);
snprintf(msg, sizeof(msg), "Mag (%4.1f / %4.1f / %4.1f )\r\n", max.x, mag.y, mag.z);
}
for (int s=0; s < NUMSENSORS, s++) {
tcaselect(s);
if (!sensor[s].begin_I2C(0x0C)) { // hardware I2C mode, can pass in address & alt Wire
Serial.println("Sensor %d not found ... check your wiring!", s+1);
continue; // when sensor isn't found, move on to next one
}
if (sensor[s].readData(&x, &y, &z)) {
snprintf(msg, sizeof(msg), "S%s ( %4.1f / %4.1f / 4.1f )\r\n", msg, s+1, x, y, z);
Serial.printlnf("OK - Sensor%d ( %4.1f / %4.1f / %4.1f )", s+1, x, y, z);
}
else {
snprintf(msg, sizeof(msg), "S%s ( --.- / --.- / --.- )\r\n", msg, s+1);
Serial.printlnf("Unable to read XYZ data from the sensor %d.", s+1);
}
}
Particle.publish("Sensors", msg);
msLastPublish = millis();
}