Introduction: Automatic Car Parking System

I am very excited to create an IoT project with the arduino . Today I am going to teach you guys how to make an awesome Remote Car Parking System. Move on to the next step to find more!

Step 1: Introduction

Hey guys, in this tutorial we are going to build an Automatic Car Monitoring System. We will be learning a lot of stuff. We will learn to :

•Code in PHP and SQL.

•Sync C code with PHP

•Create a professional website

•Make a real world solution.

•Prototype with Arduino

This project's main purpose is to produce a real life solution to the car parking problem which the whole world is facing frequently. People usually roam around in the parking lots trying to find a suitable place to park in. To solve that problem I have created the automatic car parking system. Now we will proceed to the next step and take a look at the parts required to build this project successfully.

Step 2: Parts

Step 3: Connections

In this step we will looking at the connections of the project which we are dealing with. The connections are quite easy. What you have to do is follow the instructions given below.

◘ Connect the Vcc pin to the positive rail on your breadboard.

◘ Connect the Gnd pin to the negative rail on your breadboard.

◘ Connect the Trig pin to any digital pin on the arduino.

◘ Connect the Echo pin to any digital pin on the arduino.

◘ Finally, connect the positive rail of the breadboard to 5V pin on the arduino and the negative rail of the breadboard to the Gnd pin on the arduino.

We have therefore completed connecting all the pins. Now try this sample code out to check if it works.

I also have some useful comments in the file attached below, so that conceptual understanding would be easier.

Now, having completed the connections let us proceed to the software part.

<p>#define trigPin 26 //here, it will be the pin which you connected it to<br>#define echoPin 27 //here, it will be the pin which you connected it to</p><p>void setup() {
  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  long duration,distance;
digitalWrite(trigPin,HIGH); //we will send the sound wave 
delayMicroseconds(200); //wait for it to reach
digitalWrite(trigPin,LOW); //stop sending the sound waves
delayMicroseconds(10); //wait for it to sense
duration=pulseIn(echoPin,HIGH);  //start recieving them
distance=(duration/2) /29.1; //a simple equation to get the distance in meters
//Printing it to the console.
if (distance>200){
  Serial.println("Can't read"); 
}
else{
  Serial.println(distance);
}
}</p>

Step 4: Software

For building this project successfully you must have the following software installed in your computer. A download link will be provided.

•Arduino : https://www.arduino.cc/en/Main/Software

•Xampp : https://www.apachefriends.org/download.html (this is the web server application)

•Sublime Text Editor : https://www.sublimetext.com/download

•You will also need an Amazon IP address if you want to access it across the globe.

The Arduino software is used to program the arduino. You can download Sublime Text Editor if you want, because you can write the program in the notepad itself. But I prefer Sublime. It is a great software to write code in. Xampp is a simple web server solution provided by apache. More information on Xampp can be found in this website : https://en.wikipedia.org/wiki/XAMPP

We will be using the MariaDB database and the PHP interpreter available in Xampp.

Step 5: A Few Explanations

Before we move onto the major meat of the whole project, I need to make sure that you understand what this project after all does. First we will capture the data from the ultrasonic sensor, which you should have tried out with the sample code provided in STEP 3. Then we should push that data to a webpage which is quite straightforward. And then we need to get the data with PHP and push that data to the MariaDB database. After passing it into the database, we need to get the data from the database and then use visual representations so that the user can easily interpret the application. We will be using five different coding files to do this project successfully. Now , let us proceed to our first coding file which will the arduino code.

Step 6: Arduino Code

In this step we will be looking at the arduino code for the Smart Car Parking project. The code will get the data from the ultrasonic sensors and it will post it to the webpage. Here is the code :

#include <p>#include 
#include </p><p>Servo servo;
//declaring variables to get the right data
int available1;
int available2;
int available3;
int available4;
int available5;
//distance for teh ultrasonic sensor
long distance1;
long distance2;
long distance3;
long distance4;
long distance5;
//duration taken for the sensor to transmit ultrasonic waves
long duration1;
long duration2;
long duration3;
long duration4;
long duration5;
//opening the servo for the cars to enter
long servoduration;
long servodistance;
//the output pins for the sensor
#define trig1  26
#define trig2  28
#define trig3  30
#define trig4  32
#define trig5  34
//the input pins for the sensor
#define echo1  27
#define echo2  29
#define echo3  31
#define echo4  33
#define echo5  35
//the out and in pins for the sensor
#define servotrig 38
#define servoecho 39
//ethernet datapins
char server[] = "192.168.137.1";
EthernetClient client;
//default computer address
byte mac[]={
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};</p><p>int response;</p><p>void setup() {</p><p>  Ethernet.begin(mac);
  Serial.begin (9600);
  delay(100);</p><p>//  response = client.connect(server, 80);
//  
//  if (response) {
//    Serial.println("connected");
//  } else {
//    // if you didn't get a connection to the server:
//    Serial.println("connection failed");
//  }
  //defining the modes of the pins for the sensor
  
  pinMode(trig1,OUTPUT);
  pinMode(trig2,OUTPUT);
  pinMode(trig3,OUTPUT);
  pinMode(trig4,OUTPUT);
  pinMode(trig5,OUTPUT);
  
  pinMode(echo1,INPUT);
  pinMode(echo2,INPUT);
  pinMode(echo3,INPUT);
  pinMode(echo4,INPUT);
  pinMode(echo5,INPUT);
  
  pinMode(servotrig,OUTPUT);
  pinMode(servoecho,INPUT);</p><p>  servo.attach(37);
}</p><p>void loop() {
  //finding the distance for each of the sensors
  digitalWrite(trig1, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trig1, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig1, LOW);
  duration1 = pulseIn(echo1, HIGH);
  distance1 = (duration1/2) / 29.1;</p><p>  
  digitalWrite(trig2, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trig2, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig2, LOW);
  duration2 = pulseIn(echo2, HIGH);
  distance2 = (duration2/2) / 29.1;</p><p>  digitalWrite(trig3, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trig3, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig3, LOW);
  duration3 = pulseIn(echo3, HIGH);
  distance3 = (duration3/2) / 29.1;</p><p>  digitalWrite(trig4, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trig4, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig4, LOW);
  duration4 = pulseIn(echo4, HIGH);
  distance4 = (duration4/2) / 29.1;</p><p>  
  digitalWrite(trig5, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trig5, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trig5, LOW);
  duration5 = pulseIn(echo5, HIGH);
  distance5 = (duration5/2) / 29.1;</p><p>  Serial.print("Distance 1:");
  Serial.print(distance1);
  Serial.println("cm");</p><p>  Serial.print("Distance 2:");
  Serial.print(distance2);
  Serial.println("cm");</p><p>  Serial.print("Distance 3:");
  Serial.print(distance3);
  Serial.println("cm");</p><p>  Serial.print("Distance 4:");
  Serial.print(distance4);
  Serial.println("cm");</p><p>  Serial.print("Distance 5:");
  Serial.print(distance5);
  Serial.println("cm");</p><p> delay(100);</p><p>//getting the actual value required to do the project successfully
 
  if(distance1<6)
  {
    available1=0;
  }
  else{
    available1 =1;
  }  </p><p>  if(distance2<6)
  {
    available2=0;
  }
  else{
    available2 =1;
  }</p><p>  if(distance3<6)
  {
    available3=0;
  }
  else{
    available3 =1;
  }
 if(distance4<6)
  {
    available4=0;
  }
  else{
    available4 =1;
  }
  
  if(distance5<6)
  {
    available5=0;
  }
  else{
    available5 =1;
  }</p><p>  
  digitalWrite(servotrig, LOW);  
  delayMicroseconds(2); 
  digitalWrite(servotrig, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(servotrig, LOW);
  servoduration = pulseIn(servoecho, HIGH);
  
    if(servodistance<10)
  {
    servo.write(90);
  }
  else
  {
    servo.write(-90);
  }</p><p>//passing the variables to the php page.</p><p>    if (!client.connected()) 
    {
      Serial.print("Available1=");
      Serial.println(available1);
      Serial.print("Available2=");
      Serial.println(available2);
      Serial.print("Available3=");
      Serial.println(available3);
      Serial.print("Available4=");
      Serial.println(available4);
      Serial.print("Available5=");
      Serial.println(available5);
      client.stop();
     delay(100);    
      if (client.connect(server, 80)) 
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("GET /UpdateAvailability.php?Position=");
        client.print("1");
        client.print("&&");
        client.print("Available=");
        client.print(available1);
        client.println(" HTTP/1.1");
        client.println("Host: 192.168.137.1");
        client.println("Connection: close");
        client.println();
        client.stop();
       } 
      else 
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }</p><p>      if (client.connect(server, 80)) 
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("GET /UpdateAvailability.php?Position=");
        client.print("2");
        client.print("&&");
        client.print("Available=");
        client.print(available2);
        client.println(" HTTP/1.1");
        client.println("Host: 192.168.137.1");
        client.println("Connection: close");
        client.println();
        client.stop();
       } 
      else 
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
    
     if (client.connect(server, 80)) 
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("GET /UpdateAvailability.php?Position=");
        client.print("3");
        client.print("&&");
        client.print("Available=");
        client.print(available3);
        client.println(" HTTP/1.1");
        client.println("Host: 192.168.137.1");
        client.println("Connection: close");
        client.println();
        client.stop();
       } 
      else 
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
    
    
     if (client.connect(server, 80)) 
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("GET /UpdateAvailability.php?Position=");
        client.print("4");
        client.print("&&");
        client.print("Available=");
        client.print(available4);
        client.println(" HTTP/1.1");
        client.println("Host: 192.168.137.1");
        client.println("Connection: close");
        client.println();
        client.stop();
       } 
      else 
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
     if (client.connect(server, 80)) 
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("GET /UpdateAvailability.php?Position=");
        client.print("5");
        client.print("&&");
        client.print("Available=");
        client.print(available5);
        client.println(" HTTP/1.1");
        client.println("Host: 192.168.137.1");
        client.println("Connection: close");
        client.println();
        client.stop();
       } 
      else 
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
    
    }
}</p>

Step 7: Xampp Interface

In this step we will explore the Xampp interface. Xampp as I previously mentioned has a web server which helps us use PHP, Perl and SQL for making projects. The web server we are using is called as Apache. Find more about Apache in this website :

https://en.wikipedia.org/wiki/Apache_HTTP_Server

In order to create the table, you need to first install Xampp and then start MySQL and Apache. Make sure that those ports are available. You can find an image relating to this in the image bar above this.

Please note that if MySQL does not function properly, then it is probably because of Skype which also uses the same port as MySQL. So you need to right click on the Skype application in the task-bar and click on quit.


So, we need to create a table and then look at how to update the value. Note the word update. We cannot push values to a null (empty) variable. There should be some value in the table in the beginning so that we can change them accordingly later. Please follow these steps to create the table successfully. You may also look at the pictures above for easier understanding.

◘Click on new.

◘Create a database name.

◘Then create a table name. (remember this name as you will need it for the next step)

◘Then you will be given a layout, just enter the name for each row and then choose which datatype you want. Then leave the rest as it is. (the row name will also be very important)

◘Enter the default values and leave em aside.

◘After creating the table, you will see that it has been added to the side bar.

Now that we have successfully created the table, we will move onto the next step. In the next step we will look at the SQL connection code.

Step 8: Adding the SQL Connection

In this page , we will be adding the code which connects to the database. The database will need some code to connect to as it will have a password and a username, so we should connect to the database with those specifications. Here is the code :
*********************************************************************************************************************************Sorry that I am not able to post the code in this instructable. It is not accepting html code, so therefore, please visit this link where my code is available in GitHub. Thanks a lot.

*********************************************************************************************************************************

https://github.com/Akash2002/State-Fair/blob/maste...

The link attached above has the dbcon (database connection) code. Once again, sorry for the inconvenience.

Now that we have written the SQL connection code, let us proceed to get and update the values.

Step 9: Updating the Table

In this step we will go ahead and update the availability of the table so that we can look at the table which will be more easily understandable. We need to first get the values and then update them . Note that you have to use your table name and the names of the rows which was why I had cautioned you guys before. Here is the link to this code : https://github.com/Akash2002/State-Fair/blob/maste...

The only major step to make the whole program function is to add the data to the website. Let us do that in the next step before heading to make an awesome professional looking webpage with Bootstrap.

Step 10: Parking Lot Display

In order to show the values to the user we must select and fetch them from the database. So we can do that with PHP. Showing them to the screen is will not suffice as the user seeing the screen should understand what the program is trying to say. In order to make it decipherable, we must add a tiny bit of bootstrap into it. We will also be adding something called as a glyph-icon , it is a term in bootstrap which you can look into in more depth in this link : http://getbootstrap.com/components/

Here is the GitHub link to my code : https://github.com/Akash2002/State-Fair/blob/maste...

Note that your data display page should look like the one in the image.

Now, the only thing remaining is the fancy part of the whole program which is the main webpage. Let us finish that off in the next step.

Step 11: Fancy Styling

We will be using a lot of bootstrap with this. Bootstrap is a framework by Twitter which is immensely helpful in creating websites. Here are the links to my webpage : https://github.com/Akash2002/State-Fair/blob/maste...

https://github.com/Akash2002/State-Fair/blob/maste...

Note : You have to provide a different IP address for the code. I have a different one and it will not work for you. Use localhost/(name of your webpage) OR try using your IP address.

There are two links attached, you can also take a look at all of my files associated with this project in this link :

https://github.com/Akash2002/State-Fair

Finally ,I would like to congratulate you on completing a pretty difficult and complicated project on the Smart Car Parking System. I would also like you to contact me through my YouTube channel or with the help of the comments in Instructables itself if you have any questions. And please don't forget to vote for my project for Spark-Fun Home Automation contest.