tag:blogger.com,1999:blog-13715909220624617222024-03-18T06:03:33.813+00:00Random TutorRandom tutorials on technology.Unknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-1371590922062461722.post-69113910611684472162014-01-19T17:15:00.001+00:002014-10-24T12:02:18.827+01:00Installing Ubuntu on a Retina Macbook Pro - the easy way<b>Update</b>: <i>If you have Mac OS X 10.10 (Yosemite) installed, then the rEFInd installation needs to be handled differently. Please check the <a href="http://www.rodsbooks.com/refind/yosemite.html">rEFInd website</a> for more details.</i><br />
<br />
In my <a href="http://randomtutor.blogspot.co.uk/2013/02/installing-ubuntu-1304-on-retina.html">previous installation guide</a>, I outlined the first way that I found of installing Ubuntu on a Retina Macbook Pro. One of the challenges of the installation was that the boot manager, rEFInd, needed the Linux kernel to be copied from the Linux partition to the Mac OS X partition. This then becomes a painful process that needs to be repeated every time there is a kernel update on Ubuntu. Fortunately, there is a better way! Thanks to a comment on the <a href="http://www.rodsbooks.com/refind/">rEFInd</a> website, I found out that file system drivers can be installed that allow rEFInd to read the Linux partition.<br />
<br />
This post outlines the full installation instructions, but if you already followed the previous guide, you can update the rEFInd installation and configuration file. I've included some instructions on that in the post.<br />
<br />
<h2>
<span style="font-family: inherit; font-size: large;">
1. Partition the Hard Drive</span></h2>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">This step is nice and easy on the Mac. Just launch Disk Utility, click on the laptop's hard drive and click on the Partition tab. From there the Mac OS X partition can be resized. Disk Utility allows you to create a new partition with the extra space, but I just left it as Free Space, so that it would be created by the Ubuntu installer.</span><br />
<br />
<h2>
<span style="font-family: inherit; font-size: large;">
2. Create the Ubuntu USB Installer</span></h2>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">The latest stable release of Ubuntu is available at </span><a href="http://www.ubuntu.com/download">http://www.ubuntu.com/download</a> - y<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">ou'll need the 64-bit Mac (AMD64) desktop image.</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Instructions for creating a bootable USB stick are provided </span><a href="http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">, you'll just need to remember to use the latest ISO that was downloaded above. Note: unlike some disk image files, Mac OS X cannot mount the disk image, but it will boot fine from it.</span><br />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span>
<br />
<h2>
<span style="font-family: inherit; font-size: large;">
3. Install an EFI Boot Manager: rEFInd</span></h2>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Previously, I've used Refit as a boot manager and boot loader for Ubuntu on a Mac, but that doesn't seem to be maintained any more. So, for the Retina Macbook Pro I've switched to </span><a href="http://www.rodsbooks.com/refind/" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">rEFInd</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">, which is just a Boot Manager. We'll then be loading Ubuntu using EFI instead of Grub, which means we can leave the old world of BIOS behind. See the </span><a href="http://www.rodsbooks.com/refind/" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">rEFInd website</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"> for more information about boot managers and loaders, EFI and Grub.</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Download the binary rEFInd zip file from </span><a href="http://www.rodsbooks.com/refind/getting.html" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">http://www.rodsbooks.com/refind/getting.html</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"> and unzip it by double-clicking the file in the Mac OS X Finder. You'll want to check out the </span><a href="http://www.rodsbooks.com/refind/installing.html" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">rEFInd installation instructions</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">, but I chose the simplest option of installing rEFInd in the Mac OS X partition. There are other possibilities, but this seemed the easiest for me to manage - especially if something went wrong.</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">The installation needs to be done in the Terminal, by running the </span><i style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">install.sh</i><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"> script:</span><br />
<pre style="background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 629.625px;"><code style="word-wrap: normal;"> cd ~/Downloads/refind-bin-0.7.7
./install.sh --alldrivers
</code></pre>
<br />
The '--alldrivers' option installs the file system drivers for rEFInd, which allows the boot manager to be able to access the Ubuntu kernel files on the Linux file system. If you are upgrading from a previous installation of rEFInd, it will keep your existing rEFInd config file and copy the latest version with a different name.<br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">The script will prompt for your password so it can run with administrator privileges using sudo. Once the script has run, rEFInd is installed and you can see the configuration files at /EFI/refind.</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">You'll need to reboot a couple of times before you can see the rEFInd menu appearing. We'll need to configure rEFInd later.</span><br />
<h2>
<span style="font-family: inherit; font-size: large;">
4. Installing Ubuntu</span></h2>
<div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Connect your bootable USB drive into the Macbook Pro and reboot - you should see the USB stick as an option in the rEFInd boot menu, so boot from that. In the Ubuntu installer, select the </span><i style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Try Ubuntu </i><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">option and it will take you to the Ubuntu desktop at a resolution of 2880x1800 - you may need a magnifying glass handy to read the text. That said, it does look beautiful!</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">By default, Ubuntu has 'touch to click' enabled for the trackpad by default, which I found difficult work with. So the first thing I did was to turn that off in the </span><i style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Mouse and Trackpad</i><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"> area of </span><i style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">System Settings</i><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">. Next, it's worth changing the screen resolution to something more usable - I selected 1680x1050 (16:10). After that, if you are using WiFi, then you'll need to connect to the network.</span><br />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span></div>
<div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span></div>
<div>
<h3 style="background-color: white; color: #444444; font-family: Cantarell; margin: 0px; position: relative;">
4.1 Install Ubuntu</h3>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">You're now ready to install Ubuntu. We want to do this without installing the Grub boot-loader, which would put us back into the old-world BIOS mode. To install without Grub, run the following in a terminal:</span><br />
<pre style="background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 629.625px;"><code style="word-wrap: normal;"> ubiquity -b
</code></pre>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">This will launch the installer and you can follow the instructions to install Ubuntu on the free space that we created earlier, alongside Mac OS X.</span></div>
<div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span></div>
<div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Once you are done, you can reboot into Mac OS X. You'll notice that the Ubuntu partition is not showing up in rEFInd as yet - that's what we need to fix next.</span></div>
<div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span></div>
<div>
<h2>
<span style="font-family: inherit; font-size: large;">
5. Configure rEFInd</span></h2>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">We need to configure rEFInd so that it sees that Ubuntu partition and has all the correct details so it can boot from it. The rEFInd configuration is in /EFI, which will need root access to be able to update the details. I found that </span><a href="http://barebones.com/products/textwrangler/" style="background-color: white; color: #4d469c; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px; text-decoration: none;">TextWrangler</a><span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"> is a great option for editing config file as it allows you to authenticate to update the files. Make sure that you download TextWrangler from the Barebones site as the version in the Apple App Store does not have this facility.</span><br />
<br style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;" />
<h3 style="background-color: white; color: #444444; font-family: Cantarell; margin: 0px; position: relative;">
5.1 Configuration File Changes</h3>
<h3 style="background-color: white; color: #444444; font-family: Cantarell; margin: 0px; position: relative;">
<span style="font-size: 13px; font-weight: normal; line-height: 18.200000762939453px;">It's worth checking the rEFInd site for more details about the configuration file changes, as there are a lot more options than I will cover. The main configuration file is /EFI/refind/refind.conf. If you've upgraded from a previous version of rEFInd, you may want to use the sample config file instead of the current version that you are using. We need to configure rEFInd so that it scans the hard disk for other boot options. The following file shows only the lines that I've </span><span style="font-size: 13px; font-weight: normal; line-height: 18.200000762939453px;"><i>changed</i></span><span style="font-size: 13px; font-weight: normal; line-height: 18.200000762939453px;"> from the default config file:</span></h3>
</div>
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;"><br /></span>
<br />
<pre style="background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 629.625px;"><code style="word-wrap: normal;"># Enable the scan for file system drivers
scan_driver_dirs EFI/tools/drivers,drivers
# Choose which drives to scan. This will only scan the internal hard drive.
scanfor internal
# Load the Linux file system driver
fs0: load ext4_x64.efi
fs0: map -r
</code></pre>
<br />
These configuration file changes ensure that the Linux file system driver is loaded (ext4_x64.efi) and that the internal hard disk is scanned for bootable partitions. If there is more than one kernel found on the Linux file system, rEFInd will display all the available kernels.<br />
<br />
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">That's it. If you want to make the boot screen a it prettier, you can copy a PNG or BMP image file to /EFI and add an extra line to the configuration file:</span><br />
<pre style="background-color: #f0f0f0; border: 1px dashed rgb(204, 204, 204); font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; width: 629.625px;"><code style="word-wrap: normal;">banner /EFI/MilkyWay.png
</code></pre>
<div>
<code style="word-wrap: normal;"><br /></code></div>
<div>
<code style="word-wrap: normal;"></code><br />
<h2>
<code style="word-wrap: normal;"><span style="font-family: inherit;">
<span style="font-size: large;">6. Reboot Into Ubuntu</span></span></code></h2>
<code style="word-wrap: normal;">
<span style="background-color: white; color: #444444; font-family: Cantarell; font-size: 13px; line-height: 18.200000762939453px;">Once you reboot you should see the Ubuntu icon in the rEFInd boot menu, and it should start up. When it does, you'll see Ubuntu in its 2880x1800 pixel glory. I've increased the default text size in Ubuntu and the browsers to roughly 1.5 times the normal size, and that has made a great work machine.</span></code></div>
<div>
<br /></div>
<br />Unknownnoreply@blogger.com19tag:blogger.com,1999:blog-1371590922062461722.post-61556402738398517222013-02-24T19:00:00.001+00:002014-02-07T17:46:39.642+00:00Installing Ubuntu on Retina Macbook Pro<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-iclh2E7QHA8/USpAhV2_viI/AAAAAAAAA-o/clN-yXELklY/s1600/Screenshot+from+2013-02-24+16%253A27%253A55.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-iclh2E7QHA8/USpAhV2_viI/AAAAAAAAA-o/clN-yXELklY/s640/Screenshot+from+2013-02-24+16%253A27%253A55.png" height="400" width="640" /></a></div>
<br />
<b><span style="font-size: large;">There is an update to this post that shows a simpler and more maintainable approach to the <a href="http://randomtutor.blogspot.co.uk/2014/01/installing-ubuntu-on-retina-macbook-pro_19.html">installation</a>.</span></b><br />
<br />
Installing Ubuntu on a Mac tends to be a little trickier than installing on a PC as Macs use EFI instead of BIOS. Now, with the introduction of Apple's Retina Macbook Pro screens we have an additional complication. However, Canonical and the Ubuntu community have been investing some time in getting the Retina Macbooks to play nice with Ubuntu 13.04, so I decided to get the latest, bleeding edge version. The great thing is that there has been an effort to keep the trunk version stable, so I've that getting the pre-release version of Ubuntu 13.04 to be a great solution.<br />
<br />
If you search the Internet for information about running Ubuntu on the Retina Macbook Pro, you'll find tales of issues with the screen resolution (running at 2880x1880 with tiny icons and text), and driver issues with WiFi and sound. Well, I'm pleased to say, that Ubuntu 13.04 (with the 3.8.0-6 kernel) resolves these issues. There are some extra steps to take during the installation process, but these are related to EFI rather than Ubuntu itself. This guide is a walkthrough of the steps that I took to partition Macbook Pro hard drive and to install a dual-boot system: Mac OS X 10.8 and Ubuntu 13.04 (pre-release).<br />
<br />
<h4>
Partition the Hard Drive</h4>
This step is nice and easy on the Mac. Just launch Disk Utility, click on the laptop's hard drive and click on the Partition tab. From there the Mac OS X partition can be resized. Disk Utility allows you to create a new partition with the extra space, but I just left it as Free Space, so that it would be created by the Ubuntu installer.<br />
<br />
<h4>
Create the Ubuntu USB Installer</h4>
The latest stable release of Ubuntu is available at <a href="http://www.ubuntu.com/download">http://www.ubuntu.com/download</a>, but I wanted pre-release version, which is at <a href="http://cdimage.ubuntu.com/daily-live/current/">http://cdimage.ubuntu.com/daily-live/current/</a>. You'll need the 64-bit Mac (AMD64) desktop image.<br />
<br />
Instructions for creating a bootable USB stick are provided <a href="http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx">http://www.ubuntu.com/download/help/create-a-usb-stick-on-mac-osx</a>, you'll just need to remember to use the latest ISO that was downloaded above.<br />
<br />
<h4>
Install an EFI Boot Manager: Refind</h4>
Previously, I've used Refit as a boot manager and boot loader for Ubuntu on a Mac, but that doesn't seem to be maintained any more. So, for the Retina Macbook Pro I've switched to <a href="http://www.rodsbooks.com/refind/">Refind</a>, which is just a Boot Manager. We'll then be loading Ubuntu using EFI instead of Grub, which means we can leave the old world of BIOS behind. See the <a href="http://www.rodsbooks.com/refind/">Refind website</a> for more information about boot managers and loaders, EFI and Grub.<br />
<br />
Download the binary Refind zip file from <a href="http://www.rodsbooks.com/refind/getting.html">http://www.rodsbooks.com/refind/getting.html</a> and unzip it by double-clicking the file in the Mac OS X Finder. You'll want to check out the <a href="http://www.rodsbooks.com/refind/installing.html">Refind installation instructions</a>, but I chose the simplest option of installing Refind in the Mac OS X partition. There are other possibilities, but this seemed the easiest for me to manage - especially if something went wrong.<br />
<br />
The installation needs to be done in the Terminal, by running the <i>install.sh</i> script:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> cd ~/Downloads/refind-bin-0.6.7
./install.sh
</code></pre>
<br />
The script will prompt for your password so it can run with administrator privileges using sudo. Once the script has run, Refind is installed and you can see the configuration files at /EFI/refind.<br />
<br />
You'll need to reboot a couple of times before you can see the Refind menu appearing. We'll need to configure Refind later.<br />
<br />
<h4>
Installing Ubuntu</h4>
The main challenge I hit during the installation process is that we need to copy the Ubuntu kernel file and RAM disk image over to the Mac OS X partition. However, though you can see the Mac OS X partition from Ubuntu, it is read-only. So, we'll need a method of copying the files over once Ubuntu has been installed.<br />
<br />
One method is to upload the files to another server when you are booted into Ubuntu - which means that you need a network connection running. On the Retina Macbook Pro, there is no built-in Ethernet, so you're only option is to use WiFi. However, when booted off the USB stick the Macbook's WiFi does not work without extra drivers. So the option is to use an external WiFi card that does work from the installer (I used an <a href="http://amzn.to/X6a88w">Edimax EW-7811UN Nano Adapter</a>), use an external USB drive, or an <a href="http://amzn.to/YoIeUO">Apple Thunderbolt Ethernet Adapter</a> (untested). However, given that Ubuntu 13.04 is still in development, you may find that WiFi works for you when you boot from the USB stick.<br />
<br />
Connect your bootable USB drive into the Macbook Pro and reboot - you should see the USB stick as an option in the Refind boot menu, so boot from that. In the Ubuntu installer, select the <i>Try Ubuntu</i> option and it will take you to the Ubuntu desktop at a resolution of 2880x1800 - you may need a magnifying glass handy to read the text. That said, it does look beautiful!<br />
<br />
By default, Ubuntu has 'touch to click' enabled for the trackpad by default, which I found difficult work with. So the first thing I did was to turn that off in the <i>Mouse and Trackpad</i> area of <i>System Settings</i>. Next, it's work changing the screen resolution to something more usable - I selected 1680x1050 (16:10). After that, if you are using WiFi, then you'll need to connect to the network.<br />
<br />
<h3>
Install Ubuntu</h3>
You're now ready to install Ubuntu. We want to do this without installing the Grub boot-loader, which would put us back into the old-world BIOS mode. To install without Grub, run the following in a terminal:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> ubiquity -b
</code></pre>
This will launch the installer and you can follow the instructions to install Ubuntu on the free space that we created earlier, alongside Mac OS X. At the end of the installation, make sure that you choose the option to <i>Continue to try Ubuntu</i>, as there is some more work to do here.<br />
<br />
<h3>
Copy the Kernel and RAM Disk Image Files</h3>
The next step is to copy the kernel file and the RAM disk image to your external server or flash drive. The files that you need are in /boot and will be called something like vmlinuz-3.8.0-6-generic and initrd.img-3.8.0-6-generic. If you have your Internet connection working, the simplest option is to upload the files to <a href="http://one.ubuntu.com/">Ubuntu One</a>, <a href="http://www.dropbox.com/">DropBox</a> or <a href="http://drive.google.com/">Google Drive</a>.<br />
<br />
<h3>
Make a Note of the UUID</h3>
You'll need to know the UUID of the partition that you have just installed Ubuntu on. You can see this in the Nautilus File Manager window when you select the Ubuntu partition (in the title bar). Or, you can see it by running:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> sudo blkid /dev/sda5
</code></pre>
<br />
You'll need to replace <i>sda5</i> with the partition that you used. The UUID will be a long list of characters like <i>27c9a93d-6cc8-2395-9a97-d105353e5c07 </i>- we'll need this for the Refind configuration file.<br />
<br />
Once you are done, you can reboot into Mac OS X. You'll notice that the Ubuntu partition is not showing up in Refind as yet - that's what we need to fix next.<br />
<br />
<h4>
Configure Refind</h4>
We need to configure Refind so that it sees that Ubuntu partition and has all the correct details so it can boot from it. The Refind configuration is in /EFI, which will need root access to be able to update the details. I found that <a href="http://barebones.com/products/textwrangler/">TextWrangler</a> is a great option for editing config file as it allows you to authenticate to update the files. Make sure that you download TextWrangler from the Barebones site as the version in the Apple App Store does not have this facility.<br />
<br />
<h3>
Copy the Kernel and RAM Disk Image Files</h3>
The first step is to create a new folder in the EFI config area that will hold the Ubuntu kernel and disk image: /EFI/ubuntu. You'll need to copy the Ubuntu kernel and RAM disk image files into the new folder from the server or flash drive that you used earlier.<br />
<br />
<h3>
Configuration File Changes</h3>
It's worth checking the Refind site for more details about the configuration file changes, as there are a lot more options than I will cover. The main configuration file is /EFI/refind/refind.conf. We need to set up a configuration section for Ubuntu, so edit the file and add the following section:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">1: menuentry Linux {
2: icon EFI/refind/icons/os_ubuntu.icns
3: volume 5:
4: loader EFI/ubuntu/vmlinuz-3.8.0-6-generic
5: initrd EFI/ubuntu/initrd.img-3.8.0-6-generic
6: options "ro root=UUID=27c9a93d-6cc8-2395-9a97-d105353e5c07"
7: #disabled
8: }
</code></pre>
Note that there is a <i>menuentry Linux</i> section already in the file, and I have just edited that section. Some of the key points to note:<br />
<br />
<ul>
<li>Line 2: the icon that will be displayed on the boot manager screen. In this case, I'm using the Ubuntu logo instead of Tux.</li>
<li>Line 3: you can find the volume by running <i>diskutil list.</i></li>
<li>Lines 4-5: the paths to the kernel and RAM disk image.</li>
<li>Line 6: the UUID of the Ubuntu partition that you made a note of earlier.</li>
<li>Line 7: make sure that you comment out or remove the <i>disabled</i> line, otherwise it will not show up in the boot menu.</li>
</ul>
That's it. If you want to make the boot screen a it prettier, you can copy a PNG or BMP image file to /EFI and add an extra line to the configuration file:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">banner /EFI/MilkyWay.png
</code></pre>
<br />
<h4>
Reboot Into Ubuntu</h4>
Once you reboot you should see the Ubuntu icon in the Refind boot menu, and it should start up. When it does, you'll see Ubuntu in its 2880x1800 pixel glory. So once you changed the resolution to a reasonable setting, you should find Ubuntu running beautifully:<br />
<br />
<ul>
<li>The WiFi adapter works out of the box.</li>
<li>The sound card works.</li>
<li>The web cam works.</li>
</ul>
<br />
In fact, I've been using Ubuntu on the Retina Macbook Pro as my main work machine for a week now and I've not found anything that does not work.<br />
<br />Unknownnoreply@blogger.com123tag:blogger.com,1999:blog-1371590922062461722.post-54017484339590818572013-01-31T22:56:00.003+00:002013-01-31T23:14:06.347+00:00Web-based IR Remote on the Raspberry PiThere are many devices that use infrared remote controls - TV's, DVD players, cameras, power sockets. So getting a Raspberry Pi to be able to send remote control signals opens up many possibilities for projects. Combining the GPIO pins with a web server on the Raspberry Pi means that we can create a web UI to control these devices.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-UzubWPTFDfk/UQr6r4EGVeI/AAAAAAAAA8A/ZE02qXJdoHU/s1600/IMG_20130131_230919.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="http://4.bp.blogspot.com/-UzubWPTFDfk/UQr6r4EGVeI/AAAAAAAAA8A/ZE02qXJdoHU/s320/IMG_20130131_230919.JPG" width="320" /></a></div>
<br />
<h2>
Installing LIRC on the Raspberry Pi</h2>
One of the great things about running a Linux operating system on the Raspberry Pi is that it provides access to a wealth of software projects that can run on the device. One of these is the <a href="http://www.lirc.org/">Linux Infrared Remote Control</a> (LIRC) project that provides a way of receiving and transmitting IR signals from many remote controls. LIRC provides a method of recording signals from a remote control and storing them in a configuration file, so that they can be sent directly from the Raspberry Pi.<br />
<br />
Installing LIRC on the Raspberry Pi needs the latest firmware to be on the Raspberry Pi. One of the clearest guides I've found on the process of updating your firmware and installing LIRC is on <a href="http://alexba.in/blog/2013/01/06/setting-up-lirc-on-the-raspberrypi/">Alex Bain's site</a>. That walks you through the process of installing LIRC and getting the initial configuration done.<br />
<br />
<h2>
IR Receiver and Transceiver Circuits</h2>
Once LIRC is set up, we need to set up an infrared receiver and transmitter circuit.<br />
<br />
<h3>
IR Receiver</h3>
The receiver circuit is the simplest to set up. I've used a TSOP382, but check the data sheet for your IR receiver as the pins for ground and Vcc are sometimes the other way round. If you connect those pins incorrectly you could fry the IR receiver. Also, note that I'm connecting the Vcc pin to 3.3V as the TSOP382 works fine on that voltage.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-zFDyQr-zoNw/UQrdeDp18wI/AAAAAAAAA6o/vOAhwJj_1Js/s1600/IRReceive.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="272" src="http://3.bp.blogspot.com/-zFDyQr-zoNw/UQrdeDp18wI/AAAAAAAAA6o/vOAhwJj_1Js/s320/IRReceive.png" width="320" /></a></div>
The <a href="http://learn.adafruit.com/ir-sensor/overview">Adafruit</a> site has a great walkthrough of testing and using an IR receiver.<br />
<br />
<h3>
IR Transmitter</h3>
The IR transmitter circuit is a bit more complex as the current output from the Raspberry Pi GPIO pins will only give you a very weak signal from the IR transmitter LED. If you search on the Internet you'll find a number of different circuits that you can use to amplify the signal so that you can get a few meters distance of signal from your IR LED.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-DuyRAT_xH5Y/UQrkCocR6bI/AAAAAAAAA7A/Nqgq_O1N4Ys/s1600/IRtransmit.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-DuyRAT_xH5Y/UQrkCocR6bI/AAAAAAAAA7A/Nqgq_O1N4Ys/s320/IRtransmit.png" width="318" /></a></div>
I'm sure sure what the IR LED I've used as I rescued it from a Sony DVD remote control. In terms of the components to use for the circuit, you'll find a <a href="http://www.ladyada.net/make/tvbgone/design.html">number</a> of <a href="http://aron.ws/projects/lirc_rpi/">options</a>. I used the components that I had available (Q1=BC547 transistor, R1=220ohm, R2=omitted as I didn't have a low enough resistor). Though I haven't measured the range of the signal, it works fine from 4m away from the devices.<br />
<br />
<br />
<h2>
Web-Controlled Remote</h2>
Once LIRC is setup and you can configured at least one remote, sending commands to through the IR LED is as simple as:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> # device command
irsend SEND_ONCE Humax KEY_POWER
</code></pre>
<br />
Of course, we don't want limit people to controlling the TV by ssh-ing into the Raspberry Pi to change the channel. So we need a web-based UI that people can access through a computer, tablet or smart phone. I got my 11-year old son to work on a design using <a href="http://inkscape.org/">Inkscape</a> so that I'd have an SVG image that I could use as the web interface on an HTML5 web page. After a few iterations on the design, we came up with this:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-axIygh9tcgw/UQrtGoUhOKI/AAAAAAAAA7Q/0fwUkBfdS3Q/s1600/Screen+Shot+2013-01-31+at+22.14.40.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-axIygh9tcgw/UQrtGoUhOKI/AAAAAAAAA7Q/0fwUkBfdS3Q/s320/Screen+Shot+2013-01-31+at+22.14.40.png" width="251" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
The advantage of using an SVG file is that it will scale naturally with the device that it is viewed one, so should work well on a smartphone's 4-inch screen as well as on a 20-inch monitor.<br />
<br />
<h3>
Flask Web Application</h3>
The web application has been written using the <a href="http://flask.pocoo.org/">Flask</a> micro-framework, similar to my <a href="http://randomtutor.blogspot.co.uk/2013/01/controlling-raspberrypi-from-web.html">previous post</a>. This time, the application reads the LIRC config file (/etc/lirc/lircd.conf) and gets the names of the devices that have been configured, and presents them to the user.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-aY1Czp_k46s/UQrvsjV8CdI/AAAAAAAAA7g/pUZ68nmh-nw/s1600/Screen+Shot+2013-01-31+at+22.26.14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="216" src="http://3.bp.blogspot.com/-aY1Czp_k46s/UQrvsjV8CdI/AAAAAAAAA7g/pUZ68nmh-nw/s320/Screen+Shot+2013-01-31+at+22.26.14.png" width="320" /></a></div>
The web page uses <a href="http://jquerymobile.com/">JQuery Mobile</a> to make it look good on smaller devices. Once the user selects the relevant device, the remote control is shown and the user can press a button on the touchscreen to operate the remote. Each button is configured to send a different operation e.g. pressing V+ will send KEY_VOLUMEUP. As we've seen previously, LIRC's <i>irsend</i> command only needs the device name and the operation command code to send the signal to the LED. With Flask, this can be done in less than 50 lines of code:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> #!/usr/bin/env python
from lirc.lirc import Lirc
from flask import Flask
from flask import render_template
from flask import request, redirect, url_for
BASE_URL = ''
app = Flask(__name__)
# Initialise the Lirc config parser
lircParse = Lirc('/etc/lirc/lircd.conf')
@app.route("/")
@app.route("/<device>")
def index(device=None):
# Get the devices from the config file
devices = []
for dev in lircParse.devices():
d = {
'id': dev,
'name': dev,
}
devices.append(d)
return render_template('remote.html', devices=devices)
@app.route("/device/<device_id>")
def device(device_id=None):
d = {'id':device_id}
return render_template('control.html', d=d)
@app.route("/device/<device_id>/clicked/<op>")
def clicked(device_id=None, op=None):
# Send message to Lirc to control the IR
lircParse.send_once(device_id, op)
return ""
if __name__ == "__main__":
app.debug = True
app.run('0.0.0.0')
</code></pre>
<br />
The only complexity that we come across of parsing the LIRC config file to get the names of the devices. Unfortunately, they have not used a standard format like YAML for their config file, so the parser may run into problems if you add comments on the same line as your commands. However, sending the command to LIRC is straightforward using a shell command to <i>irsend</i>:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from subprocess import call
...
def send_once(self, device_id, message):
"""
Send single call to IR LED.
"""
call(['irsend', 'SEND_ONCE', device_id, message])
</code></pre>
<br />
The full code is available on GitHub: <a href="https://github.com/slimjim777/web-irsend">https://github.com/slimjim777/web-irsend</a>.<br />
<br />
<br />Unknownnoreply@blogger.com30tag:blogger.com,1999:blog-1371590922062461722.post-36003595939743910612013-01-22T00:32:00.003+00:002013-01-31T22:58:15.715+00:00Controlling the RaspberryPi from a Web ApplicationThe <a href="http://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi/overview">AdaFruit website</a> has an excellent tutorial on connecting a 16x2 LCD display to a Raspberry Pi. That's great, but what if you want to be able to send messages to the display from a web application? In the <a href="http://randomtutor.blogspot.com/2013/01/python-web-application-on-raspberrypi.html">previous tutorial</a>, we saw how to create a basic Python-based web application using the <a href="http://flask.pocoo.org/">Flask</a> framework. Let's create a new web application that will interact with the Raspberry Pi.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-nvUlzs-EoDw/UP3Ld8oDDsI/AAAAAAAAA58/k7HjgATxSSM/s1600/IMG_20130121_230732.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="http://2.bp.blogspot.com/-nvUlzs-EoDw/UP3Ld8oDDsI/AAAAAAAAA58/k7HjgATxSSM/s1600/IMG_20130121_230732.jpg" width="400" /></a></div>
<br />
<br />
<h2>
The Goal</h2>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-R3Q9f0SF_-8/UP2z6Co4LSI/AAAAAAAAA5Y/M69hixPKA18/s1600/overview.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="http://3.bp.blogspot.com/-R3Q9f0SF_-8/UP2z6Co4LSI/AAAAAAAAA5Y/M69hixPKA18/s1600/overview.png" width="400" /></a></div>
Using Flask, we will create a simple web application with with a form that allow a user to send a message to the Raspberry Pi using a web browser. This will allow us to control the Raspberry Pi from a PC, tablet or smart phone. When the form is submitted, it will send the message to the LCD via the device's GPIO ports. All the code, including the web application, is in Python and HTML... with some HTML and <a href="http://jquerymobile.com/">JQuery Mobile</a> thrown in (just because we can).<br />
<br />
<h2>
To the Code</h2>
We'll be using the same circuit and the AdaFruit_CharLCD.py library, so make sure you have everything up-and-running as per the <a href="http://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi/overview">AdaFruit tutorial</a>. So let's set up the folder structure that we'll need:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> /lcd
lcd.py
/adafruit
__init__.py
Adafruit_CharLCD.py
/templates
index.html
</code></pre>
<br />
The source for application is available at <a href="https://github.com/slimjim777/web-lcd">https://github.com/slimjim777/web-lcd</a>. The application uses the Adafruit class to communicate with the LCD from the Raspberry Pi. The only changes to the code were to convert tabs to spaces in the class (Python is generally very fussy over combining tabs and spaces for indentation).<br />
<br />
<h3>
Main LCD Application</h3>
The main LCD application is lcd.py, which is the app controller for the Flask framework.<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from flask import Flask
from flask import render_template, request
from adafruit.Adafruit_CharLCD import Adafruit_CharLCD
app = Flask(__name__)
# Initialise the LCD class
lcd = Adafruit_CharLCD()
lcd.begin(16,2)
@app.route("/")
def index():
return render_template('index.html', value=value)
...
</code></pre>
<br />
The script initializes the Adafruit LCD class and sets the size of the display (16x2 characters). After that the Flask <i>index</i> method is defined that renders the Jinja2 template. The template contains the web form to allow users to submit messages to the LCD:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> {% extends "base.html" %}
{% block body %}
<div data-role="header">
<h1>Raspberry Pi Controller</h1>
</div>
<div data-role="content">
<form method="post" action="change">
<label for="slider-1">Message to Send:</label>
<input type="text" name="lcd" id="lcd" />
<br />
<input type="submit" value="Submit" />
</form>
{% if value %}
<p>Message sent: {{ value }}</p>
{% endif %}
</div>
{% endblock %}
</code></pre>
<br />
The Jinja2 template extends from the <i>base.html</i> template, which contains the main details of the HTML page, including importing the JQuery Mobile libraries and CSS (to style the web page for mobile devices).<br />
<br />
<h3>
Starting the Web App</h3>
To start the web application, run:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> sudo python lcd.py
</code></pre>
This needs to be run with elevated permissions (using <i>sudo</i>) as it is communicating to the GPIO ports. Now, browsing to port 5000 on the Raspberry Pi will bring up the web form.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-MV7J5OqQrkY/UP3ht_qmwcI/AAAAAAAAA6M/uiKSGUSeMBc/s1600/Screen+Shot+2013-01-22+at+00.46.10.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="165" src="http://4.bp.blogspot.com/-MV7J5OqQrkY/UP3ht_qmwcI/AAAAAAAAA6M/uiKSGUSeMBc/s1600/Screen+Shot+2013-01-22+at+00.46.10.png" width="400" /></a></div>
<br />
When the form is submitted, it send an HTTP POST request to the <i>change</i> method, including the message text that has been submitted.<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> @app.route("/change", methods=['POST'])
def change():
if request.method == 'POST':
# Get the value from the submitted form
lcdText = request.form['lcd']
print "---Message is", lcdText
# Send the message to the LCD
lcd.clear()
lcd.message(lcdText)
else:
lcdText = None
return render_template('index.html', value=lcdText)
</code></pre>
<br />
The <i>change</i> method retrieves the message text from the HTTP POST request and stores it in the <i>lcdText</i> variable. Now, and this is the really clear bit, because all this code is running on the Raspberry Pi, that text can be sent through the device's GPIO port to the LCD using the <i>Adafruit_CharLCD</i> class. That's the beauty and simplicity of using a Python-based web-framework - the same code that someone else has developed to drive the LCD can be used directly in the web application.<br />
<br />
This is a fairly simple example without much of a real-world use. But a similar approach could be taken to drive any component or device that is connected to the Raspberry Pi. That could be to switch a relay to turn on the central heating in your home automation system, or to <a href="http://randomtutor.blogspot.co.uk/2013/01/web-based-ir-remote-on-raspberry-pi.html">send an message to an IR LED</a> that will change the channel on the TV.<br />
<br />Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-1371590922062461722.post-28003574003419319122013-01-21T20:35:00.000+00:002013-01-21T21:16:46.311+00:00Python Web Application Server on the RaspberryPi<br />
One of the great features of the Rev. B RaspberryPi is the built-in Ethernet connection, which opens up many possibilities for web-connected projects. This enables this small device to be able to operate as a web server, ideal for running custom web applications. Since the RaspberryPi can also be connected to a host of other sensors and devices through the GPIO ports, this opens up the possibility of creating web-controlled devices.<br />
<br />
<h2>
Choosing a Web Framework</h2>
Since Python is the main programming language for controlling the RaspberryPi, I thought that the ideal web server for the device is a Python-based web framework. This means that the code for controlling the RaspberryPi's GPIO could be integrated right into the code for the web server.<br />
<br />
There are a number of Python web frameworks available - so that run some heavy-weight Internet websites. However, the ideal solution is a web framework that is:<br />
<ul>
<li>Easy to install</li>
<li>Quick to learn</li>
<li>Simple to write</li>
<li>Fast and lightweight</li>
</ul>
Even when considering this criteria, there are multiple options. I settled on one that I really like: <a href="http://flask.pocoo.org/">Flask</a>. The Flask project describes the framework as a microframework that is fun to work with. That sounds like the perfect accompaniment to a RaspberryPi!<br />
<br />
<h2>
Installing Flask on the RaspberryPi</h2>
Installing Flask on the RaspberryPi is really straightforward:<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> sudo apt-get install python-pip
sudo pip install Flask
</code></pre>
That's it! What we get is a built-in web server, a Python web framework which is WSGI-compliant, and a built-in templating engine (Jinja2 templates).<br />
<br />
<h2>
Hello World Web App</h2>
Once you have Flask installed, then you'll be itching to write your first "Hello World!" web app. Just find a suitable location on your RaspberryPi to store your code (I like to use a 'Sites' directory in the home folder and create a sub-directory for each project e.g. ~/Sites/hello), and a hello.py file as follows:<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run('0.0.0.0')
</code></pre>
<br />
If you've checked out the Flask website, you'll recognise this code as their starting example, with one difference: the addition of '0.0.0.0'. This makes sure that the web server is going to listen on all network devices, so you can browse to the web site from anywhere on your local network.<br />
<br />
To start the web server just run:<br />
<pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> $ python hello.py
* Running on http://0.0.0.0:5000/
</code></pre>
<br />
This will start the built-in web server and it will be listening for web requests on port 5000. You can browse to your web site from any computer, tablet or smart phone that is on your local network by navigating to http://the-ip-address-of-your-pi:5000/ e.g. http://192.168.1.105:5000/. There you will be greeted by your glorious, welcoming message.<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-ra3ZH4_9yzw/UP2bGtd7QjI/AAAAAAAAA4w/OmstlyJjA5M/s1600/helloworld.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-ra3ZH4_9yzw/UP2bGtd7QjI/AAAAAAAAA4w/OmstlyJjA5M/s1600/helloworld.png" height="150" width="320" /></a></div>
<br />
<h2>
Adding a Templated Web Page</h2>
Generating HTML from Python code quickly becomes unmanageable, so the recommended approach is to use a templating engine. The Flask install includes the <a href="http://jinja.pocoo.org/docs/templates/">Jinja2</a> templating engine which makes it quick and easy to develop web applications. Flask is configured to look for templates in a directory called <i>templates</i>. So create a folder structure like this:<br />
<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> /hello
hello.py
/templates
hello.html
</code></pre>
<br />
<br />
So, let's create the hello.html template:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> <!DOCTYPE HTML>
<html>
<header>
<title>Welcome!</title>
</header>
<body>
<h1>Hello from the RaspberryPi</h1>
</body>
</html>
</code></pre>
<br />
And now change hello.py to use the template. This is done using render_template:<br />
<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello(name=None):
return render_template('hello.html')
if __name__ == "__main__":
app.run('0.0.0.0')
</code></pre>
<br />
Restart the web server by running <i>python hello.py</i> and visit the RaspberryPi web page to see your new templated, web application.<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-jbqse0mrnQ4/UP2nH8lfFqI/AAAAAAAAA5I/HsBr1H6HFoo/s1600/hw1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-jbqse0mrnQ4/UP2nH8lfFqI/AAAAAAAAA5I/HsBr1H6HFoo/s1600/hw1.png" height="143" width="320" /></a></div>
<br />
That's it! You now have the basis for developing web applications that run on the RaspberryPi, written in Python. For more information on Flask, I recommend working through the tutorials on the <a href="http://flask.pocoo.org/docs/quickstart/">Flask website</a>. Once you have a better grasp of working with Flask, you'll be ready to add Python code that controls your RaspberryPi directly from your web application. Stay tuned for the next lesson.<br />
<br />
<br />Unknownnoreply@blogger.com3