Educating the world

Our blog has over 10,000 readers a month

Installing and setting up a Raspberry Pi

July 15th, 2013

I’m doing this on the Mac this time because it comes with loads of tools pre-installed to help. I’m also assuming that the Raspberry Pi has a keyboard and an active network connection (wired or wireless) with a DHCP server floating about somewhere.

I am assuming that the Raspberry Pi has a keyboard and an active network connection (wired or wireless) with a DHCP server floating about.

  1. We need to install the operating system on the SD card. For this we will need to be the root user. You could prefix all the commands with sudo, but I find it easier just to sudo bash which will run another shell with elevated privileges.

    MrN-host:RaspberryPi mrn$ sudo bash
    Password:
    bash-3.2#

  2. Insert the SD card. The Mac will put an icon on the desktop representing the SD cards filesystem. Before anything we need to take control of the SD card.
  3. On the command line type: mount

    /dev/disk0s2 on / (hfs, local, journaled)
    devfs on /dev (devfs, local, nobrowse)
    map -hosts on /net (autofs, nosuid, automounted, nobrowse)
    map auto_home on /home (autofs, automounted, nobrowse)
    /dev/disk1s1 on /Volumes/NO NAME (msdos, local, nodev, nosuid, noowners)

  4. The name of the icon on the Desktop was “NO NAME", but it could be different for you if you have renamed the volume. Look for the device with the same name. In my case /dev/disk1s1.
  5. We unmount the SD card:

    diskutil umount /dev/disk1s1

  6. Type mount again and the “NO NAME” device should have disappeared.
  7. The /dev/disk1s1 is the name of the partition so /dev/disk1 is the name of the SD card’s device. We’ll need that later.
  8. Now, go to the Raspberry Pi download page and get a copy of the Raspberry Pi operating system. For this example I’ll use the recommended operating system Raspbian “wheezy” (2013-05-25-wheezy-raspbian). If it is possible, get the torrent version; there was 200 in my swarm and it downloaded in a few seconds.
  9. Unzip the download:

    unzip 2013-05-25-wheezy-raspbian.zip

  10. Now the fun bit. We’re going to lay the downloaded file on to the SD card, well on to its block device. It took 15 minutes to copy the 1.8GB disk image on to the SD card.

    # dd if=2013-05-25-wheezy-raspbian.img of=/dev/disk1 bs=1m
    1850+0 records in
    1850+0 records out
    1939865600 bytes transferred in 1128.683885 secs (1718697 bytes/sec)

  11. The disk image that has been laid down on the SD card contained a single partition which when completely written was recognised by the Mac and automatically mounted. A boot disk icon should appear on the Desktop.
  12. Drag the boot disk icon to the Trash.
  13. Pull the card out of the Mac and place into the Raspberry Pi and switch it on.
  14. Boot up the Raspberry Pi and it should drop you off at a menu screen where we set up a few things.
  15. First up we will check and install the latest version of the menu screen or raspi-config. Use the up/down keys to go down to 8 Advanced Options and hit return. Now go down to A5 Update. The menu will restart running the latest version.
  16. Now we’ll configure a few things to help us along the way.
  17. Most important is to change the default password. Not for any security concerns but because the default password is “raspberry” which is not only too long but also plays havoc with my dyslexia. I can never remember if it’s “bp” or “pb", anyway I digress. I usually just use “pipi". Go into 1. Change User Password and follow the instructions.
  18. During the course of our installations and set up we may need to reboot a few times so we don’t want it going in to the windows environment each time we do. Switch it off with 3 Enable Boot to Desktop.
  19. As we are good to the world also consider using 6 Add to Rastrack. It just tells a magic server that you have installed a Raspberry Pi so they can do stats and things. There’s no registration.
  20. Penultimately go to 8 Advanced Options and then A2 Hostname. The current hostname is raspberrypi which is a bit long. You could end up writing this everywhere. So I’ll just name it after the owner drswifty.
  21. And finally select 1 Expand Filesystem. This takes the 1.8GB disk image you installed and expands it to fill the whole SD card giving you access to the other 14GB!. This can take a while, about 20 minutes for a 16GB card.
  22. When you exit with Finish, it will ask you if you want to reboot. Select No. We still need to find out what its IP address is so we can continue remotely from our favourite laptop using PuTTY.
  23. You will be dropped at the command prompt so issue the command ifconfig. Your external address will be listed in either the eth0 or wlan0 interfaces.

    eth0 Link encap:Ethernet HWaddr b8:27:eb:9b:6b:84
    inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:170 errors:0 dropped:0 overruns:0 frame:0
    TX packets:113 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:15605 (15.2 KiB) TX bytes:14637 (14.2 KiB)

    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)

    wlan0 Link encap:Ethernet HWaddr 4c:60:de:61:61:04
    UP BROADCAST MULTICAST MTU:1500 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:1000
    RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

  24. Finally, reboot the machine so the hostname change comes into effect.

    sudo reboot

  25. When the machine reboots login as pi with whatever password you selected.

Quick and simple guide to Raspberry Pi operating systems

July 15th, 2013

From the Raspberry Pi download page and shortened!

  1. Noobs - A way to try all the main ones in one re-imagiable download.
  2. Raspbian “wheezy” - Recommended, with development tools.
  3. Soft-float Debian “wheezy” - Same as Raspbian but compiled for the slower soft-float hardware. Used for Oracle JVM version 7 or less.
  4. Arch Linux ARM - Boots in 10 seconds but definitely not for beginners.
  5. Pidora - Fedora.
  6. RISC OS - Acorn.

Learning Yii with Yii Developer's Tutorial

July 11th, 2013

One of the things that helped me out the most when I was learning Yii was their series on YouTube called Yii Developer’s Tutorial. I kind of stumbled across it when I was searching for help on getting started.

Unfortunately all the videos in the series didn’t seem to be listed anywhere. They were spread across 2 different peoples YouTube channels so it meant using the search engine to find the next episode!

I thought I’d group them together for you, like I did for the Armstrong & Miller - WWII RAF Sketches.

  1. Introduction: Concepts, MVC architecture and Active Records.
    Part 1

  2. Setting up your development environment.
    Part 2.1
    Part 2.2
    Part 2.3

  3. Creating your first Yii application. Generate Controllers, Views and Models via Gii. Connect to database. Friendly URLs.
    Part 3.1
    Part 3.2
    Part 3.3
    Part 3.4

  4. Yii Architectural Review. Yii is scalable, supports CRUD actions, MVC but is not classed as an enterprise application framework because it doesn’t support transactions, no business rules or constraints, no workflow control.
    Part 4

There is a another set of vids (showing my age there!) can be found on Yii’s website in the screencasts section. These start with showing how a controller can write Hello World on the screen and go through the steps of getting Hello World from the database.

Understanding Yii Layouts and Views

July 5th, 2013

When learning Yii I found it difficult to match the different layout strategies used in the demo applications that are bundled with Yii, specifically Helloworld, Hangman and Blog.

A layout is essentially another view. Layouts are optional as everything can be done in the view. If all your pages share a common look or feel then you may want to refactor the majority of the static (or nearly static) content into the Layout, leaving the view to provide the body of the content that is specific to that page.

If you’ve written a lot of PHP applications then you’ll recognise the following:

PHP

<?php
include("header.php");
?>
The page and maybe some more PHP.
<?php
include("footer.php");
?>

If “The page and maybe some more PHP.” is the view then the rest is the layout. Or to put it another way a layout is a decorator for a view.

A nice example of this is the Blog demo. The view contains all the page specific body but the menus and side bars are held in the layout. You could add the layout to each view and not use layouts at all but this would mean repeating a lot of the same code across all the views.

The first thing to understand is the precedence of the layout overrides. As with all these things the closer to the code that’s running the higher the priority.

  1. Action: With the highest priority, this layout can be set inside the body of the controller action.

    PHP

    class SiteController
       extends CController
    {
      public function actionIndex()
      {
        $this->layout "mylayout";
        $this->render('index');
      }
  2. Controller:The layout can be set at the controller level by overriding the layout variable from the base class.

    PHP

    class PostController
       extends Controller
    {
      public $layout='column2';
  3. Theme: The layout may be taken from the current theme.
  4. Module: The layout may be taken from the current module.
  5. Parent Modules: If the current module has no layout, then traverse up its parents until you find one.
  6. Default: Finally the Yii application defines the default layout [Yii::app()->layout] which is meant as a catch all.
  7. None: All these places are searched but if at the end of the day they don’t point to anything then a layout won’t be used.

Helloworld
This demo is the simplest because it does not render a view, it only outputs raw text (or data). Let’s trace the path through the application.

  1. /helloworld/ is accessed and Yii looks for the configuration file /helloworld/protected/config/main.php in order to find the first controller to load.
  2. The main.php configuration file is not found so Yii then looks for the default controller called Site. It loads /helloworld/protected/controllers/SiteController.php
  3. The default action for a controller is Index so actionIndex() is called which just echos some stuff to the screen.

Hangman
This demo uses a single layout for all pages.

  1. /hangman/ is accessed and Yii looks for the configuration file /hangman/protected/config/main.php in order to find the first controller to load.
  2. The defaultController is game, so Yii loads /hangman/protected/controllers/GameController.php
  3. The default action has been set to “play” by overriding the defaultAction field of the GameController class. So actionPlay() is called first.
  4. At the end of actionPlay() the function render() is called with either “guess” as the view or “play". Let’s continue assuming “play” was selected.
  5. Now Yii will render /protected/views/game/play.php into a variable.
  6. Next it will look for a layout to place that rendering. Following the priorities from above it will eventually get to the default layout for the application (CWebApplication) which is layouts/main.php
  7. Yii will make sure that layouts/main.php exists then it will render that layout, setting $content to the result of rendering the view file.

Blog
This demo uses 3 layout files to render the site. main.php is the default catch-all layout and column1.php & column2.php are intermediate layout files.

  1. /blog/ is accessed and Yii looks for the configuration file /blog/protected/config/main.php in order to find the first controller to load.
  2. The defaultController is post, so Yii loads /blog/protected/controllers/PostController.php
  3. PostController.php sets the class layout to “column2″, so all views will use “column2″ as their layout.
  4. The default action is Index so actionIndex() is called next. When it gets to the end it calls render() to render the index view.
  5. /blog/protected/views/post/index.php is used to draw the blog posts and the result is returned to the renderer.
  6. The renderer uses the result of the view-render as the $content in the column2 layout.
  7. The column2 layout has the following form. It means that stuff between beginContent and endContent will be given to /layouts/main as its $content.

    PHP

    <?php
    $this->beginContent('/layouts/main');
    ?>
    ...stuff...
    <?php
    $this->endContent();
    ?>


There’s another pretty decent article on Larry Ullman’s site, Working with Layouts in Yii.

Setting up XDebug under XAMPP

July 3rd, 2013

I would like to debug PHP in Eclipse PDT while it runs under an Apache server with a bit of MySQL thrown in. In order to do the remote debug one needs to configure PHP to use XDebug which is a standard cross-platform debugger that is used by a variety of languages to debug over the wire. It is based on DBGp, a common debugger protocol for languages and debugger UI communication.

I downloaded and installed XAMPP which bundles together a LAMP stack in one easy download and installs them so they all work together (or so I thought).

I followed the standard instructions to switch on XDebug except that I installed it under e:\xampp instead of the default c:\xampp.

  1. Launch the XAMPP Control Panel.
  2. On the Apache row click Config, then PHP (php.ini) to load the PHP configuration file.
  3. Forward search for [XDebug]
  4. Make sure the following options are uncommented (i.e. remove the semi-colon at the front of the line) and fill in the entries to match those below.

    [XDebug]
    zend_extension = “E:\xampp\php\ext\php_xdebug.dll”
    xdebug.remote_enable = 1
    xdebug.remote_handler = “dbgp”
    xdebug.remote_host = “127.0.0.1″
    xdebug.remote_port = “9000″

  5. Stop Apache
  6. Start Apache

On starting Apache the following message pops up:

The procedure entry point zend_unmangled_property_name_ex could not be located in the dynamic link library php5ts.dll

After a lot of really boring reading in the usual forums and help web sites I find out that the php_xdebug.dll is not compiled correctly for the version of PHP I’m using. This is very strange because it was downloaded as a bundle so everything should be compatible with everything else, but it wasn’t.

To confirm this type:

cd e:\xampp\php
php -m

which gives the following output:

Failed loading E:\xampp\php\ext\php_xdebug.dll
[PHP Modules]
bcmath
bz2
calendar
Core
….
zip
zlib

[Zend Modules]

Microsoft Windows dictates an internal DLL binary format which coincidentally is released with a new version of Microsoft’s Visual studio. This makes programs compiled with different versions of compiler incompatible with each other - thanks for that. It causes problems in every computer language that is compiled with a Microsoft compiler. PHP is just one of them. Python is another. If you compile Python under Visual Studio 9 you will have to recompile all the support modules with the same compiler. So not only do you have to care about whether it was compiled into 32 or 64 bit code but you also have to care about which compiler was used too.

The name of the XAMPP binary tells you which version of Visual ‘C’ was used to create the application suite. In my case I downloaded xampp-win32-1.8.2-0-VC9-installer.exe. From the file name we can see that this is a windows 32 bit version compiled using Visual ‘C’ 9.

This only gives us some of the story. For the rest we need to run XAMPP and get it to tell us how it was compiled. In the document root E:\xampp\htdocs create a file called p.php and fill it with:

<?php phpinfo(); ?>

Next open you browser and go to: http://127.0.0.1/p.php.

There are several lines of importance:

PHP Version 5.4.16
Compiler MSVC9 (Visual C++ 2008)
Architecture x86
Zend Extension Build API220100525,TS,VC9
PHP Extension Build API20100525,TS,VC9

The phpinfo() confirms that Microsoft’s Visual ‘C’ version 9 (MSVC9) was used as the compiler which was built in Visual C++ 2008. The Visual C++ 2008 tells us that the Apache needs the Visual C++ Redistribution libraries to be installed. You wouldn’t be able to run Apache with out them. If may explain why XAMPP may work on a newer server but give DLL errors on an older one.

x86 is the hardware architecture and indicates that it is a 32 bit build.

Zend Extension Build and PHP Extension Build have their compiler options although in this case the only important part of this is the TS bit. I think that in some builds of PHP the TS may not exist or it might be NTS instead.

Next we have to download a new version of XDebug that will fit into our environment. So navigate to:
http://xdebug.org/download.php

In the Releases section look for the version that has the compiler flags we need. We’ll start with the latest version (XDebug 2.2.3 at the time of writing). I’m using PHP version 5.4 compiled using VC9 with TS and for a 32 bit build. So I download PHP 5.4 VC9 TS (32 bit) (php_xdebug-2.2.3-5.4-vc9.dll).

There are 2 ways to install it.

  1. Rename php_xdebug-2.2.3-5.4-vc9.dll to php_xdebug.dll
  2. Stop Apache
  3. Copy php_xdebug.dll into E:\xampp\php\ext
  4. Start Apache

Windows being what it is with locking files means that you have to stop Apache before copying the DLL in to the correct place which will slightly increase the downtime. This might work better as a tested upgrade on a production system where you don’t want to touch the configuration files.

Alternatively you can:

  1. Copy php_xdebug-2.2.3-5.4-vc9.dll into the E:\xampp\php\ext directory
  2. Edit the zend_extension line in the php.ini to point to this version.

    [XDebug]
    zend_extension = “E:\xampp\php\ext\php_xdebug-2.2.3-5.4-vc9.dll”
    xdebug.remote_enable = 1
    ….

  3. Stop Apache
  4. Start Apache

Most people choose this option because it gives you a better infrastructure for testing different versions as well as an easy rollback or upgrade path in case anything goes wrong. It also reminds you which version of XDebug you are using. The DLLs stay in place and you are only changing the configuration files. As a result there is no gap between the Apache restart.

Finally navigate to your phpinfo() page and there should be a section for XDebug.