Onion Omega + node.js Blynk Library - How to write code

I’m new to Omega and node.js. My understanding of node.js is very superficial.

Basically, I have some issues with Blynk reconnect. If connection to Internet is lost for some reason (ie. wifi router went of or IP change sometimes during the Night) then soon after Internet connection is established node.js script produces SSL error.
I’ve done some search, but still I’m not sure how to handle this error in such a way that script will continue to work and trying to reconnect until successfully connected to Blynk server to continue to work.

My code:
#!/usr/bin/env node
/* blynk-service.js
 * Let's communicate with Blynk
 * Run all functions related to Blynk application
 */

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon), or check your email.
var auth = " token ";
var sys = require('sys')
var exec = require('child_process').exec;
var oledExp = require("/usr/bin/node-oled-exp");
var BlynkLib = require('/usr/bin/blynk-library');
var Gpio = require('/usr/bin/onoff-node').Gpio;

var blynk = new BlynkLib.Blynk(auth);
var light = new Gpio(0, 'out');
var v0 = new blynk.VirtualPin(0); // Button1
var v1 = new blynk.VirtualPin(1); // Button2

blynk.on('connect', function() {
	blynkConnectionStatus = 1;
    blynkConnectionMessage = "Blynk CONECTED";
});

blynk.on('disconnect', function() {  // This event never happens
    blynkConnectionStatus = 0;
    blynkConnectionMessage = "Blynk DISCONECTED";
});

v0.on('write', function(param) {
	var val = parseInt(param);		// Extract integer; param is a string, ie. [ 0 ]
	blynk.virtualWrite(v0, val);		// Write back the button state to the Application
	if (val === 0){ 					// Set button (in Blynk App) color for state OFF
		blynk.setProperty(v0, "color", "#9AA7AF");
	} else {       					// Set button (in Blynk App) color for state ON
		blynk.setProperty(v0, "color", "#0BE29E");
	}
});

v1.on('write', function(param) {
	var val = parseInt(param);		// Extract integer; param is a string, ie. [ 0 ]
	blynk.virtualWrite(v0, val);		// Write back the button state to the Application
});

// ... cut ....

setInterval(function() {
	checkWifiActive();
	if (wifiConnectionStatus === 1 && blynkConnectionStatus === 0){
		// Initiate connection to Blynk service
		blynk.begin(auth);
	}
	// Print Blynk Connection status message
	oledPrint(6, blynkConnectionMessage);
	
}, 19933);

What works here:

  • Buttons,
  • connection to Blynk App

What doesn’t work:

  • event blynk.on('disconnect', ... never happens
  • reconnection to Blynk throw error and break script execution
  • update of other button with virtualWrite(v0, val) does nothing
  • change of color property of a button depending on button state doesn’t working

How to write code for Omega (node.js) regarding setInterval() function?

  • If I putt all the code inside setInterval() how will this affect response delay?
  • What should be in setInterval()?
  • How to detect and print Blynk connection status?
  • How to reconnect automatically?

Now I got the output from terminal:

[  476.895439] Interface apcli0 link up! (WPA2PSK AES)
Disconnect blynk
REARMING DISCONNECT
Connecting to: blynk-cloud.com 8441
SSL authorization...
Connected

So, basically we can detect that Blynk service was disconnected after it reconnects.
But, how to detect that Blynk is disconnected right now?

Neither Blynk Python or Blynk NodeJS libraries seem to have the full command structure of Blynk’s C++ Library… which I guess is to be expected as they are reportedly in Alpha and Beta stages respectfully. User contribution on their respective GitHub pages is welcome.

BlynkTimer is part of an Arduino based SimpleTimer library… I don’t know exactly how (if able) to use some of those timer commands in JS… you may need to use timers made for NodeJS instead… Google for the many options.

Meanwhile I have a simple example using setInterval() and setTimeout() in my NodeJS code examples… link in next post.

Blynk NodeJS uses a slightly different form of syntax… read through the examples on the GitHub page and what you can find in this forum for a better idea.

E.G.

Thank You Gunner on your repply.

However, none of the examples are answering my questions about how to write the code.
Meanwhile, I learned that node.js executes on events and code doesn’t need to be inside setInterval(). One should put code inside setInterval() just in case if (exact) periodic execution is needed.

Ok, then, if I put all of my code in setInterval() that would be not so great idea and would’n solve My problems and those are:

  • How to detect and print Blynk connection status?
  • How to reconnect automatically?

All examples shows only part (more or less) of the code inside “someone’s_nodejs_app.js” script.
It’s hard to learn if you are just beginning.

As stated I don’t think those commands are available for NodeJS… it is still in Beta.

If you need tried and true ‘failed connection recovery/failover’ then I recommend using a ESP8266 & C++ based client as that MCU/code combo is much more developed & documented.

Those examples are not “How to code” tutorials… Google for those types of sites on the web. But they can be used as Blynk specific pointers for one’s own learning process. This is how I am slowly learning JS for myself.

Welcome to the club… I didn’t even have those examples at the beginning, which is why I am making them in the first place :smiley:

1 Like