Giter Site home page Giter Site logo

andrea-fox / peoplecounter Goto Github PK

View Code? Open in Web Editor NEW
93.0 14.0 27.0 4.35 MB

Code for a cheap people counter based on VL53L1X sensor and ESP32/8266

License: The Unlicense

C++ 83.08% C 16.92%
vl53l1x people-counter home-automation esp32 esp8266

peoplecounter's People

Contributors

andrea-fox avatar lc7894 avatar noxhirsch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

peoplecounter's Issues

Configure MQTT for HomeAssistant

Ok, I got some more stupid questions. I have a Zigbee2MQTT USB-Stick in my server and also dockers running a Mosquitto MQTT server and another docker running Home Assistant. The Zigbee2MQTT-usb sends everything to the MQTT server and they show up in Home Assistant in Integrations -> MQTT.

In the Mosquitto log I see:

1605812716: New connection from 192.168.0.182 on port 1883.
1605812716: New client connected from 192.168.0.182 as PeopleCounterESP32Client-a164 (p2, c1, k15).

But how do I make the device show up in Home Assistant ?
I tried Adding the :

sensor:
    - platform: mqtt
      name: "People counter"
      state_topic: "peopleCounter/serialdata/tx"

But I get no new devices with or without the sensor added to config.yaml

ESP32 & VL53L3CX

Hi, I would like to implement the code using an ESP32 with the VL53L3CX newer sensor. Can you help? Thanks a lot!!!

Feature request: Use a single MQTT publisher topic and ArduinoJson.h

Hi Andrea,

Here I'm with a suggestion on how the code could be improved or functionality can be added to your code. I thought of using a single MQTT publish topic and publish all the data in json format to that. Instead of using 3 topics. That way configuring can be a bit more easy. I have already something set up. The idea is using global variables, instead of passing everything data related as a parameter to the specific functions. You would need to define those variables tho. So, for example.

/******************************** MQTT TOPIC(s) change these to your liking *******************************/
#define mqtt_topic "/peopleCounter/" // one publisher topic 

/******************************** VARIABLES FOR 'CARRYING' THE DATA *******************************/

String Passage_Event;

uint16_t Distance;

int LocalPeopleCount;

The general publish function can something like this.

void publishData(){
  StaticJsonDocument<300> doc;

  doc["passage_event"] = Passage_Event;

  JsonObject distance = doc.createNestedObject("distance");
  distance["zone"] = Zone;
  distance["measurement"] = Distance;

  doc["local_people_count"] = LocalPeopleCount;

  char buffer[300];
  serializeJson(doc, buffer);
  Serial.print(String(buffer));

  client.publish(mqtt_topic, buffer, true);
}

The data first gets formatted into a json before it gets published to the MQTT server.
Then after an entry (peoplecounter.ino line 295) you can add these lines to publish the data.

        if ((PathTrack[1] == 1)  && (PathTrack[2] == 3) && (PathTrack[3] == 2)) {
          // this is an entry
          Passage_Event = "entry";
          LocalPeopleCount++;
          publishData();
          }

You can use the same method for for the exit event and distance measuring.

And an example of the JSON data can consequently look like this.

{"passage_event":"entry",
"distance":{"zone":1,"measurement":2115}
"local_people_count":1}

Maybe I'm completely wrong and does this change only make things more difficult. Also it depends on how the users use the code of course. Anyway, it's just a suggestion. If it would help I can also share my 'version' of your code, where I did some stuff differently. (Using a cheap aliexpress time of flight sensor which has a different I2C address.)

People in the room now: 111227

Hello . is it ok?

: warning: 'RIGHT' defined but not used [-Wunused-variable]
static int RIGHT = 1;
^

One person has entered in the room. People in the room now: 111227
One person has exited the room. People in the room now: 71227

High power consumption -> not possible to run via battery

I measured the power consumption of the sensor in a setup with an ES8266 NodeMCU and found it to use (on average) 80mA. One of the main reasons for the high power consumption is the WiFi that is always on. Therefore, using a battery as power source is not suitable except you want to change the battery very often. This is a problem when there is no outlet nearby or you don't want to use a cord. According to the docs of the sensor it should be possible to use the deep sleep mode of the ESP and wake it up when someone passes the sensor:

image

I'll just leave this here as a suggestion for further development of the algorithm ๐Ÿ™‚ (Maybe I am able to find a solution on my own, but I have to learn a lot about ESP programing until I reach this point)

Link to the docs: https://www.st.com/resource/en/user_manual/dm00474730-vl53l1x-api-user-manual-stmicroelectronics.pdf

counter = 1, but never 2

Starting a new topic here.

I used:
people_counter_esp32.ino
the name of my sensor is tof_people_ard

Changed the value 500 to 5000 for wifi timeout.

The ESP32 is succesfully conected to WiFi and MQTT. I can see the topic realtime changing for:
people_counter/tof_people_ard/distance

I do not see any updates for the counter in topic:
people_counter/tof_people_ard/counter

It did work 1 time hower.

mqtt

In above image you can see the MQTT realtime going and on some moment I place my hand before the sensor.

the standard config as in the ino is in my compiled upload. And no matter what I try to place the sensor it counts 2... so the home assistant counter is never updated.

Any help appreciated!

Code question advised_oriantation_of_the_sensor

Shouldn't the switch...case for advised_orientation_of_the_sensor == false also have break;?

  if (advised_orientation_of_the_sensor){
    
    switch (ROI_size) {
        case 4:
          center[0] = 150;
          center[1] = 247;
          break;
        case 5:
          center[0] = 150;
          center[1] = 247;
          break;
        case 6:
          center[0] = 159;
          center[1] = 239;
          break;
        case 7:
          center[0] = 159;
          center[1] = 239;
          break;
        case 8:
          center[0] = 167;
          center[1] = 231;
          break;
      }
  }
  else{
    switch (ROI_size) {
        case 4:
          center[0] = 195;
          center[1] = 60;
        case 5:
          center[0] = 194;
          center[1] = 59;
        case 6:
          center[0] = 194;
          center[1] = 59;
        case 7:
          center[0] = 193;
          center[1] = 58;
        case 8:
          center[0] = 193;
          center[1] = 58;
  
      }
  }

Wifi connection issues

I have tried different ESP32s, different USB-cables, and tried connecting to several networks (2.4Ghz). But I only get the "Sensor online" -message. Then dots, but it never connects to any wifi. Is there any special procedure for connecting to a wifi network besides specifying the sid and password ?

Count People at two entrance

Hello @Andrea-Fox ,

I would like to use your code to count people on a staircase. This would require using two sensors on an ESP32, as people can leave the stairs at the beginning and end. The use of two sensors is possible after a research with i2c.
Can the VL53L0X also be used instead of the VL53L1X?

Best regards TheMBeat

Using without MQTT

I am a newbie and wanted to use this code without MQTT. want to print people counter directly to serial or to a display. could somebody help me with this?

Possible enhancement with 4 ROIs

Hi @Andrea-Fox and @noxhirsch, Thanks so much for what you have done here!

I have been thinking of possible ways to make the sensor more accurate on wider doorways.

My thinking behind this is to use more than 2 ROIs but rather use 4 zones of 8x8 two would be in the front zone and two in the back zone. ( Note I haven't had much success with an ROI set to 8 x 16 but 8x8 works well but sometimes misses counts if you approach a doorway from an angle)

I am working on trying to implement this but would be interested in your opinion on the idea?

I was considering two possible ways of implementing this.

option 1: feed the two values from the front zone into the counting algorithm at the same time and the same for the back zone
option 2: feed the lowest measurement reading from the two zones in the front and back into the algorithm.

Thank you
Mark

ESPHome compatible sketch?

Hello,

Sorry for declaring it as an issue, it's more of a question. Will there be a file available for ESPHome? It's way easier, especially for HomeAssistant to integrate automatically. Thanks!

Automation Example with Choose

This is another automation example which works with HA >0.113
With that you only need one automation for increasing and decreasing the counter.
Since I don't know if you want to add this to your examples or replace them, I didn't open a PR and post it here instead.

alias: Person Counter
trigger:
  - platform: mqtt
    topic: peopleCounter/serialdata/tx
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.payload == "1" }}'
        sequence:
          - service: input_number.increment
            entity_id: input_number.people_counter
      - conditions:
          - condition: template
            value_template: '{{ trigger.payload == "2" }}'
        sequence:
          - service: input_number.decrement
            entity_id: input_number.people_counter
mode: single

Possible to use ESP8266 ?

Is it possible to use an ESP8266 instead of a ESP32 or do you need the extra processing power that the ESP32 has ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.