I used ESPHome and Home Assistant and an open-source Python API to build a way to scan my vinyl albums’ barcodes and have them play using spotify over my home’s Chromecasts.

ESPHome Hardware

This project started with an idea: I wonder if I can make esphome read barcodes. Turns out, the answer is an emphatic yes. There are a few projects out there already, but they had one drawback - the UART interface barcode scanners they were using meant that you had to bring the item up to the scanner to scan. I wanted a handheld wireless scanner.

There are lots of handheld barcode scanners for sale, I bought an inexpensive one from Aliexpress for around $15. It operates as a USB HID Device - a USB Keyboard. When its USB dongle is plugged into a computer, scanning a barcode will type it into a text field. This particular scanner has a USB recharable battery and a speaker/led to indicate scanning success on the handheld unit.

Wireless Barcode Scanner

My first attempt to make that readable by an ESP was using a CH9350 HID module. This module takes a USB device in and then outputs HID codes over UART. I was able to get this working with an ESP32, but ran into difficulty trying to convert the HID codes into ASCII characters. Jann created an example library that I tried to adapt, but I wasn’t able to get it working inside of ESPHome. The CH9350 has some interesting capabilities, I hope I can get it working with ESPHome in the future and share a component for others to use.

Thankfully for this project, Hobbytronics had me covered with their USB Host Controller Board. For £18, I had to write no code at all. Their pre-loaded firmware did exactly what I want - output the ASCII characters that my HID barcode scanner was “typing”. I connected this to an ESP32’s UART pins and stuck both of them in a small project box with plenty of hot glue to hold everything in place.

Completed ESP Barcode Scanner

The ESPHome device config is intentionally bare bones. A text sensor is setup using a component that listens for a string coming in via serial. After 10s, the text sensor is reset to “blank” to allow retriggering of the same album. The rest of the config is just ESPHome boilerplate.

Expand ESPHome Device Config

Barcode Lookup API

With a way to scan barcodes, I needed a way to look them up and get a Spotify ID. I use Discogs to organize my vinyl collection, and it offers an API. Combining that with the Spotify API, I had access to the data I needed.

After researching options to run python scripts in Home Assistant, I decided it would be worth the extra effort to run this code as an external service and then query it in Home Assistant with a RESTful Command. I have some other plans for the API in the future and it gave me a good excuse to play with some FastAPI skills.

andrewthetechie/home-barcode-api - GitHub

The Discogs and Spotify APIs have fair limits for APIs, but to try to limit the amount of outbound calls, the Barcode API uses a DB as a cache. I deployed the API into my homelab k8s cluster and mounted a 500mb PVC to the pod for SQLite. If you’d like to deploy it for yourself, there is a Docker image and it takes its config via env vars. Maybe I’ll even write a proper README someday

I had to do some very basic data cleaning because the Discogs API returns odd results for artists. For example, Tool is returned as “Tool (2)”, which the Spotify API doesn’t like. I suspect there will be more bugs as I use this system with more than the 20 albums I grabbed out of the my collection.

Home Assistant

After adding my barcode scanner to Home Assistant via the ESPHome integration, I needed an automation to trigger when a barcode was scanned.

Expand HomeAssistant Automation

The automation looks for the text sensor’s state to change and then submits that to the API via a RESTful command. If the API returns a 200, it uses TTS to speak the artist and album name and then starts playing using Spotcast. If the automation gets a 404, it uses TTS to read back the error message (which usually results in a cheerful robot reading out a 12 digit number UPC-A.)

Parts list and final costs

Total ~$46

What’s Next?

This project was a proof of concept to reliably reading barcodes with ESPHome. In the future, I want to setup a scanner in my kitchen for use with a grocery ERP to track our groceries and household goods.

The music player is really a toy. It’ll be a neat thing to show off to visitors that gets used occasionally. My total cost wasn’t much for a fun demo and for learning more about ESPhome, Barcodes, HID devices, and Home assistant. I’ll eventually replace the turntable.