Getting "Offline" after using virtualWrite

Hello guys, my Cayenne is getting offline for about 15 seconds everytime I send a “virtualWrite” command, I wanna know what’s wrong with my code

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <CayenneDefines.h>
#include <CayenneEthernetClient.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxx";
const int buttonPin = 2;
int buttonState = 0;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  Cayenne.run();
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Cayenne.virtualWrite(V0, "1");
  }
}

Welcome to Cayenne!

First thing I notice is you are sending a string and Cayenne doesn’t accept string. You might also be sending data too fast. Try:

  if (buttonState == HIGH) {
    Cayenne.virtualWrite(V0, 1);
    delay(2000);
  }
  else{
    Cayenne.virtualWrite(V0, 0);
  }

Hello, It’s still getting disconnect. And I don’t know why it’s taking sooo long to connect…

void loop()
{
Cayenne.run();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Cayenne.virtualWrite(V0, 1);
delay(2000);
}
else{
Cayenne.virtualWrite(V0, 0);
}
}

That’s the code, right? And when I was using “0” and “1” it was working!!

Edit:

I had to add delay(2000); in else condition. It’s working perfecly now!! Thanks a lotttt

1 Like

Yep, my bad, meant to put it there too… Glad to hear it’s working!