How to add 2 state display widget?

I am new to Cayenne so please forgive my ignorance. I have googled much and cant find a good tutorial on the display widget.

With my project I want to read a pin (Wemos D1 mini) and know remotely its state.

In my arduino code I have set it as an input and set it low to begin.

Do I need to use the CAYENNE_OUT() command?

All I have been able to successfully do so far is control assorted LED’s using the CAYENNE_IN() command line.

I would also appreciate links to good beginner CAYENNE tutorials. Have not found many good ones.

And, is there a good CAYENNE library cheat sheet?


Are you looking Maybe for Button State ? Then have a Look on My Projekt.
WeMos D1 Fan Control. I read the GPIO of the Fan And Send the state to Cayenne.

You dont need any commands to read pin state, just add new 2 state widget in dashboard to point to digital pin you want to have your eyes on :slight_smile:

Thanks Sven. Yes, I want a read button like your Lüfter button. Have quickly looked at your code but need to spend some more time on it.

But, in trying to add the widget it asks for a channel. What channel do I use If one is not asigned in the code?

Many thanks!

No need for code for monitoring pin state only thing you need is; in the loop section.
Since you will be monitoring Digital pin, select desired Digital pin in dropdown (NOT Virtual) in second drop select pin you want to monitor and thats it.
You will see 2 state monitor added to dashboard.

1 Like

@tett is usinng MQTT connection so he has to add code to show value of two state widget.

void loop() {

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
      x = digitalRead(4);
      Cayenne.virtualWrite(1, x, "digital_sensor", "d");

this will automatically create a two state widget on your dashboard.

1 Like

He did not mention connection type, anyway one of this 2 solutions will work for him. :+1:

Many Thanks! I will get to this later today.

Can you point me to good instructional tutorials or reference materials where I can learn about programming arduinos with cayenne? Can’t find much yet with google.



@tett here is a small tutorial:
on your cayenne dashboard first you need to add a device to connect to cayenne. go to add new > devices/widget >bring your own thing and take note of MQTT username, password and clientid.
then add cayenne MQTT library to you arduino ide . then add this code with your MQTT credential :

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

void setup() {
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

void loop() {

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, lastMillis);
    //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);

upload this code on to your device and this will add value widget automatically to your dashboard.
till now is a basic tutorial to connect to cayenne using MQTT.
next what you want depends on how you code your arduino.

1 Like