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?