I’m a bit stuck with my first ever ESPHome project and looking for a little guidance please.
My setup consists of:
Lolin D1 Mini Pro
ST Microelectronics VL53L1X Time of Flight sensor
The sensor is connected via I2C.
I am going to be using the sensor to determine the distance from the sensor to the surface of the heating oil in my oil tank.
I am powering the D1 Mini Pro on the 5V pin, the board is powering the sensor from the 3.3V pin, and the sensor board reduces this down to the required 2.8V.
As the sensor is not supported directly by ESPHome, I am using a custom component:
#include "esphome.h"
#include <Wire.h>
#include <VL53L1X.h>
VL53L1X tof_sensor;
class MyCustomSensor : public PollingComponent, public Sensor {
public:
// constructor
MyCustomSensor() : PollingComponent(5000) {} // polling every 5s
void setup() override {
// This will be called by App.setup()
Wire.begin();
Wire.setClock(400000); // use 400 kHz I2C
tof_sensor.setTimeout(500);
tof_sensor.setAddress(0x29);
if (!tof_sensor.init()) {
ESP_LOGE("VL53L1X custom sensor", "Failed to detect and initialize sensor!");
while (1);
}
tof_sensor.setDistanceMode(VL53L1X::Short);
tof_sensor.setMeasurementTimingBudget(500000);
tof_sensor.startContinuous(50);
}
void update() override {
uint16_t mm = tof_sensor.read();
if (!tof_sensor.timeoutOccurred()) {
publish_state(mm);
} else {
ESP_LOGE("VL53L1X custom sensor", "Timeout during read().");
}
}
};
and
esphome:
name: oiltank
includes:
- tof_vl53l1x.h
libraries:
- "Wire"
- "VL53L1x"
esp8266:
board: d1_mini_pro
# https://esphome.io/components/i2c.html
i2c:
sda: GPIO4
scl: GPIO5
scan: True
# VL53L1X, 0x29
frequency: 400kHz
sensor:
- platform: custom
lambda: |-
auto my_sensor = new MyCustomSensor();
App.register_component(my_sensor);
return {my_sensor};
sensors:
name: "Distance"
accuracy_decimals: 0
unit_of_measurement: "mm"
- platform: wifi_signal
name: "WiFi Signal Sensor"
update_interval: 60s
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
password: "password"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Oiltank Fallback Hotspot"
password: "password"
captive_portal:
web_server:
port: 80
I have compiled and flashed the above onto my D1 Mini Pro and I am seeing successful WiFi communication and distance readings coming back form the sensor and showing in my Home Assistant.
The problem I have is that all the readings I am getting from the sensor are too high by around 50mm. For example, a tape measure set at 450mm and I get a sensor reading of 500mm.
The company that make the sensor do offer an API for download, which contains calibration functions that can be called, which should be able to take care of this offset, and although I have downloaded the API, I’m still very new to everything ESP/Arduino/Home Assistant related and learning as I go.
I have no idea how I would ‘use’ this downloaded API, compile it into my ESPHome code, interact with the calibration function, store the calibrated offset so that it gets read by the sensor at Boot time etc.
I have read through the API documentation, but a lot of it is a bit advanced for my level of knowledge, as I have no knowledge of coding/dev work.
The other option I wondered about was to simply set an offset of 50mm in ESPHome so that this is applied to the readings before they are displayed in my front end.
Looking at the ESPHome documentation, it seems you can create an offset ‘filter’ for a sensor, however this doesn’t seem to apply if you are using a custom component? Unless I’ve misunderstood, which is entirely possible.
The exact distance isn’t necessarily important, as once I have the oil tank brimmed, I will know the distance reading for ‘Full’ and I have the tank dimensions, so I know what would be the empty reading, so converting to litres left or % left etc should be reasonably straightforward with a little help. I just don’t know the best way to proceed and wondered if anyone could offer some advice please?
Thanks in advance!