Raspberry Pi as a web server

April 3, 2013 in Tutorial

While I was trying to set up my Raspberry Pi I took the following notes to document my process in hope someone else can benefit from the notes. The idea behind this post will ultimately find it’s way to the Wall-E Project that I will also be blogging about

The overall top level functionality  is to use the Raspberry Pi as a LAMP server by:

  1. Download a Linux distro – Debian for Raspberry Pi:
  2. Download Apache – Webserver open source software.
  3. PHP – Allows further functionality of Apache for websites
  4. MySQL – Provides the needed database support

SOFTWARE TOOLS NEEDED

To accomplish this the following software is needed:

GETTING THE SD CARD READY

Insert the SD card into the Windows based laptop. Make sure you have the Linux Distro extracted and ready to be written to  SD card.

    • Use win32ImageWritter to “burn” the image into the SD card. Make sure card is formatted.

Steps:

Format the SD card. If the card has been previously used then Windows will not recognized the whole card. The boot partition will be seen by Windows. So make sure to collapse the complete SD card, and reformat it with Windows


 

With the card installed pick the drive assigned by Windows to your card. Click on Option and chose the following:

 

This should restore your card to all its original size (if not new). After roughly 15 min you should get the following if using a 4GB SD card:

DISTRO

While the SD card is formatting, download the Raspberry PI Linux distribution. Debian has a lot of support so it is the one in this tutorial.

If you use the torrent bit you will have a faster download speed (the link can change as new distro are released)

 http://downloads.raspberrypi.org/images/raspbian/2012-12-16-wheezy-raspbian/2012-12-16-wheezy-raspbian.zip.torrent

  1. Once downloaded the file, extract the zip file to a location where you can remember. The file you will need is a .imp file (image file).

  2. The .imp file cannot be copied directly into the SD card, as it is an image. You will need software to properly copy the file. Open W32DIskImager program and select the .imp file. Transfer the image to the SD card

  3. Once the image is transferred the SD card is ready to be used in the raspberry Pi

  4. Power up the Rpi. It will start to scroll different messages and the screen will go blue with a menu to chose from:

    1. Click on expand_roofs to utilize the complete SD card

    2. click on keyboard configuration  to change to the  keyboard you have

      1. pick all defaults or make the changes to any  custom setting needed by your system

    3. change the time zone

    4. enable SSH server – This will allow you to connect to the R-pi via  the  network cable

    5. Click (or press TAB) on finish if you believe you download the newest revision of the  distro

  5. The raspberry pi should show now a prompt:

pi@raspberrypi””$

Type:  sudo reboot to restart the r+pi

Change main user Pi to you own

  1. Once the R=pi boots up it will ask you for the username and password. On a new installation they are:

    1. Username: pi

    2. Password: raspberry

  2. Type: passwd

    1. Enter the new password twice

Follow the instructions per: http://www.penguintutor.com/linux/raspberrypi-webserver

Wireless dongle

To enable your wireless dongle, you will need to use the GUI desktop. For this you will need to be using the HDMI or video output from the Pi itself.

  1. Type: startx

  2. This will load the graphical interface of the R-Pi

  3. Go to the start button on the lower right corner (similar to windows), and select the WPA-GUI (under the “internet” heading)

  4. Click on “Scan”, a new window pops up, press scan again and then chose your wireless network

  5. Enter security code under PSK:

  6. Once you are connected, make sure to save the configuration by clicking on File > Save Configuration

  7. Once enabled you will need to remove the suspend time on the wireless when R-Pi goes to sleep

    1. Run the command:

cat /sys/module/8192cu/parameters/rtw_power_mgnt

    1. 0= Power Management disable

    2. 1= Min Power Management

    3. 2= Max power management

To disable this, you need to create a new file:

sudo nano /etc/modprobe.d/8192cu.conf

and add the following:

# Disable power management

options 8192cu rtw_power_mgnt=0

Once you save the file and reboot your RPi, the WiFi should stay on indefinitely. This is assuming that your wireless dongle uses an 8192cu chipset.

It is also a good idea to set up your IP address for static to ensure we always have the same IP address:

Start by typing:

sudo nano /etc/network/interfaces

Make sure to follow the format below to ensure you can connect later fia SSH

  • Python comes pre-installed in wheezy. You can run compile with python from the terminal, so we will create files in Windows PC with Notepad++, transfer the file to Pi to compile and run.

    • The other option is to run starX and use the IDE that comes installed

    • We could also use the terminal but the interface is not suitable for learning.

  • Enable an environment to transfer files to the R-Pi by

    • In the terminal create directory in the R-Pi called Tutorial:  mkdir Tutorial

    • In the windows PC download and install WinSCP

      • Select Session File protocol: SCP

      • Enter Host Name: IP address of R-Pi ( Type ifconfig in R-Pi to find out IP address)

      • Username: Log on user for R-Pi

      • Password: R-Pi log on password

      • Private Key File: Empty

      • Click on save.. so you can load up later easier

      • Click on log in

      • A new pop up window will ask the password one more time.

Once logged in you will have the following screen:

      • Now you can drag and drop files from one side to the other making very easy to edit Python files on the PC and compile them in the R-Pi while still learning the process of how it compiles

Setting Up GPIO

sudo apt-get install python-rpi.gpio

  • make sure to import rpi.gpio in your python code.

Using Bottle

  • Router settings will need to be in place to forward queries that you need.

    • Since your R-Pi has an static IP address per above, then we will use it to make sure that the router will forward request to a port to go to the R-Pi

    • From a web browser, go to the router home page, defaults are 192.168.1.1

    • Log into the router via the web browser

    • Look for port forwarding option or port management. Sometimes is listed under Games

    • Set the IP address of the R-Pi to be forwarded and the port the router should listen to. For example:

  • You can use both TCP and UDP, TCP is just fine if needed.

Bottle allows easy access to a webserver framework. It to allows the control of the serial port via a web server in the future.

  • To install Bottle type the following:

sudo apt-get install python-bottle

  • A starting point is the following code:

Line 1: Tells you where python is located at

Line 3: Inserts bottles functions that we will be using

Line 5: Sets what will trigger the bottle. When you enter in browser: 192.168.5.9:1147/hello

Line 7: Output from raspberry pi to browser

Line 9: Sets up the website IP address, the port which R-Pi will be listening to.

Can HTML be placed on Bottle?

Setting Up the Serial port for serial communication (http://codeandlife.com/2012/07/29/arduino-and-raspberry-pi-serial-communication/)

  • In order to use the serial port in the R-Pi you need to disable the already setup functionality where the R-pi transmits a terminal prompt upon power up. You will also need to download the serial libraries to gain access to python

type:

sudo nano /etc/inittab

Towards the end of the file find the getty command for the ttyAMA0 serial port:

Make sure to save by CTRL+X and then “Y”

  • Type the following: sudo nano /boot/cmdline.txt

    • A single line file will open, look for and remove:  console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

    • This stops R-pi from sending serial messages via the serial port during power up

  • To help trouble shooting and have a serial terminal in general you will need to install minicom

Type: sudo apt-get install minicom

  • Reboot R-pi to save all the settings.

  • Let’s use minicom to ensure that our raspberry pi can transmit and read what is transmitting by setting a jumper in the R-Pi from Tx and Rx.

    • Type: minicom -b 9600 -o -D /dev/ttyAMA0

  • Install python-serial package to gain access to serial port from Python.

    • Type: sudo apt-get install python-serial

Starting a program from bootup