3

I have a Raspberry Pi set up with a Node.js app that responds when it sees button push from an Amazon Dash Button. It was originally supposed to be a silent doorbell from https://github.com/initialstate/silent-doorbell, but I would like to just have it play a local sound file. Which I think should be easy enough, but my inexperience with coding leaves me just trying new stuff that I find all over the internet.

I can play the file from the terminal with the following and it plays just fine:

$ omxplayer example.mp3

But, no matter how I try to put it in the Node.js app and trigger when the Dash Button is pressed it won't work.

var dash_button = require('node-dash-button'),
    dash = dash_button('XX:XX:XX:XX:XX:XX'), //REPLACE WITH YOUR ADDRESS
    exec = require('child_process').exec;
    Omx = require('node-omxplayer');
    player = Omx('~/node_modules/node-dash-button/example.mp3');

let spawn = require('child_process').spawn;

dash.on('detected', function() {
    console.log('Button pushed!');
    player.play();
});

When run with my latest as above, I get this:

/home/pi/node_modules/node-dash-button/doorbell.js:7
let spawn = require('child_process').spawn;
    ^^^^^
SyntaxError: Unexpected identifier
    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

After upgrading Node.js to the newest version as suggested by @Quentin using the Major Version Upgrade directions on this site http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/ I was able to get past this. Now I can't get past how to properly use omxplayer. When running the same code as above after the Node.js upgrade I now get this error after pressing the Amazon Dash button which then crashes the app:

pi@raspberrypi:~/node_modules/node-dash-button $ sudo node doorbell.js 
Button pushed!
/home/pi/node_modules/node-omxplayer/index.js:103
                        throw new Error('Player is closed.');
                        ^

Error: Player is closed.
    at writeStdin (/home/pi/node_modules/node-omxplayer/index.js:103:10)
    at EventEmitter.Omx.omxplayer.play (/home/pi/node_modules/node-omxplayer/index.js:133:27)
    at Readable.<anonymous> (/home/pi/node_modules/node-dash-button/doorbell.js:13:12)
    at emitOne (events.js:96:13)
    at Readable.emit (events.js:188:7)
    at PcapSession.<anonymous> (/home/pi/node_modules/node-dash-button/index.js:87:28)
    at emitOne (events.js:96:13)
    at PcapSession.emit (events.js:188:7)
    at PcapSession.on_packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:99:10)
    at packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:44:14)

I tried a few different things to try and get the player to spawn with no luck. The index.js file referenced mentions to use the player.running command but I still get the player is closed error when attempting to use this.

Erik
  • 31
  • 3

1 Answers1

0

You are using a version of Node older than the 4.x series.

Consequently it sees let as an identifier rather than the keyword, so it doesn't expect it to be immediately followed by another identifier (spawn).

Upgrade your installation of Node to a current version.


Alternatively, use a different variable declaration, such as var.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Yes, that was the problem, thanks. I had thought that I was on the latest version available for Raspberry Pi. After a little research I realized I had to force it. So using this site http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/ under the Major Version Upgrade I was able to get up to the newest version. After that I had to rebuild and my current issue is solved. Now onto why omxplayer is throwing "Error: Player is closed." – Erik Jan 17 '17 at 01:21