Connecting ESP8266 with Cayenne using MQTT - Video

This is a video we made in collaboration with Acrobotic. It’s a great getting started video for using an ESP8266 with Cayenne over MQTT communication protocol. Enjoy!


very nice video. it worked smoothly and clear

Very good!

hey i am trying to use my own mqtt broker and cayenne to work at the same time in parallel with each other. The reason being if my internet is down i would still be able to use mosquitto running on the Pi and vice versa if my Pi server is down then Cayenne would also be able to control. I made some quick adjustments and have had 90% success. It works great but only when connected to mosquitto. If it is not connected it goes into a boot error and resets any help would be appreciated. see code below nodemcu esp826612e

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

char token[] = "XXXXXX";
#define MQTT_SERVER ""
char ssid[] = "XXXXXX";
char password[] = "XXXXXXX";
//LED on ESP8266 GPIO2
const int lightPin = 4;

char* lightTopic = "/test/light2";

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup() {
   Cayenne.begin(token, ssid, password);
  //initialize the light as an output and set to LOW (off)
  pinMode(lightPin, OUTPUT);
  digitalWrite(lightPin, LOW);

  //start the serial line for debugging

  //start wifi subsystem
  //WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server

  //wait a bit before starting the main loop

void loop() {;
  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {

  //maintain MQTT connection

  //MUST delay to allow ESP8266 WIFI functions to run

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

  //convert topic to string to make it easier to work with
  String topicStr = topic;

  //Print out some debugging info
  Serial.println("Callback update.");
  Serial.print("Topic: ");

  //turn the light on if the payload is '1' and publish to the MQTT server a confirmation message
  if (payload[0] == '1') {
    digitalWrite(lightPin, HIGH);
    client.publish("/test/confirm", "Light On");


  //turn the light off if the payload is '0' and publish to the MQTT server a confirmation message
  else if (payload[0] == '0') {
    digitalWrite(lightPin, LOW);
    client.publish("/test/confirm", "Light Off");


void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if (WiFi.status() != WL_CONNECTED) {
    //debug printing
    Serial.print("Connecting to ");

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {

    //print out some more debug once connected
    Serial.println("WiFi connected");
    Serial.println("IP address: ");

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if (WiFi.status() == WL_CONNECTED) {
    // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      // Generate client name based on MAC address and last 8 bits of microsecond counter
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      clientName += macToStr(mac);

      //if connected, subscribe to the topic(s) we want to be notified about
      if (client.connect((char*) clientName.c_str())) {
        Serial.print("\tMTQQ Connected");

      //otherwise print failed for debugging
      else {

//generate unique name from MAC addr
String macToStr(const uint8_t* mac) {

  String result;

  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);

    if (i < 5) {
      result += ':';

  return result;

i just also realised that that the original code with out including Cayenne causes a reset. I just never noticed it because the server was always available to connect to or it eventually connected like it was supposed to do.