Sep 20, 2013

Raspberry Pi As A Wi-Fi Access Point

Hey guys, happy Mid-Autumn Festival.  Tired of BBQ and mooncakes? Let's have a quick project by turning a Raspberry Pi into a Wi-Fi access point.  The first Pi image I tried was OpenWrt 12.09 (Attitude Adjustment), which is a very small Linux distro for embedded systems.  But turned out the image just doesn't work out of the box at the 1st boot.  A few boot files seem to be outdated and should be replaced by those from other distros like OpenELEC.  Really frustrated indeed :(.  So I fell back again to my home router with Raspbian installed.  There are lots of write-ups regarding making Pi a Wi-Fi AP already, but none of them worked for me tho :(.  After trying a few things out, I made my own steps.



Wi-Fi dongles


Buffalo WLI-UC-GNM W-Fi dongle was used in the test. See http://elinux.org/RPi_USB_Wi-Fi_Adapters for more hardware info.

$ lsusb | grep -i wireless
Bus 001 Device 004: ID 0411:01a2 BUFFALO INC. (formerly MelCo., Inc.) WLI-UC-GNM Wireless LAN Adapter [Ralink RT8070]


Package Installation


We need a few more packages to complete the task:

$ apt-get install hostapd isc-dhcp-server

Package Removal


 It took me some time to figure out a link detection daemon called ifplugd is bad for us to do the following configuration:
  • MAC address modification for interface eth0.  Yes, in my scenario the MAC address of the interface to the Internet is locked to a specific value.  This is not required for you guys I think.
  • Static IP address assignment for interface wlan0.  This is essential for further routing and forwarding configuration
$ apt-get remove ifplugd


Topology and Interface Configuration


  • WAN: eth0, Ethernet connected to the Internet. IP address is DHCP assigned.
  • LAN: wlan0, wireless interface for the hotspot. IP address is statically assigned to 10.8.8.254
$ more /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
  hwaddress ether 00:90:de:ad:be:ef

auto wlan0
iface wlan0 inet static
  address 10.8.8.254
  netmask 255.255.255.0


HostAPd Configuration


Quoted from the hostapd manpage, hostapd is an IEEE 802.11 AP, IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator.  For brevity most of the comments in the configuration files are removed, but you can easily look them up on the net.

$ more /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211

ssid=Pi-Fi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=MySecret
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP


The values in bold above can be changed to fit your needs.

$ more /etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"

DHCP Server Configuration


An IP addresses pool will be defined, along with some TCP/IP configuration to be dispatched to the clients.

$ more /etc/dhcp/dhcpd.conf
authoritative;
ddns-update-style none;
ignore client-updates;

subnet 10.8.8.0 netmask 255.255.255.0 {

    option routers                  10.8.8.254;
    option subnet-mask              255.255.255.0;
    option domain-name              "home.tw";
    option domain-name-servers      8.8.8.8;

    range 10.8.8.130 10.8.8.169;
    default-lease-time 86400;
}


We just listen on the interface wlan0:

$ more /etc/default/isc-dhcp-server
INTERFACES="wlan0"


Routing and NAT Configuration


Add the following lines to the file /etc/rc.local:

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE


That's it. Let's make some services start on boot, then reboot.

$ sudo update-rc.d hostapd enable
$ sudo update-rc.d
isc-dhcp-server enable
$ sudo reboot



After the reboot, if everything goes well our Wi-Fi SSID will be broadcasted in the air like a normal AP does.  Interfaces info follow:

$ ifconfig
eth0      Link encap:Ethernet  HWaddr
00:90:de:ad:be:ef 
          inet addr:192.168.111.193  Bcast:192.168.11.255  Mask:255.255.252.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:104189 errors:0 dropped:0 overruns:0 frame:0
          TX packets:84076 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:105045101 (100.1 MiB)  TX bytes:17906110 (17.0 MiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

mon.wlan0 Link encap:UNSPEC  HWaddr 4C-E6-76-D5-92-37-00-00-00-00-00-00-00-00-00-00 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32587 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3042110 (2.9 MiB)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 4c:e6:76:d5:92:37 
          inet addr:10.8.8.254  Bcast:10.8.8.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:85472 errors:0 dropped:0 overruns:0 frame:0
          TX packets:104367 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:17480008 (16.6 MiB)  TX bytes:108515924 (103.4 MiB)


Notice the IP address of wlan0 should be correctly assigned, and an extra interface mon.wlan0 will be brought up by hostapd as well.  In my test, three devices could get on the Internet simultaneously via this hotspot without any problem.

References:
  1. http://www.howtogeek.com/167425/how-to-setup-wi-fi-on-your-raspberry-pi-via-the-command-line/. But I didn't try the WPA supplicant part.
  2. http://elinux.org/RPI-Wireless-Hotspot. Does not work for me. :(

Jun 12, 2013

Guten Morgen - Raspberry Pi As A Morning Alarm

Since my home router is in my kids' bedroom, I think it'd be great if  an alarm feature can be added to the Raspberry Pi to kick their asses up in the morning.  Other than a RPi we're gonna need these peripherals:
  • a speaker with 3.5mm audio jack for audio playback
  • a motion sensor as a switch to stop the alarm
The motion sensor is implemented with a webcam that can do motion detection. With two addiotnal packages installed on Raspbian Wheezy, and lots of time trying out the motion detection configuration, our GutenMorgen morning alarm is ready to go.

Install the audio file player and motion detection software:

$ sudo apt-get install omxplayer motion

The morning call is triggered via crontab (6:39 AM, Monday to Friday)

$ crontab -l 
39 6 * * 1-5 cd guten-morgen; ./guten-morgen.sh

Once the audio file is being played, the motion detection starts and if a motion event was triggered, the omxplayer process will be killed to silence the speaker.

Screenshots:

Raspberry Pi as a morning alarm (w/ USB powered speaker and a USB webcam component on top of RPi)
Thank David Su for donating an engineering sample of webcam component designed for laptops.  Before that I've tried some webcams but with no avail.  Check the RPi Verified Peripherals page to see if your accessories work on RPi.

Close-up of the webcam component (as a motion sensor)
Live demo:

May 25, 2013

Raspberry Pi Overdose

More Pi's arrived.  One of them will be used to replace my current home router, which is an old laptop running Ubuntu. (See this blog for details)



The OS to be installed is Raspbian “wheezy”, but Raspbian is too heavy for me thus by running the following command, GUI stuff or large packages could be easily found.  After removing the unnecessary packages, the disk space occupied is reduced to 1.1 GB, which makes it fit very well in a 2 GB SD card.

List the packages by installed size:

$ dpkg-query -W -f='${Installed-Size} ${Package}\n' | sort -n

For more entertainment the case is hand-made using cardboard, check the Punnet project for details.


Mar 31, 2013

HTPC Comes True With Raspberry Pi

Today we're going to install a light-weighted Linux distribution - OpenELEC, to make a home theater PC (HTPC) with the Raspberry Pi.  OpenELEC is designed to be simple and fast by running XBMC in standalone mode to turn your PC into a media center.  Current latest version of OpenELEC is 3.0.0 as of this writing and the OS image could be downloaded HERE.  The installation takes less than 10 minutes with very small disk space footprints (less than 250 MB).  SSH server was enabled by default and you could login as root (password: openelec), but there's no passwd command available to change the password in a traditional way.  Poor security design!

Booting OpenELEC


openelec:~ # df -h
Filesystem                Size      Used Available Use% Mounted on
none                    185.1M     87.4M     97.6M  47% /dev
/dev/mmcblk0p1          124.7M     96.1M     28.7M  77% /flash
/dev/mmcblk0p2            1.7G     61.2M      1.6G   4% /storage
/dev/loop0               87.4M     87.4M         0 100% /
none                    186.5M         0    186.5M   0% /dev/shm
/dev/sda1               916.9G    727.4G    142.9G  84% /var/media/cove


openelec:~ # passwd
-sh: passwd: command not found


Although the CPU in Raspberry Pi is an ARM 700 MHz processor (could be over-clocked up to 1 GHz), but the GPU (Broadcom VideoCore IV) is fast enough for 1080p video playback, which is the key to an HTPC.  Since controlling an HTPC with a keyboard and mouse is way too geeky, we'll dump them and use an IR remote instead (shown below).


With the Raspberry enclosed in the black case, the composition looks less ugly tho.
Port usage in the USB hub (from left to right):
  • Power supply for the Pi
  • IR remote receiver
  • USB external hard disk


While playing 1080p video, the CPU temperature is like 62 °C (144 °F) , guess it could go up to 70 °C (158 °F) in summer, and the power consumption is less than 8W with peripherals attached shown above.

Hardware info of the Raspberry Pi

The last thing to do is enabling the AirPlay feature in XBMC.  Go to System -> Settings -> Services -> AirPlay to enable it, then your iOS devices could either play the music or display the photos to the XBMC screen (mirroring not feasible due to hardware limitation).

AirPlay feature in XBMC



Note that the iOS device and the XBMC must be within the same local network (or subnet to be specific) for the service to be broadcasted correctly. (no configuration is needed on iOS devices.) Click the AirPlay icon while browsing the photo to airplay it to the remote screen.

AirPlay icon on iOS

Photo on iPad airplayed to the remote XBMC screen

[Updated on Apr. 4, 2013]

Additional notes regarding the remote control, since the Raspberry Pi is CEC-enabled, which is a feature in HDMI specification that allows the user to operate multiple devices with one remote control, if your TV set is also CEC-enabled, you can just use your TV remote to control XMBC.  Pretty cool, huh?

Luckily the TV (Sharp LC-52GX30T) I used is CEC-enabled. To check if your TV supports HDMI-CEC or not, login to the OpenELEC and type "cec-client":

openelec:~ # cec-client
No device type given. Using 'recording device'
CEC Parser created - libCEC version 2.1.1
no serial port given. trying autodetect:
 path:     Raspberry Pi
 com port: RPI

opening a connection to the CEC adapter...
DEBUG:   [             108]     unregistering all CEC clients
DEBUG:   [             111]     Broadcast (F): osd name set to 'Broadcast'
DEBUG:   [             117]     InitHostCEC - vchiq_initialise succeeded
DEBUG:   [             118]     InitHostCEC - vchi_initialise succeeded
DEBUG:   [             120]     InitHostCEC - vchi_connect succeeded
DEBUG:   [             124]     logical address changed to Broadcast (f)
DEBUG:   [             125]     RegisterLogicalAddress - registering address e
DEBUG:   [             306]     logical address changed to Recorder 1 (1)
DEBUG:   [             307]     logical address changed to Free use (e)
DEBUG:   [             307]     Open - vc_cec initialised
NOTICE:  [             308]     connection opened
DEBUG:   [             314]     << Broadcast (F) -> TV (0): POLL
DEBUG:   [             314]     initiator 'Broadcast' is not supported by the CEC adapter. using 'Free use' instead
TRAFFIC: [             314]     << e0
DEBUG:   [             316]     processor thread started
DEBUG:   [             346]     >> POLL sent
DEBUG:   [             346]     TV (0): device status changed into 'present'
DEBUG:   [             346]     << requesting vendor ID of 'TV' (0)
TRAFFIC: [             346]     << e0:8c
TRAFFIC: [             538]     >> 0f:87:08:00:1f
DEBUG:   [             538]     >> TV (0) -> Broadcast (F): device vendor id (87)
DEBUG:   [             538]     TV (0): vendor = Sharp (08001f)
DEBUG:   [             539]     expected response received (87: device vendor id)

NOTICE:  [             539]     registering new CEC client - v2.1.1
DEBUG:   [             539]     detecting logical address for type 'recording device'


Or take a look at the lower-right corner after XBMC boots, a pop-up message will be shown (see image below) if a CEC-enabled device is connected.

Message shows this TV is CEC-enabled

Here's a quick test results on CEC features against my TV:
  • Use the TV remote to control XBMC - PASS
  • Shutdown XBMC and the TV will be turned off automatically - PASS
  • While TV is on, turn on the Pi and the TV video source will be switched to the XBMC automatically - PASS
  • While TV is off, turn on the Pi and the TV will be turned on automatically - FAIL
Details on the CEC features tested could be configured in the System / Settings / Input devices / Peripherals / CEC Adapter.

Mar 30, 2013

Hungry? Grab a Byte of Raspberry Pi

Yeah! New gadgets arrived.  A quick intro if you have no idea what it is.  Raspberry Pi is a credit-card-sized computer developed in the UK for educational purpose.  If you need a power-up-and-ready-to-go computer this is definitely NOT what you want.  This one was ordered directly from the RS Components website, with a black case and plus the shipping fee, the total cost is USD $49.49 (TWD $1480).  Still pretty affordable.

Shipped from the UK


Raspberry Pi and the black case

The Raspberry Pi (model B)

Since the Raspberry Pi was designed for educational purpose, we need to get the essential accessories and put them altogether to make it work.  Required items are:

  • 5V micro USB power supply
    • The standard USB to micro USB cable (the black cable shown below) is an accessory of an old Nokia 5310 mobile phone
    • AC to USB converter is an accessory of iPhones (the white one shown below)
  • An SD card (2GB or more recommended)
  • A USB keyboard and mouse (not needed if remote login is enabled after configuration)
  • TV or monitor with HDMI input (not shown below)

Required accessories

Optional but highly recommended items:
  • A USB hub, at least 4 ports, with external power source
  • An external USB hard drive
  • Internet connectivity
    • Will be used by software update and time sync via NTP protocol as there is no real-time clock integrated in the Raspberry Pi to keep the cost down.

Optional but highly recommended stuff
Next, we need an OS for the Pi to boot.  We'll be using Raspbian "wheezy" as it's the recommended Linux distro from the official site.  You can download it HERE, and follow the instructions on that page to dump the OS image to the SD card to make it bootable.  Once everything is ready, plug in the micro USB power supply and have fun.

  • By default, login as pi, with password raspberry
  • Type "startx" for the X Window, the graphical UI

Dinner is served ;)

Raspbian "wheezy" up and running

How to Update Flash Player Add-on on Linux Mint

It's been like a week or so the Firefox browser on Linux Mint (13, Maya) keeps deactivating the Adobe Flash Player, which is pretty annoying.  Here's a quick fix for this - remove the old package shipped by Linux Mint and get the one from Ubuntu Update Repo.

Type the following commands:

$ sudo apt-get remove mint-flashplugin-11
$ sudo apt-get install flashplugin-installer


Re-start your Firefox and you're good to go.

Outdated plug-ins got deactivated by Firefox

Before the update (original version)


After the update

Mar 25, 2013

ZyXEL P-6101C as Home Router and Wi-Fi Access Point

If you're using HiNet as your ISP in Taiwan, the ADSL modem they lent you could do much more than you think with a little more configuration.  Today we're going to turn our ZyXEL P-6101C ADSL modem into a home router, and a Wi-Fi AP.  Here's a list of services that we need to configure in this box:
  • DHCP server
  • PPPoE auto dial-up
  • Wireless service
By default, the IP address of the box is 192.168.1.1, and the login credential is fixed to 2 or 3 simple combinations based on the area you live.  It's publicly discussed so we're not going to cover this :b.  Please google it by yourself.

DHCP server 


Go to Setup -> LAN, and make a reasonable IP address pool.



On the same page, DNS server could also be configured.


Wireless service


Go to Wireless -> Basic to configure the SSID of your access point.


Setup auth type, encryption, and the passphrase.


PPPoE dial-up


Go to Quick Setup page, fill in your PPPoE username and password, and don't forget to enable the WLAN entry we previously configured.



If everything goes well, you'll see the new SSID in the AP list.  Happy surfing!