Help with connection MQTT-ARGON

Hello. I need to send in real time sensors value from Argon Console to MQTT… I can’t understand how this connection works with my argon.

I try this code but I don’t understand where I can see the results of the code:

#include <MQTT.h>

void callback(char* topic, byte* payload, unsigned int length);

MQTT client("broker.hivemq.com", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = '\0';

    RGB.control(true);
    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else {
        Serial.printlnf("Unknown message: %s", p);
        RGB.control(false);                         // give up RGB control
    }
}

bool mqttConnect() {                                // one simple function to easily (re)connect at multiple places in code
    client.connect("sparkclient");
    if (client.isConnected()) {
        client.subscribe("outTopic/message");
        return true;
    }
    
    return false;
}

void setup() {
    Particle.function("mqttPub", mqttPub);          // test function (send RED, GREEN, BLUE via console)
    mqttConnect();                                  // initial connection
}

void loop() {
    if (client.isConnected()) 
        client.loop();
    else {                                          // when connection is lost, tell us so and reconnect
        Serial.println("connection lost - reconnect");
        mqttConnect();
    }
}

int mqttPub(const char* msg) {
    if (client.isConnected()) { 
        client.publish("outTopic/message", msg);
        return strlen(msg);                         // success: report length of received message
    }
    
    return -1;                                      // failure: return -1
}

I have download MQTT.fx but I don’t know how to use it. I have also ad account on HiveMQ.
Can you help me? thanks a lot.

When you want to send data from the Argon there is a Particle.function() that will send whatever you pass in to your MQTT broker as outTopic/message.
You can easily send such a function call via console.particle.io/devices (there select your device)

When you want to see whether the Argon receives data sent by the broker you’d need to send a outTopic/message with the content RED, GREEN or BLUE and see the on-board RGB LED change colour accordingly.

If you send anything else you’d need a USB Serial monitor (e.g. particle serial monitor --follow) to observer the output "Unknown message: ....".

I’m sorry I don’t undestard.
I need to send my sensors value from Argon to something in real time to matlab.
I need to update in real time an algorithm written in matlab using the values recorded by the sensors.
I think that one of this is my case: HiveMQ Cloud

This is your intent

And the sample program does exactly that when …

So when you want to know how to write that in your code with your data you need to look in the samlpe code, understand what is done there and then adopt and adapt the illustrated technique for your case :wink:

BTW, “real time” is a strong term and isn’t really the forte of MQTT - especially when using a 3rd party broker.
However, if you want “live” data and are willing to accept the inevitable latency, you can still use MQTT.

Can you help me?
this is the right code??

// This #include statement was automatically added by the Particle IDE.
#include <MQTT-TLS.h>
void callback(char* topic, byte* payload, unsigned int length);

#define LET_ENCRYPT_CA_PEM  /
"-----BEGIN CERTIFICATE-----" /
"<REDACTED>"
"-----END CERTIFICATE----- "

const char letencryptCaPem[] = LET_ENCRYPT_CA_PEM;

/**
* if want to use IP address,
* byte server[] = { XXX,XXX,XXX,XXX };
* MQTT client(server, 1883, callback);
* want to use domain name,
* MQTT client("www.sample.com", 1883, callback);
* mqtt.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started
**/
MQTT client("test.mosquitto.org", 8883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);

if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}

#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned long lastSync = millis();
void setup() {
if (millis() - lastSync > ONE_DAY_MILLIS) {
Particle.syncTime();
lastSync = millis();
}

RGB.control(true);

// enable tls. set Root CA pem file.
// if you don't use TLS, comment out this line.
client.enableTls(letencryptCaPem, sizeof(letencryptCaPem));
Serial.println("tls enable");

// connect to the server
client.connect("sparkclient");

// publish/subscribe
if (client.isConnected()) {
Serial.println("client connected");
client.publish("outTopic/message", "hello world");
client.subscribe("inTopic/message");
}
}

void loop() {
if (client.isConnected())
client.loop();
delay(200);
}

how can I see the result in mosquitto??

I don’t understand how to get the data from argon to matlab.

The code of the sensors is this one: I use a multiplexer TCA, five Hall sensors, one IMU and a RGB.

#include "Particle.h"
#include "TCA9548A-RK.h"
TCA9548A mux(Wire, 0);

#include <math.h>

#include <application.h>
#include <spark_wiring_i2c.h>

//*****************************LED!
#include <Grove_ChainableLED.h>
#define NUM_LEDS 1

//setting the ports dedicated to retrieve data from the sensor
ChainableLED leds (D4, D5, NUM_LEDS);

//initialization to 0 of variables
float hue = 0.0;

//********************BNO055 I2C address is 0x28(40)
#define Addr 0x28
int xAcc = 0, yAcc =  0, zAcc = 0, xGyro = 0, yGyro = 0, zGyro = 0, xMag0 = 0, yMag0 = 0, zMag0 = 0;
int MagInt0 = 0;

// *****************************************MLX90393 I2C Address is 0x0C(12)
#define Addr1 0x0C
int xMag1 = 0, yMag1 = 0, zMag1 = 0;
int MagInt1 = 0;

#define Addr2 0x0C
int xMag2 = 0, yMag2 = 0, zMag2 = 0;
int MagInt2 = 0;

#define Addr3 0x0C
int xMag3 = 0, yMag3 = 0, zMag3 = 0;
int MagInt3 = 0;

#define Addr4 0x0C
int xMag4 = 0, yMag4 = 0, zMag4 = 0;
int MagInt4 = 0;

#define Addr5 0x0C
int xMag5 = 0, yMag5 = 0, zMag5 = 0;
int MagInt5 = 0;

void setup() { //setup totale 
//LED
    leds.init(); // we need to initialize our leds
    Particle.function("turnOn", turnOnLED);
    Serial.begin(9600);
    Serial.println("WE START");

//MULTIPLEXER
    Serial.begin(9600);
	mux.begin();
   

//*************************************************************************************************** 
//setup BNO055
mux.setChannel(0);
// Set variable
  Particle.variable("i2cdevice", "BNO055");
  Particle.variable("xAccl", xAcc);
  Particle.variable("yAccl", yAcc);
  Particle.variable("zAccl", zAcc);
  Particle.variable("xGyro", xGyro);
  Particle.variable("yGyro", yGyro);
  Particle.variable("zGyro", zGyro);
  Particle.variable("xMag", xMag0);
  Particle.variable("yMag", yMag0);
  Particle.variable("zMag", zMag0);

  // Initialise I2C communication as Master
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select OPR_MODE register
  Wire.write(0x3D);
  // Accelerometer, Magnetometer and Gyrometer enabled
  Wire.write(0x07);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select PWR_MODE register
  Wire.write(0x3E);
  // Normal mode
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select PAGE_ID register
  Wire.write(0x07);
  // Shift to Page-1
  Wire.write(0x01);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select ACC_CONFIG register
  Wire.write(0x08);
  // Range = 4G, B/W = 62.5, Normal mode
  Wire.write(0x0D);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select MAG_CONFIG register
  Wire.write(0x09);
  // Data o/p rate = 10 Hz, Regular mode, normal mode
  Wire.write(0x0B);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select GYRO_CONFIG1 register
  Wire.write(0x0A);
  // Range = 2000 dps, B/W  = 32 Hz
  Wire.write(0x38);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select GYRO_CONFIG2 register
  Wire.write(0x0B);
  // Normal mode
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select PAGE_ID register
  Wire.write(0x07);
  // Shift to Page-0
  Wire.write(0x00);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);



//***************************************************************************************************
//setup MLX1
mux.setChannel(1);
  // Set variable MLX1
  Particle.variable("i2cdevice", "MLX90393_1");
  Particle.variable("xMag1", xMag1);
  Particle.variable("yMag1", yMag1);
  Particle.variable("zMag1", zMag1);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr1);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr1, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire.read();
  }

  // Start I2C Transmission{ 
  Wire.beginTransmission(Addr1);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr1, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire1.read();
  }
  delay(300);


//***************************************************************************************************
//setup MLX2
mux.setChannel(2); // posizione del sensore
  // Set variable MLX2
  Particle.variable("i2cdevice", "MLX90393_2");
  Particle.variable("xMag2", xMag2);
  Particle.variable("yMag2", yMag2);
  Particle.variable("zMag2", zMag2);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr2, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int d = Wire.read();
  }

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr2, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int d = Wire.read();
  }
  delay(300);
  
//***************************************************************************************************  
 //setup MLX3
mux.setChannel(3); // posizione del sensore
  // Set variable MLX2
  Particle.variable("i2cdevice", "MLX90393_3");
  Particle.variable("xMag3", xMag3);
  Particle.variable("yMag3", yMag3);
  Particle.variable("zMag3", zMag3);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr3);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr2, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int e = Wire.read();
  }

  // Start I2C Transmission
  Wire.beginTransmission(Addr3);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr3, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int e = Wire.read();
  }
  delay(300);
  
  
//***************************************************************************************************
//setup MLX4
mux.setChannel(4);
  // Set variable MLX1
  Particle.variable("i2cdevice", "MLX90393_4");
  Particle.variable("xMag4", xMag4);
  Particle.variable("yMag4", yMag4);
  Particle.variable("zMag4", zMag4);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr4);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr4, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int f = Wire.read();
  }

  // Start I2C Transmission{ 
  Wire.beginTransmission(Addr4);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr4, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int f = Wire1.read();
  }
  delay(300);
  

//***************************************************************************************************
//setup MLX5
mux.setChannel(5);
  // Set variable MLX1
  Particle.variable("i2cdevice", "MLX90393_5");
  Particle.variable("xMag5", xMag5);
  Particle.variable("yMag5", yMag5);
  Particle.variable("zMag5", zMag5);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr5);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr5, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int g = Wire.read();
  }

  // Start I2C Transmission{ 
  Wire.beginTransmission(Addr5);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr5, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int g = Wire.read();
  }
  delay(300);
}



void loop() { //loop totale

//void loop BNO055
mux.setChannel(0);
unsigned int data[6];

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select Acceleration data register
  Wire.write(0x08);
  // Stop I2C transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

  // Convert the data
  xAcc = ((data[1] * 256) + data[0]);
  if (xAcc > 32767)
  {
    xAcc -= 65536;
  }

  yAcc = ((data[3] * 256) + data[2]);
  if (yAcc > 32767)
  {
    yAcc -= 65536;
  }

  zAcc = ((data[5] * 256) + data[4]);
  if (zAcc > 32767)
  {
    zAcc -= 65536;
  }

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select magnetometer data register
  Wire.write(0x0E);
  // Stop I2C transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // xMag lsb, xMag msb, yMag lsb, yMag msb, zMag lsb, zMag msb
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

  // Convert the data
  xMag0 = ((data[1] * 256) + data[0]);
  if (xMag0 > 32767)
  {
    xMag0 -= 65536;
  }
  yMag0 = ((data[3] * 256) + data[2]);
  if (yMag0 > 32767)
  {
    yMag0 -= 65536;
  }
  zMag0 = ((data[5] * 256) + data[4]);
  if (zMag0 > 32767)
  {
    zMag0 -= 65536;
  }

  // Start I2C transmission
  Wire.beginTransmission(Addr);
  // Select gyrometer data register
  Wire.write(0x14);
  // Stop I2C transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

  // Convert the data
  xGyro= ((data[1] * 256) + data[0]);
 if (xGyro> 32767)
 {
 xGyro -= 65536;
 }
  yGyro = ((data[3] * 256) + data[2]);
  if (yGyro > 32767)
  {
    yGyro -= 65536;
  }
  zGyro = ((data[5] * 256) + data[4]);
  if (zGyro > 32767)
  {
    zGyro -= 65536;
  }

 MagInt0 = sqrt(xMag0*xMag0 + yMag0*yMag0 + zMag0*zMag0); 



//***************************************************************************************************   
//void loop MLX1    
mux.setChannel(1);
  unsigned int data1[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr1);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr1, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr1);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr1, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data1[0] = Wire.read();
    data1[1] = Wire.read();
    data1[2] = Wire.read();
    data1[3] = Wire.read();
    data1[4] = Wire.read();
    data1[5] = Wire.read();
    data1[6] = Wire.read();
  }

  // Convert the data
  xMag1 = data1[1] * 256 + data1[2];
  if (xMag1 > 32767)
  {
    xMag1 -= 65536;
  }

  yMag1 = data1[3] * 256 + data1[4];
  if (yMag1 > 32767)
  {
    yMag1 -= 65536;
  }

  zMag1 = data1[5] * 256 + data1[6];
  if (zMag1 > 32767)
  {
    zMag1 -= 65536;
    
  }
 
  MagInt1 = sqrt(xMag1*xMag1 + yMag1*yMag1 + zMag1*zMag1); 



//***************************************************************************************************
//void loop MLX2
 mux.setChannel(2);
 unsigned int data2[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr2, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int d = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr2, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
    data2[2] = Wire.read();
    data2[3] = Wire.read();
    data2[4] = Wire.read();
    data2[5] = Wire.read();
    data2[6] = Wire.read();
  }

  // Convert the data
  xMag2 = data2[1] * 256 + data2[2];
  if (xMag2 > 32767)
  {
    xMag2 -= 65536;
  }

  yMag2 = data2[3] * 256 + data2[4];
  if (yMag2 > 32767)
  {
    yMag2 -= 65536;
  }

  zMag2 = data2[5] * 256 + data2[6];
  if (zMag2 > 32767)
  {
    zMag2 -= 65536;
    
  }
  
  MagInt2 = sqrt(xMag2*xMag2 + yMag2*yMag2 + zMag2*zMag2); 
  
  
//***************************************************************************************************
//void loop MLX3
 mux.setChannel(3);
 unsigned int data3[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr3, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int e = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr3);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr3, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data3[0] = Wire.read();
    data3[1] = Wire.read();
    data3[2] = Wire.read();
    data3[3] = Wire.read();
    data3[4] = Wire.read();
    data3[5] = Wire.read();
    data3[6] = Wire.read();
  }

  // Convert the data
  xMag3 = data3[1] * 256 + data3[2];
  if (xMag3 > 32767)
  {
    xMag3 -= 65536;
  }

  yMag3 = data3[3] * 256 + data3[4];
  if (yMag3 > 32767)
  {
    yMag3 -= 65536;
  }

  zMag3 = data3[5] * 256 + data3[6];
  if (zMag3 > 32767)
  {
    zMag3 -= 65536;
    
  }
  
  MagInt3 = sqrt(xMag3*xMag3 + yMag3*yMag3 + zMag3*zMag3); 
 
  
//***************************************************************************************************   
//void loop MLX4   
mux.setChannel(4);
  unsigned int data4[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr4);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr4, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int f = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr4);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr4, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data4[0] = Wire.read();
    data4[1] = Wire.read();
    data4[2] = Wire.read();
    data4[3] = Wire.read();
    data4[4] = Wire.read();
    data4[5] = Wire.read();
    data4[6] = Wire.read();
  }

  // Convert the data
  xMag4 = data4[1] * 256 + data4[2];
  if (xMag4 > 32767)
  {
    xMag4 -= 65536;
  }

  yMag4 = data4[3] * 256 + data4[4];
  if (yMag4 > 32767)
  {
    yMag4 -= 65536;
  }

  zMag4 = data4[5] * 256 + data4[6];
  if (zMag4 > 32767)
  {
    zMag4 -= 65536;
    
  }
 
  MagInt4 = sqrt(xMag4*xMag4 + yMag4*yMag4 + zMag4*zMag4); 
  

//***************************************************************************************************   
//void loop MLX5   
mux.setChannel(5);
  unsigned int data5[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr5);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr5, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int g = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr5);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr5, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data5[0] = Wire.read();
    data5[1] = Wire.read();
    data5[2] = Wire.read();
    data5[3] = Wire.read();
    data5[4] = Wire.read();
    data5[5] = Wire.read();
    data5[6] = Wire.read();
  }

  // Convert the data
  xMag5 = data5[1] * 256 + data5[2];
  if (xMag5 > 32767)
  {
    xMag5 -= 65536;
  }

  yMag5 = data5[3] * 256 + data5[4];
  if (yMag5 > 32767)
  {
    yMag5 -= 65536;
  }

  zMag5 = data5[5] * 256 + data5[6];
  if (zMag5 > 32767)
  {
    zMag5 -= 65536;
    
  }
 
  MagInt5 = sqrt(xMag5*xMag5 + yMag5*yMag5 + zMag5*zMag5); 


  
  
//*****************codice RGB
    if ((MagInt0 ||MagInt1 || MagInt2|| MagInt3 || MagInt4 || MagInt5) >= 1000){ 
        hue = 1.0;   //red
        leds.setColorHSB (0, hue, 1.0, 0.5); 
        }
        
     else if ((MagInt0 || MagInt1 || MagInt2 || MagInt3 || MagInt4 || MagInt5) < 1000 || (MagInt1 || MagInt2 || MagInt3 || MagInt4 || MagInt5) >=250){   
        hue = 0.18;  //yellow 
        leds.setColorHSB (0, hue, 1.0, 0.5);
        }
        
     else if ((MagInt0 || MagInt1 || MagInt2 || MagInt3 || MagInt4 || MagInt5) < 250){  
        hue = 0.33; //green
        leds.setColorHSB (0, hue, 1.0, 0.5); 
        }

//Output data to dashboard IMU
 Particle.publish("X-Axis of rotation : ", String(xGyro));
 delay(1000);
  Particle.publish("Y-Axis of rotation : ", String(yGyro));
  delay(1000);
  Particle.publish("Z-Axis of rotation : ", String(zGyro));
  delay(1000);
  Particle.publish("Acceleration in X-Axis : ", String(xAcc));
  delay(1000);
  Particle.publish("Acceleration in Y-Axis : ", String(yAcc));
  delay(1000);
  Particle.publish("Acceleration in Z-Axis : ", String(zAcc));
  delay(1000);
  Particle.publish("Magnetic field in X-Axis IMU: ", String(xMag0));
  delay(1000);
  Particle.publish("Magnetic field in Y-Axis IMU: ", String(yMag0));
  delay(1000);
  Particle.publish("Magnetic field in Z-Axis IMU: ", String(zMag0));
delay(1000);
Particle.publish("Magnetic field Intensity IMU: ", String(MagInt0));
delay(1000);


//Output data to dashboard HALL1
  Particle.publish("Magnetic Field in X-Axis MLX1: ", String(xMag1));
   delay(1000);
  Particle.publish("Magnetic Field in Y-Axis MLX1: ", String(yMag1));
   delay(1000);
  Particle.publish("Magnetic Field in Z-Axis MLX1: ", String(zMag1));
   delay(1000);
  Particle.publish("Magnetic Field Intensity MLX1 is : ", String(MagInt1));
  delay(1000);
 
//Output data to dashboard HALL2
  Particle.publish("Magnetic Field in X-Axis MLX2 : ", String(xMag2));
   delay(1000);
  Particle.publish("Magnetic Field in Y-Axis MLX2 : ", String(yMag2));
   delay(1000);
  Particle.publish("Magnetic Field in Z-Axis MLX2: ", String(zMag2));
   delay(1000);
  Particle.publish("Magnetic Field Intensity MLX2 is : ", String(MagInt2));
  delay(1000);
  
  //Output data to dashboard HALL3
  Particle.publish("Magnetic Field in X-Axis MLX3 : ", String(xMag3));
   delay(1000);
  Particle.publish("Magnetic Field in Y-Axis MLX3 : ", String(yMag3));
   delay(1000);
  Particle.publish("Magnetic Field in Z-Axis MLX3: ", String(zMag3));
   delay(1000);
  Particle.publish("Magnetic Field Intensity MLX3 is : ", String(MagInt3));
  delay(1000);
  
  //Output data to dashboard HALL4
  Particle.publish("Magnetic Field in X-Axis MLX4 : ", String(xMag4));
   delay(1000);
  Particle.publish("Magnetic Field in Y-Axis MLX4 : ", String(yMag4));
   delay(1000);
  Particle.publish("Magnetic Field in Z-Axis MLX4: ", String(zMag4));
   delay(1000);
  Particle.publish("Magnetic Field Intensity MLX4 is : ", String(MagInt4));
  delay(1000);
  
  //Output data to dashboard HALL5
  Particle.publish("Magnetic Field in X-Axis MLX5 : ", String(xMag5));
   delay(1000);
  Particle.publish("Magnetic Field in Y-Axis MLX5 : ", String(yMag5));
   delay(1000);
  Particle.publish("Magnetic Field in Z-Axis MLX5: ", String(zMag5));
   delay(1000);
  Particle.publish("Magnetic Field Intensity MLX5 is : ", String(MagInt5));
  delay(1000);
}

int turnOnLED (String args){ //HSB is a standard for colour. type on google to see how to use!
    hue = args.toFloat();
    leds.setColorHSB(0, hue, 1.0, 0.5); //the value for the angle "hue" is expressed in radiants!
    
    return 1;
}

Some basic hints to make your code readable and maintainable

  1. Honestly consider each and every hint you already got at an earlier time - if you did, I wouldn’t have to reiterate some of the following points as I already pointed them out two weeks ago
  2. I would recommend you avoid using distinct variables xMag1, xMag2, … but rahter learn to use arrays.
  3. when dealing with multiple instances of the same kind of variables xMag, yMag, zMag, … you should consider using a struct.
  4. when using hardware like your sensors, there often are useful libraries that make working with these much simpler than having to deal with raw I2C communication
  5. don’t create tons of Particle.variable() instances. When you have a “compound” set of data, keep that set together in one Particle.variable()
  6. the same goes for Particle.publish() calls - a single string holding all data is much more effective and doesn’t require that many delay() calls
  7. DON’T copy/paste large blocks of code but use functions!
  8. when posting code in a forum try to have a minimal example that focuses on the issue at hand and don’t overload any reader with tons of redundant lines of code, otherwise they won’t actually read that code at all.

And finally to your MQTT question.
If you want to receive data from another MQTT client (i.e. your Argon as data source) your data sink client needs to subscribe to the topic the source publishes.

2 Likes

Thanks a lot, I’m recently using argon and this community .I still have a lot to learn