Raspberry Pi 7 Segment display

Introduction

One of my #RaspberryPi Zeros is called PiClock, and has an 8 digit seven segment LED display. The program it runs displays the time, and sends it to two other Pis, that display it on Unicorn HD HATs. Between midnight and 8 am, it flashes the message “SLEEP” every five minutes, as well. The software library that it uses can display numbers, and most upper and lower case letters, but not all of them. I rather liked the idea of animating sequences of single segments on it, because, well you know, blinkenlights. I had a look at the software library, “7seg.py”, to see if I could get it to do that.

It turns out that the library uses a Python dictionary to look up the byte to send to the display for each of the characters it can display. Decoding the hexadecimal bytes took a few minutes, working from the code for the digits from 1 to 5.

The first bit is always a 0. The remaining seven are the seven segments, in the order abcdefg, which are laid out like this…

So, the codes for illuminating single segments are as follows…

Now to amend the library! I needed some typeable characters to put in the dictionary, ready to be used in strings in the python code. For no obvious reason, I chose a selection of brackets and the tilde character, and amended the library file. The selection of brackets didn’t work!

After trying characters until they did work, I ended up with #][£<$~ as the symbols for the segments abcdefg.

I’m only showing the amended part of the file, where the pattern to send to the display is looked up. The arrangement of the brackets and tilde for the segments is as follows…

Now I’m ready to program PiClock to do silly animations, which will be fun, and a lot easier than using the WordPress editor. Note to self: See if you can find a WYSIWYG editor for WordPress.

A marker

All the old stuff is here, and the remaining stuff on Blogger and Blogspot has been abandoned, and will not update.

It must be obvious by now that I have no idea what I’m doing with WordPress.

Rick Stein’s cookery books

Now that I think about it, these are so very much more than just recipe books. They’re works of art in themselves, with terrific photography. Not just the photographs of the food itself, but the pictures of the places Rick has visited. Mind you, some folks will feel cheated when they find just how many pages are sumptious photographs, rather than recipes, perhaps.

The fish and shellfish book, as you would expect from somebody with world-famous fish restaurants, has an excellent section on the methods used to make the various dishes. Want to know how to dismantle a crab? It’s there, with clear pictures. All of his books have thoughtful descriptions of the destinations, their cultures, and anecdotes about the people Rick met, who cooked dishes from him.

Secret France, Road to Mexico, Fish and Seafood, India… I use them all.

But, I say, Rick! Using the same picture in two books? I thought I was suffering from déjà vu… Both the Fish book and India have a picture of Amritsar fish. One is zoomed in a little, but…

The same photograph in two of Rick Stein’s books.

Python and SQL with matplotlib.

# Quick hack to graph last 500 greenhouse temperatures from weather database.
import mariadb
import matplotlib.pyplot as plt
conn = mariadb.connect(user="pi",password="password",host="localhost",database="weather")
cur = conn.cursor()
tempIN     = []
tempOUT    = []
timestamps = []
# Get the most recent 500 records.
cur.execute("SELECT greenhouse_temperature, ambient_temperature, created FROM WEATHER_MEASUREMENT 
             ORDER BY created DESC LIMIT 500")
for i in cur:
    tempIN.append(i[0])
    tempOUT.append(i[1])
    timestamps.append(i[2])  
conn.close()
plt.figure(figsize=(14, 6))
plt.title(label="Greenhouse and outside temperature up to "+str(timestamps[0]))
plt.xlabel("Date and time")
plt.ylabel("Temperature in Celsius")
plt.plot(timestamps, tempIN, label='Greenhouse temperature')
plt.plot(timestamps, tempOUT, label='Outside temperature')
plt.axhline(y=5.0, color='r', linestyle='-.')
plt.legend()
plt.savefig("/var/www/html/GHtemp.jpg")
plt.show()

Adventures with Bread, part 94

 Rye bread, again…

You know how it is. There’s a recipe on the flour bag, and you think you’ll try it out. Well, you know, rye bread is tasty…
 
Rye bread recipe from the back of a rye flour bag.
Cotswold Flour’s Rye Bread recipe.

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Last night, I made the poolish, and it was lovely and frothy by morning. I got the mighty Kenwood Chef out, with its dough hook, and followed the recipe carefully, all the way up to the bit telling me to prove it for 1.5 to 2 hours. After an hour, I found this situation…
 
Almost invisible bread tin, with dough rising madly, and flopping over the sides of the tin.
Underneath this over-excited dough, you can just about see the bread tin.
That’s a pretty standard sized loaf tin, but it makes a change for a rye dough to rise so well. I scooped as much of it up as I could, and put it all in a bigger tin, which I put in the oven before I remembered to take a picture.
 
The same amount of dough, in a bigger, shallower tin, in the oven.
Baking begins…

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Then it was time to take the spaniels for their first walk of the day. Luckily, I didn’t meet anyone, and was back in time to remove the tin from the oven, and see what I had created.
 
Big, flat loaf, baked.
The result of 40 minutes in the oven, at 220°C
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
The obligatory crumb shot.
This is called the “crumb shot”. Pretty good crumb, if you ask me.

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
And there’s the result. I buttered the slice, and ate it, in the interests of science. It has a good flavour, and I’m looking forward to the rest of it, over the next couple of days. I can’t help thinking I should have had some pastrami ready to go on it…
 
 
 

Python on Raspberry Pi, a note about structure, or something.

I’ve been struggling with a problem with a Pi camera for a couple of days. Instead of being able to start up the camera, I just had error messages about MMAL running out of resources.

Now, I knew I’d seen it before, and sure enough, Stack Overflow had quite a lot of questions about it. But I’d seen them before. And then I remembered that I never found out why the problem went away before.

As an experiment, I tried something that I thought couldn’t possibly work, and suddenly everything worked. All it took was moving the camera instantiation from the top of the program to just below all the function declarations.

At a guess, the camera startup can’t get the resources it needs, because the Python interpreter is chewing its way though all the function declarations, and using up something the camera software wanted.

It’s an age or so, since I wrote a language interpreter, and it was for a simple language, Pilot, but I know interpreters have reasons for liking programs in a particular order, so that’s my guess…

#MMALresources

A Python time-lapse program.

A free program…

This is the Python code I cobbled together to make a time-lapse movie of my rather exciting flowering cactus. I’m sure this has been done better by lots of people. It runs on a Raspberry Pi Zero, with not much memory, and no online storage, so it sends the pictures to another Pi Zero, called PiBigStore, which happens to have a 2 Terabyte USB drive. Help yourself to a copy, if you like. Change the server name, and password, obviously. If you know ways this can be improved, feel free to comment.

# Time lapse pictures
import os
import time
import ftplib
from picamera import PiCamera
import schedule

def send_to_PiBigStore():
    hour = int(time.strftime(“%H”))
    #print(hour)
    if hour < 7 or hour > 21:
        time.sleep(250)
        return
    
    file_name = “cactus” + time.strftime(“%Y%m%d-%H%M%S”) + “.jpg”
    camera.capture(“/var/tmp/” + file_name)
    
    connected = True
    ftp = ftplib.FTP()
    try:
        ftp.connect(“PiBigStore”)
    except ftplib.all_errors:
        connected = False
        print(“Couldn’t connect to PiBigStore.”)
        ftp.quit()
        
    try:
        ftp.login(“pi”,”password goes here”)
    except ftplib.all_errors:
        connected = False
        print (“Failed to login to PiBigStore server.”)
        ftp.quit()
    
    if connected:
        ftp.cwd(“/media/pidrive/data/cactus/”)
        ftp.storbinary(‘STOR ‘+file_name, open(“/var/tmp/”+file_name, “rb”))
        print (“Sent to PiBigStore “, file_name)
    ftp.quit()
    os.remove(“/var/tmp/”+file_name)

# Main loop
schedule.every(5).minutes.do(send_to_PiBigStore)
camera = PiCamera()
camera.rotation = 90

while True:
    schedule.run_pending()
    time.sleep(10)
A foot-tall cactus on a windowsill, with a Raspberry Pi Zero with camera, mounted on a Lego tower.

Curse you, munmap_chunk()!

I still haven’t spotted a working solution to the problem where weather station programs in Python on Raspberry Pi fail, with no traceback details, after a couple of days.

I think it must be some resource in either the operating system, or the Python interpreter, running out, with very poor error reporting. I will leave it to people more familiar with the OS and interpreter to find out what it is, and fix it, in the fairly certain knowledge that everyone who could fix it has better things to do.

I found out that a Python program can actually restart itself, and changed mine to restart once a day. If that doesn’t fix it, I’ll let you know… (Update: it works.)

#RaspberryPi #Python 

Also: I used to have a comment on Stack Overflow, about this, but I removed it. The link below now just points to somebody else having the same problem.

My Stack Overflow comment on this.

Tofu squeezing

Tofu – a thing you need to know…

I tried cooking tofu several times, and was often very disappointed by the way it just broke up, and fell apart, when I tried. The results I got were nothing like the lovely illustrations people put by their recipes. Instead of pert, bouncy cubes of tofu, all I got was mush…


It tasted fine, sure, but something was wrong


There’s something they don’t tell you in those recipes, and it’s this. Tofu is basically ground up soybeans, and water. Actually, quite an astonishing amount of water! There are several grades of tofu, and the ones labelled “extra firm” have less water. Less, sure, but still a lot. You want to know how much? Look!

I treated myself to a tofu press from eBay, ignoring the ones with a wimpy little spring to do the pressing. It came with a piece of cheesecloth to wrap the tofu block in, which I did, but I had to find a usable weight. I did try balancing cans on top of the press, but eventually, I found my wife already had a suitable weight for the job…

Tofu in a press with a 6Kg weight on it and a jug with the water that squeezed out of the block.
Those standard size boxes of tofu contain over 175ml of water! Get it out, and you can cut the tofu into cubes, marinate it in something tasty, which will soak right into where the water used to be, and fry them without them falling apart. Instead, they crisp up nicely on the outside, and more importantly, they stay together as cubes.


Update: It turns out there’s at least one brand of extra firm tofu that isn’t full of water. It’s this one…

Cauldron Extra Firm Tofu Package

 

Duck and Mushroom ramen.

 

Duck and fancy mushroom ramen

Well, it’s what I made from the remains of the Sunday dinner duck, with the addition of some fancy mushrooms, home-made naruto and more.

There’s no helpful tip, or anything like that, with this post, so you really shouldn’t