Node.js blynk-library error on reconnect

Hello,
I am having a problem with my node.js script (blynk-library) throwing an error on reconnecting. The script runs just fine throughout the day. In the middle of the night however between 3-6am the app will permanently disconnect (script crashes). I assume the disconnect is related to my router or ISP. The script is called through rc.local on my raspberry pi zero w. To simulate a disconnect I ran the script via ssh…then pulled the ethernet cable. The blynk app disconnected then showed Connecting… When the ethernet cable was plugged back in- the app established a connection…and a few seconds later this error was thrown:

Error: Uncaught, unspecified “error” event. (ECONNRESET)
at Blynk.emit (events.js:163:17)
at Blynk.error (/home/pi/humidity/node_modules/blynk-library/blynk.js:590:8)
at . (/home/pi/humidity/node_modules/blynk-library/blynk.js:553:48)
at emitOne (events.js:96:13)
at emit (events.js:188:7)
at TLSSocket. (/home/pi/humidity/node_modules/blynk-library/blynk-node.js:223:16)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)

Background info:

  • App: iOS version 2.12.0
  • Node.js version: 6.9.5
  • Blynk-library version: 0.4.7

Script:
It checks the humidity and temp then displays it in two labeled values. A slider establishes a humidity range which turns a humidifier off and on. When the slider is at max (60) the humidifier is always on. When the slider is at low(20) the humidifier is always off.

Here is the main setInterval loop code:

setInterval(function() {

      var readout = sensorLib.read();
      var num1 = readout.temperature;
      var num2 = num1*1.8+32;
      var num3 = num2.toFixed(1);

      console.log('Humidity:    ', readout.humidity.toFixed(1) + ' %');
      console.log('Temperature: ', num3 + ' F');
      blynk.virtualWrite(3, num3);
      blynk.virtualWrite(4, readout.humidity.toFixed(1));

      var temphumidity=readout.humidity.toFixed(1);

	if(low != 20 && high != 60){
		blynk.setProperty(6, "label", "Auto: "+low+"-"+high+" %");
		if(temphumidity <= low){
		trig.writeSync(1);
		ledstatus=1;
		}
		if(temphumidity >= high){
		trig.writeSync(0);
		ledstatus=0;
		}
	}else{
	if(low == 20){
		blynk.setProperty(6, "label", "off");
		trig.writeSync(0);
		ledstatus=0;
		}
	if(high == 60){
		blynk.setProperty(6, "label", "on");
		trig.writeSync(1);
		ledstatus=1;
		}
	}
led.turnOn();
ledupdate();
}, 2000);

Please ignore the funky variable math as I have run into problems converting returned values into integers…

so maybe you just need to handle “error” event of Blynk… as usual with node.js?

I occasionally get same error… sometimes not for hours, but usually not for days…

Even after my favorite Googling routine, I haven’t quite figured out how to ‘catch’ this error as it doesn’t seem related to any of my existing controls, so I don’t know where to put the usual error routines that I do have (and don’t understand anyhow :blush:) …

if (err) {
        console.log("\n" + stderr);
}

And, until I saw this post I was unsure if it was even Blynk related anyhow.

Can you suggest a suitable error routine, or perhaps suggest a good link to learn such mystical arts :stuck_out_tongue:

1 Like

https://nodejs.org/api/events.html#events_error_events

“When an error occurs within an EventEmitter instance, the typical action is for an ‘error’ event to be emitted. These are treated as special cases within Node.js.
If an EventEmitter does not have at least one listener registered for the ‘error’ event, and an ‘error’ event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits.”

=>

blynk.on('error', (err) => {
  console.error('whoops! there was an error');
});
1 Like

Thanks for that piece of code. The simple javascript I wrote for the web quite a while back never threw errors.
I just tried the code. The result:

around 10 repeats of:

whoops! there was an error
Error undefined

then…

-Connecting to: ‘blynk-cloud’.com 8441
-SSL authorization…
-Connected
-Authorized

then…

Connecting to: ‘blynk-cloud’.com 8441
Connecting to: ‘blynk-cloud’.com 8441
Connecting to: ‘blynk-cloud’.com 8441
Connecting to: ‘blynk-cloud’.com 8441
SSL authorization…
SSL authorization…
SSL authorization…
SSL authorization…
SSL not authorized
SSL not authorized
SSL not authorized
Connected

That sequence (less the undefined errors) repeats over and over…

I know SSL can cause connection issues with Arduino and ESP… so try it without the SSL… port 8442

Thanks gunner. Found one of your old posts on how to modify nodejs blynk startup. Changed:

var blynk = new blynkLib.Blynk(AUTH);

to:

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

same sequence of events…except now it indicates port 8442. :confused:

Yes, that is the ‘Normal’ port for basic connection… the SSL one is 8441, but as stated SSL adds that heavier encryption and that can often cause connection issues.

So… even without SSL it keeps disconnecting?

Is your RPi connected to your router via Ethernet or WiFi?

DHCP or Static?

And not sure if it matters, but what model of RPi is it?

Since I am at a loss at to what is happening here… I have thought of a workaround to the problem:

I’ll catch the error and log it to a file with the npm package: fs-error-logger and let the script crash. Once that file is created/modified… npm package: restart will restart the script. This should also give indication as to how often this error occurs. I will have to modify the code to read back the slider value in the app but that should not be a problem. Will play around with the code and see what happens…

So… no further info for us to use to assist in the troubleshooting?

Sorry about that…
Yes, even with the TCP connection the same error sequence occurs. The hardware is a raspberry pi zero w (connected via wifi) it has a fixed IP address. Regarding the WAN IP… its dynamic I believe. My LAN IP does not change.

Well, I am a bit confused about your issue now… is it still the ECONNRESET error that stops your script or the SSL not authorized error?

I have added in the error capture that @vshymanskyy supplied, and now I can disconnect my RPi3 (Ethernet connection) and after about 15 seconds reconnect, then a few seconds later (once VNC link reconnects) I see this on my CLI and the script resumes :smiley: (I did it twice in a row)… so that should work for you as well.

pi@RPi3:~ $ sudo /etc/rc.local
My IP address is 10.10.3.204 fd00:xxxxxxxxxxxxxxx:5109 
OnOff mode
Connecting to TCP: 10.10.3.13 8442
Connected
Authorized
whoops! there was an error
Error ECONNRESET
Connecting to TCP: 10.10.3.13 8442
Connected
Authorized
whoops! there was an error
Error ECONNRESET
Connecting to TCP: 10.10.3.13 8442
Connected
Authorized

Of course now that I think about it, the ECONNRESET error makes sense… my network is heavily loaded, so prior to the error capture, whenever the network burped, my script crashed.

Wow. So now this problem gets even more puzzling. Before I continue I would like to say Gunner that I am glad my problem solved yours! :grinning: Also, the errors I receive back are undefined unlike your errors which have ECONNRESET indicated.
So…I reverted back to the ssl connection. And pulled the ethernet plug. This time I let the errors just run instead of stopping the script. Eventually this message occurred every so often:

Disconnect blynk
REARMING DISCONNECT

Now at this point I stopped the script. I immediately reran the script (with ethernet plugged in)…blynk connected. I pulled the ethernet cable again and then plugged back in a few seconds later. This time these messages were thrown and the script continued?!..

Disconnect blynk
REARMING DISCONNECT
Humidity:     27.1 %
Temperature:  75.7 F
Humidity:     27.1 %
Temperature:  75.7 F
Connecting to: blynk-cloud.com 8441
Humidity:     27.1 %
Temperature:  75.7 F
{ Error: connect ENETUNREACH 10.0.0.1:8441
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
  code: 'ENETUNREACH',
  errno: 'ENETUNREACH',
  syscall: 'connect',
  address: '10.0.0.1',
  port: 8441 }
Humidity:     27.1 %
Temperature:  75.7 F
Humidity:     27.0 %
Temperature:  75.7 F
Humidity:     27.0 %
Temperature:  75.7 F
Humidity:     27.0 %
Temperature:  75.7 F
Connecting to: blynk-cloud.com 8441
SSL authorization...
Connected
Authorized
Humidity:     27.0 %
Temperature:  75.7 F
Humidity:     27.0 %
Temperature:  75.7 F
Humidity:     27.0 %
Temperature:  75.7 F
...continued without issue and responded to all app input

Any comments on this mystery??? One thing that I am thinking might be causing some issue is the fact that the raspberry pi zero will not generally accept “globally” installed modules. Each one has to be placed in every project’s node_modules directory…
Just a thought though…

I think I’ll have to re-check this scenario…

Me too… even though I wasn’t aware that it was a problem until I saw yours…

But your error was defined, and exact wording as mine. See next quote from your OP:

meanwhile…

This one is also defined (telling you that your connection was unreachable… probably since you unplugged it :stuck_out_tongue_winking_eye: ), but it didn’t stop the script since it was ‘caught’ (if I understand the terminology correctly) or perhaps it is just not an error that needs to always crash the script, or the disconnection didn’t last long enough to cause a crash… guesses is all I gots :wink:

Gunner…yes I should have been more clear. Before I used vhymanskyy’s error handler the error was defined as well as when it was displayed in the console. When I used the error handler they were listed as undefined.

A further piece to the puzzle…

I timed how long it took for my script to receive the errors.

Sequence 1:
-Ran script- connected
-Unplugged ethernet
-At 30 seconds the errors started…45 seconds after that the Disconnect blynk - REARMING DISCONNECT messages were displayed and repeated at random intervals
-The script was stopped, ethernet reconnected and script restarted
-Pulled ethernet cable for one minute
-Plugged ethernet back in…same initial errors (undefined) and the script would not reconnect

Sequence 2:
Identical to Sequence 1 EXCEPT the ethernet was pulled for 5 seconds and reconnected…

There were no errors and the script ran as if nothing ever happened.

So this problem pretty much has to do with the REARMING DISCONNECT as well as how long the internet connection is lost… correct?

:face_with_raised_eyebrow:

Can’t honestly say for sure… still learning js as I go, as it is :stuck_out_tongue_winking_eye: But I suspect it is some form of RPi heartbeat allowance… much like Blynks (in Arduino/ESP) in which you can have small breaks in the communication link, but exceed the heartbeat and all :imp: breaks loose, disconnections happen and many, many, much red “issues and Errors…” forum topics start popping up :stuck_out_tongue_closed_eyes:

Posting and resolving the connection errors is understandable however. If a project cannot remain connected its basically useless. Also some projects must remain connected otherwise very bad things can happen if for example someone is controlling a potentially dangerous device such as a heater, etc. :fire::fire::fire:

No fear, no humorous comments were directed AT you… just a reality of the forum.

As for anyone hacking together potentially dangerous appliances and buggy control mechanisms… while unskilled in full operational understanding of either or both… well… More power to them!!, cuz that’s Making!! :smiley: (disclaimer - do not do dangerous things! :wink: )