Bag lady gets lit up! Part 2

Wow, this was a bit of a learning curve for me!

This part will explain how I used the Pi Zero software to make the bag light-up headless.  That is, start up the code automatically without it being attached to a keyboard and screen.


Coding the arrangement of lighting patterns in Python was relatively straightforward.  I just followed Jon Witt’s instructions in his book here: Wearable tech projects with a Pi Zero

The first new thing I had to learn was ‘What is a shebang?’ because that was why my first attempts didn’t work.

A shebang is this line at the top of a Python program:


Apparently, when you are working with keyboard and screen this statement, which gets Python started, is automatically added if it wasn’t coded at the top.  When you are working headless you have to add it yourself.  So that was the main problem I had with getting the code to work headless.

After you’ve got the Python program completed correctly, the automatic start-up is coded in the LX Terminal.

Full instructions are in Jon’s book and I can’t profess to understand what they do but I managed to save them to my Pi Zero using the Terminal.  Well, I confess, I typed these in and saved them several times because I thought this was where my mistake lay.  However it was, ‘All about that Shebang!’  So, in the end after typing and saving several times, I had a typo in the LX Terminal script – therefore please be careful with those characters and spaces.

(By the way, if you haven’t heard ‘All about that Bass’ by Meghan Trainor then I’m sorry about my silly reference to her song.)

In the text, one thing I wasn’t sure about was this word: systemctl .  This stands for system control so don’t get mixed up between the l and 1 .

The headless start-up uses a piece of software called systemd which I don’t know much about – but it works, as you can see in this video: A LED …  So I’d created a system ‘service definition’. An explanation from a Linux site states: A Linux service is an application (or set of applications) that runs in the background waiting to be used, or carrying out essential tasks.  So I have ‘defined’ one of these to carry out the headless start up.

As I hadn’t included a button switch in the circuit, I also had to add code to my Python program to make sure the Pi Zero shuts down properly.

Mike Horne of CamJam and Pi Wars gave me this method of shutting down.  (Go to Pi Wars in April – it’s great!)

At the top of your Python program type:

from subprocess import call

and at the end of your code put:

call(“shutdown -h now”, shell=True)            

  (In ‘-h’ the h stands for ‘halt’ I think)

and the Pi Zero will shutdown correctly after the light pattern finishes.

Next time I do a wearables project I’ll incorporate a button switch as they’re only 97p here: at CPC

In some ways, it would have been easier to use an Adafruit Gemma or similar, like I did on my first wearables project, but I’ve learnt so much from using a Pi Zero here.  And I do appreciate being able to use bog-standard Python to control the LEDs rather than a different API (Application Programming Interface) such as the Arduino one. This was described here: Gemma wearables starter pack – 2

I chickened out of using SSH to start up my Pi Zero from another computer so that’s something I need to tackle in the future.

Well, I’m so pleased I finished it (and it works!) in time for the 6th Big Birthday of the Raspberry Pi on 3rd – 4th March!


2 thoughts on “Bag lady gets lit up! Part 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s