MQTT is an exciting technology that’s driving much of the current development of Cayenne and ultimately will be a large part of the future of our platform. At one point Cayenne only had the Cayenne Arduino Library for Arduino connectivity before implementing the Cayenne MQTT Arduino Library for use with our Bring Your Own Thing API.
The purpose of this post is to share a step-by-step way to convert your sketch code from the non-MQTT library to the MQTT one. The intended audience is someone less familiar/comfortable with coding who was attracted to Cayenne for ease of use reasons and might not know where to begin with the MQTT library/API.
Experienced users and developers may find it easier to re-factor their code on their own, but may still find this article useful as a general reference while doing so.
Esp8266 Users: This post applies to you as well if you were connecting your ESP8266 device through our original Arduino option. The only change would be to grab the Cayenne-MQTT-ESP8266 library instead of the Arduino one linked in step #1 below.
Step-by-step conversion
- Install the Cayenne MQTT Arduino library by launching the Arduino IDE, then going to Sketch > Include Library > Manage Libraries… and searching for CayenneMQTT. Click “Install” on the entry that appears.
-
In the Arduino IDE, go to File > Examples > CayenneMQTT > Connections and choose the connection type you were using for your original Arduino sketch. We’ll use this connection sketch as a base for your transition.
-
Carry over any non-Cayenne connectivity related
#include
or#define
statements from the old sketch into the new one. For example, if transitioning from our legacy DS18B20 sketch, you’d bring across:#include <OneWire.h>
#include <DallasTemperature.h>
But not:
#include <CayenneEthernet.h>
(because this is already replaced with
#include <CayenneMQTTEthernet.h>
in the new connection sketch) -
If you have any
#define
virtual pin constants, (things like ‘V1’, ‘V9’), we’ll convert them to numeric MQTT Channels, since the MQTT connectivity does not use the concept of virtual pins.So something like:
#define VIRTUAL_PIN V8
Becomes:
const int VIRTUAL_PIN = 8;
-
Replace this legacy authentication section:
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "AuthenticationToken";
with this MQTT authentication section:
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
-
Carry over any further code that sits outside of the
setup()
andloop()
code blocks. -
In the
setup()
block replace:Cayenne.begin(token);
With:
Cayenne.begin(username, password, clientID);
Then carry over any further code in thesetup()
block. -
In the
loop()
block replace:Cayenne.run();
with:
Cayenne.loop();
Then carry over any further code in the
loop()
block. Add any carried over code into theif
statement that maintains publishing timing in the new example sketch: -
If you have any
CAYENNE_OUT
statements, we’ll convert them to uniquely named stand-alone functions that can be called from theloop()
function.For example, if your sketch has a block like:
CAYENNE_OUT(VIRTUAL_PIN)
{
Your code here
}
Change it to:
void newFunctionName(int VIRTUAL_PIN)
{
Your code here
}
-
The old system read
CAYENNE_OUT
functions automatically as part ofCayenne.run()
, but the new one does not, so we need to actually call anyCAYENNE_OUT
functions we renamed in Step #8. You can do this by adding a single line in the body of theif
statement in theloop()
.Using the function from Step #8 as an example, we’ll call it like this:
newFunctionName(VIRTUAL_PIN);
-
If you have any
Cayenne.syncAll()
orCayenne.syncVirtual()
functions, these are no longer necessary in the context of the MQTT library and can be removed. They will still compile if you leave them, but won’t do anything. -
At this point you should be ready add the device to your Cayenne dashboard. From there, go to Add New > Device/Widget > Bring Your Own Thing. On this page you’ll get the MQTT authentication info (Username, Password, Client ID) that you can use to fill in the section added to your sketch in step #4. You can also (optionally) name your device on this page.
-
Upload your sketch and upon connection with our servers, the website will advance from the ‘Connect your device’ page to a blank dashboard.
With MQTT, any sensor data published from the sketch file will generate temporary widgets automatically, so there is no need to add them through the Add New menu on the Cayenne dashboard as you have done in the past!
To make the green temporary widgets permanent, just click the + icon in the upper-right corner of the widget.
Any actuators will need still need to be created on the Cayenne dashboard through Add New > Device/Widget > Custom Widgets. If the widget is set to an MQTT channel which matches the pin number of the actual device, there may be no need for additional code. For example, if you have a button widget set to MQTT channel 6 and your relay on physical pin 6, this should automatically be handled by the CAYENNE_IN_DEFAULT()
function that is part of the example sketch we used. If you want to use different MQTT channels than pin numbers, or have additional code you’d like to add for a specific actuator, you can address them directly with a function for a specific channel (i.e. CAYENNE_IN(1)
will be called when any actuator set to MQTT channel 1 is pressed on the Cayenne Dashboard).