Motion Detect Relay On

Hi the motion sensor is working. I want to make the sensor detect the relay trigger. connecting a relay to the relaypin. but the relay does not work when it detects. how can I make a change in the code


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "efd722b668f24b9c92c58b8478883f35";
char ssid[] = "okhan";
char pass[] = "atakan2009";
//char server[] = "xxx.xxx.xxx.xxx";

#define relaypin 02 
#define pirPin 00

int pirState;
int val;
int x;

SimpleTimer timer;

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval(){
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(relaypin, HIGH);  
      }
      else {
        digitalWrite(relaypin, LOW); 
      }
   }

  void pir(){
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("EVDE HAREKET ALGILANDI!");
 }
    }
  }

void setup(){
  //Blynk.begin (auth, ssid, pass, server, 8080);//local server
   // You can change server:
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

  pinMode(relaypin, OUTPUT);
  pinMode(pirPin, INPUT);

  timer.setInterval(1000L, PIRval);
 timer.setInterval(1000L, pir);
   }

void loop(){
   Blynk.run();
   timer.run();
}

First of all, you’ve chosen poor GPIO pins for your relay and PIR. Read this:

Secondly, you’re calling two functions at EXACTLY the same time:

I’m not sure why you don’t just combine these into one function. Also, you don’t need to re-read the value of the PIR Pin, you have its value stored in the global variable “val”.

Thirdly, are you sure that your relay is Active HIGH? most relays on the market seem to be Active LOW, meaning that they need a LOW command to activate the relay.

Pete.

hello, my codes are working fine. I can close and open the notification from the application. but the sensor continues to pull the relay even though the application is off the notification. only the notification is turned on and off. I want to turn off the relay at the same time

what I want to do is open the relay only when I activate it from the application. The relay will run when I set the alarm, the relay will not work when I cancel the alarm. but the sensor always detects …

Your explanation of your problem, your current functionality and your desired functionality leaves a lot to be desired.

I think you’re saying that you want to be able to use a widget deactivate the relay as well as deactivating notifications, and that at the moment, that widget only deactivates the notifications.

If this is the case then you need to re-configure your two functions in to one (as I suggested earlier) and do the if (x==1) test at the top of your function and the code that operates your relay and sends the notification below that if test.

Pete.

pete, thank you for your comments, can you help me on the code? My school homework and I’m really in a bad situation right now. I pray a lot if you help

I’ve already explained what you need to do.

Pete.

my english is very bad, i can’t find turkish source, i think i will understand better if you can make sample on code, can you help

Nice try, but I’m a bit too old for a trick like that!

It’s your project, and the idea is that you learn from the process. You won’t learn if you don’t experiment and make a few mistakes along the way.

Pete.

1 Like

No… your code is limping along

We don’t get any school credits for doing your homework :stuck_out_tongue:

But we can help, as @PeteKnight already has, point you in the right direction.

Now you need to take it from there and start reading and thinking for yourself.

Hi, I understand you very well. I want to learn, but my English is very bad. I’m translating using google translate, but it doesn’t translate exactly. so I want an example. please on this sample! If you tell me how to close the notification and relay at the same time, I would be very happy! There is no Turkish source.

I’ve changed the formatting of thees two if functions. Hopefully, the way that the code is indented now will make it easier for you to understand what they do:

void PIRval()
{
  val = digitalRead(pirPin);
  if (val == HIGH)
    {
       digitalWrite(relaypin, HIGH);  
    }
  else
    {
       digitalWrite(relaypin, LOW); 
    }
}


void pir()
{
  if (x == 1)  // This is your test to see if the Blynk widget switch is on(1) or off(0)
    {
      if (digitalRead(pirPin) == HIGH)
        {
           Blynk.notify("EVDE HAREKET ALGILANDI!"); // This is a problem, it will send a notification too often
        }
    }
}

YOU need to combine these into one function that:
Checks if x==1
If it is, set your relay HIGH and send a notification
If it is not, set your relay LOW

This will work if you only call the function every 5 seconds or less, as this is the limit for how often notifications can be sent.

Pete.