EspHome et écran 3.5 3.5" TFT SPI 480*320 V2.0

Bonsoir,
Si j’ai pu faire fonctionner l’écran ili9488 avec l’ESP32 TTGO T8 V1.1 ESP32 4 Mo PSRAM sans le tactile pour l’instant mais j’ai des pistes, je me suis pour l’instant concentré sur le ili9341.
Le branchement que je propose est identique pour les deux écrans, pour un ESP32 30 pin et le TTGO T8 V1.1 ESP32 4 Mo PSRAM.
Même si mon projet n’est pas terminé, je partage le branchement et le code yaml ESPHOME.
Pas top présentation mais mais voici le branchement, suivre de 1 à 14 vers les GPIO :

Pour un ili9341 avec un ESP32 VROOM-32 30 pin suivre aussi les GPIO.
Dans le principe, 3 pages avec des flèches en bas pour changer de page, style à améliorer :wink:
La page 1 : Actions tactiles pour allumer des lumières et changement de couleur du texte suivant On / Off
ON


OFF

Ecran 2 : Informations températures…

Ecran 3 : Autre mise en page…

Une approche fonctionnelle :wink:

Inspiration :

Pour le code ESPHOME :

esphome:
  name: esp32displaytouch
  friendly_name: Esp32 9341 touch

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "UD6gWnftSqqILNKrKkjhgf4+oyu0epgLWf3c8sw+4KY="

ota:
  password: "8ce94a8e33f9d39fm235k51d3f7278e1"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.28
    gateway: 192.168.1.254
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32Displaytouch"
    password: "HBEUMlpqiHVD"

captive_portal:

psram:
  mode: octal
  speed: 80MHz
  
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23
  miso_pin: GPIO19
    
sensor:
  - platform: homeassistant
    id: current_temperature_salon
    entity_id: sensor.psalon_temperature
  - platform: homeassistant
    id: current_temperature_ext
    entity_id: sensor.temperature_ext
  - platform: homeassistant
    id: current_temperature_chambre
    entity_id: sensor.temperature_23_chambre
  - platform: homeassistant
    id: current_temperature_garage
    entity_id: sensor.temperature_garage
  - platform: homeassistant
    id: target_temperature_sejour
    entity_id: climate.sejour
    attribute: temperature

# Example configuration entry
time:
  - platform: homeassistant
    id: homeassistant_time

text_sensor:
  - platform: homeassistant
    id: current_saint_ave_next_rain
    entity_id: sensor.saint_ave_next_rain
    attribute: 1_hour_forecast
  - platform: homeassistant
    id: preset_mode_sejour
    entity_id: climate.sejour
    attribute: preset_mode
  - platform: homeassistant
    id: presence_bob
    entity_id: device_tracker.iphone_x2a

binary_sensor: 
  - platform: homeassistant
    id: lampe_marine
    entity_id: light.lampe_marine
    internal: true
  - platform: homeassistant
    id: lampe_cuisine
    entity_id: light.cuisine
    internal: true
  - platform: homeassistant
    id: lampe_plante
    entity_id: light.salon_plante_10
    internal: true

  - platform: touchscreen
    id: touch_key0
    x_min: 6
    x_max: 70
    y_min: 6
    y_max: 70
    #on_press:
    #- logger.log: "Key0 was touched"
    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key0
        then: 
        - homeassistant.service:
            service: light.toggle
            data: 
              entity_id: light.lampe_marine

  - platform: touchscreen
    id: touch_key3
    x_min: 6
    x_max: 70
    y_min: 82
    y_max: 152

    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key3
        then: 
        - homeassistant.service:
            service: light.toggle
            data: 
              entity_id: light.cuisine

  - platform: touchscreen
    id: touch_key6
    x_min: 6
    x_max: 70
    y_min: 162
    y_max: 232

    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key6
        then: 
        - homeassistant.service:
            service: light.toggle
            data: 
              entity_id: light.salon_plante_10

  - platform: touchscreen
    id: touch_key9
    x_min: 6
    x_max: 70
    y_min: 252
    y_max: 296
    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key9
        then: 
        - display.page.show_previous: my_display 

  - platform: touchscreen
    id: touch_key11
    x_min: 185
    x_max: 252
    y_min: 252
    y_max: 296
    on_state: 
      if: 
        condition: 
          binary_sensor.is_on: touch_key11
        then: 
        - display.page.show_next: my_display  

font:
  - file: "fonts/arial-1.ttf"
    id: my_font
    size: 16
    glyphs: "<>♡ÆØÅæøå!'%(){}+,-_.:;*=°?~#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdeéfghijklmnopqrstuvwxyzôç"
  
  - file: "fonts/arial-1.ttf"
    id: my_font12
    size: 12
    glyphs: "<>♡ÆØÅæøå!'%(){}+,-_.:;*=°?~#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdeéfghijklmnopqrstuvwxyzôç"

  - file: "fonts/arial-1.ttf"
    id: my_font20
    size: 20
    glyphs: "<>♡ÆØÅæøå!'%(){}+,-_.:;*=°?~#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdeéfghijklmnopqrstuvwxyzôç"

  - file: "fonts/arial-1.ttf"
    id: my_font18
    size: 18
    glyphs: "<>♡ÆØÅæøå!'%(){}+,-_.:;*=°?~#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdeéfghijklmnopqrstuvwxyzôç"

  - file: "fonts/comic-sans-ms.ttf"
    id: my_font2
    size: 18
    glyphs: "<>♡ÆØÅæøå!'%(){}+,-_.:;*=°?~#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdeéfghijklmnopqrstuvwxyzôç"


# Define a PWM output on the ESP32
output:
  - platform: ledc
    pin: 32
    id: gpio_32_backlight_pwm

# Define a monochromatic, dimmable light for the backlight
light:
  - platform: monochromatic
    output: gpio_32_backlight_pwm
    name: "Display Backlight"
    id: back_light
    restore_mode: ALWAYS_ON

switch:
  - platform: gpio
    name: "LedBleu2"
    pin: 2
    
display:
  - platform: ili9xxx
    #model: ili9488
    model: ili9341
    #cs_pin: GPIO22
    #dc_pin: GPIO21
    #reset_pin: GPIO1
    cs_pin: GPIO15
    dc_pin: GPIO05
    reset_pin: GPIO4
    rotation: 180
    #auto_clear_enabled: false
    update_interval: 1s
    id: my_display

    pages:

      - id: page1
        lambda: |-
          it.rectangle(0,  0, it.get_width(), it.get_height());
          auto red = Color(255, 0, 0);
          auto white = Color(255, 255, 255);
          auto yellow = Color(255, 255, 0);
          it.line( 80,   0,  80, 319);
          it.line(160,   0, 160, 319);
          it.line(  0,  80, 239,  80);
          it.line(  0, 160, 239, 160);
          it.line(  0, 240, 239, 240);

          it.rectangle(4, 4, 72, 72, yellow);

          if (id(lampe_marine).state) {
             it.print(8, 24, id(my_font20), my_green, "Light1");
          } else{
            it.print(8, 24, id(my_font20), my_red, "Light1");
          }

          it.rectangle(4, 84, 72, 72, yellow);

          if (id(lampe_cuisine).state) {
             it.print(8, 110, id(my_font20), my_green, "Light2");
          } else{
            it.print(8, 110, id(my_font20), my_red, "Light2");
          }

          it.rectangle(4, 164, 72, 72, yellow);
          if (id(lampe_plante).state) {
             it.print(8, 186, id(my_font20), my_green, "Light3");
          } else{
            it.print(8, 186, id(my_font20), my_red, "Light3");
          }
          
          it.rectangle(4, 244, 72, 72, white);
          it.print(20, 270, id(my_font20), "<<<");
          it.rectangle(164, 244, 72, 72, white);
          it.print(180, 270, id(my_font20), ">>>");

#if (id(touchid).touched)
#it.print(165, 70, id(my_font20), "Touche :");
#it.print(12, 24, id(my_font20), "Light1");
#it.print(12, 100, id(my_font20), "Light2");

      - id: page2
        lambda: |-
          auto white = Color(255, 255, 255);
          it.rectangle(0,  0, it.get_width(), it.get_height());
          it.print(5, 3, id(my_font18), my_red, TextAlign::LEFT, "By Bob : ");
          it.strftime(118, 9, id(my_font12), "%d-%m-%Y %H:%M:%S", id(homeassistant_time).now());

          it.print(5, 30, id(my_font18), "Temp salon :");
          it.printf(146, 30, id(my_font18), my_yellow, "%.1f °C", id(current_temperature_salon).state);
          it.print(5, 56, id(my_font18), "Temp Ext :");
          it.printf(146, 56, id(my_font18), my_yellow, "%.1f °C", id(current_temperature_ext).state);
          it.print(5, 82, id(my_font18), "Temp chambre :");
          it.printf(146, 82, id(my_font18), my_yellow, "%.1f °C", id(current_temperature_chambre).state);
          it.print(5, 108, id(my_font18), "Temp garage :");
          it.printf(146, 108, id(my_font18), my_yellow, "%.1f °C", id(current_temperature_garage).state);
          it.print(5, 134, id(my_font18), "Consigne :");
          it.printf(146, 134, id(my_font18), my_yellow, "%.1f °C", id(target_temperature_sejour).state);
          it.print(5, 160, id(my_font18), "Preset :");
          it.printf(146, 160, id(my_font18), my_yellow, "%s",  id(preset_mode_sejour).state.c_str());
          it.print(5, 186, id(my_font18), "Présence Bob :");
          it.printf(146, 192, id(my_font12), my_yellow, "%s",  id(presence_bob).state.c_str());
          it.rectangle(4, 246, 70, 70, white);
          it.print(20, 270, id(my_font20), "<<<");
          it.rectangle(165, 246, 70, 70, white);
          it.print(180, 270, id(my_font20), ">>>");

      - id: page3
        lambda: |-
          auto red = Color(255, 0, 0);
          auto green = Color(0, 255, 0);
          auto blue = Color(0, 0, 255);
          auto white = Color(255, 255, 255);
          // Draw a line from [0,0] to [100,50]
          //it.line(0, 0, 100, 50);
          // Draw the outline of a rectangle with the top left at [50,60], a width of 30 and a height of 42
          it.rectangle(0, 0, 119, 109, white);
          it.rectangle(0, 111, 119, 109, red);
          it.rectangle(121, 0, 119, 109, green);
          it.rectangle(121, 111, 119, 109, blue);
          // Draw the same rectangle, but this time filled.
          //it.filled_rectangle(150, 160, 130, 142);

          // Circles! Let's draw one with the center at [25,25] and a radius of 10
          //it.circle(25, 25, 10);
          // ... and the same thing filled again
          //it.filled_circle(25, 25, 10);
          it.print(5, 7, id(my_font18), "Présence :");
          it.printf(5, 37, id(my_font12), my_yellow, "%s",  id(presence_bob).state.c_str());
          it.printf(5, 124, id(my_font18), "Temp salon :");
          it.printf(5, 152, id(my_font18), "%.1f °C", id(current_temperature_salon).state);
          it.printf(126, 7, id(my_font18), "Temp Ext :");
          it.printf(126, 37, id(my_font18), "%.1f °C", id(current_temperature_ext).state);
          it.printf(126, 124, id(my_font18), "Temp garage :");
          it.printf(126, 152, id(my_font18), "%.1f °C", id(current_temperature_garage).state);
          it.rectangle(4, 246, 70, 70, white);
          it.print(20, 270, id(my_font20), "<<<");
          it.rectangle(165, 246, 70, 70, white);
          it.print(180, 270, id(my_font20), ">>>");


#interval:
#  - interval: 5s
#    then:
#      - display.page.show_next: my_display
#      - component.update: my_display

#if (id(touchid).touched)
#it.print(165, 70, id(my_font20), "Touche :");          
#it.filled_circle(id(touchid).x, id(touchid).y, 30, my_red);

touchscreen:
  platform: xpt2046
  ## Do not give this an `id` of `touch` as that appears to clash with something
  ## internal to the touchscreen component.
  id: touchid
  cs_pin: 14
  interrupt_pin: 27
  # ResinChem Tech found using interrupt_pin to be unreliable. While
  # I didn't have these issues, I find it works fine without it.
  #interrupt_pin: 21
  swap_x_y: false
  #calibration_x_min: 319
  #calibration_x_max: 3691
  #calibration_y_min: 3873
  #calibration_y_max: 453
  calibration_x_min: 3860
  calibration_x_max: 280
  calibration_y_min: 340
  calibration_y_max: 3860
  
  threshold: 400
  update_interval: 50ms
  report_interval: 200ms
  on_touch:
    - lambda: |-
          ESP_LOGI("cal", "x=%d, y=%d, x_raw=%d, y_raw=%0d",
              id(touchid).x,
              id(touchid).y,
              id(touchid).x_raw,
              id(touchid).y_raw
              );


color:
  - id: my_red
    red: 100%
    green: 0%
    blue: 0%

  - id: my_green
    red: 0%
    green: 100%
    blue: 0%

  - id: my_blue
    red: 0%
    green: 0%
    blue: 100%

  - id: my_yellow
    red: 100%
    green: 100%
    blue: 0%

  - id: my_white
    red: 100%
    green: 100%
    blue: 100%

# Example configuration entry
web_server:
  port: 80