RPi

From Bjoern Hassler's website
Jump to: navigation, search

A page to record some of my raspberry pi experiments. http://www.raspberrypi.org/

1 Installation[edit]

Save yourself some pain by using branded additional parts, e.g. for the micro SD card, as well as a good PSU (rather than an old phone charger you have lying about).

1.1 Raspian OS installation[edit]

Installation instructions here: https://www.raspberrypi.org/learning/quick-start-guide/quickstart/, http://www.raspberrypi.org/help/noobs-setup/. Easiest is to use NOOBS image.

Some notes: I have previously used the NOOBS image successfully, but for some reason (due to the lack of a HDMI monitor) I couldn't get it work this time (with my composite monitor). Instead, you can just write the Raspian image onto an SD card. So I got the http://downloads.raspberrypi.org/raspbian_latest Wheezy Jessie image from http://www.raspberrypi.org/downloads (with Jessie there is a 'full and a 'light' version available).

  • Writing the disk image to an SD card
    • dd - use this guide http://elinux.org/RPi_Easy_SD_Card_Setup to install the image with dd. Note that you need to use "rdisk" rather than "disk", i.e./dev/rdisk... - I had previously used /dev/disk, and it didn't work.
      • "disk arbitrator" can be useful to find the disk
      • sudo dd if=image.img of=/dev/rdiskXXX bs=1M
    • etcher - GUI-based writing of image, recommended on the Raspberry Pi site [1]. While I've used dd before, the advantage of Etcher is that it verifies the image install. (Yesterday I spent a few hours trying to get a Raspberry Pi to boot headless, and only when I used Etcher I realised that out of a stack of 4 cards, I'd picked the corrupted one. Bjoern 11:51, 15 January 2017 (UTC) - Actually, the cards are unbranded, and are probably corrupt - today I experienced issues with eh 2nd card. Bjoern 19:31, 15 January 2017 (UTC))

You now should now have an SD card with a working OS installation. A few more tweaks while you're writing the SD card:

  • ssh is disabled by default in Jessie [2]. When the card is written (and you are aiming for a headless setup), place an empty file called 'ssh' onto the boot partition [3].
  • Wireless: It's possible to pre-configure wireless:
    • [4] says "If a wpa_supplicant.conf file is placed into the /boot/ directory, this will be moved to the /etc/wpa_supplicant/ directory the next time the system is booted, overwriting the network settings; this allows a Wifi configuration to be preloaded onto a card from a Windows or other machine that can only see the boot partition." Further details below.

Once that's all done, pop the card into he Pi, and boot.

  • For ssh, you will need to look up the IP address on my router (the hostname raspberrypi didn't work), and logged in with "ssh pi@ip_address".
    • Depending on your network setup, "ssh pi@raspberrypi" should also work.
  • Previously you would have wanted to expand the image to fit the whole sd card: sudo raspi-config
    • However, [5] says: "When flashing a new Raspbian image, the file system will automatically be expanded to use all the space on the card when it is first booted."

1.1.1 Logging in without password / disabling password-based login (more details below)[edit]

You may also want to set up password less login (or at least change the password) [6]

sudo nano /etc/ssh/sshd_config

and make sure it has the line

PasswordAuthentication no

(no # in front of the line)

Then reboot or restart sshd

sudo /etc/init.d/ssh restart

1.1.2 Edit the host name[edit]

If you have several Raspberry Pi on your network, you'll want to change the hostname:

sudo nano /etc/hostname

and enter your hostname

1.2 Wireless / Wifi[edit]

1.2.1 During SD card installation via /boot[edit]

It's possible to pre-configure wireless, see [7] (c.f. above), "If a wpa_supplicant.conf file is placed into the /boot/ directory, this will be moved to the /etc/wpa_supplicant/ directory the next time the system is booted, overwriting the network settings".

Typically, the wpa_supplicant.conf file looks something like this:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
   ssid="..."
   psk="..."
}

So you just need to fill in your ssid and password, and the Raspberry Pi will automatically connect to that network.

1.2.2 Manual Wireless (when logged into Raspberry Pi)[edit]

See [8]. Run

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and add

network={
   ssid="SSID"
   psk="wifi_password"
}

Helpful commands: Scan for networks:

sudo iwlist wlan0 scan 

Which network am I connected to?

iwgetid -r

1.2.3 Wireless power management[edit]

Previous notes here: RPi/installation/Wireless (includes issues with wifi dongle turning itself off). This also occurs with the official Raspberry Pi dongle, and must be to do with the power management. Possible solution here: https://www.raspberrypi.org/forums/viewtopic.php?t=46569&p=647343

Run

iwconfig

and look out for

Power Management:on

If it's "on", then

sudo emacs /etc/network/interfaces

Add

wireless-power off

Restart networking

sudo /etc/init.d/networking restart

Check

iwconfig

You should see

Power Management:off

1.3 update[edit]

apt-get update 
apt-get upgrade

Not sure whether this is needed:

sudo apt-get install raspberrypi-bootloader

It's given in some instructions, but when I ran it, it was already at the latest version.

1.4 network performance[edit]

It's always good to know what the network throughput is, so iperf is your app:

sudo apt-get install iperf

1.5 Which revision do I have?[edit]

There are different revisions of the Raspberry Pi model B about, and you can type

cat /proc/cpuinfo

to find out what revision you have, c.f. http://elinux.org/RPi_HardwareHistory (and previous pages here http://www.raspberrypi.org/archives/1929 and http://www.raspberrypi.org/archives/tag/rev2). Revisions table also available here.

1.6 file sharing with mac/afs[edit]

1.6.1 Setting up the server (on Raspberry Pi)[edit]

If you have a Mac, file sharing using AFS is really easy:

sudo apt-get install netatalk

See this article on the Raspberry Pi forums regarding e.g. sharing a USB drive mounted on "/media".

sudo /etc/init.d/netatalk stop
sudo nano /etc/netatalk/AppleVolumes.default

and amend /etc/netatalk/AppleVolumes.default as follows, e.g. to share

  • ~
  • ~/some_dir
  • and a drive / USB stick mounted in /media:
:DEFAULT: options:upriv,usedots,tm,rw
# By default all users have access to their home directories.
~/ "Home Directory"
~/some_dir "Some DIr"
/media/DRIVENAME "DRIVENAME"
# End of File

Then restart netatalk:

sudo /etc/init.d/netatalk start

(You can also use "restart".)

1.6.2 Connect (on Mac)[edit]

On Mac, the share should now appear in the finder, and you can connect (using username/password for your rpi)

1.6.3 Setting up the client (on Raspberry Pi)[edit]

You may also want to connect to the share from another Raspberry Pi (to save you setting up another file sharing protocol). Suppose you have shared a directory called MY_SHARE on HOST, with user USER and password PASSWORD. On the client, install:

 sudo apt-get install afpfs-ng
 sudo apt-get install fuse

Make a mount point

sudo mkdir /media/my_share
sudo chown pi /media/my_share
sudo chgrp fuse /media/my_share

You may have to add the user 'pi' to the group 'fuse'

sudo usermod -a -G fuse pi

(log in/log out after that, and check with 'groups' that it's works) Then

sudo nano /etc/fuse.conf

and uncomment "user_allow_other". [9] You may then have to restart. Then

mount_afp 'afp://USER:PASSWORD@HOST/MY_SHARE' /media/my_share

Overall, this works ok, except that occasionally the mount just disappears. Re-mounting works.

1.6.4 On a tangent: Attaching hfs+ drives to the pi[edit]

NOTE: When I attached hfs+ formatted drives to the pi, the fs got corrupted pretty quickly. In a couple of cases it could be repaired, in another case it couldn't. So probably not stable enough for rw access.

1.7 filesharing with another pi[edit]

I did try NFS, and encountered some problems.

sshfs worked. On the cient, you can use

sudo sshfs -o allow_other pi@<ip>:/media media

Or, if you have password-free login, then on the client you need to say

usermod -aG fuse pi

(for user 'pi'), so that you can mount shares as user 'pi', and thus use the ssh credentials.

sshfs pi@<ip>:/media media

1.8 login without password[edit]

Assuming there is no .ssh/id_rsa.pub on the first computer, then this bash script will set up password less login from the first computer into SERVER with user USER (for details, see http://www.linuxproblem.org/art_9.html):

export USER='USER'
export SERVER='SERVER'
ssh-keygen -t rsa
ssh ${USER}@${SERVER} mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh ${USER}@${SERVER} 'cat >> .ssh/authorized_keys'

Now you can login from the first computer to the SERVER as user USER without password:

ssh ${USER}@${SERVER}

1.8.1 Prohibit logging in with password[edit]

Now that you've got key-based login working, you might want to disable password-based login. See e.g. http://www.linux.org/threads/how-to-force-ssh-login-via-public-key-authentication.4253/ for instructions.

In brief:

sudo emacs /etc/ssh/sshd_config

Change this settings

PasswordAuthentication no

(commented out or 'yes' by default). Check these settings:

RSAAuthentication yes
PubkeyAuthentication yes

(to make sure you can still log in). Reload ssh

sudo /etc/init.d/ssh reload

1.9 Sending Email[edit]

See iqjar: If you just want send email from the root user, you only need to do steps 1, 2, 3a. In brief, for email just for the rootuser:

sudo apt-get install ssmtp
sudo apt-get install mailutils
sudo emacs /etc/ssmtp/ssmtp.conf

and enter:

root=postmaster
mailhub=smtp.gmail.com:587
hostname=raspberrypi
AuthUser=your_gmail_user@gmail.com
AuthPass=your_gmail_password
UseSTARTTLS=YES

If you want to use mutt to send messages:

sudo apt-get install mutt

and

echo "test message" | mutt -s "a test" -- email_address@domain
echo "test message" | mutt -s "a test" -a file.txt -- email_address@domain

1.10 Buying components[edit]

I should write something here about buying components, like PSU, speaker, wifi dongle (see above) etc.

Connecting to a screen: HDMI to DVI: I bought this connector http://www.ebay.co.uk/itm/110709897629 for £2. It converts the HDMI output of the RPi to DVI, and will thus work with a DVI screen. Works well!

  • VGA. You may be able to connect the RPi to a VGA screen that you have. However, this will only work if the VGA screen is "relatively modern" and can take a "DVI over VGA" signal.
  • Composite. You should also be able to use composite. I've had problems with that.

Speaker. It's handy to have a speaker connected to the RPi. I got this one http://www.ebay.co.uk/itm/141083562291 for £1, but it's very quiet. So not ideal.

Wifi. See above.

PSU. Trying to find a cheap PSU for it. It's possible to get a 1A PSU for £5+, but trying to find something cheaper. Bjoern 11:38, 8 January 2014 (UTC) Get a good 2A PSU for it. I'd recommend either getting the official one, or one that's recommended for use with Raspberry Pi.

You might also want to get:

  • Kits of electronical components (like a resistor pack)
  • Breadboard / breakout cable
  • Jump wires
  • Temperature sensors

Things to experiment with:

Also see components.

1.11 Reducing wear on SD card (e.g. for 24-7 headless use)[edit]

An idea to reduce SD card wear is to place certain directories into a RAM disk. Adjust the fstab

sudo nano /etc/fstab

E.g. [10], [11] suggest:

tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
tmpfs /var/run tmpfs defaults,noatime,nosuid,mode=0755,size=2m 0 0
tmpfs /var/spool/mqueue tmpfs defaults,noatime,nosuid,mode=0700,gid=12,size=30m 0 0

[12] suggests:

tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
tmpfs /var/log/apt tmpfs defaults,noatime 0 0
tmpfs /var/cache/apt/archives tmpfs defaults,noexec,nosuid,nodev,mode=0755 0 0

1.12 media tools: avconv, ffmpeg[edit]

Raspbian, as a derivative of Debian, doesn't come with ffmpeg. You can install avconv via

sudo apt-get install libav-tools

However, if you do need ffmpeg, you can get the binary from the motioneye repo

wget https://github.com/ccrisan/motioneye/wiki/precompiled/ffmpeg_3.1.1-1_armhf.deb
sudo dpkg -i ffmpeg_3.1.1-1_armhf.deb

Hannes ihm sein Blog also has a recipe for building from source

mkdir ffmpeg
cd ffmpeg

# build and install x264
git clone --depth 1 git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make -j 4
sudo make install
cd ..
 
# build and make ffmpeg
git clone --depth=1 git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --arch=armhf --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make -j 4
sudo make install
cd ..
cd ..

1.13 Finally[edit]

The microSD card slots in the B+ and 2B are 'push to lock', and unfortunately they don't feel that securely stuck in. For instance, it is easily possible to 'brush out' the microSD card (i.e. run your thumb over the card, and it can come out quite easily). If your Raspberry Pi is moving around a lot, you may want of put a bit of electrical tape over the slot in your case (that lets you access the SD card), to prevent accidental ejection. In the 3B and Zero, the microSD card slot has been changed for a friction fit, which feels much securer [13], and the card is much harder to pull out.

2 Project 1: Temperature fluctuations in winter[edit]

Given that it's winter, I always wondered how good my heating controls (with combined timer/thermostat) are at getting the right temperature. Given that there is only one thermostat in a central location, and that the controls on the radiators are manual, how does the temperature varies in different rooms? How cold does get during the day or during the night (when the heating is low)? Ideal for some Pi data logging.

There are various recipies about, e.g. http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/

then cd to the id for the sensor, followed by

 cat w1_slave

gave the temperature!

At the moment it's 19.5 degrees, and data is continuous logged to a file (one per minute), together with a (manually provided) label for the location.

Bjoern 13:09, 7 December 2013 (UTC): Make sure you connect the sensor the right way round! I just got a mini breadboard, so rewired my arrangement somewhat less carefully than first time round, and promptly connected the sensor the wrong way round. It got nice and hot in the 1-2 seconds before I noticed, and now claims that my room temperature is a more or less constant 22 degrees C. Luckily I had a spare!

2.1 Speech synthesis to announce the temperature[edit]

So now it would be nice if the pi could actually say the temperature. See http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis) for instructions:

sudo apt-get install mplayer
sudo nano /etc/mplayer/mplayer.conf

and add ("no Linux Infrared Remote Control"):

nolirc=yes

And then text-to-speech:

sudo apt-get install festival
sudo apt-get install espeak

2.2 BlinkStick to show the temperature[edit]

It's quite clear to me that if you're running the RPi headerless, that you need some sort of feedback from it: Has it booted? Is it still running? Does it have network? It'll be easy to do with an LED (which admittedly is also one of the first recommended projects you might do). I haven't bought an LED set yet, but a friend happened to give me a BlinkStick he had lying about. So I am going to use the BlinkStick to get system information.

sudo apt-get install python2.7-dev
sudo apt-get install python-pip
sudo pip install blinkstick

While I got the blinkstick to work, and it makes pretty colours, it seems to interfere with the Edimax wifi dongle and networking in general. Tell me about Schrödinger's cat. Oh well. I'll get some LEDs. Bjoern 16:02, 7 December 2013 (UTC) Or maybe it wasn't that - the edimax still shuts down ever so often. Why? Bjoern 19:15, 7 December 2013 (UTC)

You have to be ever so careful with the power supply. There aren't any problems when wired ethernet is connected, but with the wifi dongle and the blinkstick, the draw was too much for the power supply! So have now put better power supply onto the pi, and it works. (Incidentally, powering the Pi from the USB port on my router seems to work as well, at least to run the Pi alone, on wired ethernet. Connecting other usb devices to it doesn't work, so it's probably only marginally enough.)

The way I have the blinkstick set up is to give these six colours:

       (0     0     0   off)
0      0     0   255 blue
1      0   255 255 cyan 
2      0   255   0   green 
3    255 255   0   yellow
4    255   0     0   red
5    255   0   255 magenta 
6    255 255 255 white

It's of course completely possible to set these up differently, but the colours like this work well (magenta is quite bright). You could extend the range downwards by fading the blue to off, but the blue is fairly faint already. When using this for temperature, you can fairly easily distinguish a range about 6 degrees, which should be plenty for most temperature preferences (for indoors, i.e. a range of a few degrees above or below 20 degrees C)!

Because it's good to know that the RPi is running, the LED turns off once every minute. If there's network, it just immediately turns back on. However, if there is no network, it blinks a few times. So you know the RPi is active, but network is lost. That's not bad!

3 Project 2: Image capture and motion detection[edit]

3.1 Using motion[edit]

Motion can be used for image capture. I thought it might be nice to capture images of the "environment" alongside temperature.

sudo apt-get install motion

You then need to edit the config file:

/etc/motion/motion.conf

And enable the daemon (if you are going to use it):

/etc/default/motion

Then start the service:

sudo service motion start

3.2 Using a PIR sensor[edit]

See e.g. this guide for the Adafruit PIR sensor [14] on the PIR sensor. Note:

  • The jumper switch: non-retriggering (jumper towards edge, H) vs. retriggering (jumper towards middle, L). If you have something that "edge triggered", you should have the "L" setting (jumper more to centre of board)
  • The pods, for
    • timeout - duration for which the output is high after detecting motion (in the pictures on the above link, you can see the positions are labeled as min/max)
    • sensitivity: clockwise makes it more sensitive (also labeled as min/max).

My PIR sensor doesn't seem to be an Adafruit one, and while it has similar switch/pods, they aren't quite in the same locations. Compared to the adafruit one, the jumper is to the right of the pods (with the cap on top, looking at the pods).

  • Retriggering is with the jumper towards the edge, non-retriggering is with the jumper to the centre (opposite of adafruit)
  • Pods
    • Same as Adafruit, the left pod (cap on top, looking at the pods) does timeout. Shortest time about 3 secs, and easily up to a few mins.
    • The other pod is sensitivity.

3.2.1 Important: Interference[edit]

I've not seen this mentioned on pages describing the PIR use, but apparently the PIR sensor can be susceptible to interference, see https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=112111&p=1120330.

3.3 Image capture via camera board[edit]

To follow. raspistill / raspivid.

4 Project 3: And now for some light entertainment: Audio and video[edit]

4.1 omxplayer[edit]

For playing video on your Raspberry Pi, use omxplayer.

4.2 Pi-DAC+[edit]

There's various higher quality audio boards, including the Pi-DAC+. Instructions are here: http://www.iqaudio.com/downloads/IQaudIO.pdf

# http://www.iqaudio.com/downloads/IQaudIO.pdf
dtoverlay=iqaudio-dacplus

# Raspbian with kernel 4.4.14+  (later versions of Jessie), use
# dtoverlay=iqaudio-dacplus,auto_mute_amp

With Raspbian Jessie, I had no problems. However, I had an old card, that was initially on an older version of Raspbian (on a Raspberry Pi B, early 2013), which didn't have i2c enabled by default. Hence

sudo nano /etc/modprobe.d/raspi-blacklist.conf

and remove i2c-bcm2708 so it's not longer blacklisted.

If you have soldered on the angled header to get access to GPIOs (underneath the board): The 40 pins are just shifted to the side (looking at the board from the top), as you would expect. The first 10 (soldered ontop, to provide power to the amp) are just the usual 10 pins. The remaining 30 are the usual when seen from the top. However, because of the angled connector soldered in underneath, the two long rows swap sides (mirror image, because it's soldered from underneath, rather than ontop, if you see what I mean).

5 Project 4: Using your raspberry pi as an Apple time machine[edit]

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=41&t=4686 http://kremalicious.com/ubuntu-as-mac-file-server-and-time-machine-volume/ http://bastibe.de/2012-07-29-using-raspberry-pi-as-time-machine.html

You need to edit these files:

sudo emacs /etc/netatalk/AppleVolumes.default
sudo emacs /etc/hosts.allow
sudo emacs /etc/avahi/services/afpd.service
sudo emacs /etc/netatalk/afpd.conf
sudo touch /media/TimeMachine/.com.apple.timemachine.supported
sudo /etc/init.d/avahi-daemon restart
sudo /etc/init.d/netatalk restart

Remember that your permissions must enable writing:

sudo chown pi:pi /media/TimeMachine
sudo chmod 755 /media/TimeMachine

6 Raspberry Pi-powered Diddy Borg[edit]

I used the DiddyBorg some time back, but thought I'd give it another go. The SD card runs Wheezy, which I "sudo apt-get update/gradede"(d), to make it work with the v2 camera. Enable camera and I2C via

  • sudo raspi-config

(or via the command line). Reboot. Install opencv:

  • sudo apt-get install python-opencv

Then

(see https://github.com/piborg/ for code).

Details are here:

6.1 Running diddy-web[edit]

You may have to flip the camera, see [15]. In diddyborg-web/diddyWeb.py, locate:

camera = picamera.PiCamera()
camera.resolution = (imageWidth, imageHeight)
camera.framerate = frameRate

and add the line

camera.vflip = True

Also, you may want to edit

sudo nano /etc/rc.local

to make diddy-web start automatically. I also added a tiny motion of the diddyborg to indicate when the programme is ready. So I added two lines:

sudo python /home/pi/diddyborg-web/diddyHello.py &
sudo python /home/pi/diddyborg-web/diddyWeb.py &

where diddyHello.py executes a tiny (5 degree) turn - big enough to let me know it's running, but small enough for the diddyborg to not fall off the table. With the 2nd line added to rc.local, the web interface runs at startup. (You'll need to know the IP address.)

Over time, the battery case doesn't hold the AAs very firmly anymore. I might pull out the springs again, or jam the AAs in a bit more, using some padding / rubber bands.

Final tip: The bar at the bottom of the web interface adjusts the speed. Reduce the speed to 50% until you're familiar with the field of view. I managed to flp the diddyborg over.

7 The Sense Hat[edit]

7.1 Important caveats[edit]

7.1.1 If using the joystick[edit]

If you are still on Wheezy, you'll need this fix for doing anything joystick related: https://www.element14.com/community/community/raspberry-pi/raspberry-pi-accessories/blog/2017/01/23/raspberry-pi-sense-hat-enabling-the-joystick

7.1.2 The temperature sensors[edit]

Note that the temperature reported by he temperature sensors will need to be corrected for proximity to the Raspberry Pi itself, see

This is disappointing, as the Sense Hat is designed as a Hat, i.e. is means to sit ontop of the Raspberry Pi (rather than moved away using an extension...).

7.2 Getting started[edit]

Some good scripts to start with are here https://github.com/bennuttall/sense-hat-examples

  • sparkle
  • temperature
  • humidiy

you'll also want to turn the LEDs off:

from sense_hat import SenseHat
sense = SenseHat()
sense.clear()  # no arguments defaults to off

and show messages:

from sense_hat import SenseHat
sense = SenseHat()
sense.show_message("Hello world!")

There's also examples here: https://github.com/RPi-Distro/python-sense-hat/tree/master/examples

API doc: http://pythonhosted.org/sense-hat/api/

To be continued...


8 Finally[edit]

Also see, http://oer.educ.cam.ac.uk/wiki/Raspberry_Pi