0
0
Drone-programmingHow-ToBeginner · 4 min read

IoT Project for Smart Parking System: Setup and Example

A smart parking system IoT project uses ultrasonic sensors to detect parking space availability and MQTT protocol to send data to a server or dashboard. This setup enables real-time monitoring and efficient parking management.
📐

Syntax

The basic components of a smart parking IoT project include:

  • Ultrasonic Sensor: Detects if a parking spot is occupied.
  • Microcontroller (e.g., ESP32): Reads sensor data and connects to Wi-Fi.
  • MQTT Protocol: Sends sensor data to a broker/server.
  • Dashboard: Displays parking availability in real-time.

Typical MQTT message format:

topic: parking/slot1/status
payload: occupied or free
python
from machine import Pin, time_pulse_us
import network
import time
import umqtt.simple as mqtt

# Setup ultrasonic sensor pins
trigger = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

# Connect to Wi-Fi
ssid = 'yourSSID'
password = 'yourPassword'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
    time.sleep(1)

# MQTT setup
client = mqtt.MQTTClient('client_id', 'broker.hivemq.com')
client.connect()

# Function to measure distance
def get_distance():
    trigger.value(0)
    time.sleep_us(2)
    trigger.value(1)
    time.sleep_us(10)
    trigger.value(0)
    duration = time_pulse_us(echo, 1)
    distance = (duration / 2) / 29.1
    return distance

# Main loop
while True:
    dist = get_distance()
    status = 'occupied' if dist < 10 else 'free'
    client.publish('parking/slot1/status', status)
    time.sleep(5)
💻

Example

This example shows a simple ESP32 Python script that reads an ultrasonic sensor to detect if a parking spot is occupied and sends the status via MQTT to a public broker.

The sensor measures distance; if less than 10 cm, the spot is considered occupied.

The MQTT topic parking/slot1/status receives messages occupied or free.

python
from machine import Pin, time_pulse_us
import network
import time
import umqtt.simple as mqtt

trigger = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

ssid = 'yourSSID'
password = 'yourPassword'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
    time.sleep(1)

client = mqtt.MQTTClient('client_id', 'broker.hivemq.com')
client.connect()

def get_distance():
    trigger.value(0)
    time.sleep_us(2)
    trigger.value(1)
    time.sleep_us(10)
    trigger.value(0)
    duration = time_pulse_us(echo, 1)
    distance = (duration / 2) / 29.1
    return distance

while True:
    dist = get_distance()
    status = 'occupied' if dist < 10 else 'free'
    client.publish('parking/slot1/status', status)
    print(f'Status sent: {status}')
    time.sleep(5)
Output
Status sent: free Status sent: free Status sent: occupied Status sent: free
⚠️

Common Pitfalls

Common mistakes when building a smart parking IoT project include:

  • Incorrect sensor wiring causing no distance readings.
  • Not waiting for Wi-Fi connection before sending MQTT messages.
  • Using a wrong MQTT topic or broker address.
  • Not handling sensor noise or false triggers.
  • Failing to secure MQTT communication in production.

Always test sensor readings separately before integrating MQTT.

python
## Wrong: Publishing before Wi-Fi connects
wlan.connect(ssid, password)

client = mqtt.MQTTClient('id', 'broker.hivemq.com')
client.connect()

# Wi-Fi connect happens after, causing failure

## Right: Connect Wi-Fi first
wlan.connect(ssid, password)
while not wlan.isconnected():
    time.sleep(1)
client = mqtt.MQTTClient('id', 'broker.hivemq.com')
client.connect()
📊

Quick Reference

Tips for a successful smart parking IoT project:

  • Use ultrasonic sensors for reliable distance measurement.
  • Choose a microcontroller with Wi-Fi like ESP32.
  • Use MQTT for lightweight, real-time messaging.
  • Test sensor and network separately before integration.
  • Secure your MQTT broker with authentication for production.

Key Takeaways

Use ultrasonic sensors and MQTT protocol to detect and send parking spot status.
Ensure Wi-Fi connection is established before publishing MQTT messages.
Test sensor readings independently to avoid integration issues.
Secure MQTT communication for real-world deployment.
Use simple topics like 'parking/slot1/status' for clear data organization.