Notification on Blynk does not appear

Hello
i want create an motion detector my code is work, but in void m1, i write

SwSerial.println("Motion Detected");
Blynk.notify("Motion Detected");

but in my smartphone doesn’t appear, but in my void setup, i write

SwSerial.println("READY");
Blynk.notify("READY");

this appears on my notification my smartphone

note : i use arduino uno with software serial USB

sorry my language so bad
thanks


#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>

char auth[] = "560910e1a47d4c31a4bd347b926502b3";

int in1 = 2;                 // Pin Relay
int led = 13;                // LED Pin
int sensor = 6;              // Pir Sensor
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)

void setup()
{
  SwSerial.begin(9600);
  Serial.begin(9600);        
  Blynk.begin(Serial, auth);
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize LED sensor as an input
  pinMode(in1, OUTPUT);      // initialize PIR sensor as an input
  digitalWrite(in1, HIGH);   // initialize PIR sensor as an input
  SwSerial.println("READY"); // SET NOTIFICATION READY
  Blynk.notify("READY");     // SET NOTIFICATION READY
}

void loop(){
  Blynk.run();
  m1();      // CALL m1
}

void m1(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(50);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = HIGH;       // update variable state to HIGH
      SwSerial.println("Motion Detected");
      Blynk.notify("Motion Detected");
      digitalWrite(in1, LOW);
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      digitalWrite(in1, HIGH);
      delay(100);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
  }

You should use a flag to limit the Blynk notifications as you are limited to 1 every 5s; and as you call the function from the main loop, the app tries to “fire” the notification a few hundred times a second and you het blocked by the server (to prevent flooding). Call that from a timer everi second or two (most pir sensors have the output active for a few seconds), implement a flag to prevent flooding and you’ll be good to go.

1 Like

Hi all, thanks for help me
it’s magicaly works :smile:

i rewrite the code like this and works


#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>

BlynkTimer timer; // Announcing the timer

char auth[] = "560910e1a47d4c31a4bd347b926502b3";

int relaypin = 2;               // Relay Pin
int ledPin = 13;                // choose the pin for the LED
int inputPin = 6;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup()
{
  pinMode(ledPin, OUTPUT);      // declare LED as output
  digitalWrite(ledPin, LOW);
  pinMode(inputPin, INPUT);     // declare sensor as input
  
  pinMode(relaypin, OUTPUT);
  digitalWrite(relaypin, HIGH);
  
  SwSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  SwSerial.println("READY");
  Blynk.notify("READY");
  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}

void loop(){
  Blynk.run();
  timer.run();        // run timer every second
}

void sensorDataSend(){

val = digitalRead(inputPin);  // read input value
Blynk.virtualWrite(V1, val);  // sending sensor value to Blynk app
  if (val == HIGH) {            // check if the input is HIGH
    if (pirState == LOW) {
      // we have just turned on
      digitalWrite(ledPin, HIGH);  // turn LED ON
      digitalWrite(relaypin, LOW);
      Blynk.notify("Motion Detected");
      // We only want to print on the output change, not state
      pirState = HIGH;
      delay(1000);
    }
  } else {
    if (pirState == HIGH){
      // we have just turned of
      digitalWrite(ledPin, LOW); // turn LED OFF
      digitalWrite(relaypin, HIGH);
      // We only want to print on the output change, not state
      pirState = LOW;
      delay(1000);
    }
  }
  delay(1000);
}

note : i put more delay 1 sec, just guide say, and works, thanks :smiley:

Thanks :smiley:

It may be working… but not properly… you have a 1 second interval timer running, but then the function it calls has a total of 2 second delays in it… and since delay() blocks ALL processing, this WILL interfere with Blynk’s communication and your timer.

Bad coding methods for IoT based programs like Blynk.