Request position limit switches

On my gate I have limit switches for open and close position. For the moment the nodemcu is send data all the time. To reduce this, it should be nice when only the position is send at the moment I open the app.
Is it possible to send a request to the nodemcu to send the position at the moment I open the app ?

not possible. But you can add a button on the dashboard to request for the state. Or send data only when there is a state change, like this:

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
	unsigned long currentMillis = millis();
	// Check sensor data every 250 milliseconds
	if (currentMillis - previousMillis >= 250) {
		// Check the sensor state and send data when it changes.
		currentState = digitalRead(SENSOR_PIN);
		if (currentState != previousState) {
			Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, "digital_sensor", "d");
			previousState = currentState;
		}
		previousMillis = currentMillis;
	}
}

Many thanks.
After everything is installed and is working fine. I will publisch the project. What is the best place for it ?


shramik_salgaonkar Community Manager
April 29

guyc:
Is it possible to send a request to the nodemcu to send the position at the moment I open the app ?

not possible. But you can add a button on the dashboard to request for the state. Or send data only when there is a state change, like this:

int previousState = -1;int currentState = -1;unsigned long previousMillis = 0;void checkSensor(){	unsigned long currentMillis = millis();	// Check sensor data every 250 milliseconds	if (currentMillis - previousMillis >= 250) {		// Check the sensor state and send data when it changes.		currentState = digitalRead(SENSOR_PIN);		if (currentState != previousState) {			Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, "digital_sensor", "d");			previousState = currentState;		}		previousMillis = currentMillis;	}}

you can create a new project here https://community.mydevices.com/c/project-with-cayenne/