Introduction: JavaScript for IoT: Controlling a Button on Raspberry Pi Via Node.js

This tutorial explains how to mount a "push to make" button on a breadboard, to connected it to Raspberry Pi and to control it with JavaScript and Node.js. The whole example is open source and it is available at GitHub under the MIT licence. The algorithm is suitable for a reset button on do it yourself (DIY) embedded devices. With minor modifications it can be also adapted for other features.

This tutorial is appropriate for developers, makers and hobbyists. No knowledge of JavaScript is needed to run the example. No prior course of algorithms is required.

JavaScript appeared in 1995. For years it was a language used only by front-end web developers. In 2009 Node.js, an amazing new technology, appeared and converted JavaScript into a general-purpose programming language. Nowadays JavaScript is useful even for embedded devices. Although JavaScript is not as good as the C programming language in terms of performance, it is a very convenient tool for rapid prototyping for Internet of Things. Follow the guidelines in this tutorial to learn how to use JavaScript and Node.js on the most popular single board computer Raspberry Pi and how to handle a physical button.

I highly recommend you the video version of this Instructable. If the embedded video does not appear on your mobile device, here is an alternative link. If you like this tutorial, please subscribe to my YouTube channel and have a look at my Instructable for a blinking LED on Raspberry Pi with JavaScript.

Step 1: Getting Ready

For this tutorial you need the following hardware components:

  • Raspberry Pi (any model or version)
  • Breadboard
  • "Make to push" button (if not sure please have a look at the video for details)
  • Male to female jumper wires (x2)
  • microSD card with Raspian GNU/Linux distribution
  • Power supply

There are different types of push buttons depending on their mechanism. I am using a “push to make” button. It is similar to a button of keyboard. When the button is pressed it makes a contact with the electronic system and breaks the current process when the button is released. You can see the difference between buttons in the video.

Step 2: Wiring

Connect the button to ground and to a GPIO pin 7 of Raspberry Pi. After plug the microSD card with the Raspbian GNU/Linux distribution, make sure the board is connected to the Internet, plug the power supply and turn on the board.

Step 3: Setting Up Development Environment

Make sure that Raspbian GNU/Linux distribution is running on your Raspberry Pi. Log in though SSH or just open a terminal application on the Pi itself. After that execute the following command to install Node.js, npm (Node Package Manager) and Git:

sudo apt-get install -y nodejs npm git

Step 4: Installing the Example

Get the open source example and install its dependencies using npm:

git clone https://github.com/leon-anavi/rpi-nodejs-examples.git
cd rpi-nodejs-examples
npm install

Step 5: Handling the Button

Execute the following command to handle button events:

 sudo nodejs button/button.js.

This JavaScript example implements an algorithm suitable for a reset button. You have to keep the button pressed for 3 seconds to see an output in the command line on your Raspberry Pi. Nothing will be displayed if you press the button for less than 3 seconds.

Please have a look at the video for more details.

Step 6: How Does It Work?

The source code is available at GitHub and in the attached zip archive. JavaScript file button.js is executed by Node.js and its dependencies are installed by npm depending on the configuration from package.json. This example relies on package wiring-pi which provide JavaScript binding to the popular open source C library wiringpi.

Node.js is very convenient for asynchronous events. My JavaScript handles all interrupts when the button is pressed or released. On press the JavaScript starts a timer for 3 seconds. It prints OK in the command line only if the button is not released before the expiration of the timer.

Comments

author
lorenzob26 made it!(author)2016-12-17

Thanks, that's a small hello world that fits my need :) (mp3 player just for keeping things apart :D)

IMG_20161217_145133.jpg
author
lorenzob26 made it!(author)2016-12-17

sorry...i tried to make it:
got this error when tried to run the script

module.js:356

Module._extensions[extension](this, filename);

^

Error: /home/pi/rpi-nodejs-examples/node_modules/wiring-pi/build/Release/wiringPi.node: undefined symbol: node_module_register

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Module.require (module.js:364:17)

at require (module.js:380:17)

at Object.<anonymous> (/home/pi/rpi-nodejs-examples/node_modules/wiring-pi/lib/exports.js:1:80)

at Module._compile (module.js:456:26)

at Object.Module._extensions..js (module.js:474:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Module.require (module.js:364:17)

About This Instructable

4,418views

24favorites

License:

Bio: Software engineer and open source enthusiast
More by leon-anavi:Transforming Raspberry Pi Into a Remote ControlPlaying OpenArena on PocketC.H.I.P.Retro Arcade Gaming on PocketC.H.I.P.
Add instructable to: