andrea-fox / peoplecounter Goto Github PK
View Code? Open in Web Editor NEWCode for a cheap people counter based on VL53L1X sensor and ESP32/8266
License: The Unlicense
Code for a cheap people counter based on VL53L1X sensor and ESP32/8266
License: The Unlicense
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
zones_calibration
Hi, I would like to implement the code using an ESP32 with the VL53L3CX newer sensor. Can you help? Thanks a lot!!!
Do you know esphome? It has seamless integration with home assistant. I'm busy on a way to integrate the people counting people concept with the VL53L1X sensor to Esphome. Don't know if it's working this way but here's is the repo: https://github.com/DutchDeffy/esphome_custom_component_VL53L1X_people_counter.
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.)
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
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:
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
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.
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!
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;
}
}
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 ?
Hi @Andrea-Fox
Thanks for this project, I noticed on ST's website that they have released version 2 of there code, from what I can see they have updated the code to include a buffer of measurements to feed into the counting algorithm for better accuracy. This might be of some interest for your project.
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
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?
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
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!
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
Is it possible to use an ESP8266 instead of a ESP32 or do you need the extra processing power that the ESP32 has ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.