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

Notification doesn't work (in Blynk NodeJS)

rpi
nodejs

#1

Hi guys,

I need help with my project.
I’ve preparing script which will send notification to blynk app.
It will be written in Javascript and it will run on RPi3 (DietPi).
Basic idea is that script will connect to DB (MySQL) and then it will check value.
If there will be correct value it will send notification to me.
I have script but no notification came to me.
Also I have tested to send mail to me but same result.
Here is my script.

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost",
  user: "monitoring",
  password: "***",
  database: "monitoring"
});

var Blynk = require('/usr/local/lib/node_modules/blynk-library/');

var AUTH = 'AUTH_CODE';

var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});


con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT value FROM power WHERE name LIKE 'napajanie'", function (err, result, fields) {
    if (err) throw err;

if(result[0].value == 0) 
{
console.log("Napajanie z baterie");
blynk.notify("Napajanie z baterie");
blynk.email("MAIL_ADDRESS","Test","Napajanie z bateerie");
}
else console.log("Napajanie zo zdroja"); 
    
  });
});

#2

I don’t think email and notifications are supported yet in the Blynk JS library.


#3

But it the’re not support there should be an error?
I tried to change blynk.notify to Blynk.notify and after this I will get an error

TypeError: Blynk.notify is not a function

I understand this that it is supported.


#4

Ok, I finally had a chance to look at the library a bit closer… in blynk.js there is references to both email and notify, so perhaps there is the base code… but I cannot see any reference in blynk-node.js


#5

UPDATE - whoops, wrong again :blush:, in my test, both notifications and email work just fine…

You do have the notification and email widgets loaded in the project, right? Latest version of the library?

blynk.email('<myemail>@gmail.com', 'RPi Email Test', 'RPi Email Test');
blynk.notify('RPi Notification Test');

There is also limitation to how frequently a notification can be sent, but I don’t see any obvious time limiting logic in your code.


#6

Most likely, blynk didn’t manage to connect before the db connect callback was called.
I suggest to use JS Promises or async/await API to have a cleaner and more understandable code.

You should wait until blynk.on('connect', ...) event fires


#7

Ok guys thanks, problem solved. Here is my code maybe somebody will use it.

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost",
  user: "monitoring",
  password: "PASSWORD",
  database: "monitoring"
});

var Blynk = require('/usr/local/lib/node_modules/blynk-library/');
var AUTH = 'MY_AUTH';

var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});

blynk.on('connect', function() {
  notify();
});

setTimeout(function(){
process.exit();
}, 2000);

function notify()
	{
		con.connect(function(err) {
		if (err) throw err;
		con.query("SELECT value FROM power WHERE name LIKE 'napajanie'", function (err, result, fields) {
		if (err) throw err;
		if(result[0].value == 0) 
			{
  				con.query("SELECT value FROM power WHERE name LIKE 'notify'", function (err, result, fields) {
  				if (err) throw err;
  				if(result[0].value == 0)
  					{ 
  						console.log("Napajanie z baterie");
  						blynk.notify("Napajanie z baterie");
  						var sql = "UPDATE power SET value = 1 WHERE name = 'notify'";
  						con.query(sql, function (err, result) {if (err) throw err;});
  					}
  				});
			}
		else {console.log("Napajanie zo zdroja");}
  });
});
}

So why I use this code?
I run it on raspberry scheduled in crontab and I used it for inform me when power is down and it will switch to my backup power in house.
Script will check db, if there is napajanie = 0 and notify = 0 it will send notification to me.
After this it will update notify to 1, so I will receive notification only once.
Values in DB are updated by arduino which control power sources.