找回密码
 立即注册
查看: 246|回复: 2

[硬件DIY] 迷你OLED单色显示屏

[复制链接]

6

主题

25

回帖

364

积分

论坛DIY达人

积分
364
金钱
333
HASS币
10
发表于 前天 11:37 | 显示全部楼层 |阅读模式
本帖最后由 slychen 于 2025-5-27 11:48 编辑

淘了个几块钱的PDU表头,拆开看了下,获得5V电源一个,stm32板子一块,1.25吋小oled单色显示屏一个。

IMG_6229.PNG IMG_6235.JPG

别的没什么用,留下显示屏用esphome搞了个天气等状态屏装在抽屉前。
IMG_6234_compressed.jpeg IMG_6232_compressed.jpeg

esphome:
  name: displaytest
  friendly_name: DisplayTest

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

# 启用日志
logger:

# 启用 Home Assistant API
api:

# OTA 更新
ota:
  platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # 启用 WiFi 失败时的备用热点
  ap:
    ssid: "DisplayTest"
    password: ""

# 启用 Web 服务器
web_server:
  port: 80

# 启用 Captive Portal
captive_portal:

# I2C 配置
i2c:
  sda: GPIO2
  scl: GPIO1

# 定时任务:6 秒自动切换(仅在非手动模式下)
#interval:
#  - interval: 6s
#    then:
#      - lambda: |-
#          if (!id(manual_mode)) {  // 仅在非手动模式下执行
#            if (id(display_mode) == 0) {
#              id(display_mode) = 1;  // 时间 -> 天气
#            } else if (id(display_mode) == 1) {
#              id(display_mode) = 0;  // 天气 -> 时间
#            }
#            id(my_display).update();  // 立即刷新屏幕
#          }
# 按键:手动切换屏幕,并暂停自动轮换 10 秒
binary_sensor:
  - platform: gpio
    pin: GPIO4
    id: display_toggle_button
    on_press:
      - lambda: |-
          id(manual_mode) = true;  // 进入手动模式
          id(display_mode) = (id(display_mode) + 1) % 3;  // 0 → 1 → 2 → 3 → 0
          id(my_display).update();  // 立即刷新屏幕
#      - delay: 10s  # 10 秒后恢复自动轮换
#      - lambda: |-
#          id(manual_mode) = false;  // 退出手动模式

# OLED 显示屏配置
display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    update_interval: 30s  # 刷新频率
    id: my_display
    lambda: |-
      it.fill(Color(0, 0, 0));  // 清空屏幕
      
      if (id(display_mode) == 0) {
        // 屏幕 0:时间(大字体)+ 日期 + 星期
        it.printf(0, 0, id(font1), "Indoor: %.1f°C", id(mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_temperature).state);    
        it.printf(85, 0, id(font1), "%.1f%%", id(mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_humidity).state);            
        it.strftime(0, 13, id(font1), "%Y-%m-%d", id(esptime).now());   // 日期
        it.printf(85, 13, id(font1), "%s", id(weather_entity).state.c_str());        
        it.strftime(0, 26, id(font1), "%A", id(esptime).now());         // 星期
        it.printf(85, 26, id(font1), "%.1f%%", id(humidity_sensor).state);
        it.strftime(0, 40, id(font2), "%H:%M:%S", id(esptime).now());   // 时间(大字体)
        it.printf(85, 40, id(font3), "%.1f°C", id(temperature_sensor).state);
      } 
      else if (id(display_mode) == 1) {
        // 屏幕 1:设备信息
        it.printf(0, 0, id(font1), "IP: %s", id(id_sensor_ip).state.c_str());
        it.printf(0, 16, id(font1), "MAC: %s", id(wifi_mac_address).state.c_str());
        it.printf(0, 32, id(font1), "Uptime: %.1f s", id(uptime_s).state);
        it.printf(0, 48, id(font1), "WiFi RSSI: %.1f dBm", id(wifi_signal_db).state);
      } 
      else {
        // 屏幕 2:电压、电流、功率、电量
        it.printf(0, 0, id(font1), "Voltage: %.2f V", id(sensor_voltage).state);
        it.printf(0, 16, id(font1), "Current: %.2f A", id(sensor_current).state);
        it.printf(0, 32, id(font1), "Power: %.2f W", id(sensor_power).state);
        it.printf(0, 48, id(font1), "Energy: %.2f kWh", id(sensor_energy).state);
      }
      
#      else {
#        // 屏幕 3:室内温湿度
#        it.printf(0, 0, id(font1), "Indoor Temp: %.1f°C", id(mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_temperature).state);
#        it.printf(0, 16, id(font1), "Indoor Humidity: %.1f%%", id(mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_humidity).state);
#      }

# 字体配置
font:
  - file: "arial.ttf"
    id: font1
    size: 12
  - file: "arial.ttf"
    id: font3
    size: 14    

  - file: "arial.ttf"
    id: font2
    size: 20  # 时间使用较大字体
time:
  - platform: sntp
    id: esptime
    timezone: Asia/Shanghai  # 你的时区
# 传感器配置
sensor:
  - platform: homeassistant
    id: temperature_sensor
    entity_id: sensor.shou_ye_temperature  # 温度传感器
    internal: true

  - platform: homeassistant
    id: humidity_sensor
    entity_id: sensor.shou_ye_humidity  # 湿度传感器
    internal: true

  - platform: homeassistant
    id: uv_sensor
    entity_id: sensor.shou_ye_uv_index  # UV 传感器
    internal: true

  - platform: wifi_signal
    name: "WiFi Signal"
    id: wifi_signal_db
    update_interval: 30s
    internal: true
    entity_category: "diagnostic"

  - platform: copy
    source_id: wifi_signal_db
    name: "WiFi Signal"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"

  - platform: uptime
    id: uptime_s
    update_interval: 30s
    internal: true    
  - platform: homeassistant
    id: mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_temperature
    entity_id: sensor.mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_temperature
    internal: true

  - platform: homeassistant
    id: mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_humidity
    entity_id: sensor.mi_2wu_ping_wen_du_ji_mi_2wu_ping_wen_du_ji_humidity
    internal: true
  - platform: homeassistant
    id: sensor_voltage
    entity_id: sensor.voltage
    internal: true

  - platform: homeassistant
    id: sensor_current
    entity_id: sensor.current
    internal: true

  - platform: homeassistant
    id: sensor_power
    entity_id: sensor.power
    internal: true

  - platform: homeassistant
    id: sensor_energy
    entity_id: sensor.energy
    internal: true

# 天气信息
text_sensor:
  - platform: homeassistant
    id: weather_entity
    entity_id: weather.shou_ye  # 天气实体
    internal: true

  - platform: wifi_info
    ip_address:
      name: "IP"
      id: id_sensor_ip
    mac_address:
      name: "MAC Address"
      id: wifi_mac_address

# 显示模式全局变量
globals:
  - id: display_mode
    type: int
    initial_value: "0"  # 0 = 时间,1 = 天气,2 = 设备,3 = 室内传感器
  - id: manual_mode
    type: bool
    initial_value: "false"  # 是否处于手动模式


# 状态灯
status_led:
  pin:
    number: GPIO3
    inverted: true
定时切换可选,传感器id选HA里合适的传感器,我用的天气预报是AccuWeather,
中文字体懒得整了;SDA和SDL及按键LED等针脚在PCB上有丝印,esp32或8266的IO脚根据自己需求更改,


评分

参与人数 1金钱 +16 收起 理由
hhh. + 16 论坛有你更精彩!

查看全部评分

回复

使用道具 举报

14

主题

192

回帖

1432

积分

论坛技术达人

积分
1432
金钱
1226
HASS币
0
发表于 前天 14:16 | 显示全部楼层
按钮能用吗🤔️ 有wifi吧 我想改造一个 热水器调温按钮的说
回复

使用道具 举报

14

主题

192

回帖

1432

积分

论坛技术达人

积分
1432
金钱
1226
HASS币
0
发表于 前天 14:40 | 显示全部楼层
啊快递费6块钱🤔️
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-5-29 13:24 , Processed in 0.202680 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表