Raspberry Pi Lessons


Oh, raspberry pie! I like it

Topic 1


Raspberry Pi Setup

Cool, you got your Raspberry Pi. Now what?

There are wealth of information and projects you can find on the web. And you can always start with the official Raspberry Pi Documentation.

For now, let's get it started! You can either use a NOOBS sd card, or download one image to load on to a sd card to install the OS. At least for the first time setup, we suggest you have the following equipment ready:

  • Need a computer to burn OS image to a SD card, if you are not using NOOBS
  • TV or monitor with HDMI port, HDMI cable
  • USB keyboard and mouse
  • 5V 2A Micro USB power supply

After first time login and setup some basics, you can connect to Raspberry Pi using other simpler approaches that do not require monitor, nor USB keyboard/mouse. The first thing is to enable SSH and Serial by running raspi-config.

Notes

  1. Load OS image to SD Card, using dd command:
    sudo dd bs=4m if=2016-11-25-raspbian-jessie.img of=/dev/rdiskN
  2. Connecting to Raspberry Pi from computer directly using Ethernet cable. If you have a Macbook, you can access it with hostname: raspberrypi.local
    ssh pi@raspberrypi.local, then login with default password: raspberry
  3. Connecting to Raspberry Pi using USB-TTL console. You can find instruction on: adafruit learning

Shrink SD card image for backup

Reference: Shrink using GParted on Ubuntu, or Resizing an image.
Here is my outline by doing on a Ubuntu box:

  1. Find out the SD card mount: df -h
  2. sudo gparted
  3. Select the right disk for SD card, such as /dev/sdc on the up-right corner
  4. Unmount the large partition, and resize it to a little bit larger than necessary
  5. Create shrinked disk image with dd.
     
        sudo dd if=/dev/sdc of=/home/USERNAME/shrink_image_name.img bs=block_size count=block_count.
        For example, if you shrink the space to 3.6G, you can use block_size=4M and block_count=900, 4M x 900 = 3600M
    
  6. On file explorer, eject the SD card and pull it out

Restore backup image

Be very very careful with next command, you need to be definitely, positively sure that "/dev/diskN" is the SD card where you want to put raspberry pi, replace "diskN" with actual disk number. If you are not sure, just don't do it, and wait until we meet next time. You could run the risk of wiping out your own computer!!
On Macbook:

    diskutil list   // to find out the disk number N of your SD card
    diskutil unmountDisk diskN
    sudo dd bs=4m if=downloaded_image_file of=/dev/rdiskN
Afterwards, eject SD card from Finder window, and pull out the SD card.

In order to get into Raspberry Pi headless, please add this two files under /boot directory (usually /Volumes/boot on Mac)

  1. To enable SSH by putting a file named "ssh", with any text content
  2. Add a wpa_supplicant.conf, to connect to your network. File content as follow:
    # /etc/wpa_supplicant/wpa_supplicant.conf
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
      ssid="your_wifi_ssid"
      psk="your_wifi_passwd"
      key_mgmt=WPA-PSK
    }    
    

Topic 2


Network Setup

This is pretty much the most important thing you care about. You need network connection to go on Internet, and you will be untethered to Raspberry Pi if you get "wifi" going.

To check network status, at command line, type ifconfig. To scan for WiFi network, type sudo iwlist wlan0 scan.

  • edit /etc/wpa_supplicant/wpa_supplicant.conf
  • change /etc/network/interfaces
  • sudo reboot
  • check output of ifconfig, look for wlan0

Notes

  1. Change wpa_supplicant.conf, to let Raspberry Pi connect to your WiFi network
    sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
    Then add the following section to wpa_supplicant.conf
    network={
        ssid="your_wifi_ssid"
        psk="your_wifi_password"
        key_mgmt=WPA_PSK
    }
    
  2. Change network interface file, so network adaptor will use wpa_supplicant.conf for network connection. Add the following lines to interface file.
    sudo vi /etc/network/interfaces
    
    auto lo
    iface lo inet loopback
    
    allow-hotplug wlan0
    iface wlan0 inet dhcp
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp
    

Enable VNC

sudo raspi-config; then go to Interface Options; set vnc to Yes.

When running Pi in headless mode, need to bring up a virtual desktop for VNC to connect to. Login to Pi, and start

vncserver &
then note down the IP address and display number, and use that information in your VNC viewer to connect to this display. When it is done, you can stop the virtual desktop on Pi by
vncserver -kill :[display_number]

Topic 3


Automatically get Raspberry Pi IP Address

  1. Install email program: mutt
    sudo apt-get install mutt
    
  2. Configure basic .muttrc for use with Gmail SMTP to send email, under /home/pi and /root
    set smtp_url="smtp://your_gmail_account@smtp.gmail.com:587/"
    set smtp_pass="your_gmail_password"
    set from="your_gmail_account@gmail.com"
    set realname="Your Name"
    
  3. Create a script to send IP address to a mobile phone in text message. Let's call it "notifyIpAddress.sh"
    #!/bin/bash
    # Email-to-SMS Gateways
    # number@txt.att.net / @vtext.com / @messaging.sprintpcs.com / @itelcel.com
    
    HOSTNAME=`hostname`
    # EMAIL=PHONENUMBER@txt.att.net
    # EMAIL=1234567890@vtext.com
    
    ETH=`ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'`
    WLAN=`ifconfig wlan0 | grep inet | awk '{print $2}' | sed 's/addr://'`
    
    echo "eth0 ip: $ETH, wlan0 ip: $WLAN" | mutt -s "$HOSTNAME ip:" $EMAIL > /dev/null
    
    Make sure to set the script executable: chmod +x notifyIpAddress.sh
  4. Add a line to call this script in /etc/network/interfaces file, immediately under wlan0 section.
    ## trigger notifyOnStartup script to send out notification
    post-up /home/pi/notifyIpAddress.sh &
    

Topic 4


Unix command line and shell environment

Reference to Linux commands for beginner

A more comprehensive and advanced reference to Linux shell environment and shell script

Topic 5


vi or vim editor

Reference to vi cheatsheet for beginner

A more comprehensive reference: vi editor tutorials

Topic 6


Build a Raspberry Pi based Amazon Echo

1. Setup speaker for voice output

Install software Text-to-speech software on reaspberry pi. Reference link. You can use any of the package: festival, espeak, pico

    sudo apt-get install festival

Also setup omxplayer to play .mp3 file over a speaker. You can copy .mp3 file from your computer to Raspberry Pi for testing, or use "wget" or "curl" to download .mp3 from Internet onto Raspberry Pi. Or copy one from your computer to Raspberry Pi:

    scp sample.mp3 pi@[your_raspberrypi_ip_address]:/home/pi
You can adjust the spearker volume by using amixer, such as:
amixer sset PCM,0 60%

2. Setup microphone for voice input

We will use PS3 Eye, which is a great inexpensive webcam too, with 4 speaker array on the back. We can use it for audio input, as well as taking video and image for future project of image / face recognition.

  1. Install motion package:
    sudo apt-get install motion
  2. Install audio/video package:
    sudo apt-get install libav-tools
  3. Plug in and make sure Pi recognize PS3 Eye as /dev/video0, if not, reboot it. Also this may help -
    sudo modprobe bcm2835-v4l2
  4. Find the audio hardware port
    arecord -l
  5. Record audio:
    arecord -D hw:1,0 -f s16_le -c 4 filename.wav
  6. Use omxplayer to check the recording:
    omxplayer filename.wav
  7. Capture image and video:
    sudo motion -n
Reference: Motion setup, Stream audio

3. Setup AVS - Alexa Voice Service

Reference: Alexa Voice Service sample wiki

Note: before you start the automatic installation process, please use "sudo raspi-config" to "expand file system" first, so we have enough disk space for the installation.

During automatic installation, please observe the output log information. You may see some warning messages, which may be fine. If you see error messages, pay attention to those and may need to be handled.

If everything goes smoothly, you may see this messages at the end.

 To run the demo, do the following in 3 seperate terminals:

Run the companion service: cd /home/pi/alexa/alexa-avs-sample-app/samples/companionService && npm start

Run the AVS Java Client: cd /home/pi/alexa/alexa-avs-sample-app/samples/javaclient && mvn exec:exec

Run the wake word agent: 
  Sensory: cd /home/pi/alexa/alexa-avs-sample-app/samples/wakeWordAgent/src && ./wakeWordAgent -e sensory

  KITT_AI: cd /home/pi/alexa/alexa-avs-sample-app/samples/wakeWordAgent/src && ./wakeWordAgent -e kitt_ai

  GPIO: PLEASE NOTE -- If using this option, run the wake word agent as sudo: cd /home/pi/alexa/alexa-avs-sample-app/samples/wakeWordAgent/src && sudo ./wakeWordAgent -e gpio   

You need to use VNC to access Pi GUI interface, and run "Terminal" within the GUI environment, because the AVS Java Client requires GUI.

Topic 7


Sonic Pi - have fun programing with music

Use VNC to gain access to Raspberry Pi GUI mode, then start Sonic Pi, by going to "Raspberry" icon -> "Programming" -> "Sonic Pi" to bring up Sonic Pi application.

Reference materials:

  1. The source
  2. Sonic Pi Tutorial on github
  3. Sonic Pi on Raspberry Pi Learning Resources

Please make a piece of music at home, and we will share during next class.

Topic 8


Minecraft game- have fun controlling game programatically

Use VNC to gain access to Raspberry Pi GUI mode, then start Minecraft game, by going to "Raspberry" icon -> "Game" -> "Minecraft" to start the game.

Reference materials: Minecraft API

You can use IDLE env (python) to control and play the game, and you can write a program to remotely control the game too.

Topic 9


Voice Control - using AVS, AWS IoT and Lambda

Download "Reverb" app, which acts as Amazon Echo device. Also download "Alexa" app, so you can manage Alexa Skill Kit (ASK).

Reference materials:

  1. Some winning entries on Internet of Voice Control contests
  2. A simple example of Dogfeeder controlled by Alexa Skill Kit: DogFeeder
  3. Alexa AVS sample projects

Develop a ASK skill for home automation.


Our proper configured image for team sync up


Here is a Raspberry Pi image for sync up our Raspberry Pi environment setup. image as of 2017.01.15
You can use restore approach to load to your SD card.