Blog

  • stats stats lay down flat

    blog stats for yesterday: over 4000 hitsOh my. This blog is usually a quiet little backwater, ticking along on a few hundred hits a day. And I’m okay with that. But yesterday, my astonishingly impractical QR code clock hit the front page of RaspberryPi.org, and blammo! More visitors than I thought possible. Are there really over 4000 people who read that? Cor, to use a good British comic-ism.

    I’ve been blogging for nearly ten years, filed under what could only charitably be called “miscellaneous”. Yesterday, I got 2% of all the hits I’ve ever had. See the tiny little bar just to the left of the big one? Yeah, that was my previous best ever, with nearly 600 hits.

  • The Quite Rubbish Clock

    Hey! This article is really old and probably doesn’t work any more: things have changed a lot in Raspberry Pi world since 2013

    Update 3: code for the demo video is here.

    Update 2: In which I actually post working code.

    Update: Eep! This post was featured on the Raspberry Pi blog today. Thanks, Liz!

    And now for something completely different:

    … a clock that isn’t human readable. You’ll need a QR code reader to be able to tell the time.

    Nokia screen on Raspberry Pi

    This, however, is not the prime purpose of the exercise. I was looking for an excuse to try some direct hardware projects with the GPIO, and I remembered I had a couple of Nokia-style surplus LCDs lying about that could be pressed into service. These LCDs aren’t great: 84×48 pixels, 3V3 logic, driven by SPI via an 8-pin header which includes PWM-controllable LED backlighting. They are cheap, and available almost everywhere: DealExtreme ($5.36), SparkFun ($9.95), Adafruit ($10, but includes a level shifter, which you really need if you’re using a 5V logic Arduino), Solarbotics ($10) and Creatron (about $12; but you can walk right in and buy one). Despite being quite difficult to use, helpful people have written drivers to make these behave like tiny dot-addressable screens.

    I’d been following the discussion on the Raspberry Pi forum about driving the Nokia LCD from a Raspberry Pi. Only when user bgreat posted some compact code that was supposed to run really fast did I dig out the LCD board and jumper wires. Building on bgreat’s nokiaSPI.py class and a few other bits of code, here’s what I built to make this singularly pointless clock:

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    # qrclock - The Quite Rubbish Clock for Raspberry Pi - scruss, 2013-01-19
    
    import time
    # need to use git://github.com/mozillazg/python-qrcode.git
    import qrcode
    from PIL import Image
    import ImageOps
    # uses bgreat's SPI code; see
    # raspberrypi.org/phpBB3/viewtopic.php?f=32&t=9814&p=262274&hilit=nokia#p261925
    import nokiaSPI
    
    noki = nokiaSPI.NokiaSPI()              # create display device
    qr = qrcode.QRCode(version=1,           # V.1 QR Code: 21x21 px
                       error_correction=qrcode.constants.ERROR_CORRECT_M,
                       box_size=2, border=1)
    bg = Image.new('1', (84, 48))           # blank (black) image background
    
    while 1:
        qr.clear()
        newbg = bg.copy()                   # copy blank background
        s = time.strftime('%Y-%m-%d %H:%M:%S')
        qr.add_data(s)                      # make QR Code of YYYY-MM-DD HH:MM:SS
        qr.make()
        qrim = qr.make_image()              # convert qrcode object to PIL image
        qrim = qrim.convert('L')            # make greyscale
        qrim = ImageOps.invert(qrim)        # invert colours: B->W and W->B
        qrim = qrim.convert('1')            # convert back to 1-bit
        newbg.paste(qrim, (18, 0))          # paste QR Code into blank background
        noki.show_image(newbg)              # display code on LCD
        time.sleep(0.4)                     # pause before next display
    

    (Convenient archive of all the source: qrclock2.zip, really including bgreat’s nokiaSPI class this time …)

    To get all this working on your Raspberry Pi, there’s a fair amount of configuration. The best references are bgreat’s own comments in the thread, but I’ve tried to include everything here.

    Enabling the SPI kernel module

    As root, edit the kernel module blacklist file:

    sudo vi /etc/modprobe.d/raspi-blacklist.conf

    Comment out the spi-bcm2708 line so it looks like this:

    #blacklist spi-bcm2708

    Save the file so that the module will load on future reboots. To enable the module now, enter:

    sudo modprobe spi-bcm2708

    Now, if you run the lsmod command, you should see something like:

    Module                  Size  Used by
    spi_bcm2708             4421  0

    Installing the WiringPi, SPI and other required packages

    WiringPi by Gordon is one of the neater Raspberry Pi-specific modules, as it allows relatively easy access to the Raspberry Pi’s GPIO pins. For Raspbian, there are a few other imaging libraries and package management tools you’ll need to install here:

    sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git
    sudo pip install spidev
    sudo pip install wiringpi

    Installing the Python QR code library

    Finding a library that provided all the right functions was the hardest part here. I ended up using mozillazg‘s fork of lincolnloop‘s python-qrcode module. mozillazg’s fork lets you use most of the lovely PIL methods, while the original hides most of them. Since I had to do some image compositing and colour remapping to make the image appear correct on the Nokia screen, the new fork was very helpful.

    To install it:

    git clone git://github.com/mozillazg/python-qrcode.git
    cd python-qrcode/
    sudo python ./setup.py install

    The tiny 84×48 resolution of the Nokia screen doesn’t give you many options for sizing QR codes. For the time display of the clock, a 21×21 module Version 1 code with two pixels per module and one module margin just fits into 48 pixels. Using a medium level of error correction, you can fit the 19-character message (such as “2013-01-19 18:56:59”) into this tiny screen with a very good chance of it being read by any QR code reader.

    (In the video, there’s a much larger QR code that’s a link to this blog post. That’s a Version 7 code [45×45 modules] at one pixel per module and no margin. This doesn’t meet Denso Wave’s readability guidelines, but the Nokia screen has large blank margins which seem to help. It won’t read on every phone, but you’re here at this link now, so you don’t need it …)

    Wiring it all up

    (Do I really need to say that you’ll be messing around with the inner delicate bits of your Raspberry Pi here, and if you do something wrong, you could end up with a dead Raspberry Pi? No? Okay. Just make sure you take some static precautions and you really should have the thing shut down and powered off.)

    You’ll need 8 female-female right-angled ones). Note that the thick border of the LCD is the top of the screen. These boards are made who-knows-where by who-knows-whom, and there’s a huge variety of labels and layouts on the pins. My one appears to be yet another variant, and is labelled:

    1. VCC
    2. GND
    3. SCE
    4. RST
    5. D/C
    6. DNK(MOSI)
    7. SCLK
    8. LED
    screen labels

    This is how I wired it (from comments in bgreat’s code and the GPIO reference):

     LCD Pin       Function      Pi GPIO Pin #   Pi Pin Name
    ============= ============= =============== =============
     1 VCC         Vcc            1              3.3 V
     2 GND         Ground        25              GND
     3 SCE         Chip Enable   24              GPIO08 SPI0_CE0_N
     4 RST         Reset         11              GPIO17
     5 D/C         Data/Command  15              GPIO22
     6 DNK(MOSI)   Data In       19              GPIO10 SPI0_MOSI
     7 SCLK        Serial Clock  23              GPIO11 SPI0_SCLK
     8 LED         Backlight     12              GPIO18 PWM0
    GPIO wiring
    back of screen

    Wire it up, and fire up the program:

    sudo ./qrclock.py

    Yes, code that accesses GPIO needs to be run as root. Pesky, but helps you avoid running code that accidentally scrams the nuclear power station you’re controlling from your Raspberry Pi …

  • Reprap.

    Reprap.

    Instagram filter used: Hudson

    View in Instagram ⇒

  • Like the sun shines out its ____

    Ariel_Rojo-cerdo_ahorradorI’m very taken with Ariel Rojo‘s Piggy Bank Lamp. It’s the first ornament I’ve seen that uses the form of a compact fluorescent bulb as an integral part.

    Not sure I’m quite taken enough with it to pay the $98 that the AGO store wants, though …

  • Oh dear, Canada.

    Oh dear, Canada.

    Instagram filter used: Rise

    Photo taken at: Real Canadian Superstore

    View in Instagram ⇒

  • Too many QR Codes

    I have, of late, been rather more attached to QR Codes than might be healthy. I’ve been trying all sorts of sizes and input data, printing them, and seeing what camera phones can scan them. I tried three different devices to scan the codes:

    • iPhone 4s – 8 MP, running either i-nigma (free) or Denso Wave’s own QRdeCODE ($2). QRdeCODE is better, but then, it should be, since it was created by the developer of the QR Code standard.
    • Nexus 7 – 1.2 MP, running Google Goggles.
    • Nokia X2-01Catherine‘s new(ish) phone, which I can’t believe only has a 0.3 MP VGA camera on it. Still, it worked for a small range of codes.

    QR Code readability is defined by the module size; that is, the number of device pixels (screen or print) that represent a single QR Code pixel. Denso Wave recommends that each module is made up of 4 or more dots. I was amazed that the iPhone could read images with a module size of 1 from the screen, like this one:

    hello_____-ei-m01-300dpi

    On this laptop, one pixel is about 0.24 mm. The other cameras didn’t fare so well on reading from the screen:

    • iPhone 4s – Min module size: 1-2 pixels (0.24-0.48 mm/module)
    • Nexus 7 – Min module size: 2-3 pixels (0.48-0.72 mm/module)
    • Nokia X2-01 – Min module size: 3-4 pixels (0.72-0.96 mm/module)

    So I guess for screen scanning, Denso Wave’s recommendation of 4 pixels/module will pretty much work everywhere.

    I then generated and printed a bunch of codes on a laser printer, and scanned them. The results were surprisingly similar:

    • iPhone 4s – Min module size: 3-4 dots (0.25-0.34 mm/module)
    • Nexus 7 – Min module size: 4-5 dots (0.34-0.42 mm/module)
    • Nokia X2-01 – Min module size: 8-9 dots (0.68-0.76 mm/module)

    A test print on an inkjet resulted in far less impressive results. I reckon you need to make the module size around 25% bigger on an inkjet than a laser, perhaps because the inkjet is less crisp.

    I have to admit I went a bit nuts with QR Codes. I made a Vcard: my vcard

    (and while I was at it, I created a new field for ham radio operators: X-CALLSIGN. Why not?). I even encoded some locations in QR Codes.

    Just to show you what qrencode can do, here’s a favourite piece of little prose:

    a_real_man

  • lit trees

    lit trees

    Instagram filter used: Lo-fi

    View in Instagram ⇒

  • Big block.

    Big block.

    Instagram filter used: Hudson

    View in Instagram ⇒

  • Adding a Bluetooth serial terminal to Raspberry Pi

    Hey! This article is really old. The advice given here will not work on a Raspberry Pi 3, and will need some care with recent versions of Raspbian.

    Sometimes you find a computer component that’s so cheap, that works so well, that you’re amazed you managed to live without it for so long. The JY-MCU Arduino Bluetooth Wireless Serial Port Module is that component for me right now.

    JY-MCU Arduino Bluetooth Wireless Serial Port Module from dx.com
    JY-MCU Arduino Bluetooth Wireless Serial Port Module from dx.com

    This little board is a cheap ($8.50!) Bluetooth serial port. It’s happy with the Raspberry Pi’s 3.3 V logic levels, and will communicate at standard rates between 1200 and 1,382,400 baud. It even comes with a nifty little cable which is just the right polarity for the Raspberry Pi’s GPIO pins. It’s really meant to do serial comms on an Arduino, but it’s not limited to that.

    What this board allows you to do is connect to your Raspberry Pi’s serial console via Bluetooth. That way, you can have your Raspberry Pi hidden away somewhere, and yet still log in as if you were talking to it directly through a serial cable. Combine this with a USB wireless adaptor (like the Belkin N150 that I use) and you’ve got a wireless device you can always connect to, even if your network goes down.

    In order to use this device with your Raspberry Pi, you’re going to have to do some reconfiguration. Exactly what reconfiguration you do depends on some additional hardware:

    1. If you have a USB-TTL Serial converter (like an FTDI Friend, FTDI Basic Breakout – 3.3V, or the one I use, the OSEPP FTDI), you can reconfigure the Bluetooth module to run at 115,200 baud, the default speed of the Raspberry Pi’s serial port.
    2. If you don’t have the serial converter, you’ll need to reconfigure the Raspberry Pi’s serial terminal to run at the JY-MCU Bluetooth adapter’s default 9600 baud.

    To reconfigure the Bluetooth module to run at 115,200 baud

    (I chose this option, as it allows me to use the Bluetooth module with Firmata on an Arduino, too.)

    The JY-MCU board comes with no instructions, but all the reconfiguration commands you’ll need are explained here: hc06_linvor_1.5_at_command_set [[hc06_linvor_1.5_at_command_set]] (cached copy; original has gone) While you’re setting the communications speed, you’ll probably also want to change the device name (so you can more easily recognize your own board, as the default is something like “Linvor”) and PIN (for that warm feeling of security that only a four digit code can provide). The device is configured using AT commands (or as we eldsters call them, Hayes commands) by plugging it directly into a USB-TTL Serial device attached to your computer. Here’s how you wire it:

    USB-TTL Serial    Bluetooth Serial
    ================= =================
    GND               GND
    VCC               VCC
    TXD               RXD
    RXD               TXD

    Note that TXD and RXD are crossed. The Bluetooth unit runs on a 3.6-6V supply, but 3.3V logic. To enter the AT commands, start a serial terminal (Hyperterm, minicom, screen …) at 9600 baud talking to the USB-Serial adapter, and copy and paste these commands in:

    AT+NAMEBluey
    AT+PIN4321
    AT+BAUD8

    You’ll have to disconnect the terminal and reconnect at 115,200 baud, as that last command just reset the Bluetooth device’s speed. You might want to use other settings than Bluey for the name and 4321 for the PIN, too.

    Update: check that your Raspberry Pi’s /boot/cmdline.txt contains:

    console=ttyAMA0,115200

    You will not get a login prompt otherwise.

    Now go to Using the Device.

    To reconfigure the Raspberry Pi’s serial terminal to run at 9600 baud

    Serial terminals traditionally ran at 9600 baud, and that seems a bit slow these days. But, if you don’t have a way of setting up the Bluetooth device differently, 9600 is what you’re stuck with. You’ll need to edit your Raspberry Pi’s /boot/cmdline.txt so that the part that previously read:

    console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

    to

    console=ttyAMA0,9600 kgdboc=ttyAMA0,9600

    Note that this file should only contain one line, so be careful you don’t add extra line breaks or your Raspberry Pi won’t boot. Save the file, reboot your Raspberry Pi, and go to the next section.

    Using the Device

    On your Raspberry Pi, connect the Bluetooth Wireless Serial Port Module as follows:

    Raspberry Pi      Bluetooth Serial
    ================= =================
    5V  (GPIO Pin  2) VCC
    GND (GPIO Pin  6) GND
    TXD (GPIO Pin  8) RXD
    RXD (GPIO Pin 10) TXD

    (Despite the minimum 3.6V rating, I’m happily running mine from the 3V3 power, GPIO Pin 1. YMMV.)

    When the board gets power, but isn’t paired, the LEDs on the Bluetooth module flash quickly. Now you need to pair the device with your computer (use 0000 as the PIN, or whatever you chose if you changed it), and it will appear as a serial port on your machine. On my Mac, that’s a device called /dev/tty.Bluey-DevB. The LEDs stop flashing when the port goes into use. Open up a serial terminal, set the device and speed correctly, and if all goes well, you should see:

    Debian GNU/Linux wheezy/sid raspberrypi ttyAMA0
    
    raspberrypi login:

    Success!