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:

Step 4 : 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);

Wemos D1 mini & Wemos DHT shield
Looking for a simple MQTT example with Multiple relays/lights
NodeMCU esp8266 + BMP280/BME280
Arduino Uno - ESP8266 Connection problem
Unable to add ESP8266
Esp8266-12e MQTT sketch wont compile
My Wifi-Shield wont connenct to the Internet?
LinkNode D1 Setup
I need help and am new to Arduino
Cayenne is not working, nothing working
[SOLVED] ESP8266 MQTT API compile errors
How to run cayenne continously with heart pulse in my code?
Online Heart Rate Monitor Using NodeMCU and Cayenne
Webhook And IFTTT
HELP ME esp8266 and read analog
Como incluir outros atuadores no nodemcu
Code that connecting the nodemcu with arduino mega
How do I link my MPU6050 to cayenne?
ESP8266 Getting Started Feedback
Current Sensor
Simple web instructions to switch relay on and off
Can arduino uno connect with the node mcu 12E?
Unable to add PWM output
Float switch
New System MQTT
My Wifi-Shield wont connenct to the Internet?
Can someone help me convert this blynk code to cayenne?
With Arduino SLIDER do not work
Flow Meter help with reading data
Sparkfun ESP8266 Thing Dev Button
Help with ESP8266 compilation warning
I need help!

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



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

~ ZolAnd


follow this steps :


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?


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.


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.