BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Motion Sensor Push Notification Android Local Server

localserver
android
#1

SparkFun Blynk Board + RaspBerry Pi Local Server
Android
Current Blynk Library 0.6.1

Hello, I am working on a simple project for my finals. I am new to programming and have managed to mash together some code that works. I am having zero success in my Android receiving the Push Notification. I am successful in reading the notification in the serial monitor.
I am unsure how to call the function other than with the timer event " timer.setInterval(20000L, pushN);"
I just set the interval duration longer than the motion sensor duration because it would keep runnning and give me multiple notifcations. How can I have it only run once it’s triggered and be able to have it reset and trigger again when the motion is activated. It seems to work with a long enough delay but idk if that’s proper as I don’t know the syntax.

I am trying to get my Android Blynk app to give me a push notification when my motion sensor is triggered. I think I am close. Here is my code. Any guidance would be greatly appreciated! <3


/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

int ledPin = 12;  // LED on Pin 12 of Blynk Board
int pirPin = 16; // Input for Motion Sensor

int pirValue; // Place to store read PIR Value
int senD; //sensor data
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx";
char pass[] = "xxxx";

BlynkTimer timer;

// Attach virtual serial terminal to Virtual Pin V7
WidgetTerminal terminal(V7);

// my attempt to have terminal communication between the virtual terminal and the serial IT only Works ONE WAY

BLYNK_WRITE(V7)
{
  String incoming = param.asStr();
  Serial.println(incoming);
}

// send a signal to a virtual pin to indicate the motion sensor is outputting signal to the relay
void myTimerEvent()
{
  senD = analogRead(ledPin);
  Blynk.virtualWrite(V5, senD);
  
  }

//  push notify when sensor output is active
void pushN()
 {
  if(senD > 1){
    Blynk.notify("Motion Detected");
    Serial.println("Motion Detected");
    }
 }

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Clear the terminal content
  terminal.clear();

  Blynk.begin(auth, ssid, pass, IPAddress(xxxxxx), xxxxx);


  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
 
  digitalWrite(ledPin, LOW);
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  // Setup interval for pushN ?
  timer.setInterval(20000L, pushN);
}

void loop()
{
  Blynk.run();
  pirValue = digitalRead(pirPin);
  digitalWrite(ledPin, pirValue);
  timer.run(); // Initiates BlynkTimer
}

0 Likes

Problems with Push Notifications
#2

Do you have the Notification widget in your app?
What do your local server logs say?

You should remove the clutter from your void loop.
You should also research the use of flags as a way of knowing that a notification has been sent.

Pete.

1 Like

#3
Thank you for the quick reply. I have the notification widget and just checked my local server Worker log and it says "ticked widget for 1 minute" .  

I do not know where i should put the Clutter in void Loop. I tried moving the two lines of code 
  pirValue = digitalRead(pirPin);
  digitalWrite(ledPin, pirValue); 

to other locations and the code didn’t work. I am getting a Heartbeat timeout from my push notification function. It shows in the serial monitor and then I get a heartbeat timeout. I think it’s from the way I have it set. The timer interval for the pushN seems to cause this.

If I am correct to assume the Woker.logs “ticked widget for 1 minute” is a confirmation that the server is pushing the Push notification? I have used the BlynkBoard firmware the last few months and have had no success in getting Push notifications working. I am now trying to just write my own code to simplify what I want.
I will look up how to use flags thank you for the insight.

0 Likes

#4

clean your loop !

{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}
3 Likes

#5

Put it into one of your timers.

3 Likes

#6

Read this:

Pete.

3 Likes

#7

Thank you for the replies. I did clean out my void loop and my code worked as it did when it was in the loop. I have looked up some posts about using flags. I have created a flag logic in my function and it sends the notification through serial but not through Push. The Worker.log on my blynk server shows it has ticked 1 widget at the appropriate time. But I think my flag logic is flawed. I still get a heartbeat timeout after the serial notification. I defined a
bool sflag = true;
and created this function to be called every 5 seconds. But I may need to use better logic to get it to not timeout?

void pushN()
 {
    
  if((senD >= 1) && (sflag == true)) {
    Blynk.notify("Motion Detected");
    Serial.println("Motion Detected");
    sflag = false;
    }
   if (senD == 0){
    sflag = true;
    }
 }
0 Likes

#8

Post your updated code.

Pete.

0 Likes

#9

Maybe I need to set the bool sflag = false instead of true? I’ll be trying different things here later today. I’ll do some more research into c++ while flags. The only thing I can think of why my android isnt getting the push notification is that the code isnt working properly because the server log says it ticks the widget. So shouldn’t it get the Push Notification ?

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

int ledPin = 12;  // LED on Pin 12 of Blynk Board
int pirPin = 16; // Input for  Motion Sensor

int pirValue; // Place to store read PIR Value
int senD; //sensor data
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx!";

BlynkTimer timer;

// Attach virtual serial terminal to Virtual Pin V7
WidgetTerminal terminal(V7);

// my attempt to have terminal communication between the virtual terminal and the serial
BLYNK_WRITE(V7)
{
  String incoming = param.asStr();
  Serial.println(incoming);
}

// send a signal to a virtual pin to indicate the motion sensor is outputting signal to the relay only works one way
void myTimerEvent()
{
  pirValue = digitalRead(pirPin);
  digitalWrite(ledPin, pirValue);
  senD = analogRead(ledPin);
  Blynk.virtualWrite(V5, senD);
  
  }

// push notify when sensor output is active
void pushN()
 {
    bool sflag = true;
  if((senD >= 1) && (sflag == true)) {
    Blynk.notify("Motion Detected");
    Serial.println("Motion Detected");
    sflag = false;
    }
   if (senD == 0){
    sflag = true;
    }
 }

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Clear the terminal content
  terminal.clear();

  Blynk.begin(auth, ssid, pass, IPAddress(x,x,x,x), 8080);


  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
 
  digitalWrite(ledPin, LOW);
  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  // Setup interval for pushN ?
  timer.setInterval(5000L, pushN);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}
0 Likes

#10
int pirValue; // Place to store read PIR Value
int senD; //sensor data
bool sflag = true;
.
.
.


void pushN()
 {
   // bool sflag = true; <---------  put it before setup !
  if((senD >= 1) && (sflag == true)) {
    Blynk.notify("Motion Detected");
    Serial.println("Motion Detected");
    sflag = false;
    }
   if (senD == 0){
    sflag = true;
    }
 }
0 Likes

#11

Thank you, I had it before the setup and just changed it to include in the function. I will make a note of that. I was running the code with it like you mentioned and it does timeout .

2 Likes

#12

please add

##define BLYNK_DEBUG
##define BLYNK_PRINT Serial

and post your serial ouptut .

1 Like

#13

Have you checked your local server error logs?
Anything related to http://fcm.googleapis.com/ in the error logs relates to the google cloud messaging service used for push notifications.

Also, have you tried uninstalling and reinstalling the app?

Pete.

1 Like

#14

Thank you all for the replies, I figured something out… The Push Notifications do not work Without an internet connection. You mentioned the google api in my error log… Which led me to think and try to give my local blynk server internet access. and Guess what IT worked! … So you can’t have a closed off local network and have push notifications work. Which is far beyond my understanding… But okay. I think this is solved. Thanks for that tip on the use of Flags.

1 Like

#15

Maybe in future you will include key pieces of information like “my local server is operating in stand-alone mode with no internet connection”.
This would have saved a great deal of effort from everyone involved.

Pete.

1 Like

#16

I apologise for my inexperience. I thought running a local server would be for off grid purposes. That’s called stand alone mode, okay. I didn’t see it called that anywhere in the guides or setup information when I was working with it this last month. I may have not recognized it. Thank you for the clarification and the insight into this project of mine.
Yeah, wow that makes sense. Because the Blynk.notify is very simple and so it the notification widget. Very easy to setup in theory. But I guess it’s easy when you are operating a server in the manner in which it’s meant to be.

0 Likes