Custom sensor component in ESPHome for VL53L4CX Time of Flight sensor?

Hi all,
I am attempting to create a custom i2c sensor component for the VL53L4CX and am running into trouble. I’m using a Wemos D1 Mini Esp32 and trying to implement an Adafruit VL53L4CX Time of Flight sensor. I have tested it in arduino IDE directly and it works as expected, but when trying to implement it into esphome it wont work.

I am using VL53L4CX as the library reference.

Following this guide to set up the custom sensor

#include "esphome.h"
#include "vl53l4cx_class.h"

#define DEV_I2C Wire
#define SerialPort Serial
int LED_BUILTIN = 2;

#define LedPin LED_BUILTIN
// Components.

// Components.
VL53L4CX sensor_vl53l4cx_sat(&DEV_I2C, 17);

class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  VL53L4CX sensor_vl53l4cx_sat;
  // constructor
  MyCustomSensor() : PollingComponent(1000) {}

  float get_setup_priority() const override { return esphome::setup_priority::BUS; }

  void setup() override {
    pinMode(LedPin, OUTPUT);
    DEV_I2C.setPins(26, 27);
    DEV_I2C.begin();
    // Configure VL53L4CX satellite component.
    sensor_vl53l4cx_sat.begin();
    // Switch off VL53L4CX satellite component.
    // sensor_vl53l4cx_sat.VL53L4CX_Off();
    //Initialize VL53L4CX satellite component.
    //sensor_vl53l4cx_sat.InitSensor(0x12);
    // Start Measurements
    //sensor_vl53l4cx_sat.VL53L4CX_StartMeasurement();
    //delay(3000);
  }

  void update() override {
    // This will be called every "update_interval" milliseconds.
    VL53L4CX_MultiRangingData_t MultiRangingData;
    VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
    digitalWrite(LedPin, HIGH);
    publish_state(42.0);
    delay(1000);
    digitalWrite(LedPin, LOW);
  }
};

This is my c++ file. As is this runs fine and outputs 42.0 once per second. If I uncomment the parts to initialize the sensor and start measurement in setup I get:

WARNING Can't connect to ESPHome API for tofsensor.local: Error connecting to ('192.168.4.174', 6053): [Errno 111] Connect call failed ('192.168.4.174', 6053)
INFO Trying to reconnect to tofsensor.local in the background

because its boot looping. I haven’t even tried to properly publish the distance state yet, was trying to get this working first as if it wont even setup properly then i’m not sure what to do. I’ve tried everything I can think of so I appreciate anyones help here.

Dear,

Did you find a solution yet? I am currently struggling with the same problem.

Many thanks,

Jan

Did you find a solution?
Or maybe @OptimalCode ?

Thanks a lot

Not yet unfortunately. Every few months i’ve gone back to it and still can’t get it right.

Nothing yet.

Does somebody know anybody who could help?

I’ve asked around numerous places and havent been able to get it working myself or gotten much help. Seems like such a niche issue that nobody has an answer really. Gonna revisit it soon and see if I can get it figured out finally.

1 Like

Some time ago I wrote a custom Library for vl53l3cx, but not for ESPHome.
I’ll probably revisit this in the future when I find some time.

1 Like

Anyone figured out how to get this VL53L3CX Tof sensor to work in ESPHome???