0

Change Client ID in Adafruit_MQTT?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Change Client ID in Adafruit_MQTT?

by partytime_excellent on Wed Jan 11, 2017 1:58 pm

I'm using the Adafruit_MQTT library to interface some ESP8266 sensors to my home automation platform. When I look at the console logs from my MQTT broker, all the connections coming from my ESP8266 modules have client IDs that look like: "65d3552e-27d2-4c2f-8fgf-e7w1000454as"

Is it possible to give them a shorter, more useful name? If so, where do I need to tinker?

partytime_excellent
 
Posts: 4
Joined: Wed Jan 11, 2017 1:53 pm

Re: Change Client ID in Adafruit_MQTT?

by jwcooper on Wed Jan 11, 2017 2:03 pm

You should be able to set the client id as shown here:
https://github.com/adafruit/Adafruit_MQ ... TT.cpp#L99

The 'cid' is the client id.

jwcooper
 
Posts: 666
Joined: Tue May 01, 2012 9:08 pm

Re: Change Client ID in Adafruit_MQTT?

by partytime_excellent on Wed Jan 11, 2017 2:17 pm

Thanks for that. I'm really new to Arduino so this is a very basic question: If I change the Adafruit_MQTT.cpp file to include a custom client_id name, would that then populate the same name to all sketches I use the library for?

Is it possible to define the CID as a parameter in the sketch, similar to how I enter my wifi ssid and password?

Code: Select all | TOGGLE FULL SIZE
#define AIO_SERVER      "192.168.1.xxx"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "user"
#define AIO_KEY         "pass"


could I add

Code: Select all | TOGGLE FULL SIZE
#define AIO_CID    "custom_client_id"


Thanks

partytime_excellent
 
Posts: 4
Joined: Wed Jan 11, 2017 1:53 pm

Re: Change Client ID in Adafruit_MQTT?

by uniontownlabs on Wed Jan 11, 2017 2:26 pm

yep, you would define that in your sketch, and then you'd pass that to the Adafruit_MQTT constructor:

Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_CID, AIO_USERNAME, AIO_KEY);


keep in mind that client IDs need to be unique for each device.

uniontownlabs
 
Posts: 112
Joined: Wed Dec 17, 2014 10:56 pm

Re: Change Client ID in Adafruit_MQTT?

by partytime_excellent on Wed Jan 11, 2017 2:32 pm

@uniontownlabs: Thank you, that did the trick!

partytime_excellent
 
Posts: 4
Joined: Wed Jan 11, 2017 1:53 pm

Re: Change Client ID in Adafruit_MQTT?

by Lo on Thu Feb 28, 2019 3:46 pm

I also want to add a device ID because I use more then one ESP8266 module.
But setting a device ID is not clear for me yet.
I use code like written in config.h as used in many of your example sketches :

Code: Select all | TOGGLE FULL SIZE
// Adafruit account :
#define IO_USERNAME    "xx"
#define IO_KEY         "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

/********** Home WIFI ********************************************/
#define WIFI_SSID       "Mywifi"
#define WIFI_PASS       "MyWiFipw"

#include "AdafruitIO_WiFi.h"
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

Can I extend this last line of code with some device ID ?
Or do I have to use the mentioned Adafruit_MQTT constructor with AIO_CID ? Or both ?
Unfortunately I cannot find this in the Arduino documentation....
It took me a long time to find this in a forum post. Why is'nt it documented ?

Hope you can help me out,
kind regards, Lo

Lo
 
Posts: 11
Joined: Fri Oct 26, 2018 11:32 am

Re: Change Client ID in Adafruit_MQTT?

by brubell on Fri Mar 01, 2019 11:56 am

Lo wrote:I also want to add a device ID because I use more then one ESP8266 module.
But setting a device ID is not clear for me yet.
I use code like written in config.h as used in many of your example sketches :

Code: Select all | TOGGLE FULL SIZE
// Adafruit account :
#define IO_USERNAME    "xx"
#define IO_KEY         "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

/********** Home WIFI ********************************************/
#define WIFI_SSID       "Mywifi"
#define WIFI_PASS       "MyWiFipw"

#include "AdafruitIO_WiFi.h"
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

Can I extend this last line of code with some device ID ?
Or do I have to use the mentioned Adafruit_MQTT constructor with AIO_CID ? Or both ?
Unfortunately I cannot find this in the Arduino documentation....
It took me a long time to find this in a forum post. Why is'nt it documented ?

Hope you can help me out,
kind regards, Lo


Are you using the Adafruit IO Arduino or Adafruit MQTT library?

brubell
 
Posts: 282
Joined: Fri Jul 17, 2015 10:33 pm

Re: Change Client ID in Adafruit_MQTT?

by Lo on Fri Mar 01, 2019 12:34 pm

brubell wrote:Are you using the Adafruit IO Arduino or Adafruit MQTT library?

Thank you for your fast answer.
I have both libraries installed. My application is much like your adafruitio_06_digital_in example

Lo
 
Posts: 11
Joined: Fri Oct 26, 2018 11:32 am

Re: Change Client ID in Adafruit_MQTT?

by brubell on Fri Mar 01, 2019 5:16 pm

Lo wrote:
brubell wrote:Are you using the Adafruit IO Arduino or Adafruit MQTT library?

Thank you for your fast answer.
I have both libraries installed. My application is much like your adafruitio_06_digital_in example


While you can't pass the client id into IO like the Adafruit MQTT Client example - you could include it as part of the metadata bundled with a data point sent from a node.

For example, the code for your first node would look like the following:
Code: Select all | TOGGLE FULL SIZE
 
// declare a nodeID
int nodeID = 1;
...
feedName ->save(value, lat=nodeID);

src - https://github.com/adafruit/Adafruit_IO ... on.ino#L85

On the Adafruit IO Web Dashboard for that feed, feedName, you'll be able to see the data along with the "node id" (where the data was sent from).


Note that this is not an ideal way of identifying nodes, but it's a start and will work as expected. I'll ask about adding a possible additional metadata parameter for identifying which device the data is coming from.

brubell
 
Posts: 282
Joined: Fri Jul 17, 2015 10:33 pm

Re: Change Client ID in Adafruit_MQTT?

by Lo on Fri Mar 01, 2019 6:03 pm

Thank you for this idea.
I think it is a pity that we can't embed a client ID in
Code: Select all | TOGGLE FULL SIZE
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
in a way like AOI_CID in the Adafruit_MQTT constructor
Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_CID, AIO_USERNAME, AIO_KEY);

I would like to have this feature because on my monitor page of Adafruit IO I see various spontaneous dsiconnections and connections with (probably random?) generated ID numbers. So it is hard to find out which thing is disconnecting and connecting again. ( ......and I hope they are clients that I know ! )
Adding the possibility of a client name would be very usefull and I hope there will come a solution for this in the Adafruit Arduino lib.

Thanks and I hope Adafruits keeps doing this nice work !

Lo
 
Posts: 11
Joined: Fri Oct 26, 2018 11:32 am

Please be positive and constructive with your questions and comments.