Can a Cayenne Button attached to a Virtual Pin be turned ON/OFF using code within the sketch?

In my Arduino project, I have three buttons, all connected with Virtual Pins (V1, V2, and V3). This is all working in the sense that I can turn the buttons on and off from within Cayenne and my code is working.

But I still have one problem. I have not seen a way using code in the Arduino sketch to change the state of any of the Virtual Pins and/or the buttons writing to them. What I need to do is turn off a button that’s currently in the ON state using code.

My initial assumption about V1, V2, and V3 was that they were Boolean values - but that wasn’t correct. No matter what is going on within V1, the value of V1==1. Similarly V2==2 and V3==3. And that’s always true, apparently.

What I need is to find out if it’s possible to have two-way communications between the code in the sketch and the buttons in the Cayenne interface.

Is there code I can write to turn off a button displayed in Cayenne?

Here’s my code:

/*
Cayenne valves
*/

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

int IRR = 0;
int RO = 0;
int LAKE = 0;
int IRR_LED1=2;
int RO_LED2=3;
int LAKE_LED3=4;

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token = β€œxxxxxxxxxx”;

void setup()
{
Serial.begin(9600);
Cayenne.begin(token);

pinMode(IRR_LED1, OUTPUT);
pinMode(RO_LED2, OUTPUT);
pinMode(LAKE_LED3, OUTPUT);

}

// VOID LOOP
void loop() {

Cayenne.run();

}

CAYENNE_IN(V1)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
IRR=1;
LAKE = 0;
digitalWrite(IRR_LED1, HIGH);
digitalWrite(LAKE_LED3, LOW);

//
Serial.print(β€œIRR=”);
Serial.print(IRR);
Serial.print(" LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” V1=β€œ);
Serial.print(V1);
Serial.print(” β€œ);
}
else
{
IRR = 0;
digitalWrite(IRR_LED1, LOW);
Serial.print(β€œIRR=”);
Serial.print(IRR);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” V1=β€œ);
Serial.print(V1);
Serial.print(” ");
}
}

CAYENNE_IN(V2)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
RO=1;
LAKE = 0;
digitalWrite(RO_LED2, HIGH);
digitalWrite(LAKE_LED3, LOW);
Serial.print(β€œRO=”);
Serial.print(RO);
Serial.print(" LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” V2=β€œ);
Serial.print(V2);
Serial.print(” β€œ);
}
else
{
RO = 0;
digitalWrite(RO_LED2, LOW);
Serial.print(β€œRO=”);
Serial.print(RO);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” V2=β€œ);
Serial.print(V2);
Serial.print(” ");
}
}

CAYENNE_IN(V3)
{
int currentValue = getValue.asInt();
if (currentValue == 1){
LAKE=1;
IRR = 0;
RO = 0;
digitalWrite(IRR_LED1, LOW);
digitalWrite(RO_LED2, LOW);
digitalWrite(LAKE_LED3, HIGH);
Serial.print(" LAKE=β€œ);
Serial.print(LAKE);
Serial.print(β€œIRR=”);
Serial.print(IRR);
Serial.print(β€œRO=”);
Serial.print(RO);
Serial.print(” V3=β€œ);
Serial.print(V3);
Serial.print(” β€œ);
}
else
{
LAKE = 0;
digitalWrite(LAKE_LED3, LOW);
Serial.print(β€œLAKE=”);
Serial.print(LAKE);
Serial.print(” IRR=β€œ);
Serial.print(IRR);
Serial.print(” RO=β€œ);
Serial.print(RO);
Serial.print(” V3=β€œ);
Serial.print(V3);
Serial.print(” ");
}
}

You can change the status of the button by:

Cayenne.virtualWrite(V1,HIGH);

or

Cayenne.virtualWrite(V1,LOW);`
1 Like

That did the trick. Thank you!!

1 Like

For reference to hopefully assist anyone else trying to tackle a similar project, here’s my final code (note: you need three Cayenne buttons addressed V1, V2, and V3):

/*
Cayenne valves
*/

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneEthernet.h>

int IRR = 0;
int RO = 0;
int LAKE = 0;
int IRR_LED1=2;
int RO_LED2=3;
int LAKE_LED3=4;
int IRR_RELAY1=6;
int RO_RELAY2=7;
int LAKE_RELAY3=8;

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token = β€œxxxxxxxx”;

void setup()
{
Serial.begin(9600);
Cayenne.begin(token);

pinMode(IRR_LED1, OUTPUT);
pinMode(RO_LED2, OUTPUT);
pinMode(LAKE_LED3, OUTPUT);
pinMode(IRR_RELAY1, OUTPUT);
pinMode(RO_RELAY2, OUTPUT);
pinMode(LAKE_RELAY3, OUTPUT);

}

// VOID LOOP
void loop() {

Cayenne.run();

}

CAYENNE_IN(V1)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
IRR=1;
LAKE = 0;
Cayenne.virtualWrite(V3,LOW);
digitalWrite(IRR_LED1, HIGH);
digitalWrite(LAKE_LED3, LOW);
digitalWrite(IRR_RELAY1, HIGH);
digitalWrite(LAKE_RELAY3, LOW);

//
Serial.print(" IRR=β€œ);
Serial.print(IRR);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.println(” β€œ);
}
else
{
IRR = 0;
digitalWrite(IRR_LED1, LOW);
digitalWrite(IRR_RELAY1, LOW);
Serial.print(” IRR=β€œ);
Serial.print(IRR);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.println(” ");
}
}

CAYENNE_IN(V2)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
RO=1;
LAKE = 0;
Cayenne.virtualWrite(V3,LOW);
digitalWrite(RO_LED2, HIGH);
digitalWrite(LAKE_LED3, LOW);
digitalWrite(RO_RELAY2, HIGH);
digitalWrite(LAKE_RELAY3, LOW);
Serial.print(" RO=β€œ);
Serial.print(RO);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.println(” β€œ);
}
else
{
RO = 0;
digitalWrite(RO_LED2, LOW);
digitalWrite(RO_RELAY2, LOW);
Serial.print(” RO=β€œ);
Serial.print(RO);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.println(” ");
}
}

CAYENNE_IN(V3)
{
int currentValue = getValue.asInt();
if (currentValue == 1){
LAKE=1;
IRR = 0;
RO = 0;
digitalWrite(IRR_LED1, LOW);
digitalWrite(RO_LED2, LOW);
digitalWrite(LAKE_LED3, HIGH);
digitalWrite(IRR_RELAY1, LOW);
digitalWrite(RO_RELAY2, LOW);
digitalWrite(LAKE_RELAY3, HIGH);
Cayenne.virtualWrite(V1,LOW);
Cayenne.virtualWrite(V2,LOW);
Serial.print(" LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” IRR=β€œ);
Serial.print(IRR);
Serial.print(” RO=β€œ);
Serial.print(RO);
Serial.println(” β€œ);
}
else
{
LAKE = 0;
digitalWrite(LAKE_LED3, LOW);
digitalWrite(LAKE_RELAY3, LOW);
Serial.print(” LAKE=β€œ);
Serial.print(LAKE);
Serial.print(” IRR=β€œ);
Serial.print(IRR);
Serial.print(” RO=β€œ);
Serial.print(RO);
Serial.println(” ");
}
}

1 Like