(Solved) Pir works well give notification but can not simultaneously control relay function ..?

this is my code

#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PULPSTONE";
char pass[] = "internet";
#define ledPin 13 
#define pirPin 12 
int nilaiPir;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (ledPin, OUTPUT);
  pinMode (pirPin, INPUT);
  digitalWrite (ledPin, LOW);
  delay (1000);
  digitalWrite (ledPin, HIGH);
  delay (1000);
  digitalWrite (ledPin, LOW);
  delay (1000);
}

void loop()
{
  awal:
  nilaiPir=digitalRead (pirPin);
  if (nilaiPir==1){
  Blynk.notify("Motion detected");
  digitalWrite(ledPin, HIGH);  
  }
  else
  {digitalWrite(ledPin, LOW); 
    goto awal;}
  Blynk.run();
}

pir works well give notification but can not simultaneously with control relay function …?
im test and i tried to put led1 and led2 on pins D1 and D2 but i can not on off led from blynk app, but pir works well every motion detecting and giving notification. how to control digital pins running simultaneously with pir sensors?
thx for advice
my board is Nodemcu

I haven’t used a goto for 35 years - and for good reasons!

Pete.

1 Like

It is disappointing how very few actually read the Documents, page, Help Center files or search this forum…

A) Don’t run everything in the void loop()

B) Learn how BlynkTimer works… and use it!

C) Search this forum for PIR topics discussing the hows and whys of others methods of using one with Blynk and other devices.

Blynk - RE-SEAR-CH

1 Like

Listen to @PeteKnight, he’s full of wisdom :slight_smile:

If I understand your code correct… As long as nilaiPir is false, the else statement will be invoked and with the goto awal everything jumps back to the start of the main loop(). So unless something triggers the PIR, you’ll never reach Blynk.run()?!?

1 Like

I might be full of something, but I’m not sure I’d describe it as wisdom!

I think you’re right. I couldn’t be bothered to try getting my head around the logic - once I saw all that stuff, and a goto, in the void loop I just gave up.

It’s amazing that the PIR notifications in Blynk actually work at all!

Pete.

im Update code now

#define BLYNK_PRINT Serial


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PULPSTONE";
char pass[] = "internet";
#define ledPin 13 
#define pirPin 12 
int nilaiPir;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (ledPin, OUTPUT);
  pinMode (pirPin, INPUT);
  digitalWrite (ledPin, LOW);
  delay (1000);
  digitalWrite (ledPin, HIGH);
  delay (1000);
  digitalWrite (ledPin, LOW);
  delay (1000);
}

void loop()
{

  getPir();
  Blynk.run();
}
void getPir(){
  nilaiPir = digitalRead(pirPin);
  if (nilaiPir==1) 
  { 
    Blynk.notify("Motion detected"); 
    return;
  }
  else if (nilaiPir==0){
    return;
  }
}

i’m have a new problem. Pir work normally but when I put 2 leds between D1-D4 , but blynk app always only can control 1 led just that on off…what the problem?

  1. Do you mean D1 and D4 or D2 and D3?
  2. Does D stand for digital or the screen printed labels on ESP8266 development boards?
  3. Are you referring to physical LED’s or Blynk virtual LED’s?

Like this

Only L1 or D1 (position 1) and L1 or D3(position2) which can be controlled on off from blynk app (Android)

L2 or D2 (position 1) and L2 or D4 (position2) Not responding…

I think you need to do more research on how ESP based boards utilise all those pins… they are NOT all created equal, which can make it tricky to control :confounded:

You didn’t show conclusively whether all your LEDS are common GND (based on the picture, perhaps, but that is just a picture) and thus expecting each pin to be sourcing current when HIGH

I believe both D3 (GPIO0) and D4 (GPIO2) have internal pull up resistors (EDIT - at least they do on my Wemos D1 Mini - NodeMCU clone) so they will react differently to a HIGH signal, since technically they are already HIGH at boot. So you probably need to trigger them LOW and have the LED wired the other way around (common 3.3v) in order to light it up when triggering the pin LOW (AKA sinking current).

I haven’t got an available board on hand to confirm all this… and I sometimes forget/get confused :blush: without “hands on testing” so I recommend you experiment and see for yourself.

When I see more than blynk.run and timer.run in the loop I usually don’t have to look any further to find what’s wrong :wink: And the goto was hideous :rofl:

On a general note:: Perhaps they should update the docs and put an emphasis one why the loop() needs to be “clean” and how timers should be used instead.

I concur with @Gunner and his disappointment, but this is a reoccurring “beginners mistake”. I think this “limitation” when coding Blynk should be mentioned already in the “GETTING STARTED” section of the docs. Most people read at least that part I think. :thinking:

Getting OT here, perhaps I should post this in a more suitable part of the forum, unless @Gunner does it for me :slight_smile: :ok_hand:

solved thanks for all

So, what was the solution?.. so the next time I tell someone to “search” they might “find” :stuck_out_tongue_winking_eye:

this is edited sketch with solved problem but im combine with dht11 sensor



#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PULPSTONE";
char pass[] = "internet";
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(D2, HIGH);
    digitalWrite(D3, HIGH);
    digitalWrite(D7, HIGH);
    digitalWrite(D8, HIGH);
  }
  else{
    digitalWrite(D2, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D7, LOW);
    digitalWrite(D8, LOW);
  }
}

#define DHTPIN 2          // What digital pin we're connected to
#define ledPower 16

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int p=digitalRead(14);
   
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
 if(p==HIGH)
 Blynk.notify("Gerakan terdeteksi");
}

void setup()
{
  pinMode (ledPower,OUTPUT);
  digitalWrite (ledPower, HIGH);
  pinMode(14,INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

before asking of course i look for other references in google and forums but always failed…

i want to combine it with wifi manager but when i tried it some times always failed because dht and pir do not respond…any suggestion? thanks

Let’s not discuss WiFiManager in this thread.

Does the sketch you posted work for PIR and relay.

Then create a new thread providing details of what you are trying to do.

Uses seperate functions on separate timers (that don’t run at the same time) for each sensor.

DHT11 is Slooow… I recommend a minimum of 5 seconds between timed checks.

all digital pin works well when i tried to use led with running pir sensor at the same time, but i have not tried to use 4 channel relay…ok i will ask in new thread.

so must edit from 1000L to 500L? that is right

Actually 5000 :wink: these represent milliseconds.

ups… sorry . That’s what I mean… thanks very much

in addition to the last question in this thread
how to add virtual button for on off (switch button) function pir sensor? some code I tried but did not work … this one with the assumption pir sensor is on D5 (gpio14)

BLYNK_WRITE(V0) // At global scope (not inside of the function)
{
    int p=param.asInt();
    if (p==1) 
    {
        digitalWrite(14, HIGH);
    }
    else 
    {
        digitalWrite(14, LOW);
    }
}

but is not working to enable and disable pir sensor button. Thanks

I thought you had the PIR working… thus hopfully understood a bit of how it works??

It is a sensor that SENDS data to a digital pin (in INPUT mode) for your code to keep checking and react to (preferably using a timer), so if you want to stop listening to it, just stop reading the pin (by disabling the timed function).

Meanwhile, that code snippet is mostly fine for toggling a digital pin (set for OUTPUT) from HIGH to LOW, but has nothing to do with reading a PIR sensor.