teaching machines

Raspbian ‘Install’ and Networking Fun — the Long Overdue Post

October 2, 2012 by . Filed under buster.

The Install

Since James has already covered imaging the a SD card with Raspian from windows, I won’t go into my failed attempt at doing the same…

Instead, I’ll do a quick walk through of the process from OS X. The process is fairly straight forward as I was following along to the Easy SD Card Setup guid from elinux.

Under the section  “Copying an image to the SD Card in Mac OS X” we are instructed to do the following:

 

With imaging completed, it’s time to fire up the Raspberry Pi! During my first try on the pi, I used a Logitech k400 wireless Touch Keyboard (which is listed as problematic for some distribution, but I’ve had no problems), and a 40″ LED TV.

After some quick playing around, I realized, as James did, that the keyboard mapping was a bit off. To correct this I ran sudo raspi-config to open Raspian’s handy little configuration utility. The process went something like this…

 

Select the configure_keyboard option

I picked Logitech Generic Keyboard as the model I was using

Notice that all the options are for ‘English (UK)’ layouts. I’m guessing that is the default because the Raspberry Pi was design (and produced I believe) in England. So instead of picking a layout here, I went down the ‘Other’ option.

Next I selected ‘English (US)’ for the country of origin.

Finally normal (and not so normal) US key mappings are available!

After installing I did some various mucking about: installed vim, setup my various aliases and functions, change the font size because it was rather small from across the room, remapped caps lock to be control, etc.

Also, I copied over a 720p video to give it a test. It ran very smooth and looked great! After I figured out how to run video fullscreen I encountered a small problem. After quitting the video the there was some small problem with the frame buffer where everything would be black. After a quick search I found a solution and wrote it up into a simple script

#!/bin/bash
if [ $# -eq 0 ]; then
  echo "`basename $0`: missing source file. Usage - `basename $0` sourceFile"
  exit 1
fi

# play video in full screen
omxplayer -r -o hdmi "$1" > /dev/null

# reset frame buffer
#TODO supposedly possible to read depth before launching 
fbset -depth 8 && fbset -depth 16

Note: the ‘-r’ flag for omxplayer adjusts the frame rate and resolution to the video, and the ‘-o’ flag redirects the audio output.

 

Networking

Thus far I had been able to SSH into the Pi from my mac without any problem, but it was time for something more direct.

I had had quite a bit of practice using sockets in C, but never given them a shot in our target language, Python. If fact, I had never given anything a shot in Python, so it was time to consult the great and powerful Google. It turns out that sockets in Python are fairly similar to sockets in C, but with a lot less legwork to get something simple up and running.

This is the very slightly modified version of the echo server example from the Python socket documentation that I ran on the Raspberry Pi:

#!/usr/bin/env python
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)

while 1:
    conn, addr = s.accept()
    print 'Connected by', addr

    while 1:
        data = conn.recv(1024)
        if not data: break
        conn.sendall(data)

conn.close()

And here is the echo client example code from the same place that I ran on my mac:
# Echo client program
import socket

HOST = '192.168.1.107'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

Here's a quick screen shot of the execution, the mac shell is on top, and the raspberry pi is on botton

The only issues I ran into trying this out were some small problem with the editor messing with the indentation that Python didn’t appreciate.