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

(Need assistance with JS error checking/trapping) Blynk on RPi occasional TCP timeout errors, must restart client

Hello! I’m successfully using Blynk with a HTU21D-F temperature and humidity sensor that’s connected to my Arduino Uno via USB to a Raspberry Pi 3 B.

I can review the historical data, check out real-time monitoring, and overall the app is totally awesome!!!

The problem is that typically after a week or so, when I open the Blynk app, it says there’s no data to display. When I remote to the Pi and look to see what’s going on, I see the following:

Connecting: FILE:/dev/ttyACM0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> openssl-connect:blynk-cloud.com:8441,cafile=/home/pi/sketchbook/libraries/blynk/scripts/certs/server.crt,nodelay
2017/05/07 09:15:29 socat[30739] N opening character device "/dev/ttyACM0" for reading and writing
2017/05/07 09:15:29 socat[30739] N opening connection to AF=2 45.55.96.146:8441
2017/05/07 09:15:29 socat[30739] N successfully connected from local address AF=2 192.168.1.13:57470
2017/05/07 09:15:29 socat[30739] N SSL connection using ECDHE-RSA-AES128-GCM-SHA256
2017/05/07 09:15:29 socat[30739] N SSL connection compression "none"
2017/05/07 09:15:29 socat[30739] N SSL connection expansion "none"
2017/05/07 09:15:29 socat[30739] N starting data transfer loop with FDs [5,5] and [6,6]
2017/05/07 12:08:39 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N socket 2 (fd 6) is at EOF
2017/05/07 12:08:40 socat[30739] N exiting with status 0
Reconnecting in 3s...
Connecting: FILE:/dev/ttyACM0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> openssl-connect:blynk-cloud.com:8441,cafile=/home/pi/sketchbook/libraries/blynk/scripts/certs/server.crt,nodelay
2017/05/07 12:08:43 socat[31329] N opening character device "/dev/ttyACM0" for reading and writing

Interestingly, when I try to control-C the terminal window, it won’t respond. I looked at the processes running on the pi via the ‘top’ command but I don’t see the blynk server process; when I try to restart the blynk server, I see on the app that it still is not posting data despite the ‘starting data transfer loop’ and normal messages that I typically see when running blynk server. However, when I restart the pi and run the blynk server again, it posts the data and everything works fine.

Can anyone help? Thanks so much in advance!

I might be TOTALLY off like i often am but you could try to install the “forever” library and run your program with sudo forever start /path. I had a similar problem but did not have the same response that you are getting so i might be wrong. But this fixed my issue

There is no “correct” way to fix it. As USB solution was designed just for demo. The simplest way would be to make some script that kills socat connection and opens it again periodically.

Dema, thanks for the input!

Dmitriy, interesting, actually I noticed the socat process via the ‘top’ command and killed it but blynk still wouldn’t connect when I ran blynk server again. I have to restart the pi. I’m thinking there must be additional processes that I need to kill in addition to the socat PID? Also, how would you recommend I set this up, via crontab, perhaps like once every 3 days restart the process(es)?

Also, I’m not sure what the ‘forever’ library is, but would this help my issue?

Thanks again!

Don’t use the USB script at all… the RPi-3-model-B has ethernet, so use that instead. Much more stable.

Gunner, so you’re saying hook up the sensor directly to the Pi instead of the Arduino (which is currently connected to the Pi via USB)?

I think I had misunderstood how you are hooking everything up :blush:

If you are running the RPi as a Local Server, then I guess you can use the USB link (as running off a linux box) to connect the UNO to the RPi, but now you need to have both the Server process and the USB script process auto-starting in case of disconnection.

However, if you are going through all that trouble on the RPI, then yes, just take it a step further and install Blynk Client on the RPI as well, and hook your sensors up directly to the RPi.

http://help.blynk.cc/hardware-and-libraries/node-js/how-to-install-nodejs-library-on-linux

I see. I think I’ll take that route, then. Just to be sure, this would be a more stable configuration, yes?

1 Like

Great advice a few weeks ago, and I got the sensor working with Blynk exclusively on the Pi without the Arduino at all. But now, a similar issue! Help, anyone…??

I reconfigured the sensor to connect directly to the Pi, and the script works great, but unfortunately after day three I got the following error. Not sure why, the pi is hard wired via ethernet cable to the internet and there were no outages during the error.

Disconnect blynk
REARMING DISCONNECT
Connecting to TCP: blynk-cloud.com 8442
Connecting to TCP: blynk-cloud.com 8442
events.js:168
      throw err;
      ^

Error: Uncaught, unspecified "error" event. (EHOSTUNREACH)
  at Blynk.emit (events.js:166:17)
  at Blynk.error (/home/pi/node_modules/blynk-library/blynk.js:590:8)
  at exports.TcpClient.<anonymous> (/home/pi/node_modules/blynk-library/blynk.js:553:48)
  at emitOne (events.js:96:13)
  at exports.TcpClient.emit (events.js:191:7)
  at Socket.<anonymous> (/home/pi/node_modules/blynk-library/blynk-node.js:59:14)
  at emitOne (events.js:96:13)
  at Socket.emit (events.js:191:7)
  at emitErrorNT (net.js:1279:8)
  at _combinedTickCallback (internal/process/next_tick.js:80:11)
  at process._tickCallback (internal/process/next_tick.js:104:9)

It appears at this point it’s a JavaScript issue. Unfortunately although I was able to program in the sensor libraries and code, I don’t know enough to throw in something for error handling. Here’s the JavaScript code for the sensor that works fine (until, of course, the error above made it stop). Can anyone help?

var i2c_htu21d = require('htu21d-i2c');
var htu21df = new i2c_htu21d();
var Blynk = require('blynk-library');
var AUTH = 'my auth code';
var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});


setInterval(function() {
htu21df.readTemperature(function (temp) {
    blynk.virtualWrite(3, (temp * 1.8 + 32));
    htu21df.readHumidity(function (humidity) {
        blynk.virtualWrite(4, humidity);
    });
}); 
}, 5000);

Unfortunately the same tcp error occurs nearly daily now. I think a simple error handling could fix this…? Can anyone help?

I have a line in my sample RPI script that contains some form of error catch… but I haven’t invested any time into really figuring out how it works.

For that I would suggest Googling something like “javascript error handling”.

This looks for physical button press and reacts accordingly with Widget LED’s

button.watch(function (err, value) {  // Watches for button press and assigns 0/1 to value

  if (err) {
    throw err;  // Some form of error handling that may not actually work as I don't know what happens next.
  }

led.writeSync(value);  // Sends value (0/1) to Physical LED
if (value  == 0) {
        blynk.virtualWrite(2, 0);  // V2 Widget (BLUE) LED off
	blynk.virtualWrite(3, 1023);  // V3 Widget (RED) LED on
    } else if (value == 1) {
                blynk.virtualWrite(2, 1023);  // V2 (BLUE) Widget LED on
		blynk.virtualWrite(3, 0);  // V3 (RED) Widget LED off
        }
});

Forgive my ignorance, I’m completely ignorant of javascript.

Is the if (err) function the place this will work? I see a button watch function is also using it. I only have a read-only sensor, so I just don’t know how to incorporate that code into my simple sensor script.

I did google javascript error handling, but it seems at first glance I’m in over my head with this and Blynk integration.

Can anyone else help or provide some direction on how to just sleep and loop if it gets one of those rare host unreachable errors?

Unfortunately I’ve searched and searched and just can’t find any definitive resources on how to handle this error since it’s calling a Blynk-specific script. All I need it to do is try again after 5 or 10 seconds. As it stands, the same error simply forces the program to quit every few days.

Connecting to TCP: blynk-cloud.com 8442
events.js:168
      throw err;
      ^
Error: Uncaught, unspecified "error" event. (EHOSTUNREACH)
  at Blynk.emit (events.js:166:17)

Anyone know how to code in a simple loop to try again in a few seconds upon seeing this error, instead of the program quitting? To reiterate, here’s my simple script:

var i2c_htu21d = require('htu21d-i2c');
var htu21df = new i2c_htu21d();
var Blynk = require('blynk-library');
var AUTH = 'my auth code';
var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});


setInterval(function() {
htu21df.readTemperature(function (temp) {
    blynk.virtualWrite(3, (temp * 1.8 + 32));
    htu21df.readHumidity(function (humidity) {
        blynk.virtualWrite(4, humidity);
    });
}); 
}, 5000);

Near as I can tell, these error handling issues are not really Blynk specific, rather Javascript specific.

I have Googled “javascript error handling” and started slowly skimming through some of the topics… but admittedly it is all a bit overwhelming for me right now… must wait for clear headed moments :stuck_out_tongue_winking_eye: https://www.google.ca/#q=javascript+error+handling

Haha exactly!! I know vba, php, sql, python, and some c, but nothing about Javascript and the amount of material on Google about Javascript error handling is overwhelming lol.

I’ve been reading up and learning basic JavaScript just to solve this issue but it still appears that I have not. Anyone else perhaps that can help with this basic error handling?

The “RPI using Blynk as a client” community seems a little low in percentage here and this JS error trapping issue is not anywhere near Blynk relivent, so it is unlikely we will find the magic bullet point that answers this question here.

But there was another JavaScript based topic that ended up with some more example sketches… including the requisite error trapping routines (and there seems to be a few ways of doing that)… but anyhow, more data to dig through is better than less :wink:

I broke out a couple of routines that have some form of error checking/trapping… and commented my best guesses as to what the steps are doing… I am basically just banging sticks together to see what happens :stuck_out_tongue_winking_eye:

// "run the command ifconfig"
v5.on('write', function(param) {  		// Watches for V5 Button

	console.log('V5:', param[0]);  		// prints value to CLI
		if (param == 1) {				// Runs the CLI command ifconfig if the button on V5 is pressed
			process.exec('ifconfig',function (err,stdout,stderr) {  // Including error check
    				if (err) {  // If error returns in data...
        			console.log("\n"+stderr);  // ...show something in log file?
    				} else {
        			console.log(stdout);  // ...else close log file?
    				}
			})
		}
});
//turn on ecasound (analog input) and turn off others
v3.on('write', function(param) {
child = exec('sudo systemctl stop shairport-sync ;\
	sudo /etc/init.d/squeezelite stop ;\
	ecasound -C -q -B:rt -b:1024 -f:16,2,48000 -i:alsahw,1,0',
    function (error, stdout, stderr) { // Including error check
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {   // If error returns in data...
             console.log('exec error: ' + error);  // ...show something in in log file?
        }
    });

Gunner, once again I appreciate the info! The examples definitely help.

If anyone else has experienced something similar or has some more specific code please feel free to share!

No problem… I am in it to learn this as well. My RPi testbed sketch stops running after a few hours/days and I also suspect it is due to poor error checking/trapping as I add functions to it.

In fact, recent attempts to add some code had my RPi bork itself due to unrelated berryboot issues… so yesterday I completely wiped it out and reloaded just Raspbian and rebuilt Blynk Client… so the only thing left that could be at fault is my code :wink: