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);
}>