Setup Raspberry Pi as a web server Part: II

In my last post I introduced the world to my proof of concept for The Standard Code Art Project, and sent you in the right direction to getting an OS (Raspbian) loaded on the Raspberry Pi so that you could boot it up. Once you have Raspbian running, the next steps will be a breeze.

The next step is actually two steps, but because we are using Raspbian as our OS, the heavy lifting has already been done, so I am going to cover both of them today. We need to download and install some software to get our development environment ready to go, but first we can configure the Raspberry Pi to connect to the internet via WiFi.

Now, the Raspberry Pi has an ethernet port, so technically you don’t need to have the Wifi configured, just plug it in and you’ll be connected. However, I found it valuable to have my Raspberry Pi using WiFi because it was really inconvenient to limit my physical workspace. I only had a 5’ ethernet cable on hand, so I had to stay within that range of my router. WiFi fixed that, and I was then able to work wherever I wanted (within the range of my wireless router).

If you are using a Raspberry Pi 3, you’re in luck because this model comes with a modem built into the processor chip, so you don’t need to buy any additional hardware. You can follow this tutorial on configuring your Wifi setup . I built the proof of concept using a Raspberry Pi 2, so that configuration is what I will focus on here. First you need to get a USB WiFi adapter. A good place to look is Amazon or Adafruit. Plug the adapter into the Pi and power it up, and because we are using Raspbian, the Pi will automatically detect the adapter during the boot process. No drivers to install. No network interfaces to configure. The only configuration we will have to do is setting up our credentials to join the network. This is really simple.

You will have to access the command line of the Raspberry Pi somehow. For the sake of this post I will recommend connecting a keyboard to one of the usb ports and use the HDMI to connect the Pi to your TV, that’s what I did. With these connected, power up the Raspberry PI. After it boots, you will be at the Raspbian Desktop GUI. Use the shortcut Alt + Ctrl + F1 to open the terminal. Now that you are in the command line, we need to edit the file that will store the network credentials. You can use the editor included with Raspbian, Nano.

nano /etc/wpa_supplicant/wpa_supplicant.conf

Append the following to the bottom of this file.

ssid=”your network name”
psk=”your network password”

Save the file exit, and reboot the Pi.

sudo reboot

When the Raspberry Pi reboots, it will again automatically detect the wifi adapter, and also automatically load the network credentials you just entered. If all goes accordingly, you Raspberry Pi will now be connected to your network via WiFi. Next step will be to use that connection to download some software so we can start writing code

For this project I built a tiny web server using Node Js. This is the tutorial I followed, step by step, to install node so I recommend doing the same. If you want to use another method, feel free, but using the Adafruit package repository, as explained in the tutorial, will guarantee that you are installing the latest, most supported version of node available for the Raspberry Pi.

Now that you have node, you can start writing some sweet code. You can use nano as your editor and write your code directly on the Pi. I would rather write code on my laptop though, using my prefered editor. This is a great option, and the fact that Raspbian comes with git installed, it’s as simple as setting up a remote repository (I used GitHub), pushing code from your laptop, and then pulling that code locally to the Raspberry Pi. I found this to be a great workflow, but it’s up to you. In my next post I’ll dive into the development of the web server that we will build, and demonstrate how we can use the code to control the GPIO of the Raspberry Pi to control the LED’s.

Jul. 22 2016