Disconnect when using "void newFunctionName(int VIRTUAL_PIN)"

uno + esp8266

when using “void newFunctionName(int VIRTUAL_PIN)” to send data, my board connects and disconnects and no widget will appear on the dashboard. But when I use “CAYENNE_OUT_DEFAULT” or “CAYENNE_OUT()” data is sent continuously.

I’m not sure what you mean “void newFunctionName(int VIRTUAL_PIN)”. Is that a function you wrote or did you maybe mean the “virtualWrite” function? Also, are you trying to send data just once, not multiple times at intervals like CAYENNE_OUT does?

Can you provide the code you are using? That might be helpful to diagnose the problem.

I tried testing it using this code.

#include <CayenneMQTTESP8266Shield.h>
char ssid = “”;
char wifiPassword = “”;
char username = “”;
char password = “”;
char clientID = “”;
const int VIRTUAL_PIN = 1;

#define EspSerial Serial
ESP8266 wifi(&EspSerial);

void setup()
{
delay(10);
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
Send(VIRTUAL_PIN);
}

void Send(int VIRTUAL_PIN)
{
Cayenne.virtualWrite(0, millis());
}

The issue there is that the Send function is being called in the loop() which is run over and over again meaning it may be sending thousands of values to Cayenne a second. Since we currently only allow 60 messages per minute this can cause your device to be disconnected or blocked due to sending too many messages.

The way around that is to only send the messages at intervals either using code like this example that sends values using a timer: Cayenne-MQTT-Arduino/SendDataOnTimer.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub.

Or you could send it every 10 seconds using millis() like this:

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if(millis() - lastMillis > 10000) {
		lastMillis = millis();
		Cayenne.virtualWrite(0, lastMillis);
	}
}

Or using the CAYENNE_OUT functions which are called at intervals automatically.

Also, just a heads up. You are defining VIRTUAL_PIN as 1, but using virtual channel 0 when sending data to Cayenne using virtualWrite. That will work, but I’m not sure if that is intentional.

2 Likes