Smart Parking with ESP32 Camera

Photo by Zan on Unsplash

Smart Parking with ESP32 Camera

A brief blog for smart parking using ESP32 with camera module and some recommended a devices that makes easy to make the smart parking system.

Query

Setup the code and connection for the smart parking system with esp32 with the camera module, proximity sensor, and servo sensor taking the picture of the vehicle send it to the cloud google drive and display the left slot on the LCD

Process

To set up a smart parking system using ESP32 with a camera module, proximity sensor, servo sensor, and display, which takes a picture of the vehicle and sends it to Google Drive and displays the available slots on the LCD, you will need to follow these steps:

Components

  1. Gather the required components:
  • ESP32 board

  • Camera module (such as the OV7670 camera module)

  • Proximity sensor (such as the HC-SR04 ultrasonic sensor)

  • Servo motor

  • Breadboard

  • Jumper wires

  • Power supply (such as a USB cable and power bank)

  • LCD display module

  • Google Drive API credentials

Connection

  1. Connect the components:
  • Connect the VCC and GND pins of the camera module to the 3.3V and GND pins on the ESP32 board, respectively.

  • Connect the SCL and SDA pins of the camera module to the corresponding pins on the ESP32 board (such as pins 22 and 21, respectively).

  • Connect the VCC and GND pins of the proximity sensor to the 5V and GND pins on the ESP32 board, respectively.

  • Connect the trig and echo pins of the proximity sensor to any two digital pins on the ESP32 board (such as pins 12 and 14).

  • Connect the signal pin of the servo motor to any digital pin on the ESP32 board (such as pin 27), and the VCC and GND pins to the 5V and GND pins on the ESP32 board, respectively.

  • Connect the SDA and SCL pins of the LCD display module to the corresponding pins on the ESP32 board (such as pins 4 and 5, respectively), and the VCC and GND pins to the 5V and GND pins on the ESP32 board, respectively.

Drive API Setup

  1. Set up the Google Drive API:
  • Go to the Google API Console and create a new project.

  • Enable the Google Drive API and create a new OAuth 2.0 client ID.

  • Download the JSON file with the API credentials.

  1. Write the code: Here's some sample code that you can use as a starting point for your smart parking system:

Code

#include <Servo.h>
#include <Wire.h>
#include "esp_camera.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <LiquidCrystal_I2C.h>

#define TRIG_PIN 12
#define ECHO_PIN 14
#define SERVO_PIN 27
#define LCD_ADDR 0x27
#define LCD_COLS 16
#define LCD_ROWS 2

Servo myservo;
int angle = 0;

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* gdrive_token = "your_GDRIVE_TOKEN";

WiFiClientSecure client;
HTTPClient http;

LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLS, LCD_ROWS);

String get_gdrive_folder_id() {
  client.setAuthorization("Bearer " + String(gdrive_token));
  http.begin(client, "https://www.googleapis.com/drive/v3/files?q=name%3D%27SmartParking%27+and+mimeType%3D%27application%2Fvnd.google-apps.folder%27&fields=files(id)&key=YOUR_API_KEY");
  http.addHeader("Content-Type", "application/json");
  int httpCode = http.GET();
  String folder_id = "";
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    StaticJsonDocument<512> doc;
    DeserializationError error = deserializeJson(doc, payload);
    if (!error) {

      http.beginRequest();
      http.write(request_body.c_str(), request_body.length());
      int bytes_sent = 0;
      while (bytes_sent < file_size) {
        int bytes_to_read = file_size - bytes_sent;
        if (bytes_to_read > 1024) { bytes_to_read = 1024; }
        uint8_t buffer[bytes_to_read];
        int bytes_read = file.read(buffer, bytes_to_read);
        http.write(buffer, bytes_read);
        bytes_sent += bytes_read;
      }
      http.endRequest();
      String response = http.getString();
      http.end();
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  myservo.attach(SERVO_PIN);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Smart Parking");
  lcd.setCursor(0, 1);
  lcd.print("Initializing...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = 5;
  config.pin_d1 = 18;
  config.pin_d2 = 19;
  config.pin_d3 = 21;
  config.pin_d4 = 36;
  config.pin_d5 = 39;
  config.pin_d6 = 34;
  config.pin_d7 = 35;
  config.pin_xclk = 0;
  config.pin_pclk = 22;
  config.pin_vsync = 25;
  config.pin_href = 23;
  config.pin_sscb_sda = 26;
  config.pin_sscb_scl = 27;
  config.pin_pwdn = -1;
  config.pin_reset = -1;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Smart Parking");
  lcd.setCursor(0, 1);
  lcd.print("Ready");
  delay(2000);
}

void loop() {
  float duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;
  int available_slots = distance / 50;
  if (available_slots < 0) { available_slots = 0; }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Available slots:");
  lcd.setCursor(0, 1);
  lcd.print(String(available_slots));
  if (available_slots > 0) {

    float angle = map(available_slots, 0, MAX_SLOTS, 0, 180);
    myservo.write(angle);
    delay(500);
    camera_fb_t* fb = NULL;
    fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Failed to capture image");
      return;
    }
    Serial.printf("Captured image with size %u\n", fb->len);
    uploadToGoogleDrive(fb);
    esp_camera_fb_return(fb);
    delay(5000);
  }
}

Final Instruction

This code calculates the available parking slots based on the distance measured by the proximity sensor, and then maps this value to an angle to control the servo motor. It then captures an image using the camera module and uploads it to Google Drive using the uploadToGoogleDrive() function. Finally, it waits for 5 seconds before repeating the process.

Note that you will need to replace YOUR_ACCESS_TOKEN and YOUR_FOLDER_ID with your own values in the uploadToGoogleDrive() function, as well as ssid and password with your WiFi credentials in the setup() function. You will also need to adjust the pin numbers and camera configuration to match your own hardware setup.

Search Quotes

  1. "Discover the Future of Parking: How Smart Parking Systems Are Revolutionizing Urban Mobility. Learn about the benefits of IoT-enabled solutions for drivers, cities, and businesses."

  2. "Say Goodbye to Parking Hassles: How Smart Parking Systems Are Streamlining Parking. Explore the latest innovations in parking technology, including sensors, data analytics, and mobile apps."

  3. "Maximize Your Parking Efficiency: How Smart Parking Systems Can Optimize Your Parking Operations. Find out how automated parking systems can reduce congestion, increase revenue, and improve customer satisfaction."

  4. "The Ultimate Guide to Smart Parking Systems: How to Choose the Right Solution for Your Needs. Get expert advice on selecting the best parking technology for your business or municipality."

  5. "From Parking Lots to Smart Cities: How Smart Parking Systems are Driving Sustainable Urban Development. Discover the role of smart parking in creating more livable, eco-friendly cities for the future."

Did you find this article valuable?

Support CodeSam by becoming a sponsor. Any amount is appreciated!