A Low-Cost Vehicle Counting System based on the Internet of Things

In urban areas where land for parking is very limited, drivers often waste time, fuel, and emissions circling around without information if unoccupied parking spaces are available or not. In this paper, we design and implement a low-cost wireless system to count the number of cars and motorcycles in a parking lot. The system consists of two sensor devices, which are installed at an entrance gate and an exit gate of a parking lot. Each device has a NodeMCU ESP8266, an HC-SR04 ultrasonic sensor, and an MPU92/65 accelerometer. REST API is used as the web service to connect sensor devices and users, that will access the parking information using a web browser. The C4.5 algorithm is utilized to construct a decision tree to classify detected objects like cars, motorcycles, or people. We show by experiment that our sensor devices and the wireless monitoring system work correctly.


Introduction
Population growth increases the need for land transportation, especially for the four-wheeled vehicles (cars) and the two-wheeled ones (motorcycles). This happens as people have to commute every day. In urban areas where land for parking is very limited, it is difficult to find parking spaces, especially during busy hours. Parking lots owned by shopping centers or business premises are either attended by operators or equipped with vehicle parking systems. However, some parking lots which are dedicated to public placesfor example, sports arenas and parksare usually unattended and do not have parking systems. In this case, drivers often waste time, fuel, and emissions circling around such parking lots without information on whether unoccupied parking spaces are available or not.
To solve this problem, an inexpensive sensor-based solution can be used, thanks to the Internet of Things (IoT) technology. In IoT, a device with one or more sensors, batteryless [1] or with batteries, can be used to sense physical phenomena, such as in [2 -5] and send data wirelessly for further processing. Some works in the literature that detect the presence of vehicles have been done. In [6], they use the background subtraction method from Open-CV to detect, count and classify objects. To detect moving objects, they use motion detection functions based on video recordings from cameras. In [7], a car detection system for an IoT-based toll payment is built. With an objective to reduce congestion, the government has implemented the Radio Frequency Identification (RFID)-based electronic transaction payments on several tolls as an alternative. In this research, every vehicle is installed with a receiver that is connected to one user data. This is used to detect the position of the vehicle when it enters the toll gate without obstructing other vehicles. Another research to find unoccupied parking spaces in off-street parking lots is presented in [8]. They utilize ultrasonic sensors and IoT technology using ESP-8266 to provide information regarding the position and status of the parking lots.
Different from the previous works, in this paper, we propose a vehicle counting system based on IoT. Our contributions in this paper are summarized as follows. 1) We design and implement a low-cost wireless system to count the number of cars and motorcycles in a parking lot. The system consists of two sensor devicesone device is deployed at an entrance of a parking lot and the other one is deployed at an exit. Each device has a NodeMCU ESP8266, an HC-SR04 ultrasonic sensor, and an MPU-92/65 accelerometer.
*Corresponding author. Tel.: +62-813-9807-8886 Universitas Katolik De La Salle Manado, Indonesia 2) We use the C4.5 algorithm to construct a decision tree, which is used to classify detected objects as cars, motorcycles, or people. Part of this work has been published as a poster abstract in [9]. The rest of this paper is organized as follows. Section 2 reviews some works in the literature relevant to ours. Section 3 shows our system design and implementation, including hardware and software. Section 4 describes the object classification method used in this paper. Section 5 shows our performance evaluation, and finally Section 6 concludes the paper.

Related Works
A system to count the number of vehicles in an open parking area in real-time using the Open-CV background subtraction method is proposed in [6]. This motion detection function is used to detect moving objects based on video recordings from a camera. It is made using functions available in the Open-CV library through the C++ Application Programming Interface. This study uses several parameters, one of which is learningRate. The value of the learningRate parameter is based on environmental conditions and determined from the test results of the learningRate parameter variation. In this study, they also classify vehicles, i.e. cars or motorcycles, using the coordinate data of the height and width to determine the size of the detected vehicles.
In [7], a car detection system for toll payment based on the Internet of Things is proposed. The purpose of this research is to reduce congestion at the toll entrance gate. The Internet of Things technology is utilized since the Radio Frequency Identification (RFID)-based electronic transaction payment system cannot reduce the congestion problem significantly. On the Internet of Things-based system, each vehicle is equipped with a receiver connected to one user data. Hence, the system is able to detect the position of the vehicle when entering the toll gate without obstructing other vehicles. This transaction process can be carried out at a radius of 10 meters from the toll gate without stopping vehicles with an average speed of 10 km/hour. In the implementation, the time required is around 10 seconds for the toll entrance gate and 15 seconds for the toll exit gate.
In [8], an application to track or find empty parking spaces in off-road parking lots by utilizing ultrasonic sensors and the Internet of Things is developed. To track or find an empty parking space, they use an ultrasonic sensor and an ESP-8266 chip, which can provide information about the position and status of a parking space, whether it is filled or empty.
Data mining is a process of dredging or gathering important information from a large amount of data, resulting in several models to identify patterns between attributes that exist in the data set. Classification is a process in data mining that aims to find valuable patterns from a relatively large amount of data, build a model that describes and differentiates the classes or concepts for future prediction. For example, in [10], the authors use large amounts of ad-hoc vehicle's real-time traffic data to predict traffic congestion in a city by using classification and clustering methods. This study shows that traffic congestion can be predicted more accurately and the percentage of accuracy can be increased by increasing the size of the training data.
In [2], the authors created a wireless system based on the Internet of Things to monitor and predict water quality in public swimming pools. They utilize a data mining prediction model, namely the Iterative Dichotomiser 3 decision tree algorithm. In [11], data mining is used to identify the sex of a tarantula by classifying the training data obtained for almost a year.
The C4.5 algorithm is one of the algorithms used for classification or segmentation and is predictive. This algorithm is the development of the Iterative Dichotomiser 3 (ID3) algorithm and hence they have the same basic principles. C4.5 is used in [12] to determine a model for predicting soil quality by using training data on soil composition. The experimental results obtained show that the expression of knowledge using the C4.5 algorithm is easy to understand, improves forecasting accuracy, and provides a reliable theoretical basis for precision fertilization. Another example of C4.5 utilization is to minimize the workload of student admissions in planning school promotions and improving planning strategies by classifying demographics [13]. In addition, the C4.5 algorithm is also used to identify diseases from existing symptoms [14].
In this work, the authors identify the possibility of indications of diabetes insipidus apart from gestational diabetes insipidus at productive age (students). They use the k-means algorithm to classify the symptoms, and the C4.5 algorithm to identify the type of diabetes. Furthermore, in [15], C4.5 is used to analyze catfish and carp diseases to determine the history of fish disease relationships, which will be used to identify fish diseases.

System Design and Implementation
The wireless vehicle counting system is illustrated in Fig. 1. We attach two sensorsan HC-SR04 ultrasonic sensor and an MPU-92/65 accelerometerto each sensor device. We use the ultrasonic sensor to measure the physical distance to an object in front of it, while the accelerometer is used to measure any vibration resulted from the moving object. The distance and vibration data from the sensors is sent to a web server wirelessly using the NodeMCU ESP8266's ESP-12E Wi-Fi module. This data is received by REST API and forwarded to the application.  In this work, we use two sensor devices, where we deploy one device at an entrance of a parking lot and the other one is at an exit. Table 1 lists the hardware and software components used in the system.
The total cost of the main components, i.e. a NodeMCU ESP8266, an HC-SR04 ultrasonic sensor and an MPU-92/65 accelerometer for one sensor device in our system is approximately US$ 8. Compared to [6] that uses a webcam and a Raspberry Pi 3 model B, the total cost is around US$ 43. The system in [7] requires that each vehicle must be equipped with a receiver, while the system in [8] requires that each parking space must be monitored by one sensor device. Even though the total cost for a receiver in [7] that consists of a NodeMCU ESP8266, a buzzer, and an LCD 16x2 is only around US$ 4, they must take into account the number of cars that use the system. Similarly, even though the total cost for a sensor device in [8] that consists of an ESP8266 D1 mini, an HC-SR04 ultrasonic sensor, and an LCD 16x2 is around US$ 4, they must provide as many devices as the number of parking spaces. Our system, on the other hand, only requires two devices and hence its total cost is the lowest. The prices reported in this paper are calculated based on prices from www.tokopedia.com.

Hardware
We show the hardware design of our sensor device in Fig. 2. Each sensor device has a NodeMCU ESP8266, an HC-SR04 ultrasonic sensor, and an MPU-92/65 accelerometer. NodeMCU and the ultrasonic sensor are attached to a breadboard, while the accelerometer is tied to a rope as a physical medium to carry vibrations. Both NodeMCU and the ultrasonic sensor are powered by external batteries, while the accelerometer gets power from NodeMCU. To connect NodeMCU and the accelerometer, we connect NodeMCU's D5 pin to the accelerometer's SDA pin, D6 pin to SCL pin, GND pin to GND pin, and 3V pin to VCC pin. Then, to connect NodeMCU and the ultrasonic sensor, we connect NodeMCU's D7 pin to the ultrasonic sensor's echo pin, while D8 pin is connected to the trigger pin. The pins configuration is summarized in Table 2 and the hardware implementation is shown in Fig. 3.

Software
The wireless vehicle counting system is divided into the client-side and the server-side. At the client-side, we have an Arduino program that is uploaded to NodeMCU of both sensor devices. The client flowchart is shown in Fig. 4. First, the Arduino program checks if all sensors are connected to NodeMCU and whether the Internet connection is available. Then, the Arduino program reads the sensors' data and sends it wirelessly to the webserver.
At the server-side, we have REST API as the back end and a PHP (Hypertext Preprocessor) program as the front end. The server flowchart is depicted in Fig. 5. First, if the Internet connection is available, we check if data is successfully received. If the data is available, it is processed and then stored in the database. To access the parking information from the web server, users can use a web browser. We utilize the C4.5 algorithm to construct a decision tree in order to classify objects as cars, motorcycles, or people. Then, we count how many cars and motorcycles that enter and exit the parking lot. The calculation result is displayed to users as the number of free parking spaces.

Object Classification Method
In this section, we present some classified objects based on data obtained from the sensor devices. We utilize the C4.5 algorithm for object classification. C4.5 is a method used for classification problems in machine learning and data mining. Its objective is for grouping and it is predictive for finding patterns with relatively large to very large values. In general, C4.5 has the following steps to build a decision tree: (1) find an attribute to be the root, (2) create a branch for each value and divide cases into branches, and (3) repeat the process for each branch until all cases on the branch have the same class. To choose a variable as the root, we calculate and select the highest gain using the following formula: where S is a set of cases, A is the attribute, and n is the number of partitions of attribute A. Furthermore, the number of cases in the i-th partition is |Si| and the number of cases in S is |S|. Before we calculate gain, we need to calculate entropy (see Equation 2) to determine how informative an input variable is, where Pi is the proposition of Si to S. Table 3 shows some of the raw data taken using the ultrasonic sensor and the accelerometer. Distances acquired by the ultrasonic sensor are measured in centimeters. The three-axis accelerometer data on the Xaxis is denoted by Ax, on the Y-axis is denoted by Ay, and on the Z-axis is denoted by Az. We also record the time when the data is captured and the class of objects to be classified. The class is divided into car, motorcycle, and person. Then, from the raw data that we obtained, we further clean and transform the data into categories in order to simplify the algorithm's calculations.
The following range formula is used to create categories for each variable:  where Xmax is the highest reading of a variable, Xmin is the lowest reading of that variable, and n is the number of categories that we want. In this paper, we use n = 3. For example, the categories for the distance variable are near (70.23 cm -78.153 cm), medium (78.153 cm -86.076 cm), and far (86.076 cm -94.00 cm). After that, for each variable, we calculate the average value for each available data to decide in which categories the data belongs. Table 4 shows the data that has been cleaned. A decision tree is then created after calculating the total entropy, entropy of each variable, the gain, and the highest gain. In this case, the total entropy = 1.5849625 and gain = 0.918295834. The calculation results of the entropy and gain are shown in Table 5.

Performance Evaluation
To evaluate our proposed system, including both hardware and software, we conduct a series of experiments, which are described in the following. The parameters used in the evaluation are given in Table 6.

Experimental setup
In this research, we do not detect the direction of vehicles. Therefore, we assume that cars and motorcycles enter the parking lot through the entrance gate and leave through the exit gate. To count the number of cars and motorcycles, we utilize two sensor devicesone at an entrance to sense them entering the parking lot and one at an exit to sense them leaving. Each sensor device is mounted at a certain height above the ground as shown in Fig. 6. The height is carefully chosen, i.e. 0.5 m, so that the ultrasonic sensor can sense moving objects in front of it. As for the sensor device's distance to the moving object, we use the HC-SR04 ultrasonic sensor detection range, i.e. from 2 centimeters to 4 meters. The accelerometer is attached to a rope that is put on the ground across the road. Therefore, when vehicles' wheels touch the rope, the accelerometer can sense the vibration.
Furthermore, we also assume that two moving objects are at least one second apart. Otherwise, they are detected as one object. The moving speed of an object is at most 10 km/hour as it is the maximum speed allowed for vehicles in the parking area. An object is detected when the readings of the ultrasonic sensor and the accelerometer change. When this happens, the sensor readings are sent to the webserver. To classify the detected objects, we use a decision tree which is constructed using the C4.5 algorithm. Our system classifies the objects as either cars, or motorcycles, or people. When the system detects a car or a motorcycle, it updates its counter and users can get the parking information using a web browser.

Experimental Results and Discussion
First of all, we test the hardware that has been built, whether all sensors can function properly and the device can send data to the webserver. We also test if the webserver can receive the data. The results of our experiment show that all sensors work properly, the device can send data and the web server can receive the data as shown in Fig. 7.   Fig. 9. Figure  8 is the main page of the application. On this page, there is a digital clock at the top of the application and the parking information in the middle, which indicates the status of the parking lot. Free means there are unoccupied parking spaces, whereas full means all parking spaces are occupied. Furthermore, Figure 9 shows parking details that contain detailed information from the acquired data. On this page, there is a bar chart that informs us how many vehicles (cars and motorcycles) enter and leave the parking lot.

Conclusion and Future Work
This paper proposes a low-cost Internet of Thingsbased vehicle counting system to count the number of cars and motorcycles in a parking lot. Two sensor devices are required in this systemone at an entrance gate and one at an exit gate of a parking lot. Each sensor device consists of a NodeMCU ESP8266, an HC-SR04 ultrasonic sensor and an MPU-92/65 accelerometer. We use REST API as the web service to connect sensor devices and users. We use C4.5 to build a decision tree to classify detected objects like cars, motorcycles, or people. We show by experiment that the sensor devices and the system work correctly.
Due to the COVID-19 pandemic and social restrictions that has been in place when we carried out the evaluation, we were unable to conduct a thorough experiment. We will continue to acquire more data over a longer period of time once we are able to do so. We also plan to deploy more sensor devices at more entrance and exit gates.