Switch button as pulse generator

@klaus.313 @vapor83 @bestes @rsiegel @ognqn.chikov @adam @kreggly what about this approcah for a push button on cayenne dashboard (MQTT).
Add a custom button widget with virtual pin 1.

// 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_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int x;
// 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() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 20000) {
    lastMillis = millis();
    if (x == 1)
    {
      Cayenne.virtualWrite(2, 1, "digital_sensor", "d");
      Serial.println("digital_sensor is 1");
    }
    else
    {
      Cayenne.virtualWrite(2, 0, "digital_sensor", "d");
      Serial.println("digital_sensor is 0");
    }
  }
}
CAYENNE_IN(1)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
    x = 1;
    Serial.println("x is 1");
  }
  else
  {
    x = 0;
    Serial.println("x is 0");
  }
}

next this will add a two state widget on dashboard.
add a trigger when the two state widget becomes 1 make digital output 0.
let me know about this approach as it works fine for me.

1 Like