# IoT-Based System for Improving Vehicular Safety by Continuous Traffic Violation Monitoring
## 2 Related Work
### The Fog Computing Layer  
The fog computing layer consists of a processor that is used to count the number of passengers, estimate the vehicle speed, and check the seatbelt status. This layer also has a storage capacity for storing important files, including the local database, the algorithms, and the map. The control unit, in each fog computing node, uses the aggregated data from the edge layer and runs the corresponding estimation algorithms. The output of this layer includes the A JSON format file containing the number of passengers, the aggregated vehicle speeds, the status of seatbelt, and the locations of the vehicles. This file is transmitted to the cloud for additional processing. The second format is a sound format that sends alerts to the drivers if the number of passengers, vehicle speed, or seatbelt use is not in compliance with the rules. Every time a vehicle changes lanes or roads, the control unit obtains the new road's speed limit from the map stored on the fog node. The local database, which is SQLite, assists in storing the vehicle's location, speed, status, and other unprocessed data. Moreover, 3G communication is used between the fog layer and the cloud layer. This layer is responsible for running the estimation algorithms. The passenger count algorithm is illustrated in Figure 2, and it operates when any of the doors are opened, instead of performing continuous counting. This reduces the computational load on the edge. This algorithm reads the movement data from the local database, decides whether a passenger is entering or leaving, counts the number of passengers (n) in the vehicle, and finally compares the result with the permitted number (limit). If the number of passengers exceeds the permitted value, the algorithm plays a warning message. The algorithm sends a fine request to the traffic station after three warning messages, with three minutes between each consecutive message. The second algorithm is the speed estimation algorithm, and its flow chart is shown in Figure 3. To avoid re-estimating the vehicle speed using the same GPS coordinates, this procedure will be initiated as soon as the GPS data for the new coordinates are available in the local database. The speed estimation method is also allowed to run every ten seconds, which makes the speed estimation much easier and reduces the computational load on the processor. The simplest way to calculate speed is to divide the distance traveled by the time. However, because of the altering road topology, it is difficult to calculate the distance between two positions for a moving object with varying speed. Thus, in order to estimate the vehicle's speed accurately, this study uses the Haversine function, H.  
The Haversine algorithms calculate the distance between two coordinates on a sphere [51]. Thus, the distance \(d\) between two consecutive latitudes (_lat_) and longitudes (_lon_) can be determined as  
\[d=2r*\arcsin[\text{sqrt}(\text{H}(\text{lat2}-\text{lat1})\cos(\text{lat1})\cos (\text{lat2})\text{H}(\text{lon2}-\text{lon1}))]), \tag{1}\]  
where \(r\) refers to the radius of the sphere, _lat1_ is the first latitude, _lat2_ is the second latitude, _lon1_ is the first longitude, _lon2_ is the second longitude, and the Haversine function \(H\) can be computed as  
\[H(x)=(1-cos(x))/2, \tag{2}\]  
where \(x\) is the central angle between the two radiuses reaching the first and the second coordinates. Once \(d\) is determined, the speed is computed by dividing \(d\) by the travel time between the two coordinates. Figure 2: The flow chart of the passenger counting algorithm in the fog layer. The algorithm counts the number of passengers when the doors are opened, and warns the driver three times if the number exceeds the permitted value, before sending a fine request to the cloud. The flow chart in Figure 3 further indicates that if the speed \(s\) exceeds the speed limits on the road, the _warningCount_ is incremented and the driver will receive a sound warning message.