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.):

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:

  1. Hardware
  2. Software
  3. Architecture
  4. Problems Encountered
  5. Screen Captures

HARDWARE:


SOFTWARE:


ARCHITECTURE:

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:

  1. 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.

  2. 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:

installation
I have used an empty cardboard box (green color above) to mount the ultrasonic sensor along with the microcontroller which runs the application code.

internal

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.