How do you use SDL2?

I have been using ESPHome, loving it.
So far, I have been compiling, flashing, debugging on my HA Green box, which is rather slow. I have installed esphome on a MacBook and it is a big improvement.

Next I found “SDL2 Display on host platform” and thought it looked interesting.
Correct me if I am wrong but this seems like a way to debug display code so it runs locally on the host??

The problem I have now is the documentation is extremely sparse and I am unable to get it to work.

I have a barebones config generated by the wizard. I have added host and display as shown in the sample.

It is not clear in the docs how to actually run the configuration.
I tried this: esphome compile testing.yaml and get errors.

For instance: Found multiple target platform blocks: esp32, host. Only one is allowed.
If I comment out the esp32 block, I get: This feature is only available on [‘esp32’, ‘esp8266’, ‘bk72xx’, ‘rtl87xx’].

I am unsure how to proceed, does anyone have a simple sample configuration and instructions for running it?

I think you need to read Host Platform — ESPHome

And supply your yaml. No yaml, no issue.

Run this with esphome run host.yaml

esphome:
  name: sdl2
  friendly_name: SDL2 Test
  project:
    name: esphome.host
    version: "1.1"

host:

logger:
  level: DEBUG

display:
  - platform: sdl
    show_test_card: true
    dimensions:
      width: 450
      height: 600

Many thanks for your response.
Once I had commented out my sections for esp32, api, ota, wifi and captive portal, plus installed pillow, then it worked.

api is fine, but ota, wifi and captive portal are not implemented on the host platform, and esp32 is itself a platform - you can only choose one from host, esp32, esp8266 etc.

Thanks for the clarification.

I get loads of errors attempting to display an image, under the host environment with SDL2.

Eg.

image:

  • file: mdi:home-lightning-bolt-outline
    id: home_row
    resize: 60x60

Should auto download like this work or will I need to keep image files referenced locally?

SDL is really designed to work with the new LVGL stuff. Try that!

Then you’re doing something wrong, but nobody can help you without more information. If you provide:

  • A complete, minimal and self-contained YAML demonstrating the problem;
  • A copy of the output with the error messages;
  • A description of your environment (i.e. Windows/MacOS/Linux, how ESPHome is installed etc.)

Then you will probably get a useful response quite quickly.

Thanks for your advice.
Instead of using mdi: images, I switched to the font version, which also made it easier to select the correct symbol programatically.

Meanwhile, I am attempting to have a yaml config running in SDL2, use a sensor exported by HomeAssistant.

I have installed esphome etc. (including brew install sdl2 libsodium) on MacOS.

Here is my YAML, incidentally, the sensor is valid :

esphome:
  name: testing
  friendly_name: Testing 
  includes:
    - battery.h # provides the const char * batterySymbol(int soc) function
  project:
    name: esphome.host
    version: "0"

logger:

api:
  encryption:
    key: "FCFOW5mzObf7RRtvey6g2cXB61id4z0y8btYwkFp7W0="

sensor:
  - platform: homeassistant 
    entity_id: sensor.ooctcl60bp_statement_of_charge
    id: battery_soc 

font: 
  - file: "gfonts://Roboto"
    id: roboto20
    size: 20

  - file: "fonts/materialdesignicons.ttf"
    id: symbols 
    size: 60 
    glyphs: [
        "\U000F008E", # battery0 
        "\U000F007A", # battery10 
        "\U000F007B", # battery20 
        "\U000F007C", # battery30 
        "\U000F007D", # battery40 
        "\U000F007E", # battery50 
        "\U000F007F", # battery60 
        "\U000F0080", # battery70 
        "\U000F0081", # battery80 
        "\U000F0082", # battery90 
        "\U000F0079", # battery100 
        "\U000F1904", # home
        "\U000F0599", # sunny
        "\U000F0D3E", # transmission
    ] 
   
host:
    
display:
  - platform: sdl
    id: sdl_display
    update_interval: 1s
    auto_clear_enabled: false
    show_test_card: false
    dimensions:
      width: 300
      height: 400
    lambda: |-
        int soc = int(id(battery_soc).state);
        it.print(100, 100, id(symbols), TextAlign::BOTTOM_CENTER, batterySymbol(soc));
        it.printf(100, 100, id(roboto20), TextAlign::TOP_CENTER, "%d%%", soc);

When I attempt to run this, I get errors with libsodium which I had already installed.

ESPHome esphome run testing.yaml
INFO ESPHome 2024.6.6
INFO Reading configuration testing.yaml...
/Users/quinn/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
INFO Generating C++ source...
INFO Compiling app...
Processing testing (platform: platformio/native)
-----------------------------------------------------------------------------------------------------------------------------------------------------
/Users/quinn/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
Dependency Graph
|-- noise-c @ 0.1.4
Compiling .pioenvs/testing/src/main.o
Linking .pioenvs/testing/program
Undefined symbols for architecture arm64:
  "__crypto_generichash_blake2b_pick_best_implementation", referenced from:
      _sodium_init in libsodium.a[20](core.o)
  "__crypto_pwhash_argon2_pick_best_implementation", referenced from:
      _sodium_init in libsodium.a[20](core.o)
  "__crypto_stream_salsa20_pick_best_implementation", referenced from:
      _sodium_init in libsodium.a[20](core.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pioenvs/testing/program] Error 1

Did I get the wrong libsodium package installed?
Should the SDL2 host environment be able to access sensors in Home Assistant?

Thanks for any suggestions.

Yes, the host platform works with HA, but see the note in the docs about discovery.

I can reproduce the link-time error on M1 Mac - for now the only work-around I have is not using encryption for the api.

The problem appears to be that there are two libsodium.a files - one in the build tree, so the correct one does not get picked up. This is not an issue on Linux :frowning: so it seems to be an Apple clang quirk.

Many thanks.
Trying this now, unsure how to turn off API encryption.

Just remove the encryption key from the config!

The problem is fixed with this PR: Fix libsodium dependency on MacOS; add check for installed library. by clydebarrow · Pull Request #7105 · esphome/esphome · GitHub

1 Like

Hmmm, that’s what I though I’d done …
I tried these;

api:
#   encryption:
#     key: "FCFOW5mzObf7RRtvey6g2cXB61id4z0y8btYwkFp7W0="

And


api:
   encryption:
#     key: "FCFOW5mzObf7RRtvey6g2cXB61id4z0y8btYwkFp7W0="

Both give an error “expected a dictionary”

The second form will throw an error, the first (with both lines commented) is fine.

Yes, you were right, thank you.

The problem now is that the value from the sensor is no longer sent or updated. A sensor that worked fine in a normal display situation.

When I used this sensor before, I simply put update_interval: 60s in the display component, it was clunky but worked. This fails with the sdl platform.
I also tried updating from the sensor, this fails as well.

sensor:
  - platform: homeassistant 
    entity_id: sensor.ooctcl60bp_statement_of_charge
    id: battery_soc 
    on_value:
        then:
            - component.update: sdl_display

I think your yaml is incorrect. As I understand it indentation must be consistent. You start with two spaces, you then move to four.

Well, that has nothing to do with SDL. Do you see sensor updates in the log? Is it connected to HA? Did you turn on “Use ping for status” in the ESPHome dashboard?

I think that is just a copy+paste issue :crossed_fingers:

I assumed the problem was using this with SDL as this sensor is working correctly in another YAML which uses an epaper display.

I am not seeing the usual repeated updates in the log but I do see the initial connection:

[16:34:01][C][api:139]: API Server:
[16:34:01][C][api:140]:   Address: :6053
[16:34:01][C][api:144]:   Using noise encryption: NO
[16:34:01][C][homeassistant.sensor:030]: Homeassistant Sensor 'battery_soc'
[16:34:01][C][homeassistant.sensor:030]:   State Class: ''
[16:34:01][C][homeassistant.sensor:030]:   Unit of Measurement: ''
[16:34:01][C][homeassistant.sensor:030]:   Accuracy Decimals: 1
[16:34:01][C][homeassistant.sensor:031]:   Entity ID: 'sensor.ooctcl60bp_statement_of_charge'

I’d better look into what “Use ping for status” is.

Thanks again