Newbie needs help

Hi there. some help to solve my problem would be apreciated. i am quite new at cayenne. On arduino solo everything works fine. but now i want to have on/off button for my project (Alarm on lasertrap and pirsensor). to arm/disarm per click on the app. at the moment i am realy confused and changed many things but it doesnt work.

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP32.h>
const int buttonPin =23;
const int photoResistor = 22; // Photoresistor at Arduino analog pin A0
const int pirPin = 21;
const int buzzerPin = 19;
const int ledPin = 18;       
const int flashPin = 15;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char ssid[] = "xxx";
char pass[]="xxxxxx";

char mqtt_username[] ="xxx";
char mqtt_password[] = "xxx";
char client_id[] = "xxx";

#define VIRTUAL_CHANNEL 1

#define ACTUATOR_PIN 4 

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN, OUTPUT);
  Cayenne.begin(mqtt_username, mqtt_password, client_id, ssid, pass);
   pinMode(buttonPin, INPUT);
   pinMode(pirPin, INPUT); 
   pinMode(buzzerPin, OUTPUT);
   pinMode(ledPin, OUTPUT);  
   pinMode(photoResistor, INPUT);
   pinMode(flashPin, OUTPUT);
}

void loop()
{
  Cayenne.loop();

  int pirDetected = digitalRead(pirPin);
  int detected = digitalRead(photoResistor);
  if ((detected == LOW) || (pirDetected == HIGH))//Abfrage Laser oder PIR Signal
    {
      if (detected == LOW){Serial.println("unerlaubtes Betreten - Lichtschranke wurde unterbrochen!");};
      if (pirDetected == HIGH){Serial.println("Einbruchsversuch - Annäherungssensor wurde aktiviert");};
      for (int i=0;i<=10;i++)     //wie oft blinkenUndPiepesen
      {
        
        digitalWrite(flashPin, HIGH);
        digitalWrite(ledPin, HIGH);  //Turn led on
        digitalWrite(buzzerPin, LOW);
        delay(150);//wie lange pause bis signalwechsel
        digitalWrite(ledPin, LOW);
        digitalWrite(buzzerPin, LOW);
        
        i=i+1;
        delay(10); //wie lange pause bis nächster durchlauf
      }
   } 
   else
   { 
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    digitalWrite(flashPin, LOW);   
   }   
   delay(50);
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN, value);

//if (ACTUATOR_PIN == HIGH)
  //{
    
 }
//else{
//      digitalWrite(ledPin, LOW);
//      digitalWrite(buzzerPin, LOW);
//      digitalWrite(flashPin, LOW);  
// }
// 
//  }

  


// This function is called at intervals to send data to Cayenne and keep the device online.
// Will create a temporary green widget on Channel 0, make it permanent by clicking on '+'. 
CAYENNE_OUT(0)
{
  CAYENNE_LOG("Send data for Virtual Channel 0");
  // This command writes the device's uptime in seconds to the Virtual Channel. 
  Cayenne.virtualWrite(0, millis() / 1000);
}>

what do you get in the serial monitor when you press the button on the cayenne dashboard?

Hi. thanks for reply.
led for on/off works. but not for my code.
Timestamp

Unit

Values

2020-09-04 5:11:35

Nr2

1

Pin

c0e83580-eebb-11ea-a67f-15e30d90bbf4

1

can you try this code:-
Assuming you have added the button on channel 1 and led is connected to digital pin 4.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP32.h>

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
#define VIRTUAL_CHANNEL 1

#define ACTUATOR_PIN 4 

void setup() {
  Serial.begin(9600);
    pinMode(ACTUATOR_PIN, OUTPUT);

  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

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

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// 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(VIRTUAL_CHANNEL)
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  int value = getValue.asInt();
  digitalWrite(ACTUATOR_PIN, value);
}

Hi. thank u for your support.
my serial monitor:
18:37:38.615 -> [5291025] Channel 1, pin 4, value 0
18:37:39.967 -> [5292357] Channel 1, pin 4, value 1
18:37:44.602 -> [5296999] Send data for Virtual Channel 0
18:38:00.361 -> [5312751] Send data for Virtual Channel 0
18:38:16.115 -> [5328503] Send data for Virtual Channel 0
18:38:27.664 -> Einbruchsversuch - Annäherungssensor wurde aktiviert
18:38:29.648 -> Einbruchsversuch - Annäherungssensor wurde aktiviert
18:38:31.665 -> [5344075] Send data for Virtual Channel 0
18:38:33.796 -> unerlaubtes Betreten - Lichtschranke wurde unterbrochen!
18:38:44.478 -> [5356869] Channel 1, pin 4, value 0
18:38:47.331 -> [5359741] Send data for Virtual Channel 0
18:38:54.704 -> unerlaubtes Betreten - Lichtschranke wurde unterbrochen!
18:38:57.748 -> Einbruchsversuch - Annäherungssensor wurde aktiviert
18:38:59.745 -> Einbruchsversuch - Annäherungssensor wurde aktiviert
18:39:02.839 -> [5375223] Send data for Virtual Channel 0
.
where to put the action in the code when channel 1 is on/off? i dont get it…:frowning:
channel 0 is only a chekup - no need

what do you mean by this? does the code I gave turn ON/OFF the led?

Sorry. Its not about the led tis is wirkung… My goal: the Connecticut between led and Main Funktion my alarmsystem. if i turn led on via cayenne( Chanel 1 Status 1) my alarmsystem is armed and Wirkung. If i shut off the lde (Status 0) the System is disarmed.

still i did not understand.

sorry that i can not explain better…here a piece of the code…
still not works…
<void loop()
{
Cayenne.loop();
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.//when value is 1 run the action //function, if value is 0 skip the action function
digitalWrite(ACTUATOR_PIN, value);

if (value ==1)
{ action();}
}
// i want to skip this if the value (sent from cayenne channel 1 pin4 is 0
// how can i skip the function action() ? this is for shut of the alarmsystem
void action(){
int pirDetected = digitalRead(pirPin);
int detected = digitalRead(photoResistor);
if ((detected == LOW) || (pirDetected == HIGH))//Abfrage Laser oder PIR Signal
{>

thanks again for your interest and support

[quote=“werthebach, post:9, topic:15199”]
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.//when value is 1 run the action //function, if value is 0 skip the action function
digitalWrite(ACTUATOR_PIN, value);

if (value ==1)
{ action();}
}
[/qunrote]

the action function should only run value is 1 and ignore when 0

Thanks bro. the code compiles without error but it still not works. i got no reaction from laser or pir. i am so sad…
/*Franks AlarmSystem Lasertrap for unallowed entry to the garden. PIR for enemy nearby.

  • Buzzer for Alarmsound, Stroboskobflashlight. For Testing Led. on the planfor later camera for pictures via mobile
    */
    <
    #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
    #include <CayenneMQTTESP32.h>
    const int buttonPin =23;
    const int photoResistor = 22; // Photoresistor at Arduino analog pin A0
    const int pirPin = 21;
    const int buzzerPin = 19;
    const int ledPin = 18;
    const int flashPin = 15;
    // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
    char ssid = “Fluffy”;
    char pass=“Fr@nk&Nel@”;

char mqtt_username =“be7ba900-ba1b-11ea-b767-3f1a8f1211ba”;
char mqtt_password = “c01dbdb7c1c55971d01528bf2bfd347bf4416e6d”;
char client_id = “43c393c0-eeba-11ea-93bf-d33a96695544”;

#define VIRTUAL_CHANNEL 1

#define ACTUATOR_PIN 4

void setup()
{

Serial.begin(9600);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(mqtt_username, mqtt_password, client_id, ssid, pass);
pinMode(buttonPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(photoResistor, INPUT);
pinMode(flashPin, OUTPUT);
//laser is dircet 5v and ground ( no pin)
}

void loop()
{
Cayenne.loop();
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.//when value is 1 run the action //function, if value is 0 skip the action function
digitalWrite(ACTUATOR_PIN, value);

if (value ==1)
{ action();}
}
void action(){
int pirDetected = digitalRead(pirPin);
int detected = digitalRead(photoResistor);
if ((detected == LOW) || (pirDetected == HIGH))//Abfrage Laser oder PIR Signal
{
if (detected == LOW){Serial.println(“unerlaubtes Betreten - Lichtschranke wurde unterbrochen!”);};
if (pirDetected == HIGH){Serial.println(“Einbruchsversuch - Annäherungssensor wurde aktiviert”);};
for (int i=0;i<=10;i++) //wie oft blinkenUndPiepesen
{
digitalWrite(flashPin, HIGH);
digitalWrite(ledPin, HIGH); //Turn led on
digitalWrite(buzzerPin, HIGH);
delay(150);//wie lange pause bis signalwechsel
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);

    i=i+1;
    delay(10); //wie lange pause bis nächster durchlauf
  }

}
else
{ //this is when alarmsystem is offline. i am there so no need for alarm. when i leave i put cayenne led ON to arm the system
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(flashPin, LOW);
digitalWrite(photoResistor, LOW);
}
delay(50);
}

// This function is called at intervals to send data to Cayenne and keep the device online.
// Will create a temporary green widget on Channel 0, make it permanent by clicking on ‘+’.
CAYENNE_OUT(0)
{
CAYENNE_LOG(“Send data for Virtual Channel 0”);
// This command writes the device’s uptime in seconds to the Virtual Channel.
Cayenne.virtualWrite(0, millis() / 1000);
}

i don’t understand what you want. Do you want your action function to run continuously when the button value is 1?

Yes. Action should run contiunusly until i put value to 0.

This Sound be the Main switch For the system

you need to call the action function from main loop.

void loop() {
  Cayenne.loop();
  if (value == 1)
  {
    action();
  }
}
int value;
CAYENNE_IN(VIRTUAL_CHANNEL)
{
  value = getValue.asInt();
  digitalWrite(ACTUATOR_PIN, value);
}