From charlesreid1

 
(24 intermediate revisions by the same user not shown)
Line 3: Line 3:
[[Image:RaspberryPi_PluggedInRPi.jpg|500px]]
[[Image:RaspberryPi_PluggedInRPi.jpg|500px]]


=Installing Operating System=
=Installing=


The kit came with an 8GB SD card with the NOOBS operating system (for beginners at raspberry pi) pre-installed. Nice touch, but I didn't want to use NOOBS, I wanted to use a Raspbery Pi version of Debian so that I can hack this thing up. I downloaded an image file from http://www.raspberrypi.org/ (it was a zip file, containing an img file).
Instructions for installing an operating system on the Raspberry Pi: [[RaspberryPi/Installing]]


==Step 0: Plug the SD card in==


You're going to be installing the operating system for the Raspberry Pi onto an SD card, so pick an SD card that you'll use, and plug it into your laptop.
==After Installing: Interfacing with Headless Pi==


==Step 1: Find your SD card==
Instructions for interfacing with headless Raspberry Pis, including how to change their boot sequence to find them on a network: [[RaspberryPi/Headless]]


I plugged my SD card into my Mac OS X (running 10.8), and ran this command to list all the devices:


<pre>
==After Installing: First Steps with Pi==
$ diskutil list
/dev/disk0
  #:                       TYPE NAME                    SIZE      IDENTIFIER
  0:      GUID_partition_scheme                        *250.1 GB  disk0
  1:                        EFI                        209.7 MB  disk0s1
  2:                  Apple_HFS Cronus                  249.2 GB  disk0s2
  3:                Apple_Boot Recovery HD            650.0 MB  disk0s3
/dev/disk1
  #:                      TYPE NAME                    SIZE      IDENTIFIER
  0:    FDisk_partition_scheme                        *7.9 GB    disk1
  1:                DOS_FAT_32 NOOBS                  7.9 GB    disk1s1
/dev/disk2
  #:                      TYPE NAME                    SIZE      IDENTIFIER
  0:    Apple_partition_scheme                        *16.7 MB    disk2
  1:        Apple_partition_map                        32.3 KB    disk2s1
  2:                  Apple_HFS Flash Player            16.7 MB    disk2s2
</pre>


That means my sd card is at <code>/dev/disk1</code>.
You can find a guide to your first steps with Raspberry Pi, mainly covering the setup process for Raspbian Linux: [[RaspberryPi/First Steps]]


==Step 2: Unmount card==
=Raspberry Pi Projects=
 
Now I unmount the SD card:
 
<pre>
$ diskutil unmountDisk /dev/disk1
Unmount of all volumes on disk1 was successful
</pre>
 
==Step 3: Write image to SD card==
 
For the last step, you use <code>dd</code> (disk formatter utility) to write that image to your SD card:
 
<pre>
$ sudo dd bs=1m if=2014-06-20-wheezy-raspbian.img of=/dev/disk1
2825+0 records in
2825+0 records out
2962227200 bytes transferred in 1571.202055 secs (1885325 bytes/sec)
</pre>
 
 
The above command took 27 minutes on my system.
 
==Resources==
 
* http://www.raspberrypi.org/documentation/installation/installing-images/mac.md
* http://elinux.org/RPi_Easy_SD_Card_Setup
 
=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:
 
<pre>
ip=169.254.113.200
</pre>
 
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!
 
[[Image:Ping169.254.png|400px]]
 
==(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:
 
<pre>
ip=169.254.113.200
</pre>
 
Then I ran
 
<pre>
$ cp cmdline.txt cmdline.direct
</pre>
 
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.
 
[[Image:RaspberryPi_Network.jpg|500px]]
 
[[Image:RaspberryPi_Laptop_Network.jpg|500px]]
 
==Step 3: SSH==
 
Now you can SSH to the IP address you specified using the default username 'pi' and the default password 'raspberry':
 
[[Image:SSH_RaspberryPi.png]]
 
=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.
 
[[Image:RaspberryPi_Config.png]]
 
I picked the first menu item, and that required a system reboot:
 
[[Image:RaspberryPi_Reboot.png]]
 
==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.
 
[[Image:RaspberryPi_PluggedInRPi.jpg|500px]]
 
Then you'll plug the other end of the network cable into a network router or a network wall jack (see IMPORTANT CAVEAT below).
 
[[Image:RaspberryPi_PluggedInRouter.jpg|500px]]
 
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:
 
<pre>
$ ssh pi@169.254.113.200
</pre>


but now the RPi will have internet connectivity:
==Hardware Focus==


[[Image:RaspberryPi_Aptitude.png]]
Most recent projects listed first.


==Local Area Network==
===Making an Internet of Things Morse Code Keyer with Raspberry Pi===


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.
Combining the morse code example below with [[Flask]], a lightweight web server for Python, allows us to create an API for remotely controlling the Raspberry Pi. We can then use the Raspberry Pi to send morse code with the LED.


Your network administrator will give you a couple of pieces of information:  
Here are the details: [[RaspberryPi/IoT Morse Code Keyer]]
* IP address - example <code>10.10.9.22</code>
* Netmask - example <code>255.255.255.0</code>
* Gateway - example <code>10.10.9.1</code>


You can enter the IP address in your <code>cmdline.direct</code> file.
===Morse Code on Raspberry Pi===


To specify the netmask and gateway, edit the file <code>/etc/network/interfaces</code> and add a section for your ethernet interface that specifies the netmask and gateway:
This page has more details on a quick-and-easy way to get a Morse code library I wrote in Python up and running on the Raspberry Pi: [[RaspberryPi/Morse Code]]


<pre>
===Raspberry Pi Timelapse Photo===
auto eth0
iface eth0 inet static
address 10.10.9.22
netmask 255.255.255.0
gateway 10.10.9.1
</pre>


This should replace a line that looks like this:
You can find a short guide to how I built a Raspberry Pi time lapse photo camera here:  


<pre>
[[RaspberryPi/Timelapse]]
#iface eth0 inet dhcp
</pre>


which says to automatically find an IP address for this ethernet interface by getting one from the DHCP server.
===Hello World LED Circuit with GPIO===


If you don't know what your network interface is, execute this command:
This project controls a simple LED circuit with the Raspberry Pi's onboard GPIO cable: [[Hello World Raspberry Pi]]


<pre>
This project uses a python code to send high/low voltage signals to pins on the GPIO, and make an LED on a breadboard blink.
ifconfig -a | grep eth
</pre>


==If You Absolutely, Positively Must Use a Dynamic IP==
==Software Focus==


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.
===Run Web Server on Pi===


===Remote Connect to Your RPi===
You can find a guide to running a lightweight web server with Flask or something similar:


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.
[[RaspberryPi/Web Server]]


===Modify SSH===
===Kali Linux on Raspberry Pi===


Open the SSH daemon config file:
Getting deeper into the world of networking and using the Pi for security and networking applications:  


<pre>
[[Kali Raspberry Pi]]
vim /etc/ssh/ssh_config
</pre>


Change the line specifying the port it listens on:
===Tor Proxy with Pi===


<pre>
More info here: [https://learn.adafruit.com/onion-pi]
Port 22
</pre>


to something else,


<pre>
==Wifi Access Point==
Port 9999
</pre>


===Get the RPi a Dynamic IP Address===
===Installing Requisite Software===


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 <code>master.txt</code>,
Basically, you will be setting up your Pi as a host access point, which requires some software. Specifically, here is the software required:


<pre>
<pre>
vim /Volumes/boot/master.txt
$ sudo apt-get install hostapd isc-dhcp-server
</pre>
</pre>


and remove the IP statement at the end,
===Set Up DHCP Server===


<pre>
Next, set up the DHCP server by editing <code>/etc/dhcp/dhcp.conf</code>
ip=169.254.113.200
</pre>


and finally, copy that to <code>master.direct</code>,
Remove the following two lines by commenting them out:


<pre>
<pre>
cp /Volumes/boot/master.{txt,direct}
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
</pre>
</pre>


Now plug it into the network or router.
Next, add the word "authoritative;" to the section where it gives you information. You should see this in your file:
 
===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:


<pre>
<pre>
$ ifconfig
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
</pre>
</pre>


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.
Finally, add the following lines to set up the IP address assignment/configuration:
 
Now I do a search for all IP addresses listening on port 9999:


<pre>
<pre>
nmap -p9999 10.0.0.0-255
subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
</pre>
</pre>


(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:
===Set Up DHCP Interfaces===
 
[[Image:Nmap9999.png]]


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).
Next, edit the file <code>/etc/default/isc-dhcp-server</code> and update the <code>INTERFACES</code> section to read:
 
Here, I SSH to the RPi:
 
[[Image:SSH9999.png]]
 
And... success!
 
[[Image:SSH9999Success.png]]
 
And here's the whole reason for doing all of this: we've got a connection to the outside world now!
 
[[Image:SSH9999Ping.png]]
 
===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 <code>-p <port></code> option when you issue your SSH command to the RPi:


<pre>
<pre>
ssh -p 9999 169.254.113.200
INTERFACES="wlan0"
</pre>
</pre>


===OOPS===
Now disable the wlan0 interface:
 
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:


<pre>
<pre>
#!/bin/sh
sudo ifdown wlan0
 
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
</pre>
</pre>


=Running Web Server on Raspberry Pi=
Now change the wlan0 network interface so that it will use manually-assigned IP address information, instead of trying to set everything automatically. Edit <code>/etc/network/interfaces</code> and add/change the lines:
 
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 <code>/usr/local/lib</code>, 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 <code>$PYTHONPATH</code> variable set correctly).
 
To get around this, you can bind to port 80 without being root by following these instructions:
 
http://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-processes-to-bind-to-privileged-ports-1024-on-l
 
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:


<pre>
<pre>
sudo apt-get update
# auto wlan0
</pre>
</pre>
Next, I upgraded the operating system:


<pre>
<pre>
sudo apt-get dist-upgrade
allow-hotplug wlan0
</pre>


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:
iface wlan0 inet static
 
    address 192.168.42.1
<pre>
    netmask 255.255.255.0
sudo apt-get install python-picamera python-picamera-docs
</pre>
</pre>


However, I was still not able to use my camera, because I had to run the Raspberry Pi configuration program. To run it:
Finally, edit <code>/etc/hostapd/hostapd.conf</code> to set up the wireless network:


<pre>
<pre>
sudo raspi-config
interface=wlan0
driver=rtl871xdrv
ssid=Pi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
</pre>
</pre>


You enable the Raspberry Pi camera in the configuration menu.
Note that the <code>driver=rtl871xdrv</code> line may need to change.
 
===Setup===
 
The setup I had used one of [http://www.adafruit.com/products/1367 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.):
 
<source lang="python">
# pic.py
 
import picamera
import time
 
camera = picamera.PiCamera()
 
i = 0
while True:


    filename = "%04d.jpg"%(i)
===More Info===


    camera.capture(filename)
More info here: [https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point]
    print "Saving photo to %s"%(filename)


    i += 1
=Powering the Pi via UART/Serial=
    time.sleep(2)


</source>
UART = Universal Asynchronous Receive/Transmit


To run this, I use screen. I log in remotely, then run the <code>screen</code> command. In that screen I run <code>python pic.py</code>. It will print out as it progresses. Running it with screen allows you to disconnect and leave the Pi unattended.
While you can normally interface with and power the Pi by plugging it into a computer or a wall wart via the mini USB port, you can also power the Pi via the pins on the board, and communicate with the Pi via a serial connection. This uses the 5V, Ground, TX, and RX pins that are on the right side (the 5V side) of the pins on the Pi board.


By plugging these into a USB serial adapter, which is plugged into a computer, the connection can be used to provide power and a connection to the Pi.


More details: http://villagescience.org/running-raspberry-pi-usb-serial-ttl-adapter/


==Hello World LED Circuit with GPIO==
=Notes by Date=


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.
[[Raspberry Pi 2015-07-29]] - basic hello world


[[Hello_World_Arduino_Pi]]
[[Raspberry Pi 2015-07-30]] - basic hello world writeup


[[Raspberry Pi 2015-07-31]] - morse code writeup, Flask Web API to control hardware


==Kali Linux on Raspberry Pi==
[[August 2015]] - notes from august 2015


Getting deeper into the world of networking and using the Pi to analyze networks: [[Kali Pi]]
[[August 2016]] - notes from august 2016


=Resources=
=Resources=
Line 407: Line 185:
* http://raspberry.io/wiki/how-to-get-python-on-your-raspberrypi/
* http://raspberry.io/wiki/how-to-get-python-on-your-raspberrypi/


{{PiFlag}}


[[Category:Raspberry Pi]]
[[Category:Raspberry Pi]]

Latest revision as of 02:30, 12 August 2017

A guide to hacking on the Raspberry Pi, a microcomputer that runs a full stack Linux OS, all on a mobile processor:

RaspberryPi PluggedInRPi.jpg

Installing

Instructions for installing an operating system on the Raspberry Pi: RaspberryPi/Installing


After Installing: Interfacing with Headless Pi

Instructions for interfacing with headless Raspberry Pis, including how to change their boot sequence to find them on a network: RaspberryPi/Headless


After Installing: First Steps with Pi

You can find a guide to your first steps with Raspberry Pi, mainly covering the setup process for Raspbian Linux: RaspberryPi/First Steps

Raspberry Pi Projects

Hardware Focus

Most recent projects listed first.

Making an Internet of Things Morse Code Keyer with Raspberry Pi

Combining the morse code example below with Flask, a lightweight web server for Python, allows us to create an API for remotely controlling the Raspberry Pi. We can then use the Raspberry Pi to send morse code with the LED.

Here are the details: RaspberryPi/IoT Morse Code Keyer

Morse Code on Raspberry Pi

This page has more details on a quick-and-easy way to get a Morse code library I wrote in Python up and running on the Raspberry Pi: RaspberryPi/Morse Code

Raspberry Pi Timelapse Photo

You can find a short guide to how I built a Raspberry Pi time lapse photo camera here:

RaspberryPi/Timelapse

Hello World LED Circuit with GPIO

This project controls a simple LED circuit with the Raspberry Pi's onboard GPIO cable: Hello World Raspberry Pi

This project uses a python code to send high/low voltage signals to pins on the GPIO, and make an LED on a breadboard blink.

Software Focus

Run Web Server on Pi

You can find a guide to running a lightweight web server with Flask or something similar:

RaspberryPi/Web Server

Kali Linux on Raspberry Pi

Getting deeper into the world of networking and using the Pi for security and networking applications:

Kali Raspberry Pi

Tor Proxy with Pi

More info here: [1]


Wifi Access Point

Installing Requisite Software

Basically, you will be setting up your Pi as a host access point, which requires some software. Specifically, here is the software required:

$ sudo apt-get install hostapd isc-dhcp-server

Set Up DHCP Server

Next, set up the DHCP server by editing /etc/dhcp/dhcp.conf

Remove the following two lines by commenting them out:

option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

Next, add the word "authoritative;" to the section where it gives you information. You should see this in your file:

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

Finally, add the following lines to set up the IP address assignment/configuration:

subnet 192.168.42.0 netmask 255.255.255.0 {
	range 192.168.42.10 192.168.42.50;
	option broadcast-address 192.168.42.255;
	option routers 192.168.42.1;
	default-lease-time 600;
	max-lease-time 7200;
	option domain-name "local";
	option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Set Up DHCP Interfaces

Next, edit the file /etc/default/isc-dhcp-server and update the INTERFACES section to read:

INTERFACES="wlan0"

Now disable the wlan0 interface:

sudo ifdown wlan0

Now change the wlan0 network interface so that it will use manually-assigned IP address information, instead of trying to set everything automatically. Edit /etc/network/interfaces and add/change the lines:

# auto wlan0
allow-hotplug wlan0

iface wlan0 inet static
    address 192.168.42.1
    netmask 255.255.255.0

Finally, edit /etc/hostapd/hostapd.conf to set up the wireless network:

interface=wlan0
driver=rtl871xdrv
ssid=Pi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Note that the driver=rtl871xdrv line may need to change.

More Info

More info here: [2]

Powering the Pi via UART/Serial

UART = Universal Asynchronous Receive/Transmit

While you can normally interface with and power the Pi by plugging it into a computer or a wall wart via the mini USB port, you can also power the Pi via the pins on the board, and communicate with the Pi via a serial connection. This uses the 5V, Ground, TX, and RX pins that are on the right side (the 5V side) of the pins on the Pi board.

By plugging these into a USB serial adapter, which is plugged into a computer, the connection can be used to provide power and a connection to the Pi.

More details: http://villagescience.org/running-raspberry-pi-usb-serial-ttl-adapter/

Notes by Date

Raspberry Pi 2015-07-29 - basic hello world

Raspberry Pi 2015-07-30 - basic hello world writeup

Raspberry Pi 2015-07-31 - morse code writeup, Flask Web API to control hardware

August 2015 - notes from august 2015

August 2016 - notes from august 2016

Resources