Momentary Relay Button Widget changes to value on its own


#1

I have a 4 channel relay that opens/closes two garage doors and a gate. I’m using 4 virtual pins, one for the on board LED and three to control relay switches. Two issues have been happening over the past few weeks, but I’m just now getting around to troubleshooting. I keep wanting to expand this to work with voice commands, but for now this has been ‘good enough’ and real simple to use.

  1. The button used to turn itself off flawlessly. The relay still turns itself off, but the button widget remains on until I click it again. From on to off.
  2. Sometimes the button widget changes to a value widget and I have to go back in and fix it back to button. I typically use the android app to trigger these.

Here’s the code I’m using, any tips?

#include <ESP8266WiFi.h>
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxx";
// Your network name and password.
char ssid[] = "wifi";
char password[] = "xxx";



// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis;
const long interval = 5000;    
void setup()
    {
      // digitalWrite(5, HIGH); // GPIO
      pinMode(16, OUTPUT);       // Pin connected to the Relay
      pinMode(5, OUTPUT);
      pinMode(D2, OUTPUT);
      pinMode(D3, OUTPUT);         
      digitalWrite(16, HIGH); 
      digitalWrite(5, HIGH); 
      digitalWrite(D2, HIGH); 
      digitalWrite(D3, HIGH); 
      Serial.begin(9600);
      Cayenne.begin(token, ssid, password);
    }

    // ==============( Void Loop  ) ====================================

    void loop()
    {
      Cayenne.run();
    }
    // Garage Left
    CAYENNE_IN(V0)
    {
      // get value sent from dashboard
      int currentValue = getValue.asInt(); // 0 to 1

      // assuming you wire your relay as normally open
      if (currentValue == 0) {
        digitalWrite(D0, HIGH);
      } else {
        digitalWrite(D0, LOW);
        delay (1000);
        digitalWrite(D0, HIGH);
        Serial.println("Garage Left Toggled");
        }
    }
    // Garage Right
    CAYENNE_IN(V1)
    {
      // get value sent from dashboard
      int currentValue = getValue.asInt(); // 0 to 1

      // assuming you wire your relay as normally open
      if (currentValue == 0) {
        digitalWrite(D1, HIGH);
      } else {
        digitalWrite(D1, LOW);
        delay (1000);
        digitalWrite(D1, HIGH);
        Serial.println("Garage Right Toggled");    
        }
    }
    // Gate
    CAYENNE_IN(V2)
    {
      // get value sent from dashboard
      int currentValue = getValue.asInt(); // 0 to 1

      // assuming you wire your relay as normally open
      if (currentValue == 0) {
        digitalWrite(D2, HIGH);
      } else {
        digitalWrite(D2, LOW);
        delay (1000);
        digitalWrite(D2, HIGH);
        Serial.println("Gate Toggled");    
        }
    }
    // Extra channel
    CAYENNE_IN(V3)
    {
      // get value sent from dashboard
      int currentValue = getValue.asInt(); // 0 to 1

      // assuming you wire your relay as normally open
      if (currentValue == 0) {
        digitalWrite(D3, HIGH);
      } else {
        digitalWrite(D3, LOW);
        delay (1000);
        digitalWrite(D3, HIGH);
        }
    }

Thanks for reading and offering any tips. I’m a total newbie to this.


#2

Personally I would control the state of the widget from the Arduino. Try this:

Cheers,

Craig

//http://playground.arduino.cc/Code/Timer#Usage for details on the timer library

#include <timer.h>

//initialize this global at the top
int garage_left_state = 0;

//setup timer in setup function

//add t.update(); to loop


//gets called when button pressed
CAYENNE_IN(V0)
{
    garage_left_state = !garage_left_state; //toggle garage state

    if(garage_left_state == 1)
    {
         t.pulse(D0, 1000, HIGH); // toggle D0 for a second
         t.pulse(V0, 1000, HIGH); // toggle V0 for a second, don't know if this will work or not.
    }
}

//gets called when Cayenne polls 
CAYENNE_OUT(V0)
{
    Cayenne.write(V0,garage_left_state);
}