I was having a bit of trouble with this display, your post helped.
In saying that using arduino framework it would crash if I tried to add the ble_device.
I could do one or the other, but both caused out of ram issues and made the wifi disconnect.
Sooo
I switched to the esp-idf framework. That would require hitting the “clean build files” occasionally as it kept giving me vtables errors and other strange issues.
So here is the example esphome file for the TTGO display with BLE_YC01 pool monitor incorporated. I haven’t decided what I will do with the buttons as yet… Perhaps turn on the pool lights and/or pool pump. I will possibly use the buttons for a second page for the other BLE sensors.
range is not spectacular… seems to be about 3-4 meters through a window.
esphome:
name: ttgo
esp32:
board: esp32dev
framework:
type: esp-idf
version: recommended
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Ttgo Fallback Hotspot"
password: !secret fb_password
#captive_portal: ### can't use this in esp-idf
# Enable logging.
logger:
level: DEBUG
api:
# Enable over-the-air updates.
ota:
#Configuration entry
color:
- id: my_red
red: 100%
green: 0%
blue: 0%
- id: my_yellow
red: 100%
green: 100%
blue: 0%
- id: my_green
red: 0%
green: 100%
blue: 0%
- id: my_blue
red: 0%
green: 0%
blue: 100%
- id: my_gray
red: 50%
green: 50%
blue: 50%
- id: my_white
red: 100%
green: 100%
blue: 100%
- id: my_orange
red: 100%
green: 60%
blue: 0%
- id: my_teal
red: 0%
green: 50%
blue: 45%
# put a ttf font called InterFont.ttf in esphome/fonts/
font:
- file: "fonts/InterFont.ttf"
id: helvetica_48
size: 40
- file: "fonts/InterFont.ttf"
id: helvetica_24
size: 20
- file: "fonts/InterFont.ttf"
id: helvetica_S
size: 16
######################################################
## ##
## To initiate to connection with the BLE device ##
## ##
######################################################
esp32_ble_tracker: #### Stop the active scan ####
scan_parameters:
active: false
ble_client:
- mac_address: C0:00:00:00:9F:3B #Use the MAC address of your BLE device
id: ble_yc01
on_connect: #### Actions to perform when connecting to the BLE device ####
then:
#### Wait until characteristic is discovered ####
- wait_until:
lambda: |-
esphome::ble_client::BLEClient* client = id(ble_yc01);
auto service_uuid = 0xFF01;
auto char_uuid = 0xFF02;
//#### When waiting for connection, we extract the available characteristics ####
esphome::ble_client::BLECharacteristic* chr = client->get_characteristic(service_uuid, char_uuid);
return chr != nullptr;
#### Official connection to the BLE device with the desired characteristic ####
- lambda: |-
ESP_LOGD("ble_client_lambda", "Connected to BLE-YC01");
esphome::ble_client::BLEClient* client = id(ble_yc01);
auto service_uuid = 0xFF01;
auto char_uuid = 0xFF02;
esphome::ble_client::BLECharacteristic* chr = client->get_characteristic(service_uuid, char_uuid);
if (chr == nullptr) {
ESP_LOGW("ble_client", "[0xFF01] Characteristic not found. State update can not be written.");
}
on_disconnect:
then:
- lambda: |-
ESP_LOGD("ble_client", "Disconnected from BLE-YC01");
binary_sensor:
- platform: status
name: "Node Status"
id: system_status
- platform: gpio
entity_category: config
pin:
number: GPIO0
inverted: true
mode:
input: true
pullup: true
name: ttgo button_0 push
id: tdisplay_button_input_0
- platform: gpio
entity_category: config
pin:
number: GPIO35
inverted: true
mode:
input: true
id: tdisplay_button_input_1
name: ttgo button_1 push
switch:
- platform: restart
name: "TTGO restart"
id: ttgo_restart
- platform: gpio
pin: GPIO4
name: " TTGO-T-Display Backlight"
id: backlight
- platform: ble_client #### To switch on and off the communication with the BLE device ####
id: ble_switch
ble_client_id: ble_yc01
name: "Enable BLE-YC01"
# replace my entity IDs with your entity IDs below
sensor:
- platform: homeassistant
id: pool_roof_temp
entity_id: sensor.pool_roof_temp
internal: true
- platform: homeassistant
id: pool_tas_temp
entity_id: sensor.tasmota_sn_th16_ds18b20_temperature
internal: true
- platform: wifi_signal
name: "ttgo wifi signal"
update_interval: 600s
- platform: template
id: int_temp
unit_of_measurement: "C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
#lambda: return temperatureRead();
- platform: template
name: "BLE-YC01 EC"
id: ble_yc01_ec_sensor
unit_of_measurement: "µS/cm"
accuracy_decimals: 0
state_class: measurement
icon: mdi:water-opacity
- platform: template
name: "BLE-YC01 TDS"
id: ble_yc01_tds_sensor
unit_of_measurement: "ppm"
accuracy_decimals: 0
state_class: measurement
icon: mdi:water-opacity
- platform: template
name: "BLE-YC01 Temperature"
id: ble_yc01_temperature_sensor
unit_of_measurement: "C"
accuracy_decimals: 1
state_class: measurement
device_class: temperature
- platform: template
name: "BLE-YC01 ORP"
id: ble_yc01_orp_sensor
unit_of_measurement: "mV"
accuracy_decimals: 0
state_class: measurement
device_class: voltage
- platform: template
name: "BLE-YC01 pH"
id: ble_yc01_ph_sensor
unit_of_measurement: "pH"
accuracy_decimals: 2
state_class: measurement
icon: mdi:ph
- platform: template
name: "BLE-YC01 battery"
id: ble_yc01_battery
unit_of_measurement: "%"
accuracy_decimals: 0
state_class: measurement
device_class: battery
icon: mdi:battery
- platform: template
name: "BLE-YC01 CL"
id: ble_yc01_cloro
unit_of_measurement: "ppm"
accuracy_decimals: 0
state_class: measurement
icon: mdi:water-opacity
- platform: ble_client #### Sensor required to manage values coming from the BLE device ####
ble_client_id: ble_yc01
id: ble_yc01_sensor
internal: true
service_uuid: FF01
characteristic_uuid: FF02
notify: true
#### Lambda to decode values and push to the associated sensors ####
lambda: |-
if (x.size() == 0) return NAN;
//ESP_LOGD("ble_client.receive", "value received with %d bytes: [%.*s]", x.size(), x.size(), &x[0]); // #### Useful for debugging ####
// ### DECODING ###
uint8_t tmp = 0;
uint8_t hibit = 0;
uint8_t lobit = 0;
uint8_t hibit1 = 0;
uint8_t lobit1 = 0;
auto message = x;
for (int i = x.size() -1 ; i > 0; i--) {
tmp=message[i];
hibit1=(tmp&0x55)<<1;
lobit1=(tmp&0xAA)>>1;
tmp=message[i-1];
hibit=(tmp&0x55)<<1;
lobit=(tmp&0xAA)>>1;
message[i]=~(hibit1|lobit);
message[i-1]=~(hibit|lobit1);
}
//ESP_LOGD("ble_client.receive", "value received with %d bytes: [%.*s]", message.size(), message.size(), &message[0]); // #### For debug ####
// #### Extraction of individual values ####
auto temp = ((message[13]<<8) + message[14]);
auto ph = ((message[3]<<8) + message[4]);
auto orp = ((message[20]<<8) + message[21]);
auto battery = ((message[15]<<8) + message[16]);
auto ec = ((message[5]<<8) + message[6]);
auto tds = ((message[7]<<8) + message[8]);
auto cloro = ((message[11]<<8) + message[12]);
// #### Sensors updated with new values
id(ble_yc01_temperature_sensor).publish_state(temp/10.0);
id(ble_yc01_ph_sensor).publish_state(ph/100.0);
id(ble_yc01_orp_sensor).publish_state(orp);
id(ble_yc01_battery).publish_state(battery/31.51);
id(ble_yc01_ec_sensor).publish_state(ec);
id(ble_yc01_tds_sensor).publish_state(tds);
id(ble_yc01_cloro).publish_state(cloro/10.0);
return 0.0; // this sensor isn't actually used other than to hook into raw value and publish to template sensors
time:
- platform: homeassistant
id: esptime
spi:
clk_pin: GPIO18
mosi_pin: GPIO19
display:
- platform: st7789v
model: TTGO TDisplay 135x240
backlight_pin: GPIO4
cs_pin: GPIO5
dc_pin: GPIO16
reset_pin: GPIO23
update_interval: 5s
rotation: 90°
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height(), id(my_blue));
//it.rectangle(0, 20, it.get_width(), it.get_height(), id(my_blue)); // header bar should be line not rectangle
it.line(0, 22, 240, 22, id(my_blue));
it.print(5, 25, id(helvetica_S), id(my_white), TextAlign::LEFT, "Roof.");
it.printf(5, 34, id(helvetica_48), id(my_red), TextAlign::LEFT, "%.0f°", id(pool_roof_temp).state);
it.print(235, 25, id(helvetica_S), id(my_white), TextAlign::RIGHT, "Filter.");
it.printf(235, 34, id(helvetica_48), id(my_blue), TextAlign::RIGHT, "%.0f°", id(pool_tas_temp).state);
it.print(120, 25, id(helvetica_S), id(my_white), TextAlign::TOP_CENTER, "Pool.");
it.printf(120, 34, id(helvetica_48), id(my_green), TextAlign::TOP_CENTER, "%.0f°", id(ble_yc01_temperature_sensor).state);
it.printf(5, 88, id(helvetica_48), id(my_orange), TextAlign::LEFT, "%.1fpH", id(ble_yc01_ph_sensor).state);
it.printf(235, 88, id(helvetica_48), id(my_teal), TextAlign::RIGHT, "%.0f%%", id(ble_yc01_battery).state);
it.strftime(5, 2, id(helvetica_S), id(my_yellow), TextAlign::TOP_LEFT, "%A %H:%M", id(esptime).now());
if (id(system_status).state) {
it.print(235, 2, id(helvetica_S), id(my_green), TextAlign::TOP_RIGHT, "Online");
}
else {
it.print(235, 2, id(helvetica_S), id(my_red), TextAlign::TOP_RIGHT, "Offline");
}