├── .gitignore ├── push-button-led ├── design.fzz ├── design_bb.png ├── design_schem.png ├── index.js ├── package.json └── readme.md ├── readme.md └── rpinode-setup.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/.DS_Store -------------------------------------------------------------------------------- /push-button-led/design.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewconnell/nodepi-playground/b4c976a68cde1232ce6eb6657a6b0ac0cdda4bdd/push-button-led/design.fzz -------------------------------------------------------------------------------- /push-button-led/design_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewconnell/nodepi-playground/b4c976a68cde1232ce6eb6657a6b0ac0cdda4bdd/push-button-led/design_bb.png -------------------------------------------------------------------------------- /push-button-led/design_schem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewconnell/nodepi-playground/b4c976a68cde1232ce6eb6657a6b0ac0cdda4bdd/push-button-led/design_schem.png -------------------------------------------------------------------------------- /push-button-led/index.js: -------------------------------------------------------------------------------- 1 | var GPIO = require('onoff').Gpio, 2 | led = new GPIO(18,'out'), 3 | button = new GPIO(17,'in','both'); 4 | 5 | function light(err,state){ 6 | console.log('.. light()'); 7 | console.log('.. state = ' +state); 8 | 9 | if(state == 1){ 10 | led.writeSync(1); 11 | } else { 12 | led.writeSync(0); 13 | } 14 | } 15 | 16 | console.log('onoff running'); 17 | 18 | button.watch(light); 19 | -------------------------------------------------------------------------------- /push-button-led/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "onofftest", 3 | "version": "0.1.0", 4 | "description": "demonstrates node controlling two GPIO powering LED & pushbutton", 5 | "main": "index.js", 6 | "author": "Andrew Connell ", 7 | "license": "ISC", 8 | "dependencies": { 9 | "onoff": "^1.0.2" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /push-button-led/readme.md: -------------------------------------------------------------------------------- 1 | push-button-led 2 | =============== 3 | Simple demonstration of getting Node.js working on the Raspberry Pi B. It uses the GPIO ports to accept input from a push button & writes to a LED. 4 | 5 | This uses the [onoff](https://www.npmjs.com/package/onoff) package so make sure to run `npm install` after downloading. 6 | 7 | *Actual designs found in `design.fzz`* 8 | 9 | Breadboard Layout 10 | ----------------- 11 | ![](design_bb.png) 12 | 13 | Schematic Layout 14 | ---------------- 15 | ![](design_schem.png) -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Node.js + Raspberry Pi Playground 2 | ================================= 3 | This repository contains sample projects that demonstrate using Node.js on a Raspberry Pi. 4 | 5 | See [these instructions](rpinode-setup.md) on how to setup a new Raspberry Pi with Node.js. 6 | 7 | push-button-led 8 | --------------- 9 | First project to show it working + working with GPIO ports. -------------------------------------------------------------------------------- /rpinode-setup.md: -------------------------------------------------------------------------------- 1 | Setup Node.js on Raspberry Pi 2 B 2 | ================================= 3 | *These were based & inspired from this article series on [AdaFruit](http://www.adafruit.com): [Why Node.js](https://learn.adafruit.com/node-embedded-development/why-node-dot-js).* 4 | 5 | The Raspberry Pi can have a keyboard & mouse connected via USB, a hardwired ethernet cable & HDMI cable to a monitor for everything to work... well and power. However I'd much rather work with it in a "headless" mode where all I plug in is a WiFi USB adapter & power. Then once it boots up (which is quite fast) and connected to the network (which is automatic if it sees your network from a previous configuration), you connect via SSH on the command line to do all work... so I'll show that here. 6 | 7 | > This whole process took me less than 45 minutes, including coding up the first project: [push-button-led](push-button-led) and left me a bit excited: https://www.youtube.com/watch?v=xl2VqNwbqw8. 8 | 9 | I used the [Raspberry Pi 2 Model B](http://www.raspberrypi.org/products/raspberry-pi-2-model-b/) for this. 10 | 11 | Run Through Initial Config of Raspberry Pi 12 | ------------------------------------------ 13 | No need to repeat this... see [this to get going](http://www.raspberrypi.org/help/quick-start-guide/). I specifically used the Raspbian OS, specifically **Debian Wheezy** that you can get form [Raspberry Pi Downloads](http://www.raspberrypi.org/downloads/). 14 | 15 | Run through the setup... it's quick once you have the OS on the memory card. I did all this with a computer setup (USB mouse, keyboard, HDMI cable to external monitor). 16 | 17 | ### Setup WiFi 18 | I used the [Edimax EW-7811Un 150Mbps 11n WiFi USB Adapter](http://www.amazon.com/gp/product/B003MTTJOY/ref=oh_aui_detailpage_o00_s01?ie=UTF8&psc=1). Once you plug it in, within the Raspbian GUI (get to it from a command line by running `startx`) open the WiFi settings. You have to manually find your network SSID, select it and enter the password. Then reboot the Pi... (from the terminal: `sudo reboot`)... when it reboots you should be online. 19 | 20 | To be safe, grab the IP of the Pi. Now you can go headless. 21 | 22 | Setup Host Computer 23 | ------------------- 24 | First, make sure your host (laptop) is setup. 25 | 26 | ### Get Network Scanner to Find the Pi 27 | You'll need to find your Pi on your network if its headless and you don't know it's IP. I used [nmap](https://en.wikipedia.org/wiki/Nmap). On MacOS this is available via homebrew: 28 | 29 | ```` 30 | $ brew install nmap 31 | ```` 32 | 33 | Find your Raspberry Pi 34 | ---------------------- 35 | To use it, from the command line, tell it to scan your network for machines with port 22 open (the SSH port): 36 | 37 | ```` 38 | $ nmap -p 22 --open -sV 192.168.0.* -Pn 39 | ```` 40 | 41 | > The above says *scan my network, specifically all IPs on 192.168.0.any that have port 22 open.* The last argument isn't required but I had to do it. 42 | 43 | You are looking for a response that includes **OpenSSH 6.0p1 Debian**... that's Raspbian Wheezy so it's likely our Pi. Near that line, a few lines above it, it should say the actual IP it was scanning when it got that. Here's what mine said: 44 | 45 | ```` 46 | $ nmap -p 22 --open -sV 192.168.0.* -Pn 47 | 48 | Starting Nmap 6.47 ( http://nmap.org ) at 2015-02-28 09:47 EST 49 | Nmap scan report for 192.168.0.104 50 | Host is up (0.0056s latency). 51 | PORT STATE SERVICE VERSION 52 | 22/tcp open ssh OpenSSH 6.0p1 Debian 4+deb7u2 (protocol 2.0) 53 | Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel 54 | 55 | Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . 56 | Nmap done: 255 IP addresses (3 hosts up) scanned in 3.39 seconds 57 | ```` 58 | 59 | Now you know your IP... mine is **192.168.0.104**. 60 | 61 | Connect to the Raspberry Pi 62 | --------------------------- 63 | Now that you know the IP of your Pi, connect to it using SSH. There are clients you can use (on Windows, get [PuTTY](http://www.putty.org/)), but I'm doing it via terminal on MacOS so I can just do the following: 64 | 65 | ```` 66 | $ ssh pi@192.168.0.104 67 | ```` 68 | 69 | > The above says *connect to 192.168.0.104 over SSH, which is port 22, and try to login as the user **pi** *. 70 | 71 | You'll be prompted to login, so enter your password. 72 | 73 | You should now be logged in as the prompt should look something like: 74 | 75 | ```` 76 | pi@raspberry ~ $ 77 | ```` 78 | 79 | Update the Raspberry Pi 80 | ----- 81 | Before installing Node.js, make sure the Pi is current by running these two commands: 82 | 83 | ```` 84 | pi@raspberry ~ $ sudo apt-get update 85 | pi@raspberry ~ $ sudo apt-get upgrade 86 | ```` 87 | 88 | Install Node.js 89 | ---- 90 | Now get the latest stable node.js package available from the node-arm site: 91 | 92 | ```` 93 | pi@raspberry ~ $ wget http://node-arm.herokuapp.com/node_latest_armhf.deb 94 | ```` 95 | 96 | After downloading the package, install it: 97 | 98 | ```` 99 | pi@raspberry ~ $ sudo dpkg -i node_latest_armhf.deb 100 | ```` 101 | 102 | This takes a while so be patient... once it finishes, verify it's installed: 103 | 104 | ```` 105 | pi@raspberry ~ $ node -v 106 | ```` 107 | 108 | Now that Node.js is installed you're done... unless you want to work with the GPIO ports on the device... and I did! 109 | 110 | Enable GPIO For the Pi User 111 | ---- 112 | You MAY not need this, but I did just to be safe... 113 | 114 | Get the [GPIO Admin](https://github.com/quick2wire/quick2wire-gpio-admin): 115 | 116 | ```` 117 | pi@raspberry ~ $ git clone git://github.com/quick2wire/quick2wire-gpio-admin.git ~/gpio-admin && cd ~/gpio-admin 118 | ```` 119 | 120 | Once you downloaded the source, build it: 121 | 122 | ```` 123 | pi@raspberry ~ $ make 124 | ```` 125 | 126 | > You should already be in the `gpio-admin` folder after downloading it with `git`... if not you need to be before running the `make` command above. 127 | 128 | Now install what you just built: 129 | 130 | ```` 131 | pi@raspberry ~ $ sudo make install 132 | ```` 133 | 134 | And finally, grant the user **pi** to the GPIO ports... 135 | 136 | ```` 137 | pi@raspberry ~ $ sudo adduser pi gpio 138 | ```` 139 | 140 | And then, refresh the experience by either rebooting (`sudo reboot`) or reload the terminal shell (`exec su -l pi`). 141 | 142 | All done! --------------------------------------------------------------------------------