Pixel-Perfect simulated ESPHome displays
How to quickly iterate on displays without leaving your desk
The front door display has been serving me well. However, updating the UI was a tedious process. Because every pixel counts on a tiny screen, I found myself in a frustrating loop of tweaking the config, flashing the firmware, and waiting - only to find I was still one pixel off. These iterations are particularly annoying because:
- It’s a bit far from my desk (I could move it to my desk, but then would feel pressure to finish very quickly, as the device isn’t there)
- The ESP8266 doesn’t love being remote-flashed over and over again; I’ve found myself needing to re-flash over USB, or even hard-bricking a couple.
For this reason, I’ve looked for tools to help me iterate on the design without physically flashing the device. There are quite a few!
Design and simulation tools
For sketching initial layouts, there are some great web-based tools. Lopaka is fantastic (though with some limitations about fonts in the free version, and no esphome support yet in the self-hosted version). There are similar tools like DisplayGenerator and the more powerful ESPHomeDesigner. These tools are great for playing around with what your UI might feel like, and also come with icons you can use.
These tools generally create a long lambda, like those seen in the ESPHome
display docs. While it’s great for prototyping, it’s often quite helpful to
move things over to a separate C++ file, which you can then manipulate with
purpose-build C++ editors and tools. For instance, here’s some of my current
code for the front door display:
| |
| |
Clearly, this has a lot of magic numbers that were repeatedly tweaked; that 36 was previously 39, 32, 35… every attempt taking a two-minute firmware flash. The logic for “what to show if the API hasn’t connected yet” is also something that went through a few iterations. All of these things need to “really run ESPHome”; let’s see if we can do it right on our PC though.
For general-purpose “simulate your entire ESPHome electronics setup”, ESPHome-Device-Sim is a small wrapper around Wokwi. It can simulate a wide variety of devices. However, if all you need is a display, like my case, there’s a much simpler way to do this.
Host platform
ESPHome’s Host Platform basically allows you to run an esphome as a process
on your Linux (or Mac, or WSL) computer. The UI uses SDL - there’s an SDL
Display (with touchscreen support), and you can use your
keyboard as a binary_sensor with SDL Binary Sensor. No firmware flashing,
very quick iteration.
For our case, all we need is the display; we can keep our drawing code the same, and the YAML looks like this:
| |
That looks like this:
To make things nice and maintainable, I want to have the “real” and “simulated” versions share code. I use packages with extend to make this reasonably clean and maintainable.
Full source code
| |
| |
| |
Happy hacking!