Raspberry Pi
From charlesreid1
A guide to hacking on the Raspberry Pi, a microcomputer that runs a full stack Linux OS, all on a mobile processor:
Installing
The page containing instructions for installing an operating system on the Raspberry Pi is over at RaspberryPi/Installing
Interfacing with Headless Pi
If you are running a headless Raspberry Pi, you can follow these instructions for modifying the Raspberry Pi boot sequence so that you can find your Pi on a network: RaspberryPi/Headless
Interfacing with Headless Raspberry Pi
(New) Step 1: Modify Raspberry Pi boot sequence
Plug the SD card into your computer. Edit the file cmdline.txt to include a directive to hard-code an IP address. Add a space and the following directive to the end of cmdline.txt:
ip=169.254.113.200
That's it. The old procedure required a cmdline.directive, but now the Raspberry Pi boots from cmdline.txt directly. Hooray for removing redundant steps!
(Outdated) Step 1: Modify Raspberry Pi boot sequence
Note: this procedure no longer works with the 2015 Debian Raspberry Pi image available on the Raspberry Pi website.
Following http://pihw.wordpress.com/guides/direct-network-connection/
Plug the SD card back into your computer. Edit the file cmdline.txt to include a directive to hard-code an IP address.
You can specify any IP address for the RPi in the range 169.254.X.Y (anything between 169.254.0.0 to 169.254.254.254).
I added this to the end of cmdline.txt:
ip=169.254.113.200
Then I ran
$ cp cmdline.txt cmdline.direct
Now plug the SD card into the RPi and boot with a hard-coded IP address. This will work because we're connecting the computer and the RPi directly.
Step 2: Cat5 Network Cable
Get a standard network cable and connect the laptop to the RPi. Remove the SD card from the laptop once you've modified cmdline and put it in the RPi to boot it up.
Step 3: SSH
Now you can SSH to the IP address you specified using the default username 'pi' and the default password 'raspberry':
First Steps with Raspberry Pi
Configuring your RPi
When you first SSH into your RPi, it will notify you that you have to run a config program to set up your RPi initially.
I picked the first menu item, and that required a system reboot:
Putting RPi on a Network
As a next step, we'll want RPi to be internet-capable so we can use aptitude to install some packages and get this operating system tuned up.
First, you'll keep your network cable plugged into your RPi.
Then you'll plug the other end of the network cable into a network router or a network wall jack (see IMPORTANT CAVEAT below).
Once you plug your RPi into the network router, it'll still have the same IP address, so you can still SSH to it the same way:
$ ssh pi@169.254.113.200
but now the RPi will have internet connectivity:
Local Area Network
If you plug your RPi into a wall jack and you're on a local area network administered by someone else, you may have internet connectivity issues. This is because you're hard-coding an IP address, but some networks must assign IP addresses themselves in order to allow a computer to get outside the local area network. In this case, you'll have to contact a network administrator and ask them to reserve an IP address for your Raspberry Pi.
Your network administrator will give you a couple of pieces of information:
- IP address - example
10.10.9.22 - Netmask - example
255.255.255.0 - Gateway - example
10.10.9.1
You can enter the IP address in your cmdline.direct file.
To specify the netmask and gateway, edit the file /etc/network/interfaces and add a section for your ethernet interface that specifies the netmask and gateway:
auto eth0 iface eth0 inet static address 10.10.9.22 netmask 255.255.255.0 gateway 10.10.9.1
This should replace a line that looks like this:
#iface eth0 inet dhcp
which says to automatically find an IP address for this ethernet interface by getting one from the DHCP server.
If you don't know what your network interface is, execute this command:
ifconfig -a | grep eth
If You Absolutely, Positively Must Use a Dynamic IP
In case you are dealing with a situation where you have to let your Raspberry Pi be dynamically configured an IP address, and you have to somehow figure out what IP address your Raspberry Pi has, and you don't have a monitor to simply print it out, here's what you can do: you can modify SSH to listen on a particular port, like 9999. Then, you can let the RPi be dynamically assigned an IP address by the network DHCP server. It will be listening for incoming connections on port 9999, and we can use nmap to look for IP addresses listening on that port.
Remote Connect to Your RPi
The first step to dynamically configuring your RPi is to first have a static IP address assigned to it, and have it directly connected to your computer. You will SSH into the RPi to modify the port that SSH listens on. We'll use that as a way to flag RPi's IP address, and use nmap to discover its IP address.
Modify SSH
Open the SSH daemon config file:
vim /etc/ssh/ssh_config
Change the line specifying the port it listens on:
Port 22
to something else,
Port 9999
Get the RPi a Dynamic IP Address
One last step remains: edit the boot file of the RPi so it doesn't hard-code the IP address anymore. Pull the SD card out of the RPi and put the card in your laptop. Edit master.txt,
vim /Volumes/boot/master.txt
and remove the IP statement at the end,
ip=169.254.113.200
and finally, copy that to master.direct,
cp /Volumes/boot/master.{txt,direct}
Now plug it into the network or router.
Discovering the RPi IP Address with Nmap
In the steps above, we modified our Raspberry Pi so that SSH would listen on port 9999, and the RPi would get a dynamic IP address instead of a static IP address. Now that we've plugged our RPi into our network, and it has its dynamic IP address, we have to find it.
We're going to use nmap search for all IP addresses listening on port 9999.
First, I ran this command from a laptop that was connected to the same router as the RPi:
$ ifconfig
I found the laptop's IP address, and used the first three digits as the fixed digits of the IP address in the nmap search. In this case, my laptop was at 10.0.0.5, and the router was at 10.0.0.1.
Now I do a search for all IP addresses listening on port 9999:
nmap -p9999 10.0.0.0-255
(Or, if you're on a small network, you can just do a scan of port 22.) Here's the results of my nmap scan of port 22:
Thus, our RPi is at 10.0.0.16 (this may be a bit more challenging on a larger LAN network where more services might be listening, but that is just a matter of trial and error).
Here, I SSH to the RPi:
And... success!
And here's the whole reason for doing all of this: we've got a connection to the outside world now!
Remote Connect to Your RPi on Port XYZ
If you've already changed the port that SSH is listening on, and you need to SSH into the RPi again, just use the -p <port> option when you issue your SSH command to the RPi:
ssh -p 9999 169.254.113.200
OOPS
If you really screw this up, and change the port the RPi is listening on, and can't figure out how to fix it, you can always just re-apply the .img file you downloaded to the SD card, wiping out your changes. No problemo.
Software Installation
A list of commands that I ran on the RPi to get things installed:
#!/bin/sh sudo apt-get update sudo apt-get install -y build-essential sudo apt-get install -y liblapack-dev sudo apt-get install -y libblas-dev sudo apt-get install -y gfortran sudo apt-get install python-dev wget http://python-distribute.org/distribute_setup.py python distribute_setup.py wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py sudo python get-pip.py sudo pip install virtualenv sudo pip install flask sudo pip install cython sudo pip install numpy sudo pip install pandas sudo pip install scipy
Running Web Server on Raspberry Pi
If you're running a web server on Raspberry Pi, you'll probably want to run on port 80. However, Raspberry Pis sometimes have a problem when installing Python packages into /usr/local/lib, so you have to install them into a custom directory. This creates a problem when trying to import the package as root, because root python won't be able to find it (won't have the $PYTHONPATH variable set correctly).
To get around this, you can bind to port 80 without being root by following these instructions:
http://www.wensley.org.uk/info#setpcaps
Flask
Flask is an excellent lightweight web server in Python. Flask makes it easy to create an API for requesting data and passing JSON between Python (which is a powerful language that can control all kinds of things on the Raspberry Pi) and Javascript (which provides a nice user front-end via a web interface).
For an example of how you might do this, see my Dymouse project on Github: http://github.com/charlesreid1/dymouse
This uses a Raspberry and a Flask script to create an API for a USB postage scale. Python provides a means for grabbing the weight reading from the postage scale; Flask turns it into JSON; and Javascript turns the JSON into HTML text/plots.
Raspberry Pi Projects
Raspberry Pi Timelapse Photo
Software
Since I hadn't connected my Pi to the net in a while, the first thing I did was to update the package manager:
sudo apt-get update
Next, I upgraded the operating system:
sudo apt-get dist-upgrade
With all of the updating and upgrading out of the way, I moved on to the actual Raspberry Pi camera itself. There is a Python module to control the Pi camera, available through aptitude:
sudo apt-get install python-picamera python-picamera-docs
However, I was still not able to use my camera, because I had to run the Raspberry Pi configuration program. To run it:
sudo raspi-config
You enable the Raspberry Pi camera in the configuration menu.
Setup
The setup I had used one of these as the camera. This is a whopping 5 MP, which is as good as a point-and-shoot, except it's extremely tiny. I was able to get it hooked up to my Raspberry Pi and working just fine with the above steps.
You can use Python code to trigger the camera to take a picture, and you can specify a filename to save to. Here's a quick script I hacked together to take a picture every 2 seconds, and save it to sequentially-numbered files (0001.jpg, 0002.jpg, etc.):
# pic.py
import picamera
import time
camera = picamera.PiCamera()
i = 0
while True:
filename = "%04d.jpg"%(i)
camera.capture(filename)
print "Saving photo to %s"%(filename)
i += 1
time.sleep(2)
To run this, I use screen. I log in remotely, then run the screen command. In that screen I run python pic.py. It will print out as it progresses. Running it with screen allows you to disconnect and leave the Pi unattended.
Hello World LED Circuit with GPIO
This project controls a simple LED circuit with the Raspberry Pi's onboard GPIO cable. A python code is used to send high/low voltage signals to pins on the GPIO, and make an LED on a breadboard blink.
Kali Linux on Raspberry Pi
Getting deeper into the world of networking and using the Pi to analyze networks: Kali Pi







