Adding a New Device using MQTT

This tutorial will help you add a new device like wemos and nodemcu to cayenne using MQTT.

Step 1 : Installing the ESP8266 Arduino Addon.

  • Start Arduino and open Preferences window.
  • Enter into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and find esp8266 platform.
  • Select the version you need from a drop-down box.
  • Click install button.

Step 2 : Installing the cayenne MQTT library.

  • on arduino ide go to sketch > include library > manage libraries.
  • search for cayenne mqtt and install both the libraries


Step 3 : Adding device on cayenne dashboard.

Step 4 : Uploading code on your wemos or nodemcu device.

  • .On your arduino ide go to File > examples > cayenne-MQTT-esp8266 > open Esp8266.


  • copy your MQTT credentials and add your wifi network info into the code.

  • select the the appropriate board from the board manager and the port it is connected.

  • upload the code into the device.

  • Open the serial monitor to check the status.

  • check your cayenne dashboard .

Step 4 : Adding a Sensor.

  • In MQTT you dont have to add a sensor widget from your dashboard.

  • the dashboard is directly populated with widget when you publish/send data from the code.

  • in your example code have a look at this part:

      if (millis() - lastMillis > 10000) {
      	lastMillis = millis();
    // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
    Cayenne.virtualWrite(0, millis());
    // Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    //Cayenne.luxWrite(2, 700);
    //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);


  • this publish/sends data to your dashboard to cayenne dashboard and creates the sensor widget.

  • here are the different data types: Cayenne Features - Developer |

Step 5 : Adding a Actuator.

  • to add a button or a slider widget in BYOT.
  • go to add new > devices/widget > custom widget > button or slider.

  • fill in all the details and add the channel number and click add widget.

  • once you have added the custom button to your dashboard you must be able to read the status of it.

  • there are two ways to do it.


    CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %u",, getValue.getId(), 
        switch ( {
     case 1 :
       int x = getValue.asInt();
  2. Using CAYENNE_IN.

       int x = getValue.asInt();

once you are able to read the status the of the button, then you can use this value to turn on a light, relay etc using

     digitalWrite(relay_pin, x);

This is great thank you for making this @shramik_salgaonkar ! I will definitely be referencing this tutorial and linking people here.


1 Like

Thank you for detailed description using ESP8266! And what about ESP32?

~ ZolAnd

follow this steps : Cayenne-MQTT-Arduino/ESP32.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub

Hello, thanks for your help. But where do I write that code? or where can I read that values?
I have tried to write the sketch that appears when put an actuator on the dasboard but when I upload the code using the arduino IDE the another relay connected and working correctly, stop working or my arduino connection get lost. All of this is new for me, I understand very well the electronic part (conection and all that) but the programming part is difficult to me and I don´t understand to much how can I still adding actuators without losing the code that has already loaded the Arduino and lose the connection.
with each relay added Do I have to upload the code in the arduino board?

1 Like

are you able to connect to cayenne?
you dont need to generate code on dashboard. the code from example which you used to connect to cayenne needs to be modified and uploaded.
for reading actuator widget (channel 1) value, you need to add this code below in the example code.

   int x = getValue.asInt();

Yes I am able to connect to cayenne. But the first time it´s hard I guess.
I am a mexican and sometimes it´s hard to me to express my ideas in english. I think I couldn´t express correctly my last message.
I am going to still try.
Thank you for all your comments, they were very helpful.
I hope it works with the 3 actuators I need.

Thank you! :slight_smile:

Sorry me again.
I have added one relay actuator, adding from the sketch that appears add new → device/widget → actoators → relay
I filled all details and clicked “step 1: sketch file”, then copy and paste on the IDE and uploaded it.
Then clickedc"step 2: add actuator" and it appeared and works correctly. But now, I want to add another relay, but I already followed the steps as they come in the cayenne documents, I already followed the steps you mentioned above, I keep doing tests and modifying the code and I can´t get the new relay to work.
Is there another way to add relays?

you have to add another widget with channel 2 and read its value.

   int y = getValue.asInt();

But @ Adam I couldn’t find this :neutral_face:

As I said in your other thread, delete all your Cayenne libraries and install only the ESP library.

1 Like

do as @adam told. look at step 2 in the tutorial.

yes @adam , it worked !!. Now learning how to add actuators.
One thing that I couldn’t figure is that How many channels are there in ESP8266 nodeMCU ? and what are there corresponding names/numbers ?

Also by the tutorial , I was able to light up inbuilt LED(channel 0, as written) . For attaching more outputs , do I have to Upload modified code each time, (I have used cayenne very easily in my rpi where it was just to add via web ).

channels are not same as pins on Nodemcu. MQTT channels are used to send/receive data between the device(eg Nodemcu) and cayenne.

you can use either

   #define led_pin D4 
    #define led_pin 2

both are same.
have a look at the pin mapping in the picture below.

yes, you have to add new code for each output you want to control using cayenne.

1 Like

Open the serial monitor to check the status.
after I do that, the montitor series doesn’t show up like the next step, can you help my problem?

what exactly are you referring to. can you provide more detail?

I am trying to send data from Wemos D1 R1 microcontroller according to the tutorial above, by using the sample-cayenemqtt-esp8266 method and have entered SSID, password, usermqtt, passmqtt, client id according to the steps in the tutorial, but when I entered I uploaded what happened was, the serial monitor on my arduino was blank and there was no writing at all. then I try to click the restart button on wemos d1 r1, the result is writing connecting to baras. then I re-check my cayenne device, and the result is the device on my cayenne is still in connect to board. and haven’t succeeded in creating each channel on the sensor. please give directions, sir

try this:- create a new device on cayenne dashboard- add new ---> devices/widget ----> microcontroller ---> arduino ---> next and you will get the screen showing MQTT credentials Copy the MQTT credentials into the code and upload.

Actually my problem is that the wemos d1 r1 that I use can’t yet display the cayenne dashboard (the display is still display for step 2 to enter the mqqtt usename, mqtt password, client id, ssid, and wifi password) even though I’ve already entered the usename mqqtt, mqtt password, client ID, SSID, and WiFi password correctly. following how it looks.

did you follow this steps???