Adding a simple limit switch

CAYENNE_IN(6)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
//do whatever you want when you turn on the button on cayenne dashboard
  digitalWrite(2,HIGH);
 }
  else
  {
//do whatever you want when you turn off the button on cayenne dashboard  
 digitalWrite(2,LOW);
  }
}

Shramik,
Thanks for sticking with me through this and sorry I keep coming back with
questions but i added the code below but it still seems like its not
responding when I toggle it on in the web interface. I can see it doing
something in the serial monitor but its not lighting the LED. Do my pins
look like they are defined proper and assigned right? D4 goes to my push
button and D3 goes to my LED. The button works perfect but I cant seem to
get my hands on the output.
Thanks a million!
Sam

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int inputPin = 2;
int x;
// WiFi network info.
char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char wifiPassword[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

// Cayenne authentication info. This should be obtained from the Cayenne
Dashboard.
char username[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char password[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char clientID[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

unsigned long lastMillis = 0;

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

void loop() {
  Cayenne.loop();
  x = digitalRead(inputPin);
  //Publish data every 10 seconds (10000 milliseconds). Change this value
to publish at a different interval.
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    Cayenne.virtualWrite(1, x, "digital_sensor", "d");

    //Write data to Cayenne here. This example just sends the current
uptime in milliseconds.
    //Cayenne.virtualWrite(0, lastMillis);

  }
}

//Default function for processing actuator commands from the Cayenne
Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for
channel 1 commands.
CAYENNE_IN(6)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
//do whatever you want when you turn on the button on cayenne dashboard
  digitalWrite(3,HIGH);
 }
  else
  {
//do whatever you want when you turn off the button on cayenne dashboard
 digitalWrite(3,LOW);
  }
}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel,
getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using
getValue.setError(), e.g getValue.setError("Error message");
}

In your setup function you need:

pinMode(3, OUTPUT);
1 Like

@scamilleri1228 as @adam said you need to set the pin as input or output.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int inputPin = 2;
int outputPin = 3;
int x;
// WiFi network info.
char ssid[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char wifiPassword[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

// Cayenne authentication info. This should be obtained from the Cayenne
Dashboard.
char username[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char password[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
char clientID[] = "XXXXXXXXXXXXXXXXXXXXXXXXXX";

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
}

void loop() {
  Cayenne.loop();
  x = digitalRead(inputPin);
  //Publish data every 10 seconds (10000 milliseconds). Change this value
to publish at a different interval.
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    Cayenne.virtualWrite(1, x, "digital_sensor", "d");

    //Write data to Cayenne here. This example just sends the current
uptime in milliseconds.
    //Cayenne.virtualWrite(0, lastMillis);

  }
}

//Default function for processing actuator commands from the Cayenne
Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for
channel 1 commands.
CAYENNE_IN(6)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
//do whatever you want when you turn on the button on cayenne dashboard
  digitalWrite(outputPin,HIGH);
 }
  else
  {
//do whatever you want when you turn off the button on cayenne dashboard
 digitalWrite(outputPin,LOW);
  }
}

Thats it!!! Thanks guys that was a huge help!

great to hear that its working as you want. if need help let us know and when done with your project, submit it to cayenne community. :slightly_smiling_face:

I’m hoping this is my last question…
I have added another output channel for controlling a relay(GPIO4). When I
cycle the power the relay energizes on startup and need to be turned off
using cayenne, how do I make it so it is powered off at by default? I don’t
want what I am controlling to start if there is a power blip, and I also
want it to fail to open if there is a power blip.
Thanks,
Sam

Oh the code, sorry.
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int inputPin = 2;

int x;
// WiFi network info.
char ssid = “XXXXXXXXXXXXXXXXXXXXX”;
char wifiPassword = “XXXXXXXXXXXXXXXXXXXXX”;

// Cayenne authentication info. This should be obtained from the Cayenne
Dashboard.
char username = “XXXXXXXXXXXXXXXXXXXXX”;
char password = “XXXXXXXXXXXXXXXXXXXXX”;
char clientID = “XXXXXXXXXXXXXXXXXXXXX”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
}

void loop() {
Cayenne.loop();
x = digitalRead(inputPin);
//Publish data every 10 seconds (10000 milliseconds). Change this value
to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Cayenne.virtualWrite(1, x, “digital_sensor”, “d”);

//Write data to Cayenne here. This example just sends the current

uptime in milliseconds.
//Cayenne.virtualWrite(0, lastMillis);

}
}

//Default function for processing actuator commands from the Cayenne
Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for
channel 1 commands.
CAYENNE_IN(6)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
//do whatever you want when you turn on the button on cayenne dashboard
digitalWrite(0,HIGH);
}
else
{
//do whatever you want when you turn off the button on cayenne dashboard
digitalWrite(0,LOW);
}
}
CAYENNE_IN(7)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
//do whatever you want when you turn on the button on cayenne dashboard
digitalWrite(4,LOW);
}
else
{
//do whatever you want when you turn off the button on cayenne dashboard
digitalWrite(4,HIGH);
}
}
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel,
getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using
getValue.setError(), e.g getValue.setError(“Error message”);
}

@scamilleri1228 its very simple. add this to your void setup().

void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
//if pin 4 is coonected to relay.
digitalWrite(4, LOW);
}

Thank you Shramik.

This is what I have now and its still closing the relay on startup

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
}

is it that when you have the button pressed the relay is off and when released the relay is on?
or you can interchange the NO and NC pins on your relay module.

So what appears is happening is when I power cycle the board with my wifi
off the relay does not close. However the minute i enable my wifi it
defaults to close. Anythoughts?

can you interchange NO NC pins on your relay module.

I want this thing to (NC) pass power if its off line, powered down or on
startup. If I wire it the other way if its powered down or fails it will
open the circuit.

try this

CAYENNE_IN(7)
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
//do whatever you want when you turn on the button on cayenne dashboard
digitalWrite(4,HIGH);
}
else
{
//do whatever you want when you turn off the button on cayenne dashboard
digitalWrite(4,LOW);
}

Still doing it but now the icons are reversed

Anyone have ideas?

@scamilleri1228 i will try the code on my nodemcu and relay module and will get back to you with a solution. give me some time. till then @rsiegel and @ognqn.chikov can help.

Thanks