Pir sensor to turn on lights

#1

Hello.I m trying a project that if the pir sensor anticipates movement AND I’m not home AND it’s after 18.00 it will turn on my lights.
The part of combining conditions will be achieved with ifttt and apilio.
My problem is that I created with cayenne this pir sensor code that will trigger a boolean on the apilio and it isn’t working.it never triggers.my pir sensor responds correctly.any ideas what might happen?maybe because it’s on digital output?

#2

is your trigger counter incrementing? how have you setup the trigger?

#3

I have set up the trigger but no,it isn’t adding up.

#4

can you share a screenshot of your trigger and the PIR code(if it is a arduino device).

#5

happy to do it!!wait a second please!

my trigger

 #define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

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

#define SENSOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 0

void setup()
{
	pinMode(SENSOR_PIN, INPUT);
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

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

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
	unsigned long currentMillis = millis();
	// Check sensor data every 250 milliseconds
	if (currentMillis - previousMillis >= 250) {
		// Check the sensor state and send data when it changes.
		currentState = digitalRead(SENSOR_PIN);
		if (currentState != previousState) {
			Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, "digital_sensor", "d");
			previousState = currentState;
		}
		previousMillis = currentMillis;
	}
}
#6

are you trying to add trigger to motion sensor or digital output?

#7

I was sending you my other trigger that actually triggers the output.(which works great!)

#8

i see you have two widgets on the same channel. Delete both of them, and when the PIR detect motion it will auto produce a green temporary green widget on the dashboard. Add it on your dashboard by clicking on +. then add a new trigger for this widget.

#9

that solved the problem!!!thanks so so much!!
also I need a help on my logic if you can.
I ve created the motion sensor trigger.Now I have to make the ifttt check if I m out of an area(easy)
BUT to do it in specific hours.)lets say after 18.00.
My last problem is how to ask cayenne even make the trigger we created false on a specific hour OR check only during night.any ideas??

1 Like
#10

you can use scheduler (event) to set the time to ON a button on the cayenne dashboard and read it in the code and use it for further.

#11

I ll check it and get back!!!thanks again!!

#12

in short this:

int x;
CAYENNE_IN(2)
{
  x = getValue.asInt();
}
void loop()
{
	Cayenne.loop();
        if (x == 1)
       {
	checkSensor();
        }
}

add a schedule to a button(channel 2) on the cayenne dashboard.

2 Likes