Im using Blynk for a completely non IOT purpose. Im using it to view / monitor our company internet circuits. My background is a network guy, with PHP experience. I’ve had to struggle with javascript, therefore, laughing at my code is acceptable . Feel free to make suggestions.
- I installed a local server on my ubuntu box.
- I run the js for “Generic Board” on the same ubuntu box.
- It calls php script files that return the values to js.
a. first check if the circuit is admin-up/oper-up.
b. if so, then collect the stats from the interface.
c. pull up that last written file values, calculate the bandwidth from old val and new val.
d. return the bandwith - repeat 3 on intervals of 10 seconds.
my JS script
var Blynk = require('/usr/local/lib/node_modules/blynk-library');
var AUTH = 'Myauthcode';
var blynk = new Blynk.Blynk(AUTH, options = {
connector : new Blynk.TcpClient( options = { addr: "MyServerIP", port: 8080 } )
});
var process = require('child_process');
var v1 = new blynk.VirtualPin(1); // Internet Router 1 TierPoint Input
var v2 = new blynk.VirtualPin(2); // Internet Router 1 TierPoint Output
var v3 = new blynk.VirtualPin(3); // Internet Router 1 ATT Input
var v4 = new blynk.VirtualPin(4); // Internet Router 1 ATT Output
var v5 = new blynk.VirtualPin(5); // Internet Router 2 TierPoint Input
var v6 = new blynk.VirtualPin(6); // Internet Router 2 TierPoint Output
var v7 = new blynk.VirtualPin(7); // Internet Router 1 TierPoint Down Led
var v8 = new blynk.VirtualPin(8); // Internet Router 1 ATT Down Led
var v9 = new blynk.VirtualPin(9); // Internet Router 2 TierPoint Down Led
var v17 = new blynk.VirtualPin(17); // Internet Router 1 TierPoint UP Led
var v18 = new blynk.VirtualPin(18); // Internet Router 1 ATT UP Led
var v19 = new blynk.VirtualPin(19); // Internet Router 2 TierPoint UP Led
function getv1() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V1.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V1out = trim1(stdout);console.log('V1:',V1out); if (V1out>0 && V1out<1024) {v1.write(V1out);}}})
}
function getv2() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V2.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V2out = trim1(stdout);console.log('V2:',V2out); if (V2out>0 && V2out<1024) {v2.write(V2out);}}})
}
function getv3() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V3.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V3out = trim1(stdout);console.log('V3:',V3out); if (V3out>0 && V3out<1024) {v3.write(V3out);}}})
}
function getv4() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V4.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V4out = trim1(stdout);console.log('V4:',V4out); if (V4out>0 && V4out<1024) {v4.write(V4out);}}})
}
function getv5() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V5.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V5out = trim1(stdout);console.log('V5:',V5out); if (V5out>0 && V5out<1024) {v5.write(V5out);}}})
}
function getv6() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V6.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V6out = trim1(stdout);console.log('V6:',V6out); if (V6out>0 && V6out<1024) {v6.write(V6out);}}})
}
function getv7() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V7.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V7out = trim1(stdout);console.log('V7:',V7out);
if (V7out == 0) {
v7.write(255);
v17.write(0);
v1.write(0);
v2.write(0);
}
else if (V7out == 1) {
v7.write(0);
v17.write(255);
getv1();
getv2();
}
// blynk.syncVirtual(1);
}})
}
function getv8() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V8.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V8out = trim1(stdout);console.log('V8:',V8out); v8.write(V8out);
if (V8out == 0) {
v8.write(255);
v18.write(0);
v3.write(0);
v4.write(0);
}
else if (V8out == 1) {
v8.write(0);
v18.write(255);
getv3();
getv4();
}
}})
}
function getv9() {
process.exec('/SAVE/Apps/Blynk/Circuit.v1/V9.php', function (err, stdout, stderr) {
if (err) {console.log("\n" + stderr);}
else {V9out = trim1(stdout);console.log('V9:',V9out); v9.write(V9out);
if (V9out == 0) {
v9.write(255);
v19.write(0);
v5.write(0);
v6.write(0);
}
else if (V9out == 1) {
v9.write(0);
v19.write(255);
getv5();
getv6();
}
}})
}
setInterval(getv7, 10000); // run getv7 every 10 seconds
setInterval(getv8, 10000);
setInterval(getv9, 10000);
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
My V7.php script
#!/usr/bin/php
<?php
error_reporting(0);
include "/SAVE/Apps/Blynk/Circuit.v1/snmp.inc";
$filename = "/SAVE/Apps/Blynk/Circuit.v1/V7.inc"; ## not used, yet
$ip = "10.3.1.251"; ## router ip address
$oid1 = "iso.3.6.1.2.1.2.2.1.7.3"; ## admin oid
$oid2 = "iso.3.6.1.2.1.2.2.1.8.3"; ## oper oid
include "/SAVE/Apps/Blynk/Circuit.v1/virt.int.up.inc";
?>
My virt.int.up.inc script
<?php
$snmp_val1 = snmp2_get($ip, $snmp_read_1, $oid1);
$snmp_val2 = explode(": ", $snmp_val1);
$snmp_val3 = snmp2_get($ip, $snmp_read_1, $oid2);
$snmp_val4 = explode(": ", $snmp_val3);
$adminup = trim($snmp_val2[1]);
$operup = trim($snmp_val4[1]);
if (($adminup==1)&&($operup==1)){$ok=1;}
else {$ok="0";}
echo $ok;
?>
My snmp.inc script
<?php
$snmp_read_1 = "public";
$snmp_timeout = 5000; // removed var from scripts, inconsistent results.
?>
My V1.php script
<?php
error_reporting(0);
include "/SAVE/Apps/Blynk/Circuit.v1/snmp.inc";
$filename = "/SAVE/Apps/Blynk/Circuit.v1/V1.inc";
$ip = "10.3.1.251";
$oid = "iso.3.6.1.2.1.31.1.1.1.6.3";
include "/SAVE/Apps/Blynk/Circuit.v1/virt.single.snmp.inc";
?>
My virt.single.snmp.inc
<?php
$snmp_val1 = snmp2_get($ip, $snmp_read_1, $oid);
$snmp_val2 = explode(": ", $snmp_val1);
$Newbyte = trim($snmp_val2[1]);
$date = date_create();
$Newdate = date_timestamp_get($date);
if (file_exists($filename)) {
include $filename;
$num_seconds=$Newdate-$Olddate;
## echo "Number of secs $num_seconds\n";
$num_bytes=$Newbyte-$Oldbyte;
## echo "Number of Bytes $num_bytes\n";
$bytes_per_sec=$num_bytes/$num_seconds;
## echo "Number of bytes per second $bytes_per_sec\n";
$bits_per_sec=round(($bytes_per_sec*8)/1000000);
## echo "Bits per second $bits_per_sec\n";
} else {
$bits_per_sec=0;
}
if (!$Newbyte) {$Newbyte="0";}
$myfile = fopen($filename, "w") or die("Unable to open file!");
$txt = "<?php\n";
fwrite($myfile, $txt);
$txt = "\$Olddate=".$Newdate.";\n";
fwrite($myfile, $txt);
$txt = "\$Oldbyte=".$Newbyte.";\n";
fwrite($myfile, $txt);
$txt = "?>\n";
fwrite($myfile, $txt);
fclose($myfile);
if ($bits_per_sec<0){$bits_per_sec=-1;}
echo $bits_per_sec;
?>