0% found this document useful (0 votes)
35 views72 pages

Smart Parking System Design Report

The document describes a smart parking system project that uses various technologies and components like centralized control, gate control, web server, GSM module, display module, and sensors to detect available parking spots and guide drivers. The design section discusses selecting and implementing these different modules at a low cost to create an effective smart parking solution.

Uploaded by

uocboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views72 pages

Smart Parking System Design Report

The document describes a smart parking system project that uses various technologies and components like centralized control, gate control, web server, GSM module, display module, and sensors to detect available parking spots and guide drivers. The design section discusses selecting and implementing these different modules at a low cost to create an effective smart parking solution.

Uploaded by

uocboy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

See discussions, stats, and author profiles for this publication at: [Link]

net/publication/334561019

Smart Parking System

Technical Report · February 2010


DOI: 10.13140/RG.2.2.28464.92164

CITATION READS

1 463

8 authors, including:

Adeeb Albukhari
The University of Queensland
7 PUBLICATIONS 2 CITATIONS

SEE PROFILE

All content following this page was uploaded by Adeeb Albukhari on 19 July 2019.

The user has requested enhancement of the downloaded file.


KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS
COLLEGE OF COMPUTER SCIENCES & ENGINEERING
COMPUTER ENGINEERING DEPARTMENT

COE 400
SYSTEM DESIGN LABORATORY

Project # 1 Title:
Parking System

Design Report

Prepared for:
Dr. Abdulhafid Bouhraoua

By

AARIF AL-AMRI MOHAMED MOHAMED


ADEEB AL-BUKHARI HUSSAIN MUSLEH
AHMAD AL-GAHTANI SAADADDIN AL-MURAD
ANAS AL-KALAJI SAIYED ALI

Due Date
2010-02-03
TABLE OF CONTENTS
TABLE OF CONTENTS………………………………………………………………….2
LIST OF FIGURES……………………………………………………………………….3
LIST OF TABLES………………………………………………………………………..3

ABSTRACT………………………………………………………………………………4

1. INTRODUCTION………………………………………………………………...4
2. REQUIRMENTS………………………………………………………………….4
3. DESIGN…………………………………………………………………………...4

3.1 SELECTION AND EVALUATION……………………………………..5


3.2 CENTRALIZED CONTROL…………………………………………….9
3.3 GATE CONTROL………………………………………………………10
3.4 WEB SERVER………………………………………………………….10
3.5 GSM MODULE…………………………………………………………10
3.6 DISPLAY MODULE……………………………………………………11
3.7 GUIDANCE MECHANISM……………………………………………12
3.8 CAR MOVEMENT DETECTION SENSORS…………………………12

4. METHODOLOGY……………………………………………………………...13
5. IMPLEMENTATION……………………………………………………………14

4.1 CENTRALIZED CONTROL……………………………………………14


4.2 GATE CONTROL………………………………………………………18
4.3 WEB SERVER………………………………………………………….19
4.4 GSM MODULE…………………………………………………………19
4.5 DISPLAY MODULE……………………………………………………21
4.6 GUIDANCE MECHANISM……………………………………………21
4.7 CAR MOVEMENT DETECTION SENSORS…………………………22

6. SOCIETAL IMPACT AND CONTEMPORARY ISSUES……………………24

6.1 SOCIATAL IMPACT…………………………………………………..24


6.2 RELEVANT CONTEMPORARY ISSUES……………………………25

7. CONCLUSIONS……………………………………………………………….25

REFERENCES……………………………………………………………………..27
APPENDICES………………………………………………………………………28

2
LIST OF FIGURES:

Figure1: MaxSonar-EZ1
Figure2: The inputs and outputs to and from the processing unit (the
interaction with the different modules)
Figure3: an example of a wooden gate.
Figure4: the integration of the GSM module and the mobile network
operator
Figure5: 7-segment display
Figure6: example of colored LEDs.
Figure7: PC/Microcontroller Serial Interface
Figure8: Sensors-Microcontroller & Central-Microcontroller Parallel
Interface
Figure9: Gate Module Pin Configuration
Figure10: Guidance Module Pin Configuration
Figure11: Sensing Mechanisim
Figure12: System Methodology

LIST OF TABLES:
Table 1: Web Server Prices
Table 2: GSM Prices
Table 3: Sensors Prices
Table 4: Display Information
Table 5: Sensors Distribution

3
ABSTRACT:
This project is mainly an implementation of a smart parking system. This system
consists of many blocks acting with each others using modern technology
communication to send, receive and process data. A microcontroller was used as the
central unit of the system and ultra sonic sensors were used to collect data regarding
the status of the parking system. Also, there is a web server used to display the status
of the parking system as well as a GSM module to use SMS massages for reservation
and status notification.

1. INTRODUCTION:
In an increasingly demanding world, the waste of time can have a serious effect on
people and business. The increase in population along with car production business
expanding has led to major cities in the world suffering from rush-hours where
parking has become a serious issue to face everyday. Many drivers find themselves
turning around in a parking lot and wasting their time without being able to find an
empty spot to park. A driver would be thankful to know in advance if the parking lot
is full. The driver will also appreciate a guidance mechanism that will guide him to
the nearest free parking line. This saves time and increases productivity for different
sectors including education.

The aim of this project is to solve the mentioned problem using means of technology
considering the ease of deploying such a system in real life. Also, additional features
such as remote reservation and getting information about the status of the lot from the
web are introduced.

2. REQUIRMENTS:

The requirements for this system are:


1. Informs drivers about availability/unavailability of parking spot using
modern communication means.
2. Guide the driver through some sort of indicators to the nearest parking spot.
3. Always maintain the list of available spots and their location.

3. DESIGN:
We decide as a solution to the proposed problem to implement a Smart Parking
System. The system will be able to provide how many slots are empty and decide the
nearest parking slot and provide reservation system. In order to achieve these
requirements our system will consist of the following components:

4
a) Centralized control: responsible for data processing and giving orders
to the other components.
b) Gate control: responsible for closing and opening the gate based on the
information provided by the central unit.
c) Web server: for the ability to check the status remotely.
d) GSM module: for the SMS service that will allow reservation and
checking the status of the lot.
e) Guidance mechanism: to guide the driver to the nearest line containing
the empty slot.
f) Car movement detection sensors: to count the number of cars in each
line of the lot.
g) Display module: to display the number of available empty slots at the
entrance.

3.1 SELECTION AND EVALUATION:


3.1.1 Centralized control:
The system is quite dynamic and includes different features and technologies.
Therefore, it should have enough processing capability and be easy to interface to
different modules to run properly.
Given the above conditions we choose the microcontroller to be: Rabbit
microcontroller (RCM 4200 RabbitCore).
Regarding the computer we choose a desktop over a laptop because of its higher
processing capability in addition to its lower cost.
Assuming a regular PC with a cost of 3000 SR, RCM 4200 development kit for
around 1000 SR ($269 on [Link] website), and a MAX233CPP voltage converter
from PC to Microcontroller and vice versa (12V for PC and 5V for Microcontroller)
for serial connection at a price around 30 SR ($7.45 on [Link]), the
total cost for this module is around 4030 SR.

3.1.2 Gate control:


We chose to use a wooden gate rather than ironic for 3 main reasons:
 Cheaper.
 Safer for cars and drivers.
 More practical: no corrosion effects.

The stepper motor used for the prototype is SMCC-547 at a cost around 30 SR
(240 Thai Baht = $7.24 from [Link]). This motor allows 18 degree rotations and
thus 5 consecutive rotations are needed to close the gate (90 degrees/18 degrees =
5). Also, a decoder dual 2-line to-4 line (SN74155N at a cost of $50/186 decoders
sold in quantity from [Link] = $0.27 = 1 SR) is used. So, the total
cost is around 31 SR.

3.1.3 Web server:

5
The well-known Apache is a good choice for a web server for many reasons. It is free
and requires no licensing. It is widely deployed with an active support and
development. It is also relatively easy to use. This reduces cost and system
development time.

Since the web server is mainly used for displaying current system status and is not
connected to a DBMS (Database Management System), using HTML for the website
is going to be sufficient for this purpose. HTML also allows for embedding high level
language applications (e.g. Java applet). This could useful to fetch current system
status information from the microcontroller, since HTML cannot be used to read data
using a serial interface/Ethernet. The figure below is an example of the to-be-
developed GUI.

Requirement Purpose Cost


Domain Name Because it is difficult to We used free service – but
remember the IP address there are services that
of the Web Server requires subscriptions
Personal Computer with Used to host and run the ~ $150
Network Interface Card Web Service
Internet Connection Internet connection to ~ $80 per month
make the site available for
public or normal Network
connection for private use
Internet Router Because we need to $200
forward the incoming
traffic to a specific
machine in the network
Dynamic DNS service Because global IP address We used free service – but
keep changing every there are services that
amount of time and we requires subscriptions
need to forward the traffic
coming from the domain
name to the IP address of
the PC hosting the website
PHP support Needed to support PHP Usually comes with
web pages and its libraries “Apache” in the same
on the web server package
Table 1: Web Server Prices

3.1.4 GSM module:


GSM modems can be a quick and efficient way to use for sending and receiving
SMS, because a special subscription to an SMS service provider is not required. The
mobile operator charges for this message sending and receiving as if it was performed
directly on a mobile phone.

6
In general, a GSM modem is recommended for use with a computer over the mobile
phones to send and receive messages. This is because some mobile phones have
certain limitations comparing to GSM modems. Some of the limitations are described
below:

 A mobile phone may not support some AT commands, command


parameters and parameter values. For example, some mobile phones do
not support the sending and receiving of SMS messages in text mode.
Usually GSM modems support more complete set of AT commands than
mobile phones.

N.B: AT commands are instructions used to control a modem.

 Most SMS messaging applications have to be available 24 hours a day. If


such SMS messaging applications use mobile phones to send and receive
SMS messages, the mobile phones have to be switched on all the time.
However, some mobile phone models cannot operate with the battery
removed even when an AC adaptor is connected, which means the battery
will be charged 24 hours a day, which shorten the life time of the battery
as well as the cell phone.
 It should also be noted that not all modern phones support the modem
interface for sending and receiving SMS messages. In particular, most
smart phones, including Blackberries, iPhone, and Windows Mobile
devices, do not support this modem interface at all.

However, there is not much difference between mobile phones and GSM modems in
terms of SMS transmission rate, since the determining factor for the SMS
transmission rate is the wireless network.

Even though GSM USB modems are less expensive (under $100) and in many cases
are faster than serial GSM modems, the serial connection RS232 is recommend for
the connection between GSM modem and the centralized control module for various
reasons. Mainly, the configurations and the initializations of RS232 for
communication are much easier and more flexible than the sophisticated ones used
for USB. For the sending rate, the performance of our system is not affected by the
connection speed between the computer and GSM modem i.e. the SMS sending rate
is about the same no matter the GSM modem is connected to the computer through a
serial cable or USB cable. This is because the determining factor for the SMS sending
rate is the wireless network as mentioned earlier. Furthermore, USB products are
being used now frequently in the industrial, but for our small project it is more than
enough to use RS232 for this simple communication.

Prices:

Item Price
GM862 Evaluation Board - RS232 $74.95
GM862 Cellular Quad Band Module $99.95

7
Table 2: GSM Prices

3.1.5 Display module:


7-sement displays were chosen over LCDs for the following reasons:
 Availability.
 Visibility.
 Cost.
 Uncomplicated Multiplexing.

This module needs a BCD to 7 segment decoder (74LS48 at a price of $1.315 = 5 SR


from [Link]) along with a 7-segment display (LTC-4727 at a price of
$2.35 = 10 SR from [Link]). So, the total cost of this module is around
15 SR.

3.1.6 Guidance mechanism:


In guidance mechanism, we had to choose between two methods: a monitor that
guides the driver to the nearest empty line by displaying the full map of the parking
lot or using LEDs to guide the diver. We chose the LEDs option for the following
reasons:

 LEDs option is cheaper than using the monitor option and it is easy to
understand.
 In maintenance, LEDs can be replaced easily.
 LEDs option is easier to implement.
 Monitor option may cause congestion at the entrance of the lot since a driver
may need some time to look at the monitor.

Since the prototype contains only two lines, only two LEDs are needed. The cost is
negligible ($0.03 for each LED from [Link]).

3.1.7 Car movement detection sensors:

Figure1: MaxSonar-EZ1
The sensor is going to be used is MaxSonar-EZ1 (figure7)and will be connected
to another microcontroller. The properties of the sensor are:

 Offers very short to long-range detection and ranging


8
 Small package with ultra low power consumption.
 Detects objects from 0-inches to 254-inches (6.45-meters).
 Provides sonar range information from 6-inches out to 254-inches with 1-inch
resolution.
The interface output formats included are pulse width output, analog voltage output,
and asynchronous serial digital output.
Pricing:
Item Price
Rabbit 5000 with MiniCore $99
RCM5700
5 LV-MaxSonar EZ0 (EZ1) 5 * $25 = $125
2 2-1 multiplexers (74157) 2 * $1.65 = $3.30
Inverter (7404) $0.29
Table 3: Sensor Prices

3.2 Centralized control:

Inputs from the modules: Outputs to the modules:


-Display module
-Car movement -Guidance mechanism
detection sensors -Gate control
-GSM module Centralized -GSM module
Control Unit -Web server

Figure2: The inputs and outputs to and from the processing unit (the interaction
with the different modules)

The Centralized Control Unit is the brain of the system. All the other modules are
connected to it. Mainly, it consists of two parts: a microcontroller and a computer that
connect through a serial link. It receives different inputs from some modules, keeps
track of any changes in the system, analyzes this data, and executes different
algorithms to process it and outputs commands and data to other modules
accordingly. This unit will be flexible and generic. Flexible in terms of accepting
changes in the size of the system, i.e. it can adapt to any reduction or increase in the
size of a parking lot. Generic in terms of, for example, no matter what type of sensor
or what web server is used it can function flexibly.

9
3.3 Gate control:

Figure3: an example of a wooden gate.

In order to make it easier for the driver to know the availability of a free parking slot
in the parking area, a gate will be placed in the entrance. This gate will be closed only
when all the parking slots are full or reserved. In our project we make this gate a
wooden gate that is controlled by the centralized control unit (the microcontroller).
The connection between the gate and the centralized control unit is going to be wired.
Figure2 illustrates an example of a wooden gate.

3.4 Web server:


The main purpose of adding this feature is to facilitate user access to parking lot
information (reserved and free slots) remotely. A user can then decide to make a
reservation using SMS from his mobile phone (refer to GSM module). The web
server has to be able to do the following:
 Display total free, total occupied, and total reserved slots along with an
up-to-date display of system states based on current information in the
centralized control module.
 Update the information displayed once a change happens in the system
status or every short period of time regularly.

The website will display the total free slots, total occupied slots and the total reserved
slots.

3.5 GSM module:


A GSM modem is a wireless modem that works with GSM/GPRS wireless networks.
It is a specialized type of modem which accepts a SIM card, and operates over a
subscription to a mobile operator, just like a mobile phone. It exposes an interface
that allows applications on PCs to send and receive messages over the modem
interface as if it was performed directly on a mobile phone. The GSM modem can be
a dedicated modem device, or it may be a mobile phone that provides GSM modem
capabilities. There two ways to connect a mobile phone or GSM modem to a
computer. For example, they can be connected through a serial cable or a USB cable.

10
The chief goals of employing GSM modem module in this project are to aid the
driver to be acknowledged with the parking details as well as to implement his
request remotely i.e. while he is at home or driving to the parking and before his
arrival. The former is achieved by sending the word status by the driver as an SMS to
the GSM modem, and based on the information stored in the centralized control
module the following will be sent to the driver’s mobile phone: Total free, total
occupied, and total reserved slots.

The latter role is accomplished through the reservation feature. Whenever the driver
is willing to occupy a free parking slot, he just sends the word Reserve to the GSM
modem and the system will update the centralized control module accordingly. For
instance, the number of free slots will be decremented and the number of reserved
ones will be incremented. Finally, there is a case which the driver must send the word
open to the GSM modem so that he can enter the parking lot. This is when the
parking slots are all full and he has already reserved a free parking slot, because in
this case the gate will be closed. The following picture demonstrates the integration of
the GSM module and the mobile network operator.

Figure4: the integration of the GSM module and the mobile network operator

3.6 Display module:

Figure5: 7-segment display

The display module is the main interest of a driver upon reaching the parking lot. The
module will show how many available slots in the parking in total (in 7-segment display,
figure4 shows an example of 7-segment display). An example of the display will look
similar to the following table:
11
7-Segment Available Lots
Main 5
Table4: display information

3.7 Guidance mechanism:

Figure6: example of colored LEDs.

Guiding the driver to the nearest parking slot will save a lot of time and will not
congest the parking lot with many drivers looking for a parking slot. For this
reason, our system will be featured with guidance mechanism. The driver will be
guided by some LEDs that will guide him to the nearest line that has the empty
parking slot from the entrance of the parking lot. LEDs will be turned ON and
OFF by connecting them to the microcontroller, which will decide which one
goes ON or OFF according to the status of the lines. Figure5 shows an example of
colored LEDs.

3.8 Car movement detection sensors:


In our project we are going to use several sensors to detect the cars whether
entering or leaving the parking system in order to determine the number of
available cars in each line of the lot. We decided the position of the sensors as
follows:
 Two sensors in each line (one sensor for entrance and one for exit from the
line (Parking Line).
 Two sensors: one at the entrance gate and one at the exit gate.

12
4. METHODOLOGY:

Figure 12: System Methodology

13
5. IMPLEMENTATION:
5.1 CENTRALIZED CONTROL:

As previously mentioned in the report, the centralized control is the brain of the
system which takes inputs from detection sensors module & GSM module to update
its state which in turn affects gate module, web server module, display module, and
guidance mechanism.

Since the centralized control module is composed of three main parts, the
implementation steps can be divided into:

 Serial Interfacing & Parallel Bits (Representing Sensors):

The main purpose of serial interfacing between the microcontroller and the PC is to
create an entity (PC) that can communicate with GSM and Web Server modules
easily using sockets without putting any burden on the microcontroller. So, the
microcontroller is still the brain of the system whereas the PC can be thought of as the
two way road that the microcontroller, GSM, and Web data move on.

This part started by testing the MAX233CPP chip which converts the microcontroller
5V data to PC's 12V data and vice versa. The following is a detailed diagram that
shows how the connection is done:

Figure 7: PC/Microcontroller Serial Interface

14
What happens here exactly is that the microcontroller uses Serial D pins (PC0 to
transmit and PC1 to receive) to communicate with PC through MAX233CPP. Some of
the pins (10 & 16, 11 & 15, 12 & 17) have to be connected together by default for the
proper operation of MAX233CPP. The PC communicates through serial pins (2 to
receive and 3 to transmit) with MAX233CPP's pins 4 (RX) and 5 (TX).
This block has been tested using the windows hyper-terminal with a dummy Dynamic
C application that simply uses the functions serDgetc() to get a character through serial
D and serDputc(X) to send a character X through serial D.
Regarding the PC/Microcontroller serial interface protocol, it works as follows:
 If PC sends character '0' which is a status request, microcontroller
responds by sending two bytes (1st one representing total free parks and
the 2nd is by default 0).
 If PC sends character '1' which is a reservation request, microcontroller
responds by sending two bytes (1st one representing total free parks and
the 2nd is either character 'E' if reservation is granted or character 'F' if
reservation is denied because total number of allowed slots for reservation
which is by default half of total slots is at its maximum).
 If PC sends character '2' which is an open gate request – can only come
from GSM module, microcontroller opens the closed gate and responds by
sending two bytes (1st one representing total free parks and the 2nd is by
default character 'G'). This is assuming that no open gate request would be
received by microcontroller for a non-authorized user (checked by GSM
module).
 If PC sends character '3' which is a reservation cancellation request, the
microcontroller decrements its number of reserved parking slots and does
not respond with anything.
During the integration part, since the sensing unit used three serial ports (namely B, C,
and D), and because many problems were faced in using more than these serial ports on
one microcontroller, it was decided to use two microcontrollers. One microcontroller is
dedicated for sensors and another one for this central unit. The sensors-microcontroller
outputs 5 bits (representing five sensors) to the central-microcontroller and receives 1
flushing bit in order to flush the values of the pins representing 5 bits that indicate a
sensor signal (0 for no car detection at the specific sensor and 1 for a single car
detection).
This is why there was a need to dedicate 6 ports (PB3-PB7) on central-microcontroller
to receive parallel data of sensors from sensors-microcontroller, and PB2 to send a
flushing signal during each iteration of the central unit sequential program to allow for
new signals to arrive (the central microcontroller detects the change in consecutive 1s
to update its counters since sensor unit sends a lot of 1s for 1 signal while the object is
still in front of the sensor).

15
Figure 8: Sensors-Microcontroller & Central-Microcontroller Parallel Interface

 Microcontroller Programming:

This part contains the code of the brain of the system which is responsible for running
everything. There were two approaches that have been thought of:
 Using a sequential program that initializes required ports and then enters an
infinite while loop which executes several procedures that are needed in every
iteration.
 Using the concept of threads to keep things running in parallel.
 The choice was to use a sequential program for the following reasons:
 It has been tested that Dynamic C behaves differently than other high level
languages like C++ or even C in certain aspects (for example there are
problems nesting three block one inside another … etc). Therefore, the
concept of threading might have created problems that could not be debugged
easily.
 For this system's purpose, there is no problem running sequentially and
keeping serial buffer receiving while the program is doing something else
because the data flow is not so fast to overflow the buffer.
 The program runs fast enough to display two LEDs representing two lines
guidance such that the human eye cannot detect any change in the lighted
LED.
Therefore, the program used is sequential (see the code in Appendix 1) and does the
following:
Initializes all variables needed and serial/parallel ports to be used in at the
beginning of the main procedure.
Enters an infinite while loop which reads sensors signals from sensors-
microcontroller, updates its local counters which represent number of cars
in the parking lot (and for each line), updates gate, guidance, and display,
and then does the serial communication followed by sending a flush signal
to the sensors-microcontroller through pin PB2 at the end of the iterations.

The explanation of code for updating gate, guidance, and display is available in
the next sections of the report.

16
 PC Programming:

The PC is an intermediate point in the communication between GSM /Web


modules and the central microcontroller. C++ is chosen as the language for the
PC program responsible for facilitating communication between microcontroller
and GSM/Web for the following reasons:
 Unlike other high level languages (i.e. Java or C#), C++ provides the good
features of a high level language along with providing the programmer the
ability to gain more control over the program. This is important because
the programmer needs to know exactly what is going on in each buffer or
piece of code especially since there is a connection with the low-level
microcontroller.
 Availability of tested pieces of code for socket connections (for
communication with GSM and Web) and serial connection for reading &
writing (for microcontroller communication).

UDP is used for the socket communication between PC and Web/GSM because it
requires no connection establishment. Although UDP is known to allow for no
compensation of lost packets, it is not a problem in this case since GSM, Web,
and PC application are all in on PC and running as different processes
communicating through sockets. The code used is available in Appendix 2

The main scenario of the system is clarified by explaining this code. The program
starts by initialization of serial connection parameters, sockets, and variables.
There are two sockets to be opened for sending: one for the GSM and the other
for the web server. The web server simply receives from PC central application
and therefore it is just updated with any changes regularly. However, GSM sends
and receives through sockets and therefore the central application is supposed to
listen to incoming data. The protocol between the central application and GSM is
the following:
 If GSM sends character 'X', application sends a status getting request
through the serial connection (sends '0' to the microcontroller), and
then upon receiving required data, it is sent to the GSM using its
socket.
 If GSM sends character 'R', application sends a reservation request
through serial connection (sends '1' to the microcontroller), and then
upon receiving this, the microcontroller arranges a reservation for this
user.
 If GSM sends character 'O', application sends an open gate request
through the serial connection for an available reservation (sends '2' to
the microcontroller), and then upon receiving this, the microcontroller
opens the gate and deletes one reservation for the corresponding user.
 If GSM sends character 'C', application sends a reservation
cancellation request (which the GSM sends if the user who reserved
has not arrived after a certain time) through the serial connection
(sends '3' to the microcontroller), and then upon receiving this, the
microcontroller simply updates its status by deleting a reservation.
17
The protocol works such that there is always data being sent between GSM and
the central application (along with web being updated in every such iteration) for
two purposes:
 To always update GSM and Web with most recent data.
 To prevent busy waiting while being idle waiting for a data to be
received on the socket (since no threads are used and the program runs
sequentially). This helps both central application and GSM to always
continue updating data while not locking the code execution.

5.2 GATE CONTROL:

The gate is mainly composed of a stepper motor (SMCC-547) and a decoder 2- to


4-line decoder (SN74155N). The procedure updateGate(int x) in the central
microcontroller code (see the code of procedure updateGuidance() in Appendix1
1) does the following:
 It either receives 1 (which indicates close gate) or 0 (which indicates
open gate.
 If close gate is requested, it sends 11, 01, 10, 00 to PD0 and PD1
which are connected as select signals for the decoder respectively.
 If open gate is requested, it sends 00, 10, 01, 11 to PD0 and PD1
which are connected as select signals for the decoder respectively.

This simply closes the gate with rotation of 18 degrees to one side if close gate
request is initiated and to the reverse side if open gate request is initiated by 18
degrees * 4 = 72 degrees total change for each case (open/close).

Note that between each consecutive writings to port PD0 and PD1 (i.e. 11, 01
…etc) there is a million iteration of code (outer loop of 1000 and inner loop of
1000) just to allow for the motor to interpret the data and does the mechanical
change accordingly. The following figure shows the pin configuration for the
gate, decoder and microcontroller:

Figure 9: Gate Module Pin Configuration

18
5.3 WEB SERVER:

We used a normal PC and converted it to a Web Server by using special software


called “Apache Web Server”. Regarding to the cost, the data to be displayed in the
website are just numbers.
Note that the microcontroller is connected to the same PC that is running the Web
Server and it sends the data to the PC through loopback connection using UDP as the
transport layer.

Parking status websites written using PHP language. Data received from the
microcontroller will be processed in this PHP web page. PHP website is executed on
client machine when it requests the web page. So, whenever the user request the PHP
web page (parking status) the page will be executed on his machine and the code of
the PHP will be in listening mode. It will be waiting for the data from the
microcontroller. The microcontroller is always sending the data to the Web Server
through loopback connection using UDP as the transport layer. So, the Web Server
will get fresh data about the status of the parking when needed at any time.

The code is combination of HTML and PHP and it shows how data are received and
processed in parking status PHP Web Page. In appendix3.

Regarding to the problem of the dynamic IP address of the internet, the web server
has a special script that is used to send the changed IP address to the dynamic DNS
server every time it changes. So, our domain name is pointing to the right IP address
of our web server. However, we are using a domain name ([Link])
because it is easier to remember and it is difficult to memories the dynamic IP address
of the web server.

Finally, we need to have access to the router that we are getting the internet
connection from. Because when user requests the status of the parking the request
goes to that router through port 80 and we need to forward this request to our web
server to reply to the received requests.

5.4 GSM MODULE:


Since the GSM modem is connected serially with the PC, some parameters need
to be initialized initially. For example the COM port, the data bits, the baud rate,
the parity bit, the stop bit and the flow control are all set up using values specified
in the GSM modem data sheet. Also, the GSM modem itself should be initialized
via the so called AT Commands so that it works properly. Instances of the
required parameters are: the message center number, the PIN and the mobile
number of the SIM card inserted in the in the modem, the sending mode whether
it is a text mode or a call mode, and the receiving mode i.e. the way the GSM
receives an SMS message and how it informs the PC accordingly. All of these
19
issues are done and initialized at the beginning of program. See appendix4 for the
AT command and the code.

Next, the program enters an infinite loop polling and listening to the serial port to
read if the GSM sends something or not. In case of receiving new SMS, the GSM
sends the following to the PC: +CMTI: “SM”, N where N is the SMS number in
the GSM storage area. To read that message, the following is sent to the GSM
modem: AT+CMGR=N. Consequently, it sends me the sender mobile number, the
date and the time, and the message itself as shown here:

+CMGR: "REC READ","+966xxxxxx",,"04/08/28,[Link]+40"


This is the new SMS received by the GSM modem

Therefore, the program always checks for specific words sent by GSM modem
like +CMTI and +CMGR and based on that, it does specific actions. For instance,
whenever the PC receives +CMTI, it directly does a String manipulation to take
the included message number N and then the request (AT+CMGR=N) to the GSM
to send back the SMS it has just received, as mentioned above. Moreover, when
the PC receives +CMGR, it does also a String manipulation to take the included
sender mobile number as well as the original message. After that the program
completes its execution normally and checks again if something is received by the
serial buffer.

Now the SMS sent by the user is checked to see what actions to be done by the
program e.g. sending the user back the requested information or updating the
main controller. Actually UDP socket programming is used for the
communication between this program and the main controller program. In the idle
state, the letter X is always being sent to the main controller to send me back the
status of the parking lots. So if the user has requested the status of the parking, the
program sends him back immediately. However, in case of receiving the word
Reserve by the user, the letter R is sent instead to the controller to check the
possibility of reservation and to update its status accordingly. Furthermore, the
letter O is sent to tell the controller to open the gate in case if the user has
reserved earlier and update its status too. The reasons of continuously sending the
main controller in the idle state are mentioned above in the PC programming
section.

Initially, the program was programmed in C# language and almost everything was
O.K and went smoothly. However, there was a problem when the user sends the
word Open. What was happening is that the program was reading this word twice
as if two users were sending at the same time. Consequently, actions done by the
main controller were executed twice and it was being updated by wrong
information. Two months were spent on this and the problem was not figured out.
Therefore, the program was written again perfectly in C++. The power of coding
using this language is that you know exactly what is going on in your program
and especially in the serial buffer, because sometime the GSM modem sends
rubbish to the PC and the manipulations then get messed up if you don’t deal with
this perfectly. This was not easy using C# as everything has been dealt with as
20
strings. On the other hand, C++ doesn’t have the capabilities as C# as it is
considered a low level language, the way it enabled sending character by
character and reads and sends the only needed commands and characters and
ignores anything else. Everything worked fine and that problem happening
initially was gone and it has never seen after coding in C++. Finally, I was
convinced that any low level application, like sending and receiving serially,
should be programmed in low level languages like C or C++.

5.5 DISPLAY MODULE:


The display module is used to allow the driver to get to know the total free slots in
the whole parking area upon arriving at the gate. A BCD to 7 segment decoder
(74LS48) is needed to be connected to the 7-segment display (LTC-4727) so that
it stores the representation value and feeds it to the 7-segment always although the
microcontroller sends the value once. Three bits are sent from the microcontroller
to the 74LS48 chip which in turn interprets its value and displays the
representation on the LTC-4727 chip. These bits are taken from PE0, PE1, and
PE2 (where PE2 is highest, then PE1, then PE0 lowest). The following figure
shows the pin configuration of this module:

Figure 10: Guidance Module Pin Configuration

5.6 GUIDANCE MECHANISM:


The guidance mechanism (for the prototype containing two lines) is done using
two LEDs which work in the following way (see the code of procedure
updateGuidance() in Appendix 1):
 Initially, when the parking lot is empty, always let LED of line 1 to be on
guiding the driver to line 1.
 If line 1 becomes full, turn off LED of line 1 and turn on LED of line two,
guiding the driver to park in line 2.

21
 if both lines are full, simply turn off both LEDs to reduce power
consumption while the gate is already closed and there is no need to guide
any driver for sure.

Two pins (PD2 and PD3) are used to turn on the LEDs by writing 1 to the pin in
every iteration. Note that the signal is not buffered but it is just being fed to the
LED in a very fast during each iteration such that the eye cannot detect the any
change.

5.7 CAR MOVEMENT DETCTION SENSORS:

The sensor team had the objective of detecting a car that entered the parking lot and
which lane it stopped. The sensors used to detect the presence of are LV-MaxSonar
EZ0 and EZ1. The EZ0 has more sensitivity than the EZ1. This sensor has three
interfaces: serial, analog and Pulse Width. Each gives a reading every 49mS. That
means, in every second the sensors sends at most 20 readings. In addition, the sensor
has a pin to enable its output.
In the project, five sensors were used distributed in the parking as described in the
following table:
Sensor Location
1 Lane 1 entrance
2 Lane 1 exit
3 Lane 2 entrance
4 Lane 2 exit
5 The entrance gate
Table 5: Sensors Distribution
At the beginning, only one sensor was interfaced to the microcontroller (Rabbit
5000). The pulse width interface was used first. The interface uses the factor 147µS
per inch. The problem faced was how to measure the time that this signal is kept high.
Cascaded counters from the microcontroller were used but the results were no as
expected.
The second interface used is the serial interface. As given in the MaxSonar datasheet,
the baud rate is 9600, 8 bits, no parity, with one stop bit. These are the settings used
to initialize the serial port in the microcontroller. The output of the serial interface is
an ASCII capital “R”, followed by three ASCII character digits representing the range
in inches up to a maximum of 255, followed by a carriage return (ASCII 13).
Therefore the mission was to detect the ASCII code for “R”.

To test the functionality of the sensor, it was connected directly to the COM1 serial
port of a PC. Then use HyperTerminal to see the output of the senor. Sample result is
(R008). That means, the distance to the closest object is 8 inches. This was a point of
satisfaction for the team.

When the sensor was connected directly to the microcontroller, the output was not as
expected. It contained many zeroes and some strange characters. This was fixed by
inverting the output of the sensor before going into the microcontroller. The problem
was that sensor and the microcontroller use different levels to represent logic 0 and
22
logic 1. Therefore, the microcontroller was not able to detect the start bit of the
sensor’s serial data.

The next step was how to get the data of all sensors to the microcontroller. An
assumption made is that a car takes about a second to pass over the sensor. Based on
that, all sensors should be swept in this interval of a second. Using one serial port,
only four readings from each sensor can be taken. This was not enough because some
readings were erroneous. Therefore, more serial ports were used.

Another approach was to connect each sensor on a separate serial port. This was
supported by the microcontroller which has six serial ports (serial A – F). Each serial
port (except serial port A) was functioning properly alone but when all are connected
together only three ports were performing well (Serial B, serial C, serial D).

To compromise, one sensor (sensor 5) was connected to one serial port and the other
four were divided into two groups. Each group contains two sensors representing the
entrance and exit of a lane. By this configuration, the number of readings per second
increased from four to ten for the multiplexed sensors and twenty for the non-
multiplexed one. More number of readings led to better accuracy.

Sensors 5, 2 and 4 are always on sensors because at any time a car is expected to
enter or leave the parking. Sensor 5 (the gate sensor) was elected to be the non-
multiplexed sensor while sensors 1 and 2 (entrance and exit of lane 1) are connected
to a multiplexer and 3 and 4 (entrance and exit of lane 2) are multiplexed. Switching
between 1 and 2 (as well as 3 and 4) will not occur until a car passes through sensor
5. After the car enters one of the two lanes switching will stop. This is illustrated in
the following figure:

23
Figure 11: Sensing Mechanisim

The detection mechanism is based on majority of the reading taken from a sensor.
The MaxSonar interprets an object in the range of 0 to six inches as six inches “006”.
For sensor 5 (and 2 and 4 if no switching), twenty reading are taken. If six of them are
“006” then it’s a hit. For 1 and 3 (and 2 and 4 if switching), ten readings (sum is 20/s)
are taken and the same rule applies.

Communication between the microcontroller driving the sensors and the main
microcontroller is done through parallel port B. If a sensor i detects a car it sets the
corresponding bit in port B (PBi) to 1. These bits are reset back to 0 when the main
microcontroller acknowledges the reading by setting the flush bit. Code is shown in
appendix5.

6. SOCIATAL IMPACT AND


CONTEMPORARY ISSUES:
6.1 Societal Impact:

Our project will be very convenience to the society since it helps in saving time
and effort to find a free parking slot specially in large lots which can be found in
Malls, Parks or Airports. Furthermore, it helps in saving money!

24
Driving your car for sh
shorter
orter distance will reduce the amount of fuel consumption,
which means less money to pay for fuel. Not only that, the user friendly way of
displaying the status of the parking lot using cell phones, web site, or the monitor
at the entrance will make lookin
lookingg for a free parking slot funny rather than a
headache.

6.2 Relevant Contemporary issues:

One of the most popular and important contemporary issue nowadays is How to
‘Green’ the environment. This project can be considered as a part of the solution
to this issue.
sue. Our project will help in decreasing the amount of harmful cars
emissions such as CO2, which will cause a lot of bad effects in our environment
see figure3.

Less time driving to find a free slot

Less amount of fuel to consume

Less amount of harmful emissions to the environment

CONCLUSION:

At the end of this project we learned a lot of concepts and skills that an engineer must
have in order to design and implement projects in an efficient way. Some of these
skills are team work, troubleshooting and solving problems with different solutions
and earned the ability
ity to choose the best fitting solution. Also, we have become able
to analyze problems and determine the required equipments to achieve each task.
Moreover, skills in writing specifications, design and implementation reports were
earned. One of the most important
portant skills learned is the ability of working on projects
with specified requirement and deadlines. The project has introduced us to a lot of
technologies that can be merged in order to build larger projects; some are GSM
25
module, microcontroller and sensors (Ultra sonic). This gives us the opportunity to
learn deeply about these technologies. Finally, the project was working according to
the specified requirements and a prototype is being built for a demo of the project
functionality.

26
References:
1) Max233CPP datasheet.
2) SN74155N datasheet.
3) SMCC-547 datasheet.
4) LTC-4727 datasheet.
5) 74LS48 datasheet.
6) COE400 Lab Manual.
7) Rabbit 3000 Microprocessor User Manual.
8) The TTL Data Book
9) [Link] - The C++ Resources Network
10) msdn - Windows API Reference
11) [Link] – php manual.
12) GM862 EVK V3RS-232 Evaluation Board datasheet.
13)
[Link]
14)
[Link]
15) [Link]
16) [Link]
17) [Link]
18) The MaxSonar®-EZ1™ High Performance Sonar Range Finder Data
[Link]
19) The MaxSonar®-EZ0™ High Performance Sonar Range Finder Data
[Link]
20) MiniCore™ RCM5700 Module Data [Link]
21) Rabbit® 5000 Microprocessor User’s Manual

27
APPENDICES

APPENDIX1:
Central Unit Microcontroller Code

#class auto
#define BD_RATE 2400

int totalFree;
int totalFreeL1;
int totalFreeL2;

char i;
char digit1;
char digit2;
long t;

int k;
int l;
int m;

int flushCounter;

int gateState;

int sensor1;
int sensor2;

int s1;
int s2;
int s3;
int s4;
int s5;

int s1prev;
int s2prev;
int s3prev;
int s4prev;
int s5prev;

int iflag;
int iflag2;

int counter;

int d1;
int d2;

int maxFree;
int expectedCar;
int crap;

//void scanSensors();
void updateGate();
void updateDisplay();
void updateGuidance();
void sndRcvSerial();
void scanSensors(int);

main()
{

28
// initialize serial
serDwrFlush();
serDdatabits(PARAM_8BIT);
serDflowcontrolOff();
serDparity(PARAM_NOPARITY);
if(serDopen(BD_RATE)==0)
printf("failed...!\n");
//serDrdFlush();

maxFree=6;
sensor1=0;
sensor2=0;
s1prev=0;
s2prev=0;

totalFree = maxFree;
totalFreeL1 = (maxFree/2);
totalFreeL2 = (maxFree/2);

gateState = 0;

flushCounter=2;
expectedCar=0;

////BitWrPortI(PBDDR, &PBDDRShadow, 0, 5); //car in


////BitWrPortI(PBDDR, &PBDDRShadow, 0, 6); //car out
////BitWrPortI(PBDDR, &PBDDRShadow, 1, 7); //gate

////BitWrPortI(PBDR, &PBDRShadow, 0, 5);


////BitWrPortI(PBDR, &PBDRShadow, 0, 6);
////BitWrPortI(PBDR, &PBDRShadow, 0, 7);

// Gate ports
BitWrPortI(PDDDR, &PDDDRShadow, 1, 0);
BitWrPortI(PDDR, &PDDRShadow, 1, 0);

BitWrPortI(PDDDR, &PDDDRShadow, 1, 1);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);

// parallel bits
BitWrPortI(PBDDR, &PBDDRShadow, 0, 3); //Input1
BitWrPortI(PBDDR, &PBDDRShadow, 0, 4); //Input2
BitWrPortI(PBDDR, &PBDDRShadow, 0, 5); //Input3
BitWrPortI(PBDDR, &PBDDRShadow, 0, 6); //Input4
BitWrPortI(PBDDR, &PBDDRShadow, 0, 7); //Input5
BitWrPortI(PBDDR, &PBDDRShadow, 1, 2); //Out1

//BitWrPortI(PBDR, &PBDRShadow, 1, 2);////////////////////


//BitWrPortI(PBDR, &PBDRShadow, 0, 2); ///////////////////

BitWrPortI(PBDR, &PBDRShadow, 0, 3);


BitWrPortI(PBDR, &PBDRShadow, 0, 4);
BitWrPortI(PBDR, &PBDRShadow, 0, 5);
BitWrPortI(PBDR, &PBDRShadow, 0, 6);
BitWrPortI(PBDR, &PBDRShadow, 0, 7);
BitWrPortI(PBDR, &PBDRShadow, 1, 2);

// Guidance
BitWrPortI(PDDDR, &PDDDRShadow, 1, 2);
BitWrPortI(PDDDR, &PDDDRShadow, 1, 3);

//Display Pins
BitWrPortI(PEDDR, &PEDDRShadow, 1, 0);
BitWrPortI(PEDDR, &PEDDRShadow, 1, 1);
BitWrPortI(PEDDR, &PEDDRShadow, 1, 2);
29
BitWrPortI(PEDDR, &PEDDRShadow, 1, 3);

updateGuidance();
updateDisplay();
updateGate(0);

while(1){

//sensor1 = BitRdPortI(PBDR,5);
//sensor2 = BitRdPortI(PBDR,6);
////sensor1 = BitRdPortI(PBDR,3);
////sensor2 = BitRdPortI(PBDR,4);

//flushCounter=flushCounter-1;

s1 = BitRdPortI(PBDR,3);
s2 = BitRdPortI(PBDR,4);
s3 = BitRdPortI(PBDR,5);
s4 = BitRdPortI(PBDR,6);
s5 = BitRdPortI(PBDR,7);

//if((s1==1)||(s2==1)||(s3==1)||(s4==1)||(s5==1))
{
BitWrPortI(PBDR, &PBDRShadow, 1, 2);
for(d1=0; d1<1000; d1++);
//for(d2=0; d2<1000; d2++);
BitWrPortI(PBDR, &PBDRShadow, 0, 2);
flushCounter=2;
}

//printf("%d, %d, %d, %d, %d\n",s1,s2,s3,s4,s5);

/*if((sensor1!=s1prev)&&(sensor1==1))
{
//sensor1 = 1;
printf("inside scanned_1 %d!!\n", sensor1);
}

//else
// s1prev= sensor1;

if((sensor2!=s2prev)&&(sensor2==1))
{
//sensor2 = 1;
printf("inside scanned_2 %d!!\n", sensor2);
}
*/
//else
// sensor2 =0;

//sensorSignals[1] = BitRdPortI(PBDR,1);
//printf("after scanned %d!!\n", sensor1);

////
/*
if ((sensor1==1)&&(totalFree>0)&&(sensor1!=s1prev))
{
totalFree=totalFree-1;
updateDisplay();
if((totalFree) == 0) ///////
{
updateGate(1); /// close gate
//updateDisplay();
}
}
30
if((sensor2==1)&&(totalFree<6)&&(sensor2!=s2prev))
{
totalFree=totalFree+1;
updateDisplay();
if((totalFree) == 1) ///////
{
updateGate(0); /// open gate
//updateDisplay();
}
}
////
*/

if ((expectedCar==0)&&(s5==1)&&(totalFree>0)&&(s5!=s5prev)) ////MAIN ENTRANCE


{
expectedCar=1;
totalFree=totalFree-1;
updateDisplay();
if((totalFree) == 0) ///////
{
updateGate(1); /// close gate
//updateDisplay();
}
}
if((s2==1)&&(totalFree<maxFree)&&(s2!=s2prev)&&(totalFreeL2<(maxFree/2))) ////LINE 2 EXIT
{
totalFree=totalFree+1;
totalFreeL2=totalFreeL2+1;
updateDisplay();
if((totalFree) == 1) ///////
{
updateGate(0); /// open gate
//updateDisplay();
}
}
if((s4==1)&&(totalFree<maxFree)&&(s4!=s4prev)&&(totalFreeL1<(maxFree/2))) ////LINE 1 EXIT
{
totalFree=totalFree+1;
totalFreeL1=totalFreeL1+1;
updateDisplay();
if((totalFree) == 1) ///////
{
updateGate(0); /// open gate
//updateDisplay();
}
}

if((expectedCar==1)&&(s1==1)&&(totalFreeL2>0)&&(s1!=s1prev)) ////LINE 2 ENTRANCE


{
totalFreeL2=totalFreeL2-1;
printf("Total Free Line 2 = %d\n",totalFreeL2);
expectedCar=0;
}

if((expectedCar==1)&&(s3==1)&&(totalFreeL1>0)&&(s3!=s3prev)) ////LINE 1 ENTRANCE


{
totalFreeL1=totalFreeL1-1;
printf("Total Free Line 1 = %d\n",totalFreeL1);
expectedCar=0;
}

////s1prev= sensor1;
////s2prev = sensor2;
s1prev = s1;
s2prev = s2;
s3prev = s3;
s4prev = s4;
s5prev = s5;
31
updateGuidance();
sndRcvSerial();
}

/*void scanSensors()
{
printf("1!!\n");
sensor1 = BitRdPortI(PBDR,0);
printf("2!!\n");
sensor2 = BitRdPortI(PBDR,1);
printf("3!!\n");
}*/

void updateGuidance()
{
if(totalFreeL1>0)
{
BitWrPortI(PDDR, &PDDRShadow, 1, 2); // L1
BitWrPortI(PDDR, &PDDRShadow, 0, 3);
}
else
{
BitWrPortI(PDDR, &PDDRShadow, 0, 2);
if(totalFreeL2>0)
BitWrPortI(PDDR, &PDDRShadow, 1, 3); // L2
else
BitWrPortI(PDDR, &PDDRShadow, 0, 3);
}
}

void scanSensors(int s)
{
t = MS_TIMER;
while((MS_TIMER-t)<3000)
{
printf("Time = %d\n", MS_TIMER);
}
sensor1=0;
updateGate(1);
}

void updateGate(int state)


{
gateState = state;

if(state == 1)
{
//BitWrPortI(PBDR, &PBDRShadow, 1, 7);
k = 1000;
l = 1000;
for (k;k > 0; k--)
for (l;l > 0; l--);
BitWrPortI(PDDR, &PDDRShadow, 1, 0);
BitWrPortI(PDDR, &PDDRShadow, 1, 1);
k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


32
BitWrPortI(PDDR, &PDDRShadow, 1, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

if(state == 0)
{
//BitWrPortI(PBDR, &PBDRShadow, 0, 7);
k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);
}

33
if(state == 1)
{
//BitWrPortI(PBDR, &PBDRShadow, 1, 7);
k = 1000;
l = 1000;
for (k;k > 0; k--)
for (l;l > 0; l--);
BitWrPortI(PDDR, &PDDRShadow, 1, 0);
BitWrPortI(PDDR, &PDDRShadow, 1, 1);
k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

if(state == 0)
{
//BitWrPortI(PBDR, &PBDRShadow, 0, 7);
k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 0, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


34
for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 0, 0);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);

k = 1000;
l = 1000;

for (k;k > 0; k--)


for (l;l > 0; l--);

BitWrPortI(PDDR, &PDDRShadow, 1, 0);


BitWrPortI(PDDR, &PDDRShadow, 1, 1);
}
}

void updateDisplay()
{
printf("Total Free = %d\n", totalFree);

WrPortI(PEDR, &PEDRShadow, totalFree); //Input4


}

void sndRcvSerial()
{
iflag=0;
iflag2=0;
i = '‫;'ے‬
while(i == '‫)'ے‬
{
i = serDgetc();

if (i != '‫)'ے‬
iflag = 1;

if(iflag==1)
{
digit1 = (totalFree/10)+48;
digit2 = (totalFree%10)+48;

serDputc(digit1);
serDputc(digit2);
}

if(iflag==1)
{
if(i=='1') // reserve request
iflag2=1;

if(i=='2') // open gate


iflag2=2;

if(i=='0')
iflag2=0;

if(i=='3')
iflag2=3;
}
if(iflag2==1)
{
if(totalFree>=(maxFree/3))
{
totalFree--;
}

35
}
else if(iflag2==2)
{
sensor1 = 1;
if (gateState == 1)
{
while((sensor1 == 1))
{
updateGate(0);
scanSensors(0);
}
}
//serDputc(50); // gate opened
}
else if(iflag2==3)
totalFree=totalFree+1;

36
APPENDIX2:
PC CODE FOR SOCKETS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <time.h>
#include <windows.h>

#define BUFFER_SIZE 4096

WSADATA w;

int send(char*);
int sendToPHP(char*);
int rcv(char*);
int sndRcvSerial();

char INBUFFER[2];
char OUTBUFFER = '0';

DWORD bytes_read = 0; // Number of bytes read from port


DWORD bytes_written = 0; // Number of bytes written to the port
HANDLE comport = NULL; // Handle COM port
int bStatus;
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts;

int totalFree;

unsigned short port_numberSnd = 11330; /* The port number to use for sending */
unsigned short port_numberRcv = 12000; /* Port number to use for receiving */

///////////////// send() Variables /////////////////


//unsigned short port_number = 19001; /* The port number to use */
SOCKET sd; /* The socket descriptor */
int server_length; /* Length of server struct */
char current_time; /* Time received */
struct hostent *hp; /* Information about the server */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
int a1=127, a2=0, a3=0, a4=1; /* Server address components in [Link] form */
int b1=127, b2=0, b3=0, b4=1; /* Client address components in [Link] form */
//int a1=192, a2=168, a3=0, a4=71; /* Server address components in [Link] form */
//int b1=192, b2=168, b3=0, b4=71; /* Client address components in [Link] form */
char host_name[256]; /* Host name of this computer */
/////////////////////////////////////////////////////

///////////////// sendToPHP() Variables /////////////////


//unsigned short port_number = 19001; /* The port number to use */
SOCKET sdPHP; /* The socket descriptor */
int server_lengthPHP; /* Length of server struct */
char current_timePHP; /* Time received */
struct hostent *hpPHP; /* Information about the server */
struct sockaddr_in serverPHP; /* Information about the server */
struct sockaddr_in clientPHP; /* Information about the client */
//int a1=192, a2=168, a3=0, a4=71; /* Server address components in [Link] form */
//int b1=192, b2=168, b3=0, b4=71; /* Client address components in [Link] form */
char host_namePHP[256]; /* Host name of this computer */
/////////////////////////////////////////////////////

///////////////// rcv() Variables /////////////////


//unsigned short port_numberRcv = 19000; /* Port number to use */
//int a1=127, a2=0, a3=0, a4=1; /* Components of address in [Link] form */
37
//int a1=192, a2=168, a3=0, a4=171; /* Components of address in [Link] form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sdReceiver; /* Socket descriptor of server
*/
struct sockaddr_in serverR; /* Information about the server */
struct sockaddr_in clientR; /* Information about the client */
; /* Where to store received data */
struct hostent *hpR; /* Information about this computer */
char host_nameR[256]; /* Name of the server */
char current_timeR; /* Current time */
int g=0;
/////////////////////////////////////////////////////

int main()
{
char send_buffer[BUFFER_SIZE];
char rcv_buffer[BUFFER_SIZE];

int i;
/* Open windows connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
system("pause");
exit(0);
}

///////////////// send() Initialization /////////////////


/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
system("pause");
exit(0);
}

/* Clear out server struct */


memset((void *)&server, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


server.sin_family = AF_INET;
server.sin_port = htons(port_numberSnd);

/* Set server address */


server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

/* Clear out client struct */


memset((void *)&client, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


client.sin_family = AF_INET;
client.sin_port = htons(0);

client.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)b1;


client.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b2;
client.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)b3;
client.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)b4;

/* Bind local address to socket */


if (bind(sd, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1)
38
{
fprintf(stderr, "Cannot bind address to socket.\n");
closesocket(sd);
WSACleanup();
system("pause");
exit(0);
}

///////////////// sendToPHP() Initialization /////////////////


/* Open a datagram socket */
sdPHP = socket(AF_INET, SOCK_DGRAM, 0);
if (sdPHP == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
system("pause");
exit(0);
}

/* Clear out server struct */


memset((void *)&serverPHP, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


serverPHP.sin_family = AF_INET;
serverPHP.sin_port = htons(11100);

/* Set server address */


serverPHP.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
serverPHP.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
serverPHP.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
serverPHP.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

/* Clear out client struct */


memset((void *)&clientPHP, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


clientPHP.sin_family = AF_INET;
clientPHP.sin_port = htons(0);

clientPHP.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)b1;


clientPHP.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b2;
clientPHP.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)b3;
clientPHP.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)b4;

/* Bind local address to socket */


if (bind(sdPHP, (struct sockaddr *)&clientPHP, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Cannot bind address to socket.\n");
closesocket(sdPHP);
WSACleanup();
system("pause");
exit(0);
}/////

///////////////// rcv() Initialization /////////////////


/* Open a datagram socket */
sdReceiver = socket(AF_INET, SOCK_DGRAM, 0);
if (sdReceiver == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
system("pause");
exit(0);
}

/* Clear out server struct */


39
memset((void *)&serverR, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


serverR.sin_family = AF_INET;
serverR.sin_port = htons(port_numberRcv);

serverR.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;


serverR.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
serverR.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
serverR.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

/* Bind address to socket */


if (bind(sdReceiver, (struct sockaddr *)&serverR, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sdReceiver);
WSACleanup();
system("pause");
exit(0);
}
/////////////////////////////////////////////////////

////////// open serial

if ((comport =
CreateFile("\\\\.\\COM1", // open com5:
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
// error processing code goes here
}
// Set timeouts in milliseconds
[Link] = 0;
[Link] = 0;
[Link] = 100;
[Link] = 0;
[Link] = 100;
bStatus = SetCommTimeouts(comport,&CommTimeouts);
if (bStatus != 0)
{
// error processing code goes here
}
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState(comport, &comSettings);
[Link] = 2400;
[Link] = ONESTOPBIT;
[Link] = 8;
[Link] = NOPARITY;
[Link] = FALSE;
bStatus = SetCommState(comport, &comSettings);
if (bStatus == 0)
{
// error processing code goes here
}

//////////

for(i=0;i<4096;i++)
{
send_buffer[i]=0;
rcv_buffer[i]=0;
40
}

send_buffer[0]='0';
send_buffer[1]='0';
send_buffer[2]='X';
send(send_buffer);
INBUFFER[1]='3';

while (1)
{

for(i=0;i<4096;i++)
{
send_buffer[i]=0;
rcv_buffer[i]=0;
}

rcv(rcv_buffer);

if(rcv_buffer[0]=='X')
{
OUTBUFFER = '0';
printf("Idle!!\n");
}

else if(rcv_buffer[0]=='R')
{
OUTBUFFER = '1';
printf("Reserving!!\n");
}

else if(rcv_buffer[0]=='O')
{
OUTBUFFER = '2';
printf("Opening!!\n");
}
else if(rcv_buffer[0]=='C')
{
OUTBUFFER = '3';
printf("Cancelling Reservation!!\n");
}
sndRcvSerial();

totalFree = (INBUFFER[0]-48)*10+(INBUFFER[1]-48);
while(totalFree<0)
{
sndRcvSerial();
totalFree = (INBUFFER[0]-48)*10+(INBUFFER[1]-48);
printf("in the red zone!!!!!\n");
}

printf("\nTotal Free: %d\n",totalFree);

///////////////////////// Send to other processes


send_buffer[0] = INBUFFER[0];
send_buffer[1] = INBUFFER[1];
send_buffer[2]='X';

//port_numberSnd = 11100; // gsm port


send(send_buffer);

//port_numberSnd = 11330; // webserver port


sendToPHP(send_buffer);
/////////////////////////
}

closesocket(sd);
closesocket(sdReceiver);
WSACleanup();
41
}

int send(char* send_buffer)


{
server_length = sizeof(struct sockaddr_in);

//*******************************************************************

if (sendto(sd, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&server, server_length)


== -1)
{
fprintf(stderr, "Error transmitting data.\n");
closesocket(sd);
WSACleanup();
system("pause");
exit(0);
}
return 0;
}

int sendToPHP(char* send_bufferPHP)


{
server_lengthPHP = sizeof(struct sockaddr_in);

//*******************************************************************

if (sendto(sdPHP, send_bufferPHP, (int)strlen(send_bufferPHP) + 1, 0, (struct sockaddr


*)&serverPHP, server_lengthPHP) == -1)
{
fprintf(stderr, "Error transmitting data.\n");
closesocket(sdPHP);
WSACleanup();
system("pause");
exit(0);
}
return 0;
}

int rcv(char* buffer)


{

client_length = (int)sizeof(struct sockaddr_in);


for(g=0;g<4096;g++)
buffer[g]=0;

/* Receive bytes from client */


bytes_received = recvfrom(sdReceiver, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&clientR,
&client_length);

if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
closesocket(sdReceiver);
WSACleanup();
system("pause");
exit(0);
}

return 0;
}
int sndRcvSerial()
{
//char INBUFFER[2];
//char OUTBUFFER = '0';
42
//strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
// Open COM port

INBUFFER[0]=0;
INBUFFER[1]=0;
bStatus = WriteFile(comport, // Handle
&OUTBUFFER, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

/*
OUTBUFFER++;
if(OUTBUFFER == '2')
OUTBUFFER = '0';
*/

if (bStatus != 0)
{
// error processing code here
}
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
2, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}
// code to do something with the data goes here

//printf("\n%c\n",OUTBUFFER);

//printf("(%c, %c) ", INBUFFER[0], INBUFFER[1]);


//CloseHandle(comport);
return 0;
}

43
APPENDIX3:
CODE FOR THE WEB SERVER

<?php
error_reporting(E_ALL | E_STRICT);

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);


socket_bind($socket, '[Link]', 11100);

$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);

//echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
$data = $buf;
$free_slots = substr($buf,0,2);
$occupied_slots= 20 - $free_slots;

//Show time:
date_default_timezone_set('Asia/Riyadh');
$date = date('l dS \of F Y h:i:s A');

?>
<html>
<head>
<title>Smart Parking</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="Keywords here">
<meta name="description" content="Description here">
<meta name="Author" content="Adeeb Hassan">
<meta name="Publisher" content="[Link]">
<meta name="robots" content="index, follow"> <!-- (robot commands: all, none, index, no index, follow, no follow) -->
<meta name="revisit-after" content="30 days">
<meta name="distribution" content="global">
<meta name="rating" content="general">
<meta name="content-language" content="english">

<script language="JavaScript" type="text/JavaScript" src="images/[Link]"></script>

<style type="text/css">
<!--
.basestyle {
color: #FFFFFF;
font-size: 10px;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
h1 {
font-size: 16px;
font-weight: bold;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
body, td, p, input {
font-size: 11px;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #333333;
}
.navlinks {
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: #FFFFFF;
}
-->
</style>

44
</head>
<body style="margin:0; ">
<table width=755 height="100%" border=0 align="center" cellpadding=0 cellspacing=0>
<tr align="center" valign="middle">
<td height="169" colspan="2"><a href="[Link]
<img src="../images/[Link]" width="216" height="155" border="0"></a><img
src="../images/[Link]" width="539" height="155"></td>
</tr>
<tr>
<td width="185" align="center" valign="top" bgcolor="#d80000"><h3>
<img src="../images/[Link]" width="185" height="14"></h3>
<table width="176" border="0" cellpadding="2" cellspacing="0">
<tr>
<td class="navlinks">
<font style="font-size: 16pt"><a href="[Link]">
<font color="#FFFFFF">::Home</font></a></font></td>
</tr>
<tr>
<td class="navlinks">
&nbsp;</td>
</tr>
<tr>
<td class="navlinks">
<font style="font-size: 16pt">::Parking Status</font></td>
</tr>
<tr>
<td class="navlinks">&nbsp;</td>
</tr>
<tr>
<td class="navlinks"><a href="[Link]">
<font color="#FFFFFF" style="font-size: 16pt">::</font><font color="#FFFFFF" style="font-size: 13pt">About
This Project</font></a></td>
</tr>
<tr>
<td class="navlinks">&nbsp;</td>
</tr>
<tr>
<td class="navlinks"><font style="font-size: 16pt">
<a href="[Link]"><font color="#FFFFFF">::Team Members</font></a></font></td>
</tr>
<tr>
<td class="navlinks">&nbsp;</td>
</tr>
<td class="navlinks"><font style="font-size: 16pt">
<a href="[Link]"><font color="#FFFFFF">:: SMS</font></a></font></td>
</tr>
<tr>
<td class="navlinks">&nbsp;</td>
</table>
<h3>&nbsp; </h3></td>
<td width="570" rowspan="2" valign="top" style="padding-left:20px; padding-top:25px; padding-right:40px;
padding-bottom:35px; ">

<p align="center"><font size="6" color="#CC0000"><b>Parking Status</b></font></p>


<p align="center">&nbsp;</p>
<p align="center"><img src="[Link]" width="104" height="103"></p>
<p align="center">&nbsp;<p align="center">&nbsp;<table border="0" width="100%" cellspacing="0"
cellpadding="0">
<tr>
<td align="left" height="65">&nbsp;</td>
<td align="left" width="238" height="65" dir="ltr"><b>
<font size="5" color="#000000">
Free Slots:</font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000"><?php echo $free_slots; ?></font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000">Slots</font></b></td>
</tr>
<tr>
45
<td align="left" height="65">&nbsp;</td>
<td align="left" width="238" height="65" dir="ltr"><b>
<font size="5" color="#000000">
Occupied Slots:</font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000"><?php echo $occupied_slots; ?></font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000">Slots</font></b></td>
</tr>
<tr>
<td align="left" height="65">&nbsp;</td>
<td align="left" width="238" height="65" dir="ltr"><b>
<font size="5" color="#000000">
Reserved Slots:</font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000"><?php echo 0; ?></font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000">Slots</font></b></td>
</tr>
<tr>
<td align="left" height="65">&nbsp;</td>
<td align="left" width="238" height="65" dir="ltr"><b>
<font size="5" color="#000000">
Total Slots:</font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000"><?php echo 20; ?></font></b></td>
<td align="center" width="119" height="65"><b>
<font size="5" color="#000000">Slots</font></b></td>
</tr>
</table>
<p>&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center"><b><font size="1" color="#FF0000">This request generated on: <?php echo $date;
?></font></b></p>
<p align="center"><b><font size="3" color="#000000">Please press &quot;F5&quot; in your keyboard or press the
following
button to refresh</font></b></p>
<form>
<p align="center"><font size="3">
<input type=button value="Refresh" onClick="[Link]()" style="font-weight: bold"></font></p>
</form>
&nbsp;</p>
<p align="center">&nbsp;</p>
<p align="center">&nbsp;</td>
</tr>
<tr>
<td height="20" valign="bottom" bgcolor="#d80000">
<img src="../images/[Link]" width="185" height="20"></td>
</tr>
<tr>
<td height="58" colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26">
<img src="../images/[Link]" width="26" height="51"></td>
<td align="center" bgcolor="#ef8406" class="basestyle">© Copyright
2009. All Rights Reserved.</td>
<td width="26">
<img src="../images/[Link]" width="26" height="51"></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>

46
APPENDIX4:
CODE FOR THE GSM MODULE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <time.h>
#include <windows.h>

#define BUFFER_SIZE 4096

WSADATA w;

int send(char*);
int rcv(char*);
void keepConAlive();
int checkTimeouts();

DWORD bytes_read = 0; // Number of bytes read from port


DWORD bytes_written = 0; // Number of bytes written to the port

unsigned short port_numberSnd = 12000; /* The port number to use for sending */
unsigned short port_numberRcv = 11330; /* Port number to use for receiving */

char rcv_buffer[BUFFER_SIZE];
char send_buffer[BUFFER_SIZE];
time_t timeouts[10];

int totalFree=0, resIndex = 0;


char cmd;

char confirmedReservations[10][15];

///////////////// send() Variables /////////////////


//unsigned short port_number = 19001; /* The port number to use */
SOCKET sd; /* The socket descriptor */
int server_length; /* Length of server struct */
char current_time; /* Time received */
struct hostent *hp; /* Information about the server */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
int a1=127, a2=0, a3=0, a4=1; /* Server address components in [Link] form */
int b1=127, b2=0, b3=0, b4=1; /* Client address components in [Link] form */
//int a1=192, a2=168, a3=0, a4=71; /* Server address components in [Link] form */
//int b1=192, b2=168, b3=0, b4=71; /* Client address components in [Link] form */
char host_name[256]; /* Host name of this computer */
/////////////////////////////////////////////////////

///////////////// rcv() Variables /////////////////


//unsigned short port_numberRcv = 19000; /* Port number to use */
//int a1=127, a2=0, a3=0, a4=1; /* Components of address in [Link] form */
//int a1=192, a2=168, a3=0, a4=171; /* Components of address in [Link] form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sdReceiver; /* Socket descriptor of server
*/
struct sockaddr_in serverR; /* Information about the server */
struct sockaddr_in clientR; /* Information about the client */
; /* Where to store received data */
struct hostent *hpR; /* Information about this computer */
char host_nameR[256]; /* Name of the server */
char current_timeR; /* Current time */
int g=0;
/////////////////////////////////////////////////////
47
int main(int argc, char* argv[])
{
char INBUFFER[512], smsNum[2], sms[10], mobNum[13], smsC = 'F', cmdToSend='X';
char OUTBUFFER[128];
DWORD bytes_read = 0; // Number of bytes read from port
DWORD bytes_written = 0; // Number of bytes written to the port
HANDLE comport = NULL; // Handle COM port
int bStatus, i, h, myInd, reservationLimit=2, lengthOfString = 0, j, processing = 0;
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts;

/* Open windows connection */


if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
system("pause");
exit(0);
}

///////////////// send() Initialization /////////////////


/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
system("pause");
exit(0);
}

/* Clear out server struct */


memset((void *)&server, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


server.sin_family = AF_INET;
server.sin_port = htons(port_numberSnd);

/* Set server address */


server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

/* Clear out client struct */


memset((void *)&client, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


client.sin_family = AF_INET;
client.sin_port = htons(0);

client.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)b1;


client.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)b2;
client.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)b3;
client.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)b4;

/* Bind local address to socket */


if (bind(sd, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Cannot bind address to socket.\n");
closesocket(sd);
WSACleanup();
system("pause");
exit(0);
}
48
///////////////// rcv() Initialization /////////////////
/* Open a datagram socket */
sdReceiver = socket(AF_INET, SOCK_DGRAM, 0);
if (sdReceiver == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
system("pause");
exit(0);
}

/* Clear out server struct */


memset((void *)&serverR, '\0', sizeof(struct sockaddr_in));

/* Set family and port */


serverR.sin_family = AF_INET;
serverR.sin_port = htons(port_numberRcv);

serverR.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)a1;


serverR.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)a2;
serverR.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)a3;
serverR.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)a4;

/* Bind address to socket */


if (bind(sdReceiver, (struct sockaddr *)&serverR, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sdReceiver);
WSACleanup();
system("pause");
exit(0);
}
/////////////////////////////////////////////////////

//strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
// Open COM port
if ((comport =
CreateFile("\\\\.\\COM4", // open com5:
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
// error processing code goes here
}
// Set timeouts in milliseconds
[Link] = 0;
[Link] = 0;
[Link] = 100;
[Link] = 0;
[Link] = 100;
bStatus = SetCommTimeouts(comport,&CommTimeouts);
if (bStatus != 0)
{
// error processing code goes here
}
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState(comport, &comSettings);
[Link] = 9600;
[Link] = ONESTOPBIT;
[Link] = 8;
[Link] = NOPARITY;
[Link] = FALSE;
49
bStatus = SetCommState(comport, &comSettings);
if (bStatus == 0)
{
// error processing code goes here
}
//printf("INBUFFER = %s", INBUFFER);
//printf("OUTBUFFER = %s", OUTBUFFER);
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
3, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;
Sleep(500);

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;
/*
OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='P';
OUTBUFFER[5]='I';
OUTBUFFER[6]='N';
OUTBUFFER[7]='=';
OUTBUFFER[8]='1';
OUTBUFFER[9]='9';
OUTBUFFER[10]='8';
OUTBUFFER[11]='6';
OUTBUFFER[12]=13;
*/

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='P';
OUTBUFFER[5]='I';
OUTBUFFER[6]='N';
OUTBUFFER[7]='=';
OUTBUFFER[8]='5';
OUTBUFFER[9]='3';
OUTBUFFER[10]='6';
OUTBUFFER[11]='2';
50
OUTBUFFER[12]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
13, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(500);
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

/*
OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='S';
OUTBUFFER[5]='C';
OUTBUFFER[6]='A';
OUTBUFFER[7]='=';
OUTBUFFER[8]=34;
OUTBUFFER[9]='+';
OUTBUFFER[10]='9';
OUTBUFFER[11]='6';
OUTBUFFER[12]='6';
OUTBUFFER[13]='5';
OUTBUFFER[14]='0';
OUTBUFFER[15]='5';
OUTBUFFER[16]='0';
OUTBUFFER[17]='3';
OUTBUFFER[18]='1';
OUTBUFFER[19]='9';
OUTBUFFER[20]='9';
OUTBUFFER[21]='9';
OUTBUFFER[22]=34;
OUTBUFFER[23]=13;
*/

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='S';
OUTBUFFER[5]='C';
OUTBUFFER[6]='A';
OUTBUFFER[7]='=';
OUTBUFFER[8]=34;
OUTBUFFER[9]='+';
OUTBUFFER[10]='9';
51
OUTBUFFER[11]='6';
OUTBUFFER[12]='6';
OUTBUFFER[13]='5';
OUTBUFFER[14]='9';
OUTBUFFER[15]='0';
OUTBUFFER[16]='1';
OUTBUFFER[17]='0';
OUTBUFFER[18]='0';
OUTBUFFER[19]='8';
OUTBUFFER[20]='8';
OUTBUFFER[21]='0';
OUTBUFFER[22]=34;
OUTBUFFER[23]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
24, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(500);
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='M';
OUTBUFFER[5]='G';
OUTBUFFER[6]='F';
OUTBUFFER[7]='=';
OUTBUFFER[8]='1';
OUTBUFFER[9]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
10, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(1000);
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
52
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='N';
OUTBUFFER[5]='M';
OUTBUFFER[6]='I';
OUTBUFFER[7]='=';
OUTBUFFER[8]='1';
OUTBUFFER[9]=',';
OUTBUFFER[10]='2';
OUTBUFFER[11]=',';
OUTBUFFER[12]='0';
OUTBUFFER[13]=',';
OUTBUFFER[14]='0';
OUTBUFFER[15]=',';
OUTBUFFER[16]='0';
OUTBUFFER[17]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
18, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(500);
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='M';
OUTBUFFER[5]='G';
OUTBUFFER[6]='D';
OUTBUFFER[7]='=';
OUTBUFFER[8]='0';
53
OUTBUFFER[9]=',';
OUTBUFFER[10]='4';
OUTBUFFER[11]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
12, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(3000);
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(100);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

rcv(rcv_buffer);
totalFree = (rcv_buffer[0]-48)*10+(rcv_buffer[1]-48);
cmd = rcv_buffer[2];

send_buffer[0] = 'X';
send(send_buffer);

printf("Total Free = %d, Command = %c\n", totalFree, cmd);

while(1)
{
myInd = 0;

for(i = 0; i < BUFFER_SIZE; i++)


{
rcv_buffer[i] = 0;
send_buffer[i] = 0;
}

if(processing == 0)
{
//printf("inside WHILE\n");
rcv(rcv_buffer);
totalFree = (rcv_buffer[0]-48)*10+(rcv_buffer[1]-48);
cmd = rcv_buffer[2];
}

printf("Total Free = %d, Command = %c\n", totalFree, cmd);

if((cmdToSend != 'X')&&(processing == 0))


{
send_buffer[0] = cmdToSend;
send(send_buffer);
cmdToSend = 'X';
}
54
else if(processing == 0)
{
send_buffer[0] = 'X';
send(send_buffer);
cmdToSend = 'X';
}

keepConAlive();

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;
for(i = 0; i < 512; i++)
INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

Sleep(100);
keepConAlive();
Sleep(100);
keepConAlive();
Sleep(100);
keepConAlive();
Sleep(100);
keepConAlive();
Sleep(100);

if(INBUFFER[0]!=0)
{
printf("INBUFFER = %s\n", INBUFFER);
for(i = 0; i<500; i++)
if(INBUFFER[i] == '+')
{
myInd = i;
//printf("index = %d\n", myInd);
break;
}
}

if((INBUFFER[myInd] == '+') && (INBUFFER[myInd+1] == 'C') && (INBUFFER[myInd+2] == 'M') &&


(INBUFFER[myInd+3] == 'T') && (INBUFFER[myInd+4] == 'I'))
{
processing = 1;
printf("\nBegin CMTI\n");

smsNum[0] = INBUFFER[14];
smsNum[1] = INBUFFER[13];

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='M';
OUTBUFFER[5]='G';
OUTBUFFER[6]='R';
OUTBUFFER[7]='=';
OUTBUFFER[8]=smsNum[0];
55
OUTBUFFER[9]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
10, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

printf("\nEnd CMTI\n");
printf("OUTBUFFER = %s\n", OUTBUFFER);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(500);
keepConAlive();
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);
keepConAlive();
}

if((INBUFFER[myInd] == '+') && (INBUFFER[myInd+1] == 'C') && (INBUFFER[myInd+2] == 'M') &&


(INBUFFER[myInd+3] == 'G') && (INBUFFER[myInd+4] == 'R'))
{

for(h = 0; h < 15; h++)


mobNum[h] = INBUFFER[h+32];

/* for(i = 72; i<500; i++)


if((INBUFFER[i] == 's')||(INBUFFER[i] == 'S'))
{
printf("\nCHARINDEX=%d\n", i);
break;
}
*/
printf("valueinINBUFFER = %c\n", INBUFFER[73]);

smsC = INBUFFER[73];

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='M';
OUTBUFFER[5]='G';
OUTBUFFER[6]='S';
OUTBUFFER[7]='=';
for(h = 0; h < 15; h++)
OUTBUFFER[h+8] = mobNum[h];
OUTBUFFER[23] = 13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
24, // Number of bytes to write
56
&bytes_written, // Number of bytes written
NULL);

Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
/*keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
*/

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);
keepConAlive();
printf("\nThe message is: %c\n", smsC) ;

if((smsC == 'S')||(smsC == 's'))


{
printf("\nReceived Status\n");
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

if(totalFree>reservationLimit)
{
OUTBUFFER[0]='F';
OUTBUFFER[1]='r';
OUTBUFFER[2]='e';
OUTBUFFER[3]='e';
OUTBUFFER[4]='=';
OUTBUFFER[5]=(totalFree/10)+48;
OUTBUFFER[6]=(totalFree%10)+48;
OUTBUFFER[7]=' ';
OUTBUFFER[8]='R';
OUTBUFFER[9]='e';
OUTBUFFER[10]='s';
OUTBUFFER[11]='e';
OUTBUFFER[12]='r';
OUTBUFFER[13]='v';
OUTBUFFER[14]='a';
OUTBUFFER[15]='t';
57
OUTBUFFER[16]='i';
OUTBUFFER[17]='o';
OUTBUFFER[18]='n';
OUTBUFFER[19]='_';
OUTBUFFER[20]='P';
OUTBUFFER[21]='o';
OUTBUFFER[22]='s';
OUTBUFFER[23]='s';
OUTBUFFER[24]='i';
OUTBUFFER[25]='b';
OUTBUFFER[26]='l';
OUTBUFFER[27]='e';
lengthOfString = 28;
}
else
{
OUTBUFFER[0]='F';
OUTBUFFER[1]='r';
OUTBUFFER[2]='e';
OUTBUFFER[3]='e';
OUTBUFFER[4]='=';
OUTBUFFER[5]=(totalFree/10)+48;
OUTBUFFER[6]=(totalFree%10)+48;
OUTBUFFER[7]=' ';
OUTBUFFER[8]='R';
OUTBUFFER[9]='e';
OUTBUFFER[10]='s';
OUTBUFFER[11]='e';
OUTBUFFER[12]='r';
OUTBUFFER[13]='v';
OUTBUFFER[14]='a';
OUTBUFFER[15]='t';
OUTBUFFER[16]='i';
OUTBUFFER[17]='o';
OUTBUFFER[18]='n';
OUTBUFFER[19]='_';
OUTBUFFER[20]='N';
OUTBUFFER[21]='o';
OUTBUFFER[22]='t';
OUTBUFFER[23]='_';
OUTBUFFER[24]='P';
OUTBUFFER[25]='o';
OUTBUFFER[26]='s';
OUTBUFFER[27]='s';
OUTBUFFER[28]='i';
OUTBUFFER[29]='b';
OUTBUFFER[30]='l';
OUTBUFFER[31]='e';
lengthOfString = 32;
}

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
lengthOfString, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();
for(i = 0; i < 512; i++)
INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
58
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);
keepConAlive();
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

OUTBUFFER[0]=26;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();
for(i = 0; i < 512; i++)
INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
}
else if((smsC == 'R')||(smsC == 'r'))
{
printf("\nReceived Reserve\n");
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

if(totalFree>=reservationLimit)
{
59
printf("\nReserving\n");
for(h = 0; h < 15; h++)
confirmedReservations[resIndex][h] = mobNum[h];

timeouts[resIndex]=time(NULL);
resIndex++;

OUTBUFFER[0]='R';
OUTBUFFER[1]='e';
OUTBUFFER[2]='s';
OUTBUFFER[3]='e';
OUTBUFFER[4]='r';
OUTBUFFER[5]='v';
OUTBUFFER[6]='a';
OUTBUFFER[7]='t';
OUTBUFFER[8]='i';
OUTBUFFER[9]='o';
OUTBUFFER[10]='n';
OUTBUFFER[11]='_';
OUTBUFFER[12]='C';
OUTBUFFER[13]='o';
OUTBUFFER[14]='n';
OUTBUFFER[15]='f';
OUTBUFFER[16]='i';
OUTBUFFER[17]='r';
OUTBUFFER[18]='m';
OUTBUFFER[19]='e';
OUTBUFFER[20]='d';
lengthOfString = 21;
cmdToSend='R';
}
else
{
printf("\nCan't Reserve\n");
OUTBUFFER[0]='R';
OUTBUFFER[1]='e';
OUTBUFFER[2]='s';
OUTBUFFER[3]='e';
OUTBUFFER[4]='r';
OUTBUFFER[5]='v';
OUTBUFFER[6]='a';
OUTBUFFER[7]='t';
OUTBUFFER[8]='i';
OUTBUFFER[9]='o';
OUTBUFFER[10]='n';
OUTBUFFER[11]='_';
OUTBUFFER[12]='I';
OUTBUFFER[13]='s';
OUTBUFFER[14]='_';
OUTBUFFER[15]='U';
OUTBUFFER[16]='n';
OUTBUFFER[17]='a';
OUTBUFFER[18]='v';
OUTBUFFER[19]='a';
OUTBUFFER[20]='i';
OUTBUFFER[21]='l';
OUTBUFFER[22]='a';
OUTBUFFER[23]='b';
OUTBUFFER[24]='l';
OUTBUFFER[25]='e';
OUTBUFFER[26]='_';
OUTBUFFER[27]='N';
OUTBUFFER[28]='o';
OUTBUFFER[29]='w';
lengthOfString = 30;
}

bStatus = WriteFile(comport, // Handle


60
&OUTBUFFER, // Outgoing data
lengthOfString, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]=26;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
61
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
}
else if((smsC == 'O')||(smsC == 'o'))
{
printf("\nReceived Open\n");
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

i = 0;
for(j = 0; j < resIndex; j++)
{
i = 0;
for(h = 0; h < 15; h++)
{
if(confirmedReservations[j][h] == mobNum[h])
i++;
}
if(i == 15)
{
//////
break;
}
}

if(i == 15)
{
//printf("\nI=15\n");
for(h = 0; h < 15; h++)
confirmedReservations[j][h] = 0;

for(j; j < resIndex; j++)


for(h = 0; h < 15; h++)
{
confirmedReservations[j][h] == confirmedReservations[j+1][h];
}

resIndex--;
for(h = 0; h < 15; h++)
confirmedReservations[resIndex][h] = 0;

OUTBUFFER[0]='G';
OUTBUFFER[1]='a';
OUTBUFFER[2]='t';
OUTBUFFER[3]='e';
OUTBUFFER[4]='_';
OUTBUFFER[5]='O';
OUTBUFFER[6]='p';
OUTBUFFER[7]='e';
OUTBUFFER[8]='n';
OUTBUFFER[9]='i';
OUTBUFFER[10]='n';
OUTBUFFER[11]='g';
lengthOfString = 12;
cmdToSend='O';
}
else
{
printf("\nI!=15\n");
OUTBUFFER[0]='Y';
62
OUTBUFFER[1]='o';
OUTBUFFER[2]='u';
OUTBUFFER[3]='_';
OUTBUFFER[4]='D';
OUTBUFFER[5]='i';
OUTBUFFER[6]='d';
OUTBUFFER[7]='_';
OUTBUFFER[8]='N';
OUTBUFFER[9]='o';
OUTBUFFER[10]='t';
OUTBUFFER[11]='_';
OUTBUFFER[12]='R';
OUTBUFFER[13]='e';
OUTBUFFER[14]='s';
OUTBUFFER[15]='e';
OUTBUFFER[16]='r';
OUTBUFFER[17]='v';
OUTBUFFER[18]='e';
lengthOfString = 19;
}

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
lengthOfString, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);
keepConAlive();

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

OUTBUFFER[0]=26;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

//printf("OUTBUFFER = %s\n", OUTBUFFER);

Sleep(500);
keepConAlive();

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;
63
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

printf("INBUFFER = %s\n", INBUFFER);


Sleep(500);
keepConAlive();

for(i = 0; i < 128; i++)


OUTBUFFER[i] = 0;

Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
}

/////////////////////////////////////////////////////////////////////////
for(i = 0; i < 128; i++)
OUTBUFFER[i] = 0;

OUTBUFFER[0]='A';
OUTBUFFER[1]='T';
OUTBUFFER[2]='+';
OUTBUFFER[3]='C';
OUTBUFFER[4]='M';
OUTBUFFER[5]='G';
OUTBUFFER[6]='D';
OUTBUFFER[7]='=';
OUTBUFFER[8]='0';
OUTBUFFER[9]=',';
OUTBUFFER[10]='4';
OUTBUFFER[11]=13;

bStatus = WriteFile(comport, // Handle


&OUTBUFFER, // Outgoing data
12, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);

for(i = 0; i < 512; i++)


INBUFFER[i] = 0;

Sleep(500);
keepConAlive();
64
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();

bStatus = ReadFile(comport, // Handle


&INBUFFER, // Incoming data
512, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}

//if(INBUFFER[0]!=0)
printf("INBUFFER = %s\n", INBUFFER);

Sleep(500);
keepConAlive();
Sleep(500);
keepConAlive();

processing = 0;
/////////////////////////////////////////////////////////////////////////

if((resIndex>0)&&(cmdToSend=='X'))
if(checkTimeouts()==1)
cmdToSend = 'C';

//printf("outer = %s\n", INBUFFER);


//printf("%c %c %c %c %c %c %c \n", INBUFFER[0], INBUFFER[1], INBUFFER[2], INBUFFER[3], INBUFFER[4],
INBUFFER[5], INBUFFER[6]);
}
CloseHandle(comport);
closesocket(sd);
closesocket(sdReceiver);
WSACleanup();
return 0;
}

int send(char* send_buffer)


{
server_length = sizeof(struct sockaddr_in);

//*******************************************************************

if (sendto(sd, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&server, server_length)


== -1)
{
fprintf(stderr, "Error transmitting data.\n");
closesocket(sd);
WSACleanup();
system("pause");
exit(0);
65
}
return 0;
}

int rcv(char* buffer)


{

client_length = (int)sizeof(struct sockaddr_in);


for(g=0;g<4096;g++)
buffer[g]=0;

/* Receive bytes from client */


bytes_received = recvfrom(sdReceiver, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&clientR,
&client_length);

if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
closesocket(sdReceiver);
WSACleanup();
system("pause");
exit(0);
}

return 0;
}

void keepConAlive()
{
int i;
for(i = 0; i < BUFFER_SIZE; i++)
{
rcv_buffer[i] = 0;
send_buffer[i] = 0;
}

rcv(rcv_buffer);
totalFree = (rcv_buffer[0]-48)*10+(rcv_buffer[1]-48);
cmd = rcv_buffer[2];

printf("Total Free = %d, Command = %c\n", totalFree, cmd);

send_buffer[0] = 'X';
send(send_buffer);
}

int checkTimeouts()
{
int i, j, h;

for(i = 0; i < resIndex; i++)


if(difftime(time(NULL), timeouts[i])>60)
{
for(h = 0; h < 15; h++)
confirmedReservations[i][h] = 0;

for(i; i < resIndex; i++)


{
for(h = 0; h < 15; h++)
{
confirmedReservations[i][h] == confirmedReservations[i+1][h];
}
timeouts[i] == timeouts[i+1];
}

resIndex--;
for(h = 0; h < 15; h++)
confirmedReservations[resIndex][h] = 0;
66
timeouts[resIndex] = 0;

return 1;
}
return 0;
}

AT Commnds:
AT // to check the connectivity of the GSM modem with PC
AT+CMGF=1" // working in the text mode
AT+CSCA= “+966505031999” // Message Center Number (STC)
AT+CMGS=”+966xxxxxxxxx” // put the mobile number of the receiver
AT+ CPIN=xxxx // Set up the PIN number
AT+CMGR=N // Read the SMS number N
AT+CNMI=”1,2,0,0,0" // Set the receiving mode
AT+CMGD=0,4 // Delete all the sent and received SMS

67
APPENDIX5:
CODE FOR THE SENSOR MODULE

#class auto
#define BD_RATE 9600
#define DINBUFSIZE 255
#define DOUTBUFSIZE 255
int count5,count4,count3,count2,count1,ones5,ones4,ones3,ones2,ones1,flip;
char buf1[180];
char buf2[180];
char buf3[180];
int SENSORS[6];
char i1,i2,i3,i4,i5;

main() {

serBopen(BD_RATE);
serCopen(BD_RATE);
serDopen(BD_RATE);

/*BitWrPortI(PBDDR, &PBDRShadow, 1, 6);


BitWrPortI(PBDDR, &PBDRShadow, 1, 7);*/
WrPortI(PBDDR, &PBDDRShadow, 0xFE);
flip=0;
SENSORS[1] = SENSORS[2] = SENSORS[3] = SENSORS[4] = SENSORS[5] = 0;
loopinit();
while(1){
//=============================SENSOR_5=====================================
====

costate{ // Sensor5 always_on


//loophead();
// costate{
printf("S1=%d, S2=%d, S3=%d, S4=%d,
S5=%d\n",SENSORS[1],SENSORS[2],SENSORS[3],SENSORS[4],SENSORS[5]);
wfd i5=cof_serBgetc();
count5=0;
ones5 =0;
while(count5<20){
if(i5==82){
wfd buf1[0]=cof_serBgetc();
wfd buf1[5]=cof_serBgetc();
wfd buf1[10]=cof_serBgetc();
wfd count1= cof_serBgetc();
if(buf1[0]=='0'&&buf1[5]=='0'&&buf1[10]=='6') {
ones5++;}
count5++;
}

wfd i5=cof_serBgetc();
}
if (ones5 >= 6) {
SENSORS[5] = 1;//ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 5);
flip=1;
// break ;
}
else
SENSORS[5] = 0;

}
//=============================SENSOR_1_AND_2================================
===
costate{
BitWrPortI(PBDR, &PBDRShadow, 1, 7); // CAN BE REMOVED
if (flip!=1){
wfd i2=cof_serDgetc();
68
count2=0;
ones2 =0;
while(count2<20){
if(i2==82){
wfd buf2[0]=cof_serDgetc();
wfd buf2[5]=cof_serDgetc();
wfd buf2[10]=cof_serDgetc();
wfd count1=cof_serDgetc();
if(buf2[0]=='0'&&buf2[5]=='0'&&buf2[10]=='6')
ones2++;
count2++;

wfd i2=cof_serDgetc();
} // While finisfing
if (ones2>=6){
SENSORS[2]++; //ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 2);
// break;
}
} // if finisfing

else{
// Now CHECKING FOR SENSOR 2
BitWrPortI(PBDR, &PBDRShadow, 1,7);// CHANGE THE SELECT BIT OF THE MUX
wfd i2=cof_serDgetc();
count2=0;
ones2 =0;
while(count2<10){
if(i2==82){
wfd buf2[0]=cof_serDgetc();
wfd buf2[5]=cof_serDgetc();
wfd buf2[10]=cof_serDgetc();
wfd count1=cof_serDgetc();
if(buf2[0]=='0'&&buf2[5]=='0'&&buf2[10]=='6')
ones2++;
count2++;
if (ones2 >=6){
SENSORS[2]++;//ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 2);
break;
}
}
wfd i2=cof_serDgetc();
} // While finisfing
BitWrPortI(PBDR, &PBDRShadow, 0, 7);// CHANGE THE SELECT BIT OF THE MUX

// Now CHECKING FOR SENSOR 1


serDrdFlush();
wfd i3=cof_serDgetc();
count3=0;
ones3 =0;

while(count3<10){
if(i3==82){
wfd buf2[0]=cof_serDgetc();
wfd buf2[5]=cof_serDgetc();
wfd buf2[10]=cof_serDgetc();
wfd cof_serDgetc();
if(buf2[0]=='0'&&buf2[5]=='0'&&buf2[10]=='6')
ones3++;
count3++;
if (ones3>=6) {
SENSORS[1]++;//ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 1);
flip=0;
break;
}
69
}
wfd i3=cof_serDgetc();
} // While finisfing

} // end the big :) else

}
//=============================SENSOR_3_AND_4================================
===
costate {
BitWrPortI(PBDR, &PBDRShadow, 1, 6); // CAN BE REMOVED
if (flip!=1){
wfd i4=cof_serCgetc();
count4=0;
ones4 =0;
while(count4<20){
if(i4==82){
wfd buf3[0]=cof_serCgetc();
wfd buf3[5]=cof_serCgetc();
wfd buf3[10]=cof_serCgetc();
wfd count1 =cof_serCgetc();
if(buf3[0]=='0'&&buf3[5]=='0'&&buf3[10]=='6')
ones4++;
count4++;

}
wfd i4=cof_serCgetc();

} // While finisfing
if (ones4>=6){
SENSORS[4]++;//ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 4);
// break;
}
} // if finisfing

else{
// Now CHECKING FOR SENSOR 4
wfd i4=cof_serCgetc();
count4=0;
ones4 =0;
while(count4<10){
if(i4==82){
wfd buf3[0]=cof_serCgetc();
wfd buf3[5]=cof_serCgetc();
wfd buf3[10]=cof_serCgetc();
wfd count1 =cof_serCgetc();
if(buf3[0]=='0'&&buf3[5]=='0'&&buf3[10]=='6')
ones4++;
count4++;
if (ones4>=6){
SENSORS[4]++;//ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 4);
break;
}
}
wfd i4=cof_serCgetc();
} // While finisfing
BitWrPortI(PBDR, &PBDRShadow, 0, 6);// CHANGE THE SELECT BIT OF THE MUX

// Now CHECKING FOR SENSOR 3


wfd i4=cof_serCgetc();
count4=0;
ones4 =0;
while(count4<10){
if(i4==82){
70
wfd buf3[0]=cof_serCgetc();
wfd buf3[5]=cof_serCgetc();
wfd buf3[10]=cof_serCgetc();
wfd count1= cof_serCgetc();
if(buf3[0]=='0'&&buf3[5]=='0'&&buf3[10]=='6')
ones4++;
count4++;
if (ones4>=6) {
flip=0;
SENSORS[3]++; //ADD IN ARRAY
BitWrPortI(PBDR, &PBDRShadow, 1, 3);
break;
}
}
wfd i4=cof_serCgetc();

} // While finisfing
BitWrPortI(PBDR, &PBDRShadow, 1, 6);// CHANGE THE SELECT BIT OF THE MUX

} // end the big :) else

}
//=============================Clear=========================================

costate{
if(BitRdPortI(PBDR, 0) == 1)
{
BitWrPortI(PBDR, &PBDRShadow, 0, 1);
BitWrPortI(PBDR, &PBDRShadow, 0, 2);
BitWrPortI(PBDR, &PBDRShadow, 0, 3);
BitWrPortI(PBDR, &PBDRShadow, 0, 4);
BitWrPortI(PBDR, &PBDRShadow, 0, 5);
}

}
//=========================================================================
=====
}
}

71

View publication stats

You might also like