I made a vl53l1x time of flight (tof) sensor to count people with esphome

Can you please share the procedure how to put this sensor into operation in ESPhome?

I share my esphome code
Download the file from the link below

https://cafe.naver.com/koreassistant/3435

My korean is a little outdated. Can’t you copy it here and format properly?

Thank you I’ll take a look at it. Installation question?
Will I create folder custom_components in the /config/esphome/ and place the downloaded counter_vl53l1x.h and tof_counter.yaml files in it (folder tof_counter)?
Or is it different?

The “counter_vl53l1x.h” file must be in the “tof_counter” folder

Thanks, I don’t have a D1 mini at home. I’ll buy a board and test the sensor.

esp32 is also possible

Sorry to say that could you help with this error message?
I could not understand why VL53L1_RdWord will throw exception.

[01:06:39]
[01:06:39] ets Jan  8 2013,rst cause:2, boot mode:(3,6)
[01:06:39]
[01:06:39]load 0x4010f000, len 3584, room 16 proceed to safe mode in 0 restarts
[01:06:42];ld\x9c\x9f<\x84d\xe0|\x8c$\xecc|\x8f\x83\xec\033\x93{\x92c\x8cc\x84\xf2'o\x9el'o\xdc\xe3\xe4c8\xe4\xc7ds$s$p\xf2n\xe0\x82l\x8c\x9c#'\xe3|\xecd\x87\x87#\x8c\xf3o'\xe7d\xc4\xc7$`\xd8\033ngl aos\x8fۓocl`a{\x92\x9bgcd`\x9c#\x8e\x93ls\xdb \xfc\xc3g\x9c[I][logger:214]: Log initialized
[01:06:42][C][ota:458]: There have been 0 suspected unsuccessful boot attempts.
[01:06:42][I][app:029]: Running through setup()...
[01:06:42][I][i2c.arduino:156]: Performing I2C bus recovery
[01:06:42][D][binary_sensor:034]: 'guest bedroom human counting Status': Sending initial state OFF
[01:06:43][D][VL53L1X custom sensor:060]: Getting Device data
[01:06:43]
[01:06:43]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[01:06:43]
[01:06:43]Exception (28):
WARNING Exception type: Access to invalid address: LOAD (wild pointer?)
[01:06:43]epc1=0x402198b4 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
WARNING Decoded 0x402198b4: VL53L1_RdWord
[01:06:43]
[01:06:43]>>>stack>>>
WARNING Found stack trace! Trying to decode it
[01:06:43]
[01:06:43]ctx: cont
[01:06:43]sp: 3ffffc50 end: 3fffffc0 offset: 0190
[01:06:43]3ffffde0:  4022bc09 00000064 3fff1f5c 4022bbfe  
WARNING Decoded 0x4022bc09: delay
WARNING Decoded 0x4022bbfe: delay
[01:06:43]3ffffdf0:  00000000 00000000 0000000e 3fff1f5c  
[01:06:43]3ffffe00:  3ffe8f8f 3ffe852c 3ffefc40 402156ff  
WARNING Decoded 0x402156ff: PeopleCountSensor::setup()
[01:06:43]3ffffe10:  00000000 3fff2bdc 3ffef708 402154dd  
WARNING Decoded 0x402154dd: esphome::Scheduler::process_to_add()
[01:06:43]3ffffe20:  3fff22d4 0000000a 3ffef708 4022f30c  
WARNING Decoded 0x4022f30c: esphome::Component::call_setup()
[01:06:43]3ffffe30:  3fff2b94 00000007 3ffef708 4022f3b1  
WARNING Decoded 0x4022f3b1: esphome::Component::call()
[01:06:43]3ffffe40:  3fff1d80 00000009 3ffef708 40214202  
WARNING Decoded 0x40214202: esphome::Application::setup()
[01:06:43]3ffffe50:  3fff2b94 0000001e 3fffff10 4023a9d5  
WARNING Decoded 0x4023a9d5: operator delete(void*) at /workdir/repo/gcc/libstdc++-v3/libsupc++/del_op.cc:48
[01:06:43]3ffffe60:  3fff2b64 00000000 00000028 402158e6  
WARNING Decoded 0x402158e6: std::_Function_base::_Base_manager<setup::{lambda()#5}>::_M_manager(std::_Any_data&, std::_Function_base::_Base_manager<setup::{lambda()#5}> const&, std::_Manager_operation) at main.cpp
[01:06:43]3ffffe70:  00000000 3fffff10 3fffff10 00000000  
[01:06:43]3ffffe80:  3ffef708 3fff1dfc 3fffff10 40217382  
WARNING Decoded 0x40217382: setup
[01:06:43]3ffffe90:  32a8c001 32a8c03c ffffff01 00000000  
[01:06:43]3ffffea0:  00000000 feefef00 feefeffe feefeffe  
[01:06:43]3ffffeb0:  3fff1438 00000000 00000000 3fff1468  
[01:06:43]3ffffec0:  00000000 00000000 32a8c001 32a8c03c  
[01:06:43]3ffffed0:  ffffff01 00000000 00000000 00000000  
[01:06:43]3ffffee0:  3fff13c8 00000000 00000000 3fff13e0  
[01:06:43]3ffffef0:  00000000 00000000 32a8c001 32a8c03c  
[01:06:43]3fffff00:  ffffff01 00000000 00000000 00000000  
[01:06:43]3fffff10:  3fff272c 3fff2730 402158bc 402159b0  
WARNING Decoded 0x402158bc: std::_Function_base::_Base_manager<setup::{lambda()#5}>::_M_manager(std::_Any_data&, std::_Function_base::_Base_manager<setup::{lambda()#5}> const&, std::_Manager_operation) at main.cpp
WARNING Decoded 0x402159b0: std::_Function_handler<esphome::optional<bool> (), setup::{lambda()#5}>::_M_invoke(std::_Any_data const&) at main.cpp
[01:06:43]3fffff20:  00000000 feefef00 3fffff60 00000001  
[01:06:43]3fffff30:  3fff1e80 3fffff28 00000001 3fff279c  
[01:06:43]3fffff40:  3fff2644 3fff24ac 3fff234c 3fff211c  
[01:06:43]3fffff50:  3fff1fe4 3fff1e0c 3fff1d4c 3fff1c0c  
[01:06:43]3fffff60:  3fff28d4 feefeffe feefeffe feefeffe  
[01:06:43]3fffff70:  3fff1e7c 3fff27f4 3fffff28 0000003c  
[01:06:43]3fffff80:  3fff279c 3fff0190 3fff2174 feefeffe  
[01:06:43]3fffff90:  feefeffe feefeffe feefeffe 3ffeffec  
[01:06:43]3fffffa0:  3fffdad0 00000000 3ffeffac 4022af80  
WARNING Decoded 0x4022af80: loop_wrapper() at core_esp8266_main.cpp
[01:06:43]3fffffb0:  feefeffe feefeffe 3ffe856c 4010073d  
WARNING Decoded 0x4010073d: cont_wrapper
[01:06:43]<<<stack<<<
[01:06:43]
[01:06:43]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[01:06:43]
[01:06:43] ets Jan  8 2013,rst cause:2, boot mode:(3,7)
[01:06:43]
[01:06:43]load 0x4010f000, len 3584, room 16 
[01:06:43]tail 0
[01:06:43]chksum 0xb0
[01:06:43]csum 0xb0
[01:06:43]v2843a5ac
[01:06:43]~ld
[01:06:43][I][logger:214]: Log initialized
[01:06:43][C][ota:458]: There have been 1 suspected unsuccessful boot attempts.

Here is my ESPHome config (I use melopero/Melopero VL53L1X and changed the “tof_gestures_initDIRSWIPE_1(800, 0, 1000, false, &gestureDirSwipeData);” to “tof_gestures_initDIRSWIPE_1(800, 0, 1000, &gestureDirSwipeData);”):

substitutions:
  device_name: guest-bedroom-human-counting
  friendly_name: guest bedroom human counting
  platform: ESP8266
  board: nodemcuv2
  device_ip: !secret guest-bedroom-human-counting_ip

packages:
  template: !include template.yaml

esphome:
  includes:
    - custom_functions/tof_vl53l1x_api.h
  libraries:
    - Wire
    - "melopero/Melopero VL53L1X"
    - "stm32duino/STM32duino Proximity Gesture"

mqtt:
  broker: !secret mqtt_broker
  username: !secret mqtt_username
  password: !secret mqtt_password
  port: !secret mqtt_port
  discovery: True
  discovery_retain: True
  discovery_prefix: homeassistantconf

i2c:

switch:
  - platform: restart
    name: $friendly_name Restart

binary_sensor:
  - platform: status
    name: $friendly_name Status

  - platform: template
    name: "Door sensor"
    lambda: |-
      if (id(tof_distance_avg).state < 1400) {
        // someone standing in door
        return true;
      } else {
        // no-one there 
        return false;
      }
    filters:
      - delayed_off: 100ms

sensor:
  - platform: wifi_signal
    name: $friendly_name RSSI
    update_interval: 60s

  - platform: uptime
    name: $friendly_name Uptime

  - platform: custom
    lambda: |-
      auto people_counter = new PeopleCountSensor();
      App.register_component(people_counter);
      return {people_counter->people_sensor};
    sensors:
      - name: People
        id: people_count
        icon: "mdi:counter"
        accuracy_decimals: 0
        unit_of_measurement: "people"

  - platform: mqtt_subscribe
    name: "People reset"
    id: people_reset
    topic: $device_name/sensor/people/reset
    internal: true
    on_value:
      then:
        - lambda: |-
            resetCounter = 1;
  - platform: template
    name: tof distance
    id: tof_distance
    accuracy_decimals: 0
    icon: "mdi:ruler"
    unit_of_measurement: "mm"
    #device_class: None
    update_interval: 1s
    lambda: |-
      return distance[1];
    filters:
      - delta: 100.0

  - platform: template
    name: tof distance avg
    id: tof_distance_avg
    accuracy_decimals: 0
    icon: "mdi:ruler"
    unit_of_measurement: "mm"
    internal: true
    #device_class: None
    update_interval: 0.1s
    lambda: |-
      return distance[1];
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
1 Like

I try to use the function code to replace the call VL53L1_RdWord(Dev, 0x010F, &wordData); and I found it failed at Dev->I2cHandle->i2c->beginTransmission(Dev->I2cDevAddr);

Could anyone help with it?

Anyone any luck with this? I tried the code from @kmue but I get errors. First is was Wire, so I added it to the libraries, but in the end I only get distance 0 readings. I used a D1 mini:

i2c:
sda: 5 #D1
scl: 4 #D2
scan: True

Is the code not good anymore?

See post #8 above for D1 Mini &/or refer to WeMos D1 mini pinouts. You have your sca and scl pins backwards. sda pin is D1/GPIO5…