Unable to send notification each time

Hello All,

I am currently facing an issue with firing a notification each time I change the status of digital pin 8. Currently the notification gets sent only the first time and after that I get no more notifications despite trying even after 2-3 minutes. I am unable to pin point exactly what is causing this issue.Any suggestions would be really helpful.
I have attached the code below for your reference.

Thank You.


#define BLYNK_PRINT DebugSerial

#include <SPI.h>
#include <DHT.h>
#include <SoftwareSerial.h>

SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

char auth[] = "m-HCfsNqS_N33ekQDyjjEX6DbBDuL0HU";

#define DHTPIN A4      
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);

const int IR = A0;
int count =0;
BlynkTimer timer;

void sendSensor()
{
  float val = analogRead(IR);
  
  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;
  }
  
  if (isnan(val)) {
    Serial.println("Failed to read from IR sensor!");
    return;
  }
  
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V7, val);

  startLED(val,h);
}

void startLED(float x, float y)
{
    if ( x < 500) {
      count = count + 1;
      if (count%2 != 0)
      {
      digitalWrite(8,HIGH);
      //delay(6000);
     
      Blynk.notify("You entered the Room!");
    
      Serial.print(count);
     }
    else if (count%2 ==0)
     {
      digitalWrite(8,LOW);
      //delay(6000);
     
      Blynk.notify("You left Room!");
      
      Serial.print(count);
     }
    }
    return;
}

void setup()
{
  // Debug console
  DebugSerial.begin(9600);
  Serial.begin(9600);
  
  Blynk.begin(Serial, auth);
  
  dht.begin();

  pinMode(8,OUTPUT);

  timer.setInterval(1000L, sendSensor);
}

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

Your code contains a function called sendSensor which is called by a timer once every second.
This in turn calls the startLED function, so your startLED function is being called once every second as well.

startLED contains an if statement that checks if the reading from your IR sensor is less than 500. It’s difficult to know what circumstances would lead to an IR sensor reading of less than 500, and that’s not something you’ve shared with us, so it’s impossible to say how often this criteria is being met.
However, each time the criteria is met, a Blynk push notification is sent.

As the maximum frequency for push notifications is one per 5 seconds, it seems possible that you are exceeding this limit.

Limitations:

  • Maximum allowed body length is 120 symbols;
  • Every device can send only 1 notification every 5 seconds;

I’d suggest that you add some serial print messages into your sendSensor and startLED functions to track the value of the variables being used, and the frequency at which you are attempting to send push notifications.

Pete.

Hey Pete,
Thanks for replying. The IR sensor reads a value less than 500 each time I place an object very close to it and at other times there is no object nearby so it gives a reading in the range of 900-1100. Hence although the sendSensor function is called each second, the startLED function performs no action.
And in the case of using a serial monitor for checking the serial print, I’m running the serial port Com3 for my project for the blynk connection in the absence of an ethernet or wifi, so im unable to access the serial monitor simultaneously.
I’m unable to figure out why the notification works only once and at the start of the program.

So why have you defined a SoftwareSerial debug port?
I’ve just noticed that you’re doing Serial.prints (instead of DebugSerial.prints) in your code, which will seriously screw-up your Blynk connection as this uses Serial.
You need to change this, and it may help resilve your issue,
You also need some proper hardware…

Pete.

I don’t understand your need for determining if the count is even or odd… but I don’t see how this prevents multiple notifications being sent at either trigger point.

Perhaps something like this might work better… do the same for the “left room” side as well.

int notifyFlag = 0;

If (x < 500 && notifyFlag == 0) {
  notifyFlag = 1;  // Disable further notifications
  digitalWrite(8,HIGH);
  Blynk.notify("You entered the Room!");
  // Timed Lambda Function - Flag Reset
  // EDIT - wrong :P - timer.setTimeout(1500L, []() {  // Run following in 1.5 seconds
  timer.setTimeout(5500L, []() {  // Run following in 5.5 seconds
    notifyFlag = 0;  // enable notifications
  });  // END Timer Function
}

Thank you for your suggestions, I will take that into consideration and make some changes in my code and then get back on this.

Hey GTT,
Thanks for replying, the reason I have used a count in this is so that I can distinguish whether the person has entered the room or left the room. As an odd value of count would indicate that I have entered the room and then switch on the lights. For an even value of count, the lights would get switched off.
Also, I will look at the code you suggested and see how it could work in mine and try to implement something similar.
Thanks again.

Got it… however that then indicates that there may be multiple notification attempts as it does its calculations.

Triggering a digital pin ON, when it is already ON, or OFF when already OFF, hurts nothing, even if many times a second… the same for sending notifications is, however, simply shutting you out (from sending more) for an undetermined amount of time.

You could also take my timer based flag and change it around so that instead of a timer. you use the exiting of the room as a way of resetting the notification flag (just before sending exit notification), and visa versa… AKA find a way of allowing only ONE notification per confirmed entrance or exit.

And unless you are a confirmed bachelor/bachelorette, you might want to account for additional people entering the room… a set of dual sensors determining direction of motion should work… basically the same principle of a rotary encoder… thus accounting for only turning off the light when everyone whom entered has left.

Thanks a lot for your input, it helps me clear a lot of my doubts. I shall work around the code you uploaded and make it work.
Also despite being a bachelor, I’m trying to take into consideration that the count method will not hold good if multiple people enter and at the moment I don’t have more sensors. Hence I was planning to send a notification saying “did you just leave the room?” When my count value becomes even. Following which based on my reply as YES or NO, the command to switch the lights ON/OFF could take place.
That’s the reason, I was trying to understand how the basic mechanism for sending a notification works on blynk as I am fairly new to this platform.

Simple… as already stated in the Docs and above… send no more than 1 every 5 seconds (Opps, I clearly missed the mark with my 1.5 second timer above :innocent: ) else it blocks you from sending more (yes that part is undocumented, but is a logical assumption confirmed with testing)

Blynk uses a messaging service provided by Google. The device sends the push notification command to the server, and I think the server checks that the same device hasn’t sent a notification request within the past 5 seconds before passing the request on to the google service.
How your device handles the google notification depends on the phones O/S and how you’ve configured your priority settings. High priority consumes more phone battery power, as described in the documentation.

I think the biggest mistake the new Blynk users make is the assumption that push notifications are the best solution for situations like yours. In my opinion they aren’t.

I work on the ‘reporting by exception’ principal, where a push notification will be generated when an exceptional situation occurs - one which the users wants to be notified about whatever the time of day or night and whatever it is that they are doing. This might be an emergency such as fire, flood or an intruder, or a situation where a mission critical system requires manual intervention.
If you send alerts whenever non-critical events occur then you simply clutter up your phone’s home screen with dozens of alerts that quickly become tiresome and are ignored.

In your situation, I’d be changing the status of an LED widget or a labelled value display to indicate the in/out status of your room.

However, you’ve already realised that your presence detection methodology is flawed, and that you need to go back to the drawing board.
Most presence detection systems that control lights use PIR or microwave sensor(s) to detect movement within an area and keep the lights on for a timed period (5 minutes maybe). If no re-trigger events have occurred once the timeout has elapsed then the lights are turned off. This obviously requires the person(s) within the room to keep moving around and triggering the sensors, and that’s not always practical. That’s why traditional light switches still have a place in most homes.

Pete.

Yes, I think the ‘reporting by exception’ would be a great approach. I will try implementing that in my project, it would be really helpful and won’t overload the screen with too many alerts.
Yes, I agree that a PIR would be one of the most efficient way to go about for motion presence detection but I was just trying to tinker a similar setup with existing sensors that I had at home and nothing else. In the future when I look to refine the entire system, I will definitely look to integrate the PIR for this.
Thanks a lot for all your input, it’s gonna be really helpful for my project.

Aditya.