ESP Home convert Arduino code block

Hi

Working on a Solar Powered Smart Mailbox and struggling with the following so any help will be appreciated. Simply want to show show new battery images as the battery percentage changes.

Trying to adopt this code into ESPhome rather than a native arduino flash.

Board is a TTGO T-Display

Arduino code:

if(BL.getBatteryVolts() >= MIN_USB_VOL){
  for(int i=0; i< ARRAY_SIZE(batteryImages); i++){
    drawingBatteryIcon(batteryImages[i]);
    drawingText("Solar Charging");
    vTaskDelay(500);
  }
}else{
    int imgNum = 0;
    int batteryLevel = BL.getBatteryChargeLevel();
    if(batteryLevel >=80){
      imgNum = 3;
    }else if(batteryLevel < 80 && batteryLevel >= 50 ){
      imgNum = 2;
    }else if(batteryLevel < 50 && batteryLevel >= 20 ){
      imgNum = 1;
    }else if(batteryLevel < 20 ){
      imgNum = 0;
    } 

ESPHome Code thats inside the display block:

  if (id(vcc).state) {   
    if (id(vcc).state >=80) {
      it.image(24, 28, id(battery_image_5));
    } else {if (id(vcc).state >=50) {
      it.image(110, 28, id(battery_image_3));
    } else {if (id(vcc).state >=20) {
      it.image(110, 28, id(battery_image_2));
    } else {if (id(vcc).state < 20) {
    it.image(110, 28, id(battery_image_1));
    }

Sensor:

sensor:

  • platform: adc
    pin: 34
    attenuation: 11db
    name: VBatt_MBox
    id: vcc
    update_interval: 10s
    on_value_range:
    below: 3.00
    then:
    - switch.turn_on:
    id: shutdown_smart_mailbox
    filters:
    • multiply: 1.72

Display Block:

display:

  • platform: st7789v
    backlight_pin: GPIO4
    cs_pin: GPIO5
    dc_pin: GPIO16
    reset_pin: GPIO23
    rotation: 270
    lambda: |-
    it.image(0, 0, id(background_1));

    it.print(24, 86, id(impact_28), id(my_white), “SMART MAILBOX”);
    it.rectangle(0, 0, it.get_width(), it.get_height(), id(my_white));
    it.rectangle(0, 22, it.get_width(), it.get_height(), id(my_white)); // header bar

    it.strftime((234 / 1), (140 / 3) * 0 + 0, id(impact_17), id(my_white), TextAlign::RIGHT, “%d-%m-%Y”, id(esptime).now());

    if (id(Smart_Mailbox_Sensor).state) {
    it.print(5, 1, id(impact_17), id(my_yellow), TextAlign::TOP_LEFT, “ONLINE”);
    }
    else {
    it.print(5, 1, id(impact_17), id(my_red), TextAlign::TOP_LEFT, “OFFLINE”);
    }

    if (id(vcc).has_state()) {
    it.printf(24, 48, id(impact_28), id(my_white), “%.2fv %.1f %%”, id(vcc).state, id(batterylevel).state);
    }

    it.image(24, 28, id(battery_image_5));
    it.image(110, 28, id(battery_image_1));

    if (id(Smart_Mailbox_Status).state) {
    it.print(60, 1, id(impact_17), id(my_red), TextAlign::TOP_LEFT, " - OPENED");
    }
    else {
    it.print(60, 1, id(impact_17), id(my_white), TextAlign::TOP_LEFT, " - CLOSED");
    }

End Result to date

Managed to get the battery imaging to change with this code…

  float chargelevel = ((id(vcc).state-3) /1.2 * 100.00);

  if (chargelevel >=80) {
    it.image(104, 30, id(battery_image_5));
  }
  else if (chargelevel < 80 && chargelevel >=60) {
    it.image(104, 30, id(battery_image_4));      
  }
  else if (chargelevel < 60 && chargelevel >=40) {
    it.image(104, 30, id(battery_image_3));      
  }
  else if (chargelevel < 40 && chargelevel >=20) {
    it.image(104, 30, id(battery_image_2));      
  }
  else if (chargelevel < 20) {
    it.image(104, 30, id(battery_image_1));      
  }

Next issue is to convert

char *batteryImages[] = {"/battery_01.jpg", "/battery_02.jpg", "/battery_03.jpg", "/battery_04.jpg", "/battery_05.jpg"};

if(BL.getBatteryVolts() >= MIN_USB_VOL){
  for(int i=0; i< ARRAY_SIZE(batteryImages); i++){
    drawingBatteryIcon(batteryImages[i]);
    drawingText("Solar Charging");
    vTaskDelay(500);
  }

Anyone have advice on how to create a image array?

1 Like

I import a library into ESPHome

esphome:
name: $devicename #smart-mailbox-91
includes:
- include/Pangodream_18650_CL.h

added Pangodream_18650_CL.h to /config/esphome/include/Pangodream_18650_CL.h

I need to convert this but where do I start?

‘Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS);’

into ESPHome?