Category Archives: raspberry pi

Using AWS Route53 as Dynamic DNS

Basic script I run on my raspberry pi to keep my ISP’s dynamic IP address changes synchronised with my AWS Route53 entry. It uses dig and the AWS CLI tool to resolve the ip address and update route53 entry.



IP=$(dig +short
DNSIP=$(dig +short $DNSNAME
[ -z "$IP" ] && IP=$(curl -s
echo $(date) >> "$LOGFILE"
echo "Resolved IP: $IP" >> "$LOGFILE"
echo "DNS IP: $DNSIP" >> "$LOGFILE"

if [ "$IP" = "$DNSIP" ]; then
  echo "IP was unchanged." >> "$LOGFILE"
  exit 0
  TMPFILE=$(mktemp /tmp/route53-temp.XXXXXXXX)
  cat > ${TMPFILE} << EOF { "Comment":"$COMMENT", "Changes":[{ "Action":"UPSERT", "ResourceRecordSet":{ "ResourceRecords":[{ "Value":"$IP" }], "Name":"$DNSNAME", "Type":"$TYPE", "TTL":$TTL } }] } EOF echo "Updating IP address..." >> "$LOGFILE"
  /usr/local/bin/aws route53 change-resource-record-sets \
    --hosted-zone-id $ZONEID \
    --change-batch file://"$TMPFILE" >> "$LOGFILE"

Ratpoison and Screen Blanking

We needed a little kiosk style machine for our ‘company’ dashboard and have opted for a little pi running python and midori. And it works like a treat.

# Install Raspbian and the following
$ ssh pi@raspberrypi
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install ratpoison midori

# We also needed for our python web app:
$ sudo apt-get install python python-pip mysql-client libmysqlclient-dev python-dev
$ sudo pip install MySQL-python boto

After that configure your window manager to use ratpoison:

$ sudo update-alternatives --config x-window-manager
There are 2 choices for the alternative x-window-manager (providing /usr/bin/x-window-manager).

  Selection    Path                Priority   Status
  0            /usr/bin/openbox     90        auto mode
  1            /usr/bin/openbox     90        manual mode
* 2            /usr/bin/ratpoison   20        manual mode

Setup your xinitrc file to disable screen saver/dpms and start ratpoison

$ sudo vi ~/.xinitrc
xset s off     # don't activate screensaver
xset -dpms     # disable DPMS (Energy Star) features.
xset s noblank # don't blank the video device
exec /etc/alternatives/x-window-manager

Setup the pi to ‘auto login’

# Edit inittab comment and add the following new line
$ sudo vi /etc/inittab
#1:2345:respawn:/sbin/getty --noclear 38400 tty1 
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Setup startx on login
(todo: probably could do this via debian x-session-manager/lightdm conf)

$ vi ~/.bash_profile 
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then

To confirm Screen saver settings run the following

$ export DISPLAY=:0.0
$ xset q
Keyboard Control:
  auto repeat:  on    key click percent:  0    LED mask:  00000000
  XKB indicators:
    00: Caps Lock:   off    01: Num Lock:    off    02: Scroll Lock: off
    03: Compose:     off    04: Kana:        off    05: Sleep:       off
    06: Suspend:     off    07: Mute:        off    08: Misc:        off
    09: Mail:        off    10: Charging:    off    11: Shift Lock:  off
    12: Group 2:     off    13: Mouse Keys:  off
  auto repeat delay:  660    repeat rate:  25
  auto repeating keys:  00ffffffdffffbbf
  bell percent:  50    bell pitch:  400    bell duration:  100
Pointer Control:
  acceleration:  2/1    threshold:  4
Screen Saver:
  prefer blanking:  no    allow exposures:  yes
  timeout:  0    cycle:  600
  default colormap:  0x20    BlackPixel:  0x0    WhitePixel:  0xffff
Font Path:
DPMS (Energy Star):
  Standby: 600    Suspend: 600    Off: 600
  DPMS is Disabled

Using rc.local to start our python script and turn off terminal blanking
(todo: probably could just launch our python app using init.d script… meh)

$ sudo vi /etc/rc.local
#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"

#Starts the python dashboard
/home/pi/dashboard/simplePython/ &

setterm -blank 0

exit 0

Tethering to Android Phone for Internet via Bluetooth

As mentioned earlier until I purchase a usb dongle I am running without a network. Just for kicks though I decided to setup tethering via bluetooth with my android phone. Which allows me to update packages etc without having to plug the raspberry pi into a network point.

Bluetooth Pairing

To make it a little easier I paired to raspberry pi from my phone.

# to check the raspberry pi bluetooth name
$ sudo hciconfig hci0 name

# to set the bluetooth name
$ sudo hciconfig hci0 name raspberrypi

# make the raspberry pi discoverable
$ sudo hciconfig hci0 piscan

# setup agent to listen for pairing request
$ sudo bluetooth-agent 12345
# from my phone I connected to raspberrpi-0 and confirmed code was displayed
Confirmation request of 7654321 fir device /org/bluez/1936/hci0/dev_0E_C9_35_42_E6_E9

# turn of discoverable
$ sudo hciconfig hci0 noscan

# check connected devices
# list currently connected bluetooth devices
$ sudo bluez-test-device list
BC:B0:0B:47:57:A8 matt’s keyboard
FA:C7:48:46:47:33 Logitech Bluetooth Mouse M555b
0E:C9:35:42:E6:E9 Galaxy Nexus

Setup Tethering

To tether to android phone for internet we need a little more configuration

# package required for 'pand' command
$ sudo apt-get install bluez-compat

# connect to phone and sets up network interface
$ sudo pand --connect 0E:C9:35:42:E6:E9 -n

# show networks
$ ifconfig
bnep0     Link encap:Ethernet  HWaddr 00:4d:9c:2a:47:94  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12 (12.0 B)  TX bytes:60 (60.0 B)

# set dhclient to use bnep0 
$ sudo dhclient bnep0

# test internet connectivity
$ ping
PING ( 56(84) bytes of data.
64 bytes from ( icmp_req=1 ttl=52 time=26.3 ms
64 bytes from ( icmp_req=2 ttl=52 time=26.6 ms

Turning off tethering

To turn of tethering and unconfigure the PAN network

# release bnep0 dhclient
$ sudo dhclient -r bnep0

# disconnect pan
$sudo pand -K

Create an alias to make this tethering easy peasy

As I’ll probably turn tethering on and off lots of times I put all this into an alias

$ vi ~/.bash_aliases
alias leash='sudo pand --connect 0E:C9:35:42:E6:E9 -n && sudo dhclient bnep0'
alias unleash='sudo dhclient -r bnep0 && sudo pand -K'

After reloading bash I can simply type the following to tether or untether

$ leash
$ unleash

If you haven’t already got it installed you should also put Power Toggles on your phone so you can quickly turn wifi / bluetooth tethering on and off.

USB Bluetooth dongle and keyboard

As I am running my raspberry pi without a network (until I can grab a usb wifi dongle) I’m using a spare usb bluetooth dongle and bluetooth keyboard I had lying around.

Bluetooth Installation

$ sudo apt-get install bluez python-gobject

# check bluetooth dongle it recognised
$ hcitool dev
	hci0	7B:13:75:FD:11:EB

# scan for discoverable devices
$ hcitool scan
Scanning ...
	BC:B0:0B:47:57:A8	matt’s keyboard
	FA:C7:48:46:47:33	Logitech Bluetooth Mouse M555b

# pair with keyboard
$ sudo bluez-simple-agent hci0 BC:B0:0B:47:57:A8
RequestPinCode (/org/bluez/3465/hci0/dev_BC_B0_0B_47_57_A8)
Enter PIN Code: 12345
# type pin + enter on keyboard
New device (/org/bluez/3465/hci0/dev_BC_B0_0B_47_57_A8)

# pair with mouse
$ sudo bluez-simple-agent hci0 FA:C7:48:46:47:33
RequestPinCode (/org/bluez/2015/hci0/dev_FA_C7_48_46_47_33)
Enter PIN Code: 0000
New device (/org/bluez/2015/hci0/dev_FA_C7_48_46_47_33)

# add devices to trusted list and connect
$ sudo bluez-test-device trusted BC:B0:0B:47:57:A8 yes
$ sudo bluez-test-device trusted FA:C7:48:46:47:33 yes
$ sudo bluez-test-input connect BC:B0:0B:47:57:A8 yes
$ sudo bluez-test-input connect FA:C7:48:46:47:33 yes

# reboot and test if keyboard connects
$ sudo shutdown -r now

# list currently connected bluetooth devices
$ sudo bluez-test-device list
BC:B0:0B:47:57:A8 matt’s keyboard
FA:C7:48:46:47:33 Logitech Bluetooth Mouse M555b

As I’m using an apple wireless keyboard I needed to jump into raspi-config and reconfigure my keyboard settings

$ sudo raspi-config
# 3 - Internationalisation Options
# I3 - Change Keyboard Layout (wait a little for it to load)
# set keyboard to Apple Aluminium Keyboard (ANSI) and default options for rest of prompts

Raspbian Install

Load Raspbian Image onto SD Card

Installation was done using OSX terminal and a 16 GB SD Card (class 10).

  • Download latest Raspbian
  • Insert SD Card and open terminal
$ df -h
Filesystem      Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk2s1    15Gi  2.1Mi   15Gi     1%        0        0  100%   /Volumes/UNTITLED

$ diskutil unmount /dev/disk2s1
$ unzip

$ sudo dd if=2013-09-25-wheezy-raspbian.img of=/dev/rdisk2 bs=1m
# note: dd does not output anything until it finishes just be patient
2825+0 records in
2825+0 records out
2962227200 bytes transferred in 295.230490 secs (10033609 bytes/sec)

$ diskutil eject /dev/disk2s1

Thats it stick the sd card into the raspberry pi and turn it on.

For those who are curious:

Why am I using /dev/rdisk2 and not /dev/disk2? rdisk is a raw path, which is faster.

I tested it to see how much faster (it wasn’t worth the wait):
5 minutes using /dev/rdisk2 vs 25 minutes using /dev/disk2

Initial Config

Now that we have the raspberry pi booted up time to ssh in and configure a few basic things.

$ ssh pi@

# set your local time.
$ sudo dpkg-reconfigure tzdata

# update package list and upgrade to latest
$ sudo apt-get update && sudo apt-get upgrade

# configure raspberry
$ sudo raspi-config
# expand filesystem to fill sd card
# change the default password (raspberry) to something else

# reboot
$ sudo shutdown -r now && exit