Under construction guides

From ETCwiki
Jump to navigationJump to search

This Tutorial

This is a guide I am making right now Aug 5 2013 that includes my first 10 working projects with setup, LEDs, switches, and status scripts that will change LEDs and perform actions based on switches or buttons. Slideshow from command line, and music player from command line, website server monitor (uptime monitor if you modify).

This guide is under construction as of August 8 2013 and will be split into pages when completed.

My name is Chris, if you want to contact me or comment on this wiki feel free to email me at ddxfish@gmail.com or use the contact form on my website www.chriswest.us - I am available for:

  • Comments on this guide
  • Requests for additions to this guide (or request for clarification)
  • Very basic questions about this guide (need me to add a few notes or tips?)
  • Hire me for consultation on Raspberry Pi (raspbian)
  • Hire me for building you a pi with any/all of these features

Required Items

  • Raspberry Pi
  • Raspberry Pi Case
  • 8GB or more SD Card (with Raspbian 7 Wheezy) (get a class 10 or more i recommend)
  • 1.5A or greater USB charger (5V 1500ma+) DC
  • Network Cable attached properly
  • HDMI Cable and HDMI TV (yes tv works best for me)
    • Alternative: RCA Video cable, and 3.5mm audio cable

Items for the guides

  • 6 LEDs (Red, Yellow, Green, Blue, Purple, White) 5mm 3.0v@16mA and 2.1v@22mA but any 5mm LED will do
  • 5 Tactile Button Breadboard Switches (get 10 cuz its easy to mess them up)
  • Pack of resistors (I would just buy a 15$ pack on amazon.com)
    • 3 x 47 ohm @ 1/4 watt (for LED)
    • 3 x 200 ohm @ 1/4 watt (for LED)
    • 3 x 1K ohm @ 1/4 watt (for switch)
    • 3 x 10K ohm @ 1/4 watt (for switch)
  • 20+ dupont (breadboard) wires (Male to Female)
  • 10+ dupont (breadboard) wires (Male to Male)
  • 22 gauge wire spool (solid core, not threaded)
  • Breadboard (around 400 point tie in, 2 or 4 power rails)

Raspberry Pi Guides List

Raspberry Pi - Initial Setup

change root password

sudo passwd

update OS and install vim (text editor)

apt-get update
apt-get upgrade
apt-get install vim

You will need to edit your bash properties in 2 files, start with

vim /root/.bashrc

Add aliases to the end of /root/.bashrc and /root/.bash_profile

alias ls='ls -al --color'
alias goho='cd /home/pi'
alias gogp='cd /home/pi/gpio'
alias savepi='chown -R pi:pi /home/pi
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\]"

To copy all of this to its sister file:

cat /root/.bashrc > /root/.bash_profile

Remove Desktop and X from Raspberry Pi

If you want to NEVER boot into desktop and not have to back up those packages every time and worry about updates for them, this is the command for you. My guides will never use the Raspberry Pi desktop. This step is not essential to follow my or any guides I know of but will simplify some types of jobs like backups. It will remove Xserver, libx11 and ALL packages that depend on it. This means removing the ability to boot into desktop! You can try to put it back on but in my opinion this is a one way journey.

Notes:

  • this command can trigger some major package changes on your system
  • for me it removed MPD and a few other packages
  • all I had to do was reinstall the packages (config files were never removed)

proceed at your own risk - one way journey

apt-get remove xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils xarchiver xauth xkb-data console-setup xinit lightdm libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}* lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal} obconf openbox gtk* libgtk* scratch tsconf xdg-tools desktop-file-utils
apt-get --yes autoremove
apt-get --yes autoclean
apt-get --yes clean

Slim down your Raspberry Pi install

I backup my pi on a regular basis and I have not been using the desktop on this install at all. I want to remove stuff that is not necessary like locales. Make sure you setup your locale before removing the others!

Setup your Locale

raspi-config

Remove unused locales (brought me back 70mb of my 500mb image) Note: this remove all BUT en_US.UTF-8 so make sure u enter the names u want to keep

apt-get install localepurge     (during install you select which locales to keep!)
localepurge

Clean up apt-get junk

apt-get autoremove
apt-get autoclean
apt-get clean

Complete SD card backup Raspberry Pi

This is a command that lets you make a COMPLETE image of any raspberry Pi sd card. You will need to do this from a linux PC (not the raspberry pi) so you will need to shut down your pi for a bit.

  • Stick the Pi SD card into your main PC running linux
  • make sure you have free space enough to backup the entire size of the card
  • this will put the image in your home directory in the file rpifull.img
  • this backs up the boot loader, and every 1 and 0 on the card
  • find the name/location of your card (/dev/sdc) and replace below. Use the "df" command
umount /dev/sdc1
umount /dev/sdc2
dd if=/dev/sdc bs=1M of=~/rpifull.img

Complete SD Card restore Raspberry Pi

This will take the image you made above and restore it to any SD card of the same size (sometimes works with diff sizes but cant guarantee).

  • Put the SD card from the pi into your main linux PC
  • Replace any /dev/sdc with your memory card
umount /dev/sdc1
umount /dev/sdc2
dd if=~/rpifull.img bs=1M of=/dev/sdc

Zerofree on Raspberry Pi

I make frequent backups of my pi using the dd command because I test and install so many different guides, hardware and software. My 32gb SD card (class 10 UHS) writes an image file that is 32gb, so each time I compress it using gzip or bzip2 and end up with a 20GB image roughly. This has the unfortunate side effect of storing all of the trash 1's and 0's on the partition where files used to be. To reduce the size of my zipped backup (this can reduce ZIPPED size of an image, not image size itself) I ran zerofree to write zeros to the unused space.

  • shutdown the pi and take out its memory card
  • boot your main pc to linux (I used linux Mint 15)
  • backup your pi using dd (do this BEFORE you mess anything up with zerofree)
  • run zerofree on your sdcard
apt-get install zerofree
umount /dev/sdc1       (change sdc1 to your raspi boot partition location)
umount /dev/sdc2       (change sdc2 to your raspi ext4 partition)
zerofree -v /dev/sdc    (replace sdc with your usb cards location, check the command "df")


Mount Windows Share from Raspberry Pi

create mount folder (do all of this as root)

mkdir /myserver

add share to auto-mount

vi /etc/fstab

add this line

//192.168.1.69/sharename /myserver auto,owner,rw,username=user1,password=hackme 0 0

test to make sure it works

mount -a

or

init 6

now check the folder after mount

cd /myserver
ls

Setup Private Keys - Raspberry Pi

This requires you to be logged into the Raspberry Pi on PuTTy or similar ssh client and logged into root

On the Raspberry Pi:

su
cd /root
mkdir .ssh
cd .ssh
ssh-keygen
cat id_rsa.pub > authorized_keys
chmod 600 authorized_keys

On your (Windows) computer:

  1. copy "id_rsa" from /root/.ssh on the pi to your computer
  2. open PuTTygen on windows
  3. load the private keyfile you just downloaded (id_rsa) Note: You will have to search for *.* All file types
  4. save as a putty private key file with extension .ppk
  5. load your RPi session in putty, and enter the key location and name in the SSH section

Setup MPD and MPC - Raspberry Pi

This guide will show you how to get Music Player Daemon (a SERVER that outputs to the pi audio, HDMI and regular, can be controlled locally or remotely) and Music Player Client to play music and command them from a terminal window or an MPD graphical client (to connect and control the music)

apt-get install mpd mpc

setup mpd

vi /etc/mpd.conf

setup these lines (just make the directory or file if it doesnt exist, use the touch command)

music_directory
playlist_directory
db_file
log_file
pid_file
state_file
sticker_file
bind_to_address (comment this line with a # at the start)
port "6600" (uncomment this line, remove the #)
user "pi"
metadata_to_use (uncomment)
mixer_type "software" (uncomment this line)

restart the pi

reboot (dont use init 6)

start MPD on the pi, look for errors

service mpd start
(wait until its done)
service mpd restart

Connect to mpd using a client on your main computer (I use Auremo MPD Client)

  1. download Auremo MPD Client (open source!)
  2. enter the ip in the setup for the pi
  3. click connect

Text to Speech

This is how to make your pi talk over its speakers using a generated voice

apt-get install festival

Now make it talk

echo "the time is" `date +%I` `date +%m` | festival --tts

How to shutdown or restart the raspberry pi

After using like 5 different Raspberry Pi's with multiple setups, OS, SD cards, and methods of restarting, I had found I was restarting and shutting down wrong and frequently destroying or losing files that were recently open or near to others in the data structure. I lost my entire first setup after 40 or so hours of learning and work due to using init 6 to restart the pi.

You will find people that will tell you this is safe. They are correct-- most of the time. You can use the reboot command also but it does the same thing and is also unsafe.

My solution was to create a restart script that stops any services I can, then kills any running programs that could interfere with my file system if forced to shutdown too early, then waits 15 seconds and uses the shutdown command after the pause. I made this script reboot.sh to avoid data corruption on my pi (it happened 4 or 5 times!). Feel free to copy and modify to your taste.

echo "Beginning forced shutdown script"
#unmount my network share (cifs)
umount -f /jane
#cron triggers scripts all the time on my setup, kill it!
service cron stop
##service running my python buttons listen script
service pybuttons stop
killall python
service mpd stop
service ntp stop
service alsa-utils stop
service networking stop
service ssh stop
killall fbi
killall samba
killall mpd
killall mpc
killall vim
killall vi
killall omxplayer
killall sftp-server
killall python
#unmount my network share (cifs) again just in case!
umount -f /jane
sleep 15
shutdown -h -r now

Bash Slideshow

This program will run from runlevel 2 (command line only) if you want. It uses the Frame Buffer to display images in a slideshow. It preloads and caches images that are upcoming.

apt-get install fbi

Now create 2 scripts to turn on and off the slideshow

cd /home/pi
mkdir scripts
cd scripts
touch sson.sh
chmod +x sson.sh
touch ssoff.sh
chmod +x ssoff.sh
vi slideshow.sh

Now fill the file sson.sh with this

#!/bin/bash
PHOTODIR=/storage/wallpapers
INTERVAL=30
fbi -T 1 -cachemem 24 -readahead -autodown -t $INTERVAL -u `find $PHOTODIR -iname "*.jpg"`

And fill ssoff.sh with this

#!/bin/bash
killall fbi

Now trigger the slideshow on and off using the scripts

./sson.sh
./ssoff.sh

Check the CPU Temperature Raspberry Pi

Make a simple script that tells u the temperature of your raspberry pi's onboard temperature sensor (no, I dont know where it is!)

Make a file called degree.sh in your scripts directory (wherever you save urs)

touch degree.sh
chmod +x degree.sh
nano degree.sh

Now, Add these lines

#!/bin/bash
/opt/vc/bin/vcgencmd measure_temp

Save and exit. Test by using

sh degree.sh

Setup LIRC on Raspberry Pi

alt RPi Connected to a TSOP4838 temperature sensor

This is a complicated guide to setting up LIRC on the Raspberry Pi to run remote commands.

Materials

  • Vishay TSOP4838 $4 (aka 38kHz Infrared (IR) Receiver Module - Part Number 276-640 at Radio Shack)

Start by installing LIRC

apt-get install lirc

Now, edit the file /etc/lirc/hardware.conf

# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS=""  

#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false

#Don't start irexec, even if a good config file seems to exist.
START_IREXEC=true

#Try to load appropriate kernel modules
LOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""

Now edit the file /etc/modules and add this line

lirc_rpi

Now edit (or make) the file /lib/udev/rules.d/98-lircd.rules and add this line

KERNEL=="lirc[0-9]*",   SUBSYSTEM=="lirc", SUBSYSTEMS=="platform", GOTO="begin"

Now test to see if receiver works, run this and press some buttons, see anything?

/etc/init.d/lirc stop
mode2 -d /dev/lirc0

Now setup your remote control or use a default lircd.conf for a remote on file with LIRC Notes: setup as many or as few buttons as you want. I would recommend doing them all now.

irrecord --list-namespace      (print or save this, you will need it during setup)
irrecord -d /dev/lirc0 ~/lircd.conf     (follow instructions and use above command for key names)

Now copy the created ~/lircd.conf to its new home

mv /etc/lirc/lircd.conf /etc/lirc/lircd.original    (backup original)
cp ~/lircd.conf /etc/lirc/lircd.conf     (Copy it home)

Now test your keys and remote setup

service lirc restart
killall irexec
irw     (now spam some buttons! Ctrl + C to exit)

Want to launch programs with your remote? Edit or create ~/.lircrc ~/ means the users home directory

touch ~/.lircrc
vi ~/.lircrc

Add these lines to ~/.lircrc Note: Replace KEY_POWER with your key from your lircd.conf Note: Use an & at the end of the config = line. It will run the command without making you wait for it to complete

begin
     prog = irexec
     button = KEY_POWER
     config = /home/pi/gpio/11blink.sh &
end

Test... Again but this time to exec the program

killall irexec
service lirc restart
irexec -d    (now test your button)



Multi LED and Switch Control

alt Raspberry Pi with Buttons and LEDs