Car Presence Detection
In this post, I’ll be sharing the project which I developed to detect if the parking spot(garage) is occupied using ultrasonic sensor.
The Home Automation project that I have developed is in java and running on Raspberry pi. However, for this project I’m using ESP32 microcontroller. For developing this application, I had few options (Java is not an option on microcontrollers for various reasons - memory constraints, large JVM foot print etc.):
- ESPHome which generates c++ compiled code based on yaml configuration file
- micropython
- Arduino/C++ (which is basically c++)
I decided to go with Arduino/C++ as that would give me the freedom to develop based on my requirements (unlike yaml configuration files). That also meant, I would get to dust off my c++ coding skills ;)
TABLE OF CONTENTS:
HARDWARE:
- ESP32 or any other microcontroller can also be used such as Raspberry pico
- HCSR04 Ultrasonic sensor
- USB C to micro USB data cable -for uploading code to microcontroller
- 5V power supply micro usb
- GPIO jumper wires
- Wire spool
🔝
SOFTWARE:
- PlatformIO
- Libraries used in the project:
- HiveMQ Cloud
🔝
ARCHITECTURE:
The ultrasonic sensor sends an ultrasonic pulse-out (trigger) which travels through the air and if there is an obstacle, it bounces back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated.
I’m making use of this to determine if there is car parked in the garage. I had to find out the distance between the sensor and the car to determine if the car is in the garage. For example - if the distance calculated is greater than 5ft, I know the car is not parked in the garage (and if less than 5ft, the car is parked). I have created a dead zone of 0.4feet to filter out erroneous readings (which is common if the object is not flat).
The code keeps sending pulse out/trigger every few milliseconds and computes the distance. If the distance measured is current distance+-0.4ft, the actual measured distance is sent to the MQTT broker. I’m using HiveMQ Cloud as the MQTT broker and using PubSubClient c++/arduino library to make the calls to MQTT broker.
I’m using home assistant to take further action based on this message. Example - send a notification if a car has arrived or departed etc.
🔝
PROBLEMS ENCOUNTERED:
-
MQTT secure encrypted connection to HiveMQ: I started this project using locally hosted MQTT broker named Mosquitto MQTT. However, because of the below issue, I switched to cloud based MQTT broker. Since the broker was hosted externally (cloud), the connection had to be encrypted.
For creating encrypted secure connection I had to create the Wifi object using WiFiClientSecure and pass the certificate. Without this the broker was rejecting the connection. -
Randomly stopped receiving metric from sensor/microcontroller: Randomly stopped getting metrics about the distance between sensor and car. Since memory and processing is very limited on microcontrollers, I didn’t add any logging. However, later I figured a way to log to rsyslog server (will be publishing more details on my setup which makes use of rsyslog and grafana loki). With the help of logging, I was able to fing out that the issue was with the microcontroller/host unable to reach (even couldn’t ping) the locally hosted MQTT broker. I’m using Netgear Orbi Mesh network (Access Points) at my home and have seen this behavior where randonly devices on the same home network can no longer ping each other. However, they are still up and running and can ping hosts on the internet. This is the reason I moved to cloud based MQTT broker.
🔝
SCREEN CAPTURES:
I have used an empty cardboard box (green color above) to mount the ultrasonic sensor along with the microcontroller which runs the application code.
This is the inside of the cardboard box, it has the microcontroller and the ultrasonic sensor (hcsr04). The reason there are more wires is because, I have one more hcsro4 sensor that is also connected to this microcontroller. So, I’m tracking the presence of 2 cars using 2 hcsr04 sensors and 1 esp32 (microcontroller).
🔝
I have been using this set up for few weeks now and used in some home assistant automations. Feel free to reach out to me if you have any questions or comments.