Friday, 27 February 2015

Raspberry Pi CodeClub Week 6

Despite a week away from the Pis, nobody had forgotten how to set up their Pi and get everything connected. It is great to see the children confidently plugging/unplugging cables and getting everything up and running. By my reckoning this was the fastest set-up time so far.

All the clubbers were getting stuck in to the reaction game project. Everyone got the Scratch version working without much help from me and then was keen to try the Python alternative.

One of the speedier typists had his Python game working very quickly. We played it and after a few goes, ended up with a 'draw'. We talked about why the code was saying that both players had won and he immediately wanted to fix this. Undeterred when I wouldn't just tell him how to do it, he smugly said that he'd just use the Internet to find the answer. I think he was surprised when I said that was a great idea! We talked about Open Source software, code re-use and some of the risks or using code written by someone else if you didn't understand how it worked yourself.

We also watched Cmdr Chris Hadfield's ISS version of Space Oddity and discussed zero-gravity and weightlessness in the context of the Astro-Pi competition. I noticed that their ideas are becoming more focused. I want them to retain the creativity that has them suggesting a translation device that would help the astronauts if they encountered any aliens, and apply it within the parameters of the competition. I really keen that I don't steer them down any particular direction and let them shape the ideas. We're getting there.

I was really pleased to hear one that one of the girls had asked for - and been given - a
Raspberry Pi for her birthday. She's got a Pi2 and I we had a group discussion about the different versions and why our humble model Bs were still fine for the kind of thing we were doing.

I told the children about the Pi birthday weekend and they made me promise to try to acquire some goodies for the club while I was there. The pressure is on to deliver now!

Saturday, 21 February 2015

Elasticsearch on Raspberry Pi

Raspberry PI is obviously a great little machine for hardware hacking and leaning about computing. But I'm increasingly finding that its versatility makes it really useful for serious development.

Being able to quickly rebuild from scratch when it comes to testing configuration options for enterprise architectures like elasticsearch clusters is particularly powerful.

Elasticsearch runs surprisingly well on Raspberry Pis. Obviously you don't get lightning performance (at least not from B/B+s, I haven't yet tried with Pi 2s) but for testing index mappings, with small amounts of data the lack of speed doesn't matter.

Here is my method for setting up an elasticsearch node on a Pi. To make a cluster, just add more Pis.

1. Download the latest Minibian image and burn it to an SD card. You'll need a big card, ideally 32GB.

2. Boot the Pi, login and modify the disk partitioning.
fdisk /dev/mmcblk0
delete partition 2, then make a new one approx 2GB (4000000 sectors). Reboot.

3. Resize the new partition:
rsize2fs /dev/mmcblk0p2
4. Now create a partition using the remianing free space on the SD card for the elasticsearch data:
fdisk /dev/mmcblk0
5. Reboot so that new partition is available, then create an exxt4 filesystem
mkfs.ext4 /dev/mmcblk0p3
6. Create a directory to use as a mount point, and then add working directories inside it:
mkdir /elasticsearch
mkdir /elasticsearch/work
mkdir /elasticsearch/data
mkdir /elasticsearch/plugins
mkdir /elasticsearc/logs
7. Edit the /etc/fstab file so that the new partition is mounted automatically. Reboot again and check that the disk layout is what you'd expect.
df -h
8. Now do a software and firmware update:
apt-get update
apt-get install rpi-update
apt-get upgrade
9. One final reboot!

10. Download and install the latest version of elasticsearch
dpkg -i elasticsearch-1.4.4.deb
11. Now edit the elasticsearch config (/etc/elasticsearch.yml)  file to add a node name and make sure the paths for the work, data, plugins and logs reflect the /elasticsearch directories your cerated in step 6.

12. Now start elasticsearch
/usr/share/elasticsearch/bin/elasticsearch --config=/etc/elasticsearch.yml

Thursday, 19 February 2015

Fixing bent GPIO pins on a Raspberry Pi

At my CodeClub last week, one of the Pis was dropped on the floor and unfortunately landed right on the GPIO pins, bending four of them.

Back in the distant past I had to frequently straighten out pins on a similar connector  (used on a cryostat if you're interested) and became quite adept at it So I brought the Pi home and managed, quite easily to straighten out the GPIO damage. I thought I'd summarise the method I used in case it is useful to others. I imagine dropped Pis are not an unusual occurrence and it would be a shame to
leave a Pi in an unusable (for GPIO fun) state when it could easily be repaired. The GPIO pins are quite robust and will survive some fairly severe distortion. But reducing the amount of flexing to and fro when attempting a repair will improve the chances of the PI continuing to be usage into its old age. 

Tips for fixing bent GPIO pins.

None of this is rocket science, but there a few things you can do to avoid further damage to your Pi. 

0. First things first: Power down your Pi and disconnect from the power supply. Don't be tempted to try to repair even a small bend while the Pi is up and running.

1. Don't rush it. If the pins get damaged during a class, club session or CodeDojo, only try and bend them back if you have sufficient time and space to do it away from other distractions.

2. Use the right tools. Don't try and use your fingernails or a pen. A short, thin flat screwdriver is best. I'd also suggest avoiding the use of pliers, even radio pliers: it is all too easy to apply too much force and sheer off the pin, or squash it in the jaws.

3. If the bent pin is not touching an adjacent pin, slide the screwdriver blade down from the top and gently apply some force to push the pin back.

4. If the tip of a bent pin is in contact with an adjacent pin, slide the blade in and upwards from the side and bottom.

5. Apply gentle force to the blade while maintaining a form grip on the Pi. I've found it is much easier to do this with the Pi housed in the bottom part of its case, but this may not be possible depending on the type of enclosure that you have.

6.. If more than one row of pins have been bent, move inwards from the end of the header, un-bending each pair in turn. Don't try to make the pins perfectly straight again at this stage. Just try to separate them and get them as upright as possible. 

6. Once you've got them reasonably upright, you can use a female header to try to get them as straight as possible. I had a spare separate header to hand but you could use one attached to an add-on board (e.g. a Pibrella) or a ribbon cable.

7. Slide the header down over just one row of pins and then gently push it down so that the bottom lies parallel to the Pi board. Don't push it too far and bend the pins the opposite way. Once the first pair is done, remove the header, move on and slide it along so that it covers the next row too.  

8. Repeat this process, moving the header along until you've dealt with all the damaged pins.

9. Eventually you'll reach the point where you can push the header down over the entire set of GPIO pins on the Pi.

10. That should be job done. The pins may still look a little wonky under close visual inspection, but as long as a header slides down relatively smoothly, it should be good enough for government work!

    Tuesday, 17 February 2015

    Raspberry Pi CodeClub Week 5

    Armed with the tested and verified worksheets, I was optimistic that everyone would be making their leds flash by the end of the session.

    I also hoped for a faster get-up-and-running time now that I'd added the HDMI settings to the /boot/config.txt files on all the Pis. This seemed to be a success as everyone got their Pis working and connected to the screens without any assistance from me. A first!

    Actually, a couple of Pis were stuck with the old 'not reading the SD card'. I'd already shown the group the 'trick' of removing the card and blowing on the contacts: a quick reminder and the children were able to fix the problem themselves.

    We also had a visitor to the club today. A reporter from the local paper had come to write an article about the school's CodeClubs being awarded Star Club status and took some great photos of the children posing with their Pis. I hope I was clear enough to explain the ethos and philosophy behind CodeClub and Raspberry Pi, but I guess I'll have to wait to see the published article to find out...

    I was struck by how confident some of the children have become at assembling their Pi and constructing the circuit. They whizzed through the simple led blink activity and launched into extending into a reaction game by adding a couple of switches.

    Quickly completing the Scratch control code the two boys who've already done some Python didn't take long to start tackling that version. Fantastic progress!

    I wanted to build some momentum around our Astro-Pi ideas by demonstrating as many of the sensors that the Astro-Pi board as I can. This week I set up a MiniMU-9 (that I'd been using for a kite-mapping idea) with my lovely PI 2 to demonstrate what kind of measurements might be possible with an accelerometer and a gyroscope. Thanks to David E Grayson's excellent visualiser code I was able to give a really graphical display.

    Casualties of war 

    We had one Pi dropped on the floor and, unfortunately, it landed smack on the GPIO pins. I resisted the impulse to try and bend them back during the club and have brought it hope to attempt a repair in calmer surroundings.

    Monday, 16 February 2015

    The evolution of Raspberry Pi: a Lego scene

    I  wanted to take a photo of all the different Pis that I have and was thinking about an interesting way to display them. My sons suggested building a Lego scene and so an enjoyable Sunday afternoon was spent constructing a timeline of Pi evolution.

     Each quadrant of the base shows a different Raspberry Pi model.

    In the  laboratory mad scientists are working on the very first Pi: the original model A.

     Next door, in the factory, a model B Rev 1 rolls along the conveyor belt, inspected by a bowler-hatted businessman.

    In quadrant 3, a high-tech crane lifts a heavy model B rev 2.

    Next, a custom lorry transports a model B+ to a CoderDojo.

    At Lego primary school, excited pupils start plugging everything in to their shiny new model A+ while the teacher carries on with their maths lesson. 

    Finally, the amazing Raspberry Pi 2 is strapped to it's rocket, ready to head to the ISS as part of Astro-Pi.

    But wait, who is that lurking in the trees? Is it a Pirate, a Monkey, a Robot and a Ninja?

    And who is the pith helmet wearing explorer hanging about with the mad scientists?

    And why is there a hotdog seller hanging around the Pi logo?

    Here are some more pictures. 

    And here are the architects of this monstrosity:

    Thursday, 12 February 2015

    UnicornHat Scrolling library updates: Happy valentines

    To help celebrate Valentines day I've updated my UnicornHat scrolling library with a new feature: special symbols.  You can now display a lovely, romantic heart (ahhhh), a silly smilie (hahah) or a scientific degrees symbol (by popular request).

    To use the symbols, they are identified in the text string to be displayed by a tilde (~) prefix and a 5-character code.

    For the heart: ~ heart.
    The smilie: ~smile
    The degrees symbol: ~degrs

    For the nicely formatted results with the degrees symbol, don't leave any spaces between it and the surrounding characters.  For example.
    unicorn_scroll('It is 25~degrsc','blue',255,0.5)

    Monday, 9 February 2015

    Raspberry Pi CodeClub Week 4

    This week we followed on from our first foray into GPIO with the almost obligatory blinking led project (the hardware equivalent of 'Hello World').

    Most of this group were already familiar with the idea from an almost identical task in the DIY Gamer sessions last year and got stuck straight in.

    I had a couple of annoying glitches this week. Firstly, a couple of Pis seemed to have problems working with the HDMI input on the Lenova all-in-one screen/PCs. The process is always a bit fiddly. Unless the HDMI lead is connected when the Pi first boots up, it will not be detected as an active input by the screen. So you have to connect the HDMI lead, power up the Pi then quickly hit the 'HDMI' button on the Lenova unit.  I've resisted it so far (to avoid tying the Pis to particular hardware) but I think I need to have to add some of the HMDI settings to the /boot/config.txt.

    The first obvious thing to set is


    which will allow the display to be connected after the Pi has booted. This will normally result in the PI not knowing the resolution and refresh rate of the monitor and defaulting to what the children call 'big words'. So to find out the  EDID settings of the monitor you can use:

    tvservice -d edid.dat

     and then use

    edidparser edid.dat

    to read the results. Then look up the appropriate HDMI mode and group here. So for example, if the edid data says

    HDMI:EDID preferred mode remained as DMT (58) 1680x1050p @ 60 Hz with pixel clock 146 MHz

    then you'd add the line


    Hopefully I'll have time to test these settings before next week's club. Assuming it works, I'm not sure whether to get the children to update their own config.txt files themselves or whether to just bite the bullet and change each SD card myself. I suspect I'll do the latter.

    The second problem was that one Pi's version of ScratchGPIO seemed to get corrupted. Whenever they loaded it they received the error 'Could not read project; file may be damaged: (bad header).

    I couldn't fix in during the club but took the offending SD card and just reinstalled Scratch.GPIO and everything was good.

    The final glitch was entirely of my own making. When I was checking the worksheets a few weeks ago I spotted an error in the Fritzing diagram (wire going to wrong GPIO pin) and corrected it. However a hardcopy version of the duff version must have sneaked into my folder and been photocopied because some of the children had been using them. This lead to some random rebooting of their Pis or the led just not lighting up.

    I couldn't work out what was going on but fortunately my son spotted the problem (he broke to to me gently: "Dad, your worksheets are rubbish").

    Luckily I was stopping the club early today so that we could talk about Astro-Pi. Some of them had already heard about this from TV (nice to know that kids still enjoy Newsround). I showed the group the excellent movie narrated by Tim Peake himself which they really enjoyed. Then we talked about what kind of things a Pi could be used for on the ISS. As I'd expect from the bunch, the ideas were certainly imaginative: using dead animals as switches (like we did with the Marshmallows last week) to see how things rotted in zero gravity was probably the most bizarre (and gruesome).  My plan is to talk about a couple of the Astro-Pi board sensors each week. Hopefully we'll be able to get our hands on an actual board soon.

    Things to do differently next time?

    Double check the worksheets for errors! And then get my sons to check again!

    Monday, 2 February 2015

    Raspberry Pi 2 - first impressions

    Thanks to the those great folk at Element14 I was able to get my hands on a (free!) Raspberry Pi 2 today on the day of release.

    Initial impressions are that this is indeed a much faster beast. The Pi boots quickly, the GUI opens almost immediately and the response time in Minecraft is (according to my sons) much improved.

    I needed a more empirical test so I thought I'd set up the Pi2 to act as my online Minecard generator. Normally, on an old (can we really call them that now?)  Model B it ran at at least 70% CPU utilisation and the whole image building process takes about 90 seconds (although that does include upload time).

    I ported all the code over to me shiny new Pi2 and swapped the network cables. Simply building a new world in Minecraft is incredibly quick and does barely taxes the CPU about 10% (I've allocated 512M to the GPU). In terms of the entire image generation process itself, it was so obviously faster that I was able to remove almost all of the time.sleep() calls in the Python that I'd needed to ensure that all the various stages completed in the correct order.

    The Pi2 builds and uploads an image in 33 seconds. At least a threefold increase in performance!

    I've returned the original Model B to service for now, so that I can do some more tests on the Pi2.

    Raspberry Pi CodeClub Week 3

    'What are we doing today?' I was accosted by one of the clubbers as I tried to sneak through the lunch hall (not easy in a hi-vis cycling jacket).
    'No really, what are we going to be doing?'
    'Marshmallows. Really.'

    I wanted a fun introduction to the GPIO and this project seemed perfect.  I wanted to stick with my philosophy of avoiding step-by-step worksheets so I condensed the instructions down into a single sheet.

    Everyone seemed really keen and there were no moans about the lack of Minecraft (I think the potential for eating the equipment helped).  I was really pleased that 2 of the group managed to get their Pis connected and running without any help from me at the start this week. And several of the others managed with only minimal assistance (powering up the Pi before connecting it to the HDMI port seems a common problem).

    By the end of the session everyone had a working squidgy button and associated Scratch code. I think I had an opportunity to talk to all the children about the GPIO pins and there were some good questions about the electronics side of the project ('why doesn't the electricity cook the marshmallow?')

    Only one experimenter plugged their jumper leads onto the wrong pins (basically they'd used the pins on the opposite side of the header). This resulted in their Pi rebooting whenever the paperclips touched. Fortunately the Pi survived but it provided a good opportunity to talk about taking care when connecting things up. I'd printed an A3 version of my GPIO poster (remixed from the always excellent Pi education materials) and this now adorns the wall of the computing suite: hopefully they will now take the warning at the bottom seriously!

    There were lots of sticky fingers at the end. One boy twigged early on that if he mushed his mallow he'd be allowed to eat it and and be given a replacement. I did have to send couple of the girls for a courtesy hand wash to avoid really icky paw prints over everything.

    Once again I left it too late to get the children to help with the disassembly. I did consider washing the paperclips for future re-use but decided not to bother in the end.

    It was great fun again this week: these sessions have been the favourite of all the CodeClub and CoderDojo activities I have run. Allowing the children to explore, free from the tyranny of long worksheets is really working well with this cohort.

    Things to do differently next time?

    The cases supplied with the Raspberry Pis are a real pain to take apart. The lid is secured with 4 clips, 2 on each side, and you have to release all of them before it can be removed. I should have taken the lids off before the children connected up their Pis at the start. None of them could get the lids off (several saying they didn't want to break it) and I found it more difficult than usual because of all the attached cables. We'll need access to the GPIO for the next few weeks so I'll leave the case lids off. 

    It does seem odd to provide Pis for schools with these cases. They are quite durable but make it difficult to get to the inner workings of the Pi (which is surely counter to the whole point of Pis in schools). 

    Casualties of war 

    Apart from the paperclips, everything else survived another session. Apart from this power supply. It would appear that this happened last week and I have no idea how the earth pin broke off, or where the broken bit ended up (I checked all the sockets).

    Sunday, 1 February 2015

    Updated UnicornHat scrolling text library

    I finally found the time to update the character definitions for the Pimoroni UnicornHat scrolling text library that I wrote before Christmas.


    In addition to fixing the spacing issue with the hash, I've added the dollar sign $, asterix *, normal brackets(), equals = and plus sign + along with the two chevrons >< and double quotes ". So now you can produce algebra!

    The code is available on Github.