MQTT subscribe to mac address

I am trying to subscribe my photons to their mac address so I can talk to each one of them univocally (Same code for all of them, that’s the idea).

I have tried the following library: https://github.com/hirotakaster/MQTT And it works. But I just can subscribe to strings (“frame/register”) but any time I tried to put mac address, I can’t (Or I don’t know) how to make the publish on other client.

Have you ever tried this?

This is how I create the subscription:

mac is byte array with mac address inside.
char client_c[6];
client_c[0] = mac[0];
client_c[1] = mac[1];
client_c[2] = mac[2];
client_c[3] = mac[3];
client_c[4] = mac[4];
client_c[5] = mac[5];

client->subscribe(client_c);

What should I use on the other side as topic? I tried printing client_c as string in console but I get weird characters.

Hello,

char client_c[13];

sprintf(client_c, “%02x%02x%02x%02x%02x%02x”, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

You should have only printable ascii characters. This way it should work.

1 Like

Thank you but I have the same problem. I am not able to send a frame to this device. Now I see the mac when I use serial.print. But if I copy&paste that string on mqtt.fx client or my node.js client, the packet is not received on my photon.

Uhm… show us the code, if you can. BTW, I am interested in how you declared the client object in code.

Moreover, what happens if you go “down one level” subscribing to something like “foo/mac_address” and from mqtt.fx you publish to “foo/mac_address” ?

Claudio

I got it working. It seems my node server didn’t restart with the new mac address string. Now it is working and I receive the frame each time I ask for it.

BTW, this is the code I have to make subscription and ask for the frame. I wasn’t sure what happens if I make the subscription each loop so before anything, I unsubscribe to the topic and create a new subscription:

client->unsubscribe(client_c); if(client->isConnected()) { client->subscribe(client_c); } while(i < 10) { if (client->isConnected()) { client->loop(); } else{ client->connect(client_c); delay(100); client->subscribe(client_c); } i++; delay(250); }

1 Like

:+1:

Two things:

  • You need to subscribe again only if you lose the connection to the broker
  • You don’t need to use isConnected() before calling loop(), since loop() itself returns the connection status

Claudio

so my code could be like:

while(i < 10) {
    if (!client->loop()) {
      client->connect(client_c);
      client->subscribe(client_c);
    }
    i++;
    delay(250);
  }

Yep.
Even connect(), subscribe() and unsubscribe() are defined as bool, so you can test if your call succeeded or not.