James West

May include blinky lights. And cocktails.

Tag: Python

Hyperpixel cocktail recipe kiosk

We're never going to finish that bottle of Creme de VioletteIt all started when we saw this lovely cocktail cabinet in a vintage shop. I said we could only buy it if we actually used it for booze, and not to store the usual crap a family of four hides in their cupboards.

Deal struck, we brought it home and have since enjoyed mixing and drinking cocktails at the weekend. It’s been fun finding a collection of drinks we enjoy, but we have recipes in books, recipes on my Notes app and recipes squirrelled away on bits of paper. There has to be a better way – and this is it!

I put a Hyperpixel 4 from Pimoroni on top of a Raspberry Pi and wrote a Python program using Laura Sach’s guizero library. Guizero lets you create simple graphical environments, and even for a learner like me it’s very accessible with clear documentation and examples.

My code is here on Github. The functions at the top are called for each relevant PushButton press, and the stuff at the bottom is the nuts and bolts holding together the app layout. I wanted to use photos as the background to the recipes as much as possible, these are stored as .jpg files which are called to fore when you press the button for that drink. As I remember to take more photos of the cocktails before we slurp sip them I’ll add more pictures to the backgrounds. The Hyperpixel display is great: it’s clear, colourful and the touch works well, even with my tiny buttons. By default the “bottom” of the screen is where the power plugs into the Raspberry Pi, and as I want to hide the plug away behind my little kiosk I used Pimoroni’s simple instructions to rotate the display 180 degrees, which moves the power connector to the back.

The Hyperpixel4 is small; it’s just a shade larger than the Raspberry Pi itself, but crams in 800 x 480 pixels. Even so, every line you display counts. My app looked great on screen but the title bar was using vital space. Despite reading the guizero documentation I couldn’t work out a way to make the title bar disappear, so I tweeted Laura and was delighted when she replied with a solution for me. Simply put, guizero is a friendly wrapper for a more complex Python library called Tkinter, and you can use those methods to modify objects in guizero. It just took one line of code, which Laura included in her tweet, but it made all the difference to how my programme looks on the Hyperpixel.

Open with Python Launcher

I wrote this program entirely on my Mac – there’s no GPIO interaction, nothing requiring the Raspberry Pi hardware at all, so I wondered if I could also regularly use this on my Mac without having to launch it from Terminal. A quick Google search led me to this page where I found out that with Python 3 installed it’s easy to right-click on a Python file and choose Open With -> Python Launcher. I dragged the icon onto my Dock and it opens when I click on it – just like a real app! I changed the power off button to a quit button so I don’t keep shutting down my laptop, and now I can easily browse these cocktail recipes on my Mac or on my little touch screen Raspberry Pi kiosk.

Desktop Twitter ticker

I’ve already made a couple of Raspberry Pi powered Twitter tickers with wildly different display sizes and, honestly, levels of success. Most importantly, they’ve both failed to be wife-compliant; that is they weren’t finished to a standard where I’d be allowed to leave them out and actually make use of them!

With wifi now built in to the Raspberry Pi Zero W, and Pimoroni’s neat Scroll pHat HD available, I wanted to have another go at this, and having learned a lot of general makery-ness over the last couple of years I thought I could manage to box it up in a smart display.

The code was the easiest part of this project; I just had to modify my previous efforts to work with the Scroll pHat HD rather then the PaPirus Zero or LED matrices I’d used before and add some GPIOZero bits to get the buttons working. You can see the code on GitHub.

Perpendicular Pi

Raspberry Pi Zero W lies flat, but hidden behind the Scroll pHat HD and ProtoZero board

The hardware was straightforward too. I used a right-angle header on the Pi which connected to a ProtoZero board with a long female header to plug into the Scroll pHat HD. I soldered the wires for my two buttons onto the ProtoZero. I needed the Pi to lie flat so I could connect power into it while the display was standing upright. (I’ve used ProtoZero boards in several of my projects, they’re really useful for breaking out GPIOs and neatly soldering your buttons, LEDs and other bits and pieces onto.)

The case was the bit I expected to find most difficult. I wasn’t good at woodwork at school, and haven’t done anything like this before.

Drawings

I did a lot of head-scratching and drawing before making any cuts.

But I had a plan.

I thought I could sandwich together sheets of plywood with a hole cut through the middle of them to make the case. There was some transparent red acrylic left over from supersecretproject#2 that would make a nice diffuser for the really very bright LEDs on the display.

So I did a lot of measuring of the assembeled hardware and drew my pictures, before heading out to treat myself to a jigsaw. This was a good decision.

A slice of the box

It doesn’t matter how scruffy the hole in the middle is, as long as there’s clearance to get the Raspberry Pi Zero W and Scroll pHat HD through there.

I cut the plywood into biscuits and then drilled out the corners of the big hole I was about to jigsaw out of the piece. I made nine of these to accommodate the hardware, and then the front and back plates with feet to stand the box up on the table. I made a tidier hole in the front panel to show the display through, but drilled just a 12mm hole in the bottom corner of the back piece for the power lead. I used wood glue to glue the front half together in one stack, including the plastic and front panel, and then glued the back half separately. I drilled holes for the buttons into the top of this back half and then sanded everything down, rounded the corners slightly and gave it a varnish. This really brought out the colours in the different layers of ply and I think looks rather nice. I pushed the nuts to secure the buttons onto the wires, soldered up the buttons and threaded then into the holes I’d drilled into the top.

Now I did a last test of everything before gluing it all up and happily found everything worked as it should.

Then I glued the rear half onto the front, pushing the Scroll pHat HD right up against the acrylic, threaded the power wire through the rear hole and plugged it back into the Pi and glued the final back slice onto the sandwich.

When it all dried everything still worked, but the feet weren’t quite the same height and it wobbled when you pressed the buttons, so I sanded a few millimetres off the offending foot and gave myself a strong and stable foundation for the project!

This slideshow requires JavaScript.

I’m pleased with how this one has come out, and think it’s certainly wife-compliant, but as always I’d love to hear your suggestions for improvements in the comments.

UPDATE: Alex Burns tried to run the code from GitHub and found it wouldn’t run. This is because the Twython OAuth packages only install for Python 3 so you have to force the program to run in Python 3. To do this I typed the following at the command line:
python3 scrolltwitter.py

ANOTHER UPDATE: It turns out that buttons and a display make a versatile hardware platform, so to grant a request from a family member I just had to write a new program to create a “Sleeps-til-Christmas-ometer”1.

Raspberry Pi camera

Avenue evening

I do like a timelapse.

I recently subscribed to The MagPi magazine, and as a welcome gift received a Raspberry Pi Zero W, official Raspberry Pi case, camera connector cable and a few other bits and pieces. I already had a Raspberry Pi camera module knocking around, and as the case comes with a front that has a hole pre-cut for the camera, this gave me the perfect excuse to lash them all together.

Here’s a full list of parts used in the project:

  • Raspberry Pi Zero
  • Official Raspberry Pi Zero Case with camera connector
  • Camera module
  • ProtoZero board
  • Pimoroni Blinkt!
  • Male GPIO header
  • Female GPIO header
  • Right angle GPIO header
  • 3 tactile buttons
  • Hook up wire
  • Suction cups

I wanted to use buttons to trigger the starting and stopping of the timelapse, and to be able to take a single image, and planned at first to solder wires onto the back of the Pi Zero and hot glue some small buttons onto the case. Then I thought it’d be nice to also include an LED to show if the camera was on or doing something. (Everything is better with LEDs, right?)

This started to seem a little messy, but I remembered that in one of my boxes of bits I’ve a few ProtoZero boards from a Kickstarter a while ago. These are neat Raspberry Pi Zero sized boards with GPIO breakouts and lanes of breadboard-type sockets for components.

Raspberry Pi camera

The Raspberry Pi case comes with different fronts you can swap out. One is plain, with no openings, one has an opening for the GPIO pins and one has a hole and mounting points for the camera module. I needed to use this one, but it meant I had to solder a female header onto the back of the Pi Zero and connect the ProtoZero board onto that upside down. I added a right angle GPIO header to the ProtoZero board which let me add a Pimoroni Blinkt! for lots of LEDs, instead of just one.

Raspberry Pi camera

I was surprised, and relieved, when this rather hacky set up worked and my first attempt at running some code for the camera actually translated button presses into action.

Despite lots of trying, and lots of bad code, I’ve not been able to work out how to make a button interrupt a process and reset the Pi ready for another event, so I changed plans slightly and now have one button to take a single still image, one to start a half hour long capture of 120 images for a short timelapse and the third button to keep on taking pictures for a timelapse until your inelegantly pull the power out.

The Blinkt! lights shine white on booting up to show the camera is ready. When you take a single photo they flash red then reset to white. During a timelapse they display a column of lights counting up to the next image capture and flash red when a photo is being taken, so you’ve some idea when the next picture is due.

Timelapsing

To put the timelapse together I FTP into the Pi, copy the images onto my MacBook and use iMovie to make them into a video.

The code is on GitHub – please comment if you can see improvements or have any ideas for me. I’m not a coder and would love to hear your suggestions. I found the GPIO Zero documentation to be really helpful, especially the recipes, and Alex Ellis‘ blog post was inspiring.

Finally, to secure the case to a window Frederick Vandebosch came up with this case mod just in time, so I ordered some suction cups off eBay and got cracking with my craft knife.

I’m really pleased with how this has turned out – even though it doesn’t have the functionality I intended when I started out I think the project has turned out really well.

I’ve broken my radio!

I’ve been trying to add new features to the radio in my previous post.

It’s meant to display the current time on the 7 segment display unless you push the button to change station. Then it displays the number of the new station from the playlist (Radio 1 is 1, Radio 2 is 2… you get the idea) for a few seconds then return to showing the clock. The clock runs as a subprocess called by Popen and I can get it to stop to allow the radio to show the new station, but then I can’t re-start the process. The code is here on GitHub, with the related config.py and is repeated below.

It’s line 42 that reads config.proc_Popen that’s got me baffled. If you’ve any clever ideas please leave a comment.

#!/usr/bin/python

import time
import datetime
import os
import config
import subprocess
from time import sleep
from gpiozero import Button
from speakerphat import clear, show, set_led
from signal import pause
from subprocess import check_call

def shutdown():
    config.proc_Popen.kill()
    os.system("mpc stop")
    clear()
    config.segment.clear()
    config.segment.write_display()
    check_call(['sudo', 'poweroff'])

def retune():
    config.station += 1
    # Assumes there are 7 stations
    if config.station > 7:
        config.station = 1
    os.system("mpc play "  + str(config.station))
    # updates the Speaker pHat LEDs to show the station
    clear()
    for x in range(config.station):
        set_led(x,180)
    show()
    # stops the clock subprocess and shows the station on the 7 seg  
    config.proc_Popen.kill()
    config.segment.clear()
    config.segment.write_display()
    config.segment.set_digit(3,config.station)
    config.segment.write_display()
    time.sleep(4.0)
    config.segment.clear()
    config.segment.write_display()
    config.proc_Popen # This is the bit that's got me baffled.

shutdown_btn = Button(17, hold_time=3)
shutdown_btn.when_held = shutdown

retune_btn = Button(23)
retune_btn.when_pressed = retune

# starts clock.py
config.proc_Popen

# sets the Speaker pHat LEDS to indicate the station
clear()
for x in range(config.station):
    set_led(x,128)
show()
os.system("mpc play " + str(config.station))
pause()

eInk Twitter display

Last year I made this Twitter display using a Raspberry Pi Zero and a PaPiRus eInk display.

I loaded the code to GitHub and it’s recently been merged with the main demo code that PiSupply provides for their customers, which I was pretty excited about.

© 2018 James West

Theme by Anders NorenUp ↑