A people counter looks simple: two beams, one direction rule, a number. The hard part is what the number means after a missed crossing, a reboot or a late-joining dashboard. This reading review covers three published sources and checks them against the smart-event-hall-esp32 prototype, which counts people with two beam sensors and reports over MQTT.
1. Direction comes from the order of two beams (roboticsengineering.in)
The design worksheet "Bidirectional people counter with two IR sensors on Arduino" describes the usual approach: two sensors a few centimetres apart along the walking direction. If the outer one is blocked first and then the inner one, it is an entry; the reverse is an exit. It works through the timing: with sensors 10 cm apart, a person at 1 m/s blocks the second beam about 100 ms after the first, so the code only has to poll faster than that. Its state machine has an idle state, two "waiting for the other beam" states, a one second timeout that cancels half-finished crossings, and a wait for both beams to clear after a count.
Lesson: the counter is a small state machine, and its timeout and re-arm rules matter as much as the direction rule. Source: https://roboticsengineering.in/projects/bidirectional-people-counter-ir-arduino/
2. Time arithmetic on a microcontroller can fail after weeks (Arduino reference, millis)
The Arduino reference for millis() says the value overflows and returns to zero after about 50 days, and that it returns unsigned long. Timeouts built from millis() have to be written with that in mind.
Lesson: write elapsed time as now - start with unsigned arithmetic, not as comparisons against a future timestamp. Source: https://docs.arduino.cc/language-reference/en/functions/time/millis/
3. A dashboard that joins late needs a retained message (HiveMQ, MQTT Essentials part 8)
HiveMQ explains retained messages: when a client publishes with the retained flag, the broker stores the last message on that topic and delivers it to each new subscriber immediately after it subscribes. Without it, a new subscriber sees nothing until the next publish.
Lesson: for a "current state" topic, decide deliberately whether the last value should survive for late subscribers. Source: https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/
Reading the prototype against these sources
I read the README and the firmware file of the repository. This is what the code shows:
- The direction logic matches source 1. An entry is beam A then beam B, an exit is B then A, using a rising-edge check with the other beam clear. A half crossing is dropped after
SEQ_TIMEOUT_MS(1500 ms). The reference design also waits for both beams to clear after a count; this code instead needs a new rising edge with the other beam clear. I did not simulate the difference, so I do not claim either behaves better. - The timeout arithmetic is rollover-safe in form. It uses
now - seqStart > SEQ_TIMEOUT_MSon unsigned values, which is the pattern source 2 implies. - The count has no recovery path of its own.
peoplelives in RAM, so a reboot returns it to zero, and a missed or doubled crossing is never corrected automatically. The only correction is the manual reset command over MQTT. The count also stops at zero on an exit, which hides some errors rather than reporting them. - The MQTT state is published every 5 seconds without the retained flag (the publish call in the sketch uses the default). A dashboard that connects between publishes waits for the next message. Retained state would be a small change, though it would also keep a stale value if the device dies, so a last will message (see my earlier article on that) would be the pair for it.
The README itself says beam spacing and timeouts still need tuning on site.
Limits
This is a code and sources review. I did not run the firmware or the simulator, and it reports no counting accuracy or hardware tests. The project is a learning prototype, not a safety or occupancy-compliance system, and a capacity alert from it should not replace a proper count where limits matter.

