Convert Python script to Node

I’m a newbie but I got the following python script from Matt Hawkins to work on raspberry pi. However, I’m having a difficult time trying to convert it to node so I can use Blynk to print the output. I’ve search for examples but I’m still unclear on which libraries to include, computations, and workflow. Can someone point me to a good quick resource to get me started?

The working python script that is working is:

#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# ultrasonic_1.py
# Measure distance using an ultrasonic module
#
# Author : Matt Hawkins
# Date   : 09/01/2013

# Import required Python libraries
import time
import RPi.GPIO as GPIO

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_TRIGGER = 19
GPIO_ECHO = 16

print ("Ultrasonic Measurement")

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
  start = time.time()

while GPIO.input(GPIO_ECHO)==1:
  stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34000

# That was the distance there and back so halve the value
distance = distance / 2

print (distance)

# Reset GPIO settings
GPIO.cleanup()

The following script is attempt to convert the python script. So far, I can’t compile it at the watch function. The error I get is:

pi@raspberrypi:~/Blynk Basic Tests $ node blynk-test-sec-td.js
/home/pi/Blynk Basic Tests/blynk-test-sec-td.js:49
Echo.watch (dist (err, value) {
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions…js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

Can someone tell me what I’m doing wrong.

var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio,
    Trigger = new Gpio(19, 'out'),
    Echo = new Gpio(16, 'in', 'both');
var NanoTimer = require('nanotimer');

// Setup Blynk 
var AUTH = 'cc601101f08142ebb2276492e2a49482';
var blynk = new Blynk.Blynk(AUTH);
var v0 = new blynk.VirtualPin(0); //distance computation
var v1 = new blynk.VirtualPin(1); //button start/stop measuring
var start = new Date();
var stop = new Date();

// Setup Timers
var timerObject = new NanoTimer();

//Setup Traps
function exit() {
    Trigger.unexport();
    Echo.unexport();
    process.exit();
}

// define the callback function
function dist(err, state) {
  // check the state of the echo pin
  // 1 == Signal Return, 0 == No signal received
  if (state == 0) {
        // keep measuring time until state changes
        start.getTime();
    } else {
        // measure time when signal was received
        stop.getTime();
        v0 = (stop - start) * 17000;
    }
}

function StartTrig(err, state) {
  // check the state of the virtual pin
  // 1 == Start, 0 == Stop
  if (state == 1) {
        // Send 10us pulse to trigger
        Trigger.writesync(1);

    }
}

Echo.watch (dist (err, value) {
  if (err) {
    throw err;
  }
  Echo.writeSync(value);
});

process.on('SIGINT', exit);

Okay, so I finally got it to compile. It turns out that the watch command should just reference a function, in this case it is the function dist. However, I am still having problems with the trigger function. Not clear if there is a ultrasonic library or how I should call out the delay; either in a setinterval or settimeout. Any help would be much appreciated. Below is the code that compiles with node so far.

var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio,
    Trigger = new Gpio(19, 'out'),
    Echo = new Gpio(16, 'in', 'both');
var NanoTimer = require('nanotimer');

// Setup Blynk 
var AUTH = 'cc601101f08142ebb2276492e2a49482';
var blynk = new Blynk.Blynk(AUTH);
var v0 = new blynk.VirtualPin(0); //distance computation
var v1 = new blynk.VirtualPin(1); //button start/stop measuring
var start = new Date();
var stop = new Date();

// Setup Timers
var timerObject = new NanoTimer();

//Setup Traps
function exit() {
    Trigger.unexport();
    Echo.unexport();
    process.exit();
}

// define the callback function
function dist(err, state) {
  // check the state of the echo pin
  // 1 == Signal Return, 0 == No signal received
  if (state == 0) {
        // keep measuring time until state changes
        start.getTime();
    } else {
        // measure time when signal was received
        stop.getTime();
        v0 = (stop - start) * 17000;
    }
}

function StartTrig(err, state) {
  // check the state of the virtual pin
  // 1 == Start, 0 == Stop
  if (state == 1) {
        // Send 10us pulse to trigger
        Trigger.writesync(1);

    }
}

Echo.watch(dist);

process.on('SIGINT', exit);