Skip to content

Blog

Introducing pi-dial - Part One

The backstory and overview

I’ve started work on yet another project: pi-dial

I’ve been looking at the Media Dial project on Adafruit for a while as a method to control the volume on my home theater receiver. I’m lucky that my home theater shares a wall with my home office, which made it easy to wire an extra set of speakers to use with Zone 2. This allows me to listen to my record collection while working in my office for my day job or when I’m at my workbench. My record player is still only 10 feet away, making it convenient to flip a record or put a new one on the turntable.

The dial runs CircuitPython, and would connect to my receiver using the denon-avr Python library or the HTTP POST commands that the receiver also excepts over the network. The dial would let me change the volume either up or or down and clicking the button would mute or unmute Zone 2. I’m lucky that Denon has a pretty well documented API using these HTTP commands and even better that Oliver aka scarface-4711 wrote the denonavr Python library to easily use Python commands to control the receiver and licensed it liberally with the MIT license.

The project became possible when my boss bought a 3D printer. When I first came across this project last year, I briefly looked at some co-op maker spaces and even a commercial company to see if I could get things printed, but during the pandemic lockdown it wasn’t going to work.

I’m doing this project for two reasons:

  1. I want to continue learning Python, especially with hardware like CircuitPython or the Raspberry Pi. This is very different than the web development I’ve done with Python so far.
  2. I like the idea of something physical to change the volume or mute. My partner walks into my office? Hit the button and mute it. Records have a different volume range? Move the dial!

I’m also excited to learn a whole new set of skills - breadboards, electricity, soldering, and more.

It also turns out I can already do this in Home Assistant, the Python-based open source home automation software, that is fantastic.

denonavr Python library configured in Home Assistant

I have a Firefox tab pinned for Home Assistant and the only reason is to be able to control my receiver. From here I can mute, change the volume or change the inputs. All using the denonavr Python library. I have a few other devices configured in Home Assistant, but I haven’t spent the time to set up a Lovelace UI or other automations. My house isn’t very “smart” and I definitely don’t have any voice assistants. I may make some of my own sensors some day, when I’m better at soldering and CircuitPython. But I digress - one step at a time.

But then I ran into a problem. CircuitPython has a limited subset of 3rd party libraries maintained by Adafruit. There is no denonavr module available, which abstracts away all of the HTTP POST commands to control the receiver over the network. But that’s ok - I can use the requests library that is in CircuitPython to do this. It will be more work, but it will help me learn that library better.

In the next blog post, I’ll share some of the challenges I ran into when I was ready to start prototyping.

Retiring My First Python Projects

It’s a sad weekend in my workshop. I’ve officially retired and archived my first two Python projects, NFLPool and MLBPool2.

I vividly remember Thanksgiving weekend a few years back when I decided it was time lo learn to code. I bought a few O’Reilly books on a Black Friday special and quickly learned that I can’t learn from books. It would be another year and a half before I buckled down and really learned Python.

I’m of the opinion that if you’re going to be community taught (I don’t believe you’re really self taught - you’re learning from the community of users and teachers, whether that’s in a formal course you’re taking online or asking questions on Reddit or Stack Overflow) you have to have an end goal in mind. It’s one of the most common questions I see on Reddit: “I’ve learned the basics, but I don’t know what to do now?” Most people need an itch to scratch, a problem to solve to really apply what they’ve learned.

NFLPool was that for me. We had spun NFLPool off of MLBPool2 and I was the commissioner. I was doing it all in a spreadsheet manually, each Tuesday having to look up the team and player stats and I knew there had to be a different way. And there was.

MLBPool2 was based on the NFLPool code, which made it easier to get up and running later. But neither project would have existed if it hadn’t been for Talk Python’s Python for Entrepreneurs course. The skeleton of both sites came from that course and modifying that code base taught me so much about Python and Pyramid, the Python web framework that powers both sites.

I didn’t know if I would stick with it, but here I am four or five years later, still learning and branching out in how I use Python. I’m still working on another web site, building it twice with two different frameworks (I really want to learn FastAPI) and also learning how to use hardware with Python. (More on my experiments with the Raspberry Pi, rotary encoders, soldering and more will be recapped in another post).

NFLPool never really had enough players to survive. MLBPool2 did, but in the second year of it being online, the commissioner changed some of the rules and how points were scored. Updating the code introduced a bug in how ERA is calculated and I was never able to find the bug.

I thought I had turned off registration in both applications, but script kiddies are still registering themselves with NFLPool. Before they find a flaw with SQL injection or something else stupid, I’ve replaced both sites with a landing page that redirects to the project pages here. If it wasn’t for them, I would have left the site and scores up to show what had been built. I've officially put both repositories in Archive mode on Github.

In the end, they both served their purposes in giving me motivation to learn how to code and for that I’ll always be grateful.

My First PyCon

I'm attending my first PyCon today. I'm pretty excited. I know some argue whether virtual vs. physical is good, but as somebody new to the community, it's almost easier to come lurk. But if I knew more people, there's no question that the hallway track in a physical conference is always fun.

Just a few of the talks I'm looking forward to:

  • "Learning python during lockdown: a surprising bonding experience with my child"
  • "More Fun With Hardware and CircuitPython - IoT, Wearables, and more!"
  • "Diversity & Inclusion Workgroup Panel"
  • "Introduction to Pydantic"

And more! This will be a fun weekend.

Richard Stallman Must Go

Back when my blog ran on Wordpress and I had analytics and comments enabled, one of the most viewed blog posts was one from almost ten years ago, where I agreed with another blogger that the FSF should be forked.

With Richard Stallman re-joining the FSF this week and the valid outrage that has followed, I remembered the blog post but not the content. Larry Cafiero's original blog is no longer on the internet, but I found his original blog post and follow-up on the Internet Archive's Wayback Machine.

It just amazes me that after all these years, we're still arguing about someone as toxic as Richard Stallman being around. Ever since I saw his keynote at GUADEC and his awful, awful talk, I've wanted nothing to do with him. I disagreed with he how he spoke to GNOME Foundation members running for the board, his views on women and the disabled, and I strongly disagree with his views on trans people. He needs to be held accountable for his views and the things he has said, and the Free Software Foundation's board needs to be held accountable for allowing him to be re-appointed to their board.

I'm proud to add my name to this open letter from major organizations and over 1,000 people calling for Richard Stallman to be removed from all leadership positions, and the Board of the FSF to step down.

Richard Stallman must go. Again.

Silver Saucer Update - March 2021

Following up on my last blog post, I did deploy Silver Saucer a couple days later. I spent a day trying to track down why MLBPool2 kept loading when I visited silversaucer.com and it turns out that the nginx web server defaults to the application with the lowest port number. I had made a mistake in the nginx configuration in one little place that took me forever to figure out. And with that, Silver Saucer is live on the web.

It still has a nasty viewport problem and the screen height is not 100% and it’s driving me crazy. Maybe if I had finished my Coursera class on HTML, CSS and Javascript I would have figured it out…

A bad habit that I have is when I get stuck, I move on to a different project. I’m hoping that my subconscious will fix it, but it’s more out of frustration. So what did I do? I bought more training courses from Talk Python and decided to learn the FastAPI web framework instead. I was surprised to see such a new project in third place in the recent PSF survey, especially as it was the first time being included as a framework.

I decided to build my site along with the training, rather than the PyPI clone the training has you do. After sorting through a couple of different bugs, guess what? I still need to learn CSS.

I’ve switched courses, moving from Coursera to Pluralsight, and am taking a new class to learn HTML & CSS. I’ll get there one of these days.

Preparing Silver Saucer for Deployment

Pyramid, my favorite Python web framework, was released version 2.0 late Sunday. The biggest change is dropping legacy Python support. I've been running the beta version of 2.0 the last couple of months while developing Silver Saucer.

The Pyramid 2.0 release helped me change gears from my hardware project back to Silver Saucer. Under the mantra of “release early, release often” I decided to start prepping to deploy Silver Saucer. I knew I had a few things to work on still, especially the big “On This Day” feature I haven’t started.

Of course I didn’t save any notes from when I last deployed a Pyramid web app. I started to make a list of things I needed to do on my DigitalOcean droplet and realized that Silver Saucer’s IP on my server wasn’t set up correctly which was why I kept getting an SSL error. That was an easy fix when I remember Dependabot was still broken on Github.

I had tried adding a pre-commit hook to use the black linter to my Github repository. It required me to create a pyproject.toml file. But doing this caused Github’s Dependabot to think I was using Poetry to manage my Python modules, when I’m not. I didn’t have any luck on the Github forums, so I just removed the file from now. Dependabot started working again right away - spewing out over two dozen pull requests to go through. It took Azure Pipelines a little while to catch up in testing out all the builds with the PRs, but only one or two failed and I still have one more PR to test and / or fix.

With that done, I thought I’d fix just one or two little things. This led me down a path of refactoring the theme, especially how the header works. I had the header image included in every page, and I was able to put it in just the layout template.

I also have a weird CSS bug with the viewport where it’s not filling the website screen 100% vertically. I spent hours on that but my CSS skills aren’t there yet, especially when using a complex CSS theme created by someone else.

I’m going to push Silver Saucer live in the next few days. The irony of creating a random Discogs player when I just started listening to my entire collection alphabetically is not lost on me. Release early, release often!

Always Learning

I haven’t blogged about my progress on Silver Saucer over the last month, but that doesn't mean I haven't been up to anything. I’ve continued to take the HTML, CSS, and Javascript on Coursera. I find CSS... difficult. Unfortunately, I also received an email from the course instructor that Coursera will be removing this course soon, so that figures. But it's motivation to get it done.

One of the things I tackled a few weeks ago was making an applet to sit in the macOS menu bar to control the volume of my Denon receiver located in my home theater. The receiver’s Zone 2 controls the volume in my office, where I’m usually listening to records. It took 30 minutes to throw some code together using RUMPS to make the applet and the denonavr Python module to control the receiver.

That led me down a path of looking into making a hardware project to make a dial to control the volume. Adafruit has a great tutorial on building one using Circuit Python, but I don’t have a 3D printer (yet).

That led me into looking more into the denonavr Python module - which is also the same module that powers Home Assistant. I’ve now reinstalled Home Assistant - it’s been a couple of years since I last tried it out. Now I can just turn to a browser page I leave pinned and control my receiver from there. I’ve spent a few days playing and tweaking Home Assistant to see most of my “smart” devices, but I’ve never been all that into having a smart home (nor will you find any voice assistants here). Of course this has given me a dozen ideas of things to build or integrate. But I think I still want to build a dial!

Denon Control in Home Assistant

I also spent a few days doing a deep dive into Docker. I have a couple containers that I run on my NAS, but never really understood the right way to update or manage them. Now that I have a little more knowledge I realize how slick containers really are.

Finally, I was on vacation last week and started to tackle another project I’ve talked about with a friend for the last couple years. We need a pump (or pumps) programmed for a project. I had some old Raspberry Pi computers laying around and a year ago we quickly prototyped a solution. Unfortunately, we needed a more powerful pump - both in how it pumps and in power consumption.

I’ve been learning about circuits and electricity by watching a lot of YouTube videos and reading a number of books I’ve picked up over the years (especially on Humble Bundle) . I then went out and bought bench power supply unit (PSU) and multimeter for my workbench. I figured out how to power the breadboard with a 3.3v / 5v breadboard power supply first and then figured out how to use the PSU, too. It was kind of exciting to see a little blue LED light up the first time I got a circuit right:

Well, that’s exciting. Sometimes it’s the little things. pic.twitter.com/QM89RgzhjU

— Paul Cutler (@prcutler) February 12, 2021

Here’s a picture of my messy workbench below with the Pi hooked up to a breakout board which connects to the breadboard. Also connected to the breadboard is a DRV8825 motor controller which is connected to a NEMA step-motor.

In theory, I have everything hooked up correctly now to start prototyping. Now the hard part starts: Is the code wrong or the hardware hooked up incorrectly?!

My workbench

Listening to my Record Collection Alphabetically

My Record Collection

I love this blog post from Discogs. I’ve often thought of doing exactly what it lays out: listen to your record collection from A to Z.

I know there are a lot of records I rarely listen to. Whether I bought them because I love the artist, because of their rarity, or just because I was taking a chance on something new, I’m sure there are records collecting dust.

I’ve done a pretty good job, especially last year in the first year of the pandemic, selling off any records I know I won’t listen to again on Discogs. I also take some pride in that if the record is rare or was released on Record Store Day for example, not to overcharge when selling some of these.

I haven’t decided what I’m going to do about artists like Ryan Adams. I was a huge fan, but I haven’t listened to one song of his since news broke a couple years ago of how he abused women.

First up today: ABBA! I’ve always loved them, probably due to hearing them so much as a kid from my step-mother. The record on top of the photo is ABBA’s Waterloo, their second album. I actually picked this up at Electric Fetus a couple years ago and until today, I’m embarrassed to say, it was still sealed - 48 years later! But not anymore!

I’m lucky that my home theater is on the other side of the wall from my home office. My office is Zone 2 and it’s an eight foot walk to put a record on. This way while I’m working during the week or writing or tinkering on projects on the weekends I almost always have music on.

ABBA - Waterloo

Silver Saucer Update

Now that I have the ability to pick a random record to play using the Discogs API, I’ve completed one of the two big features.

I’m now debating what to do next. I have a few options, all still focused on the random album feature. (I may have to cancel the “On This Day” future, but that’s a different discussion for a different time).

Learn CSS

I need to learn CSS as the theme I’m using needs to be updated. The text colors in the theme are too dark and I had to use a hack to show the text in white. This also means that the link colors are also appearing as white and you can’t tell that they are links. In the past I’ve been able to get away with just poking at a CSS file to get done what I want, but I really should take the time to learn it. The CSS file for Silver Saucer is huge and there are parts of it when I look at it that I have no idea what it does.

Bootstrap

I think I need to learn CSS before I tackle Bootstrap. Right now when the album results are returned, everything is centered on the page. I played around with trying to justify it left and right and it doesn’t work at all visually. I need to create a Bootstrap container to put the text in so I can justify it correctly.

Release Date

The Release Date returned is when that particular version of an album was released. If it’s a re-release or repressing, that date can be very different from when the album was originally released. I think I can use the master release number to go get a list of all releases and then look at the first release in that list to get the “real” release date. It doesn’t always work in some random testing I did, but it’s a start. My idea to look at the Musicbrainz API doesn’t look like it’s any better. The other challenge with this feature is working with datetime objects, which can be frustrating. Especially considering Discogs might return the release date in multiple different formats, including just the month, month and year, or date / month / year. That will be fun to standardize.

Miscellaneous

I also need to fix the data returned in Genre. It’s a list that I currently show as a list of bullets, each on its own line. It doesn’t look quite right and I need to probably update the CSS or when iterating over the list, put it on one line. I haven’t quite figured out what to do with it yet.

I also, in theory, could deploy what I have now. But I think I'll wait just a bit.

Next Steps

I’m leaning towards tackling CSS first. I've started the Responsive Web Design class on Free Code Camp, and it's good content, but the way they have you practice I don't feel as if the information is sticking in my head. I’ve also signed up for a free (to audit) five week course on Coursera to learn HTML, CSS, and Javascript. Part of me really wants to learn just a little Javascript, but most of me doesn’t - I’d rather focus on getting better at Python as I don’t feel like I’m anywhere close from transition from novice Python to intermediate Python. But if I’m going to continue to focus on using Python for web development, which is where my passion seems to lie at the moment, I need to get better at HTML and CSS. The structure of an actual class is helpful, so I’ll probably move forward with the Coursera class for the time being.

Play Singles Part 2

I was talking to my wife yesterday about my progress and she agreed that I should update my existing method used for albums to also work for singles - and that I shouldn’t just re-use that method and to avoid code duplication. It’s good to get confirmation that I’m on the right path.

But I’m stuck on how to do this.

If we look at the route to play a full album:

@view_config(route_name="play", renderer="silversaucer:templates/play/play.pt")
def play(_):

    album_release_id = RandomRecordService.get_folder_count(2162484)
    release_data = RandomRecordService.get_album_data(album_release_id)
    return {"release_info": release_data}

Here I’m manually passing the folder ID for my Discogs Folder that contains full albums (2162484). That ID is passed to a method that gets a count of how many items are in that folder to pick one at random. Once that happens, it passes the release ID of the pick to get the album’s information.

Works great for full albums. As mentioned in my last blog post, it bombs if it’s a single, because there’s no concept of a main (or “master”) release associated with EPs and singles.

So how do I refactor my code to account for this?

I started by updating the route to play a full album, adding a folder parameter to the get_folder_count method instead of passing the folder number manually:

@view_config(route_name="play", renderer="silversaucer:templates/play/play.pt")
def play(_):

    folder = 2162484
    album_release_id = RandomRecordService.get_folder_count(folder)
    release_data = RandomRecordService.get_album_data(folder, album_release_id)
    return {"release_info": release_data}

And I tell that method to now return both the release ID and the folder:

def get_folder_count(folder):

    discogs_api = folder_url + "?=" + api_token

    # TODO Add an if statement to check for a 200 or 404 response code and redirect on 404 to error page

    response = requests.get(discogs_api)

    record_json = response.json()
    return random_album_release_id, folder

Keeping with my goal of only using one method and passing the folder to it, I added an if statement to check if it’s the folder with full albums. If it is, get the data I know is working. If not, don’t get the data that’s not included in an EP or single.

@staticmethod
def get_album_data(folder, album_release_id):

    if folder == 2162484:

        release_api = (
            config.discogs_url
            + "releases/"
            + str(album_release_id)
            + "?"
            + config.discogs_user_token
        )
        print(release_api)
        response = requests.get(release_api)
        print(response)

But now my method to get the release information on an album broke, when it was working before:

  File "/Users/prcutler/workspace/silversaucer/silversaucer/controllers/music_controller.py", line 14, in play
    release_data = RandomRecordService.get_album_data(folder, album_release_id)
  File "/Users/prcutler/workspace/silversaucer/silversaucer/services/play_service.py", line 114, in get_album_data
    release_uri = release_json["uri"]
KeyError: 'uri'

Digging into it with another print statement, I see that the value for album_release_id is now returning the folder ID correctly, but `album_release_id is returning a tuple:

```Folder ID = 2162486 Album = (3200538, 2162486)

Which is why the API call below is screwed up:

``` python
 File "/Users/prcutler/workspace/silversaucer/silversaucer/services/play_service.py", line 114, in get_album_data
    release_uri = release_json["uri"]
KeyError: 'uri'
https://api.discogs.com/releases/(1366279, 2198941)?&token=

But it’s a tuple! I can work with that. The folder number is always returned as the second part of the tuple and the album_release_id is always the first index. So let’s update the release_api first to use the first index:

release_api = (
    config.discogs_url
    + "releases/"
    + str(album_release_id[0])
    + "?"
    + config.discogs_user_token
)

And now everything works! I updated the else statement to go get the data I need and not include the values that were returning a KeyError and now with one method - that includes an if / else statement, I can get back the release information for any record in my collection, no matter what folder it’s in. If and when I add other media - especially CDs or tapes that have a main release, I’ll need to update the if statement, but future me can deal with that. I don’t even have that stuff cataloged yet. (But I made sure to add a TODO to my code so I don’t forget.

And here’s a screenshot of a random 10” EP that was returned. Yay!

The Decemberists - Long LIve the King