# Real-Time Anomaly Detection in Cold Chain Transportation Using IoT Technology
## 3 Materials and Methods
### The Perception Layer
#### 3.2.3 Trip Detection  
For an IoT anomaly detection system to be useful for users in food logistics, alerts should only be sent to personnel when the perishable food is in transport. This is to avoid false alerting when, for example, the delivery vehicle is parked overnight and the refrigeration unit is not powered on. Through being able to detect a trip on the device, an alerting algorithm can be constructed which is only enabled if the vehicle trip status parameter is true, therefore ensuring warnings are only delivered when the vehicle is being used. Additionally, by having the functionality to detect a trip, the device can assume a low-power sleep state and only wake and commence recording once the vehicle is in motion. Putting the device in a sleep state while not in use will significantly reduce battery consumption, thus reducing the maintenance schedule requirements of the device.  
In order to detect a trip, therefore, a trip detection algorithm was incorporated in this work. The algorithm works as follows. When the device is powered on, it assumes a low-power sleep state. The only component active in this state is an ultra-low current piezoelectric accelerometer, which is constantly monitoring the acceleration applied to the device. If the accelerometer records a reading above a user-defined jostle threshold, itcauses an interrupt to be fired on the integrated circuit, changing the state of the logger from low-power sleep mode to broadcast mode. Once in broadcast mode, the logger obtains a GPS fix. The logger then waits for a user-specified length of time before acquiring a second GPS fix. If the second GPS location is not equal to the first GPS location, the vehicle is in motion. The logger updates the trip parameter to true and the process repeats, acquiring two GPS fixes a period of time apart and checking if they match. Note that there is a 'heartbeat' mode implemented on the logger to send data periodically even if it is not in motion. This allows the user to see that the equipment still has battery power and is operating as normal. The frequency of the heartbeat can be configured by the user. For the case of food transportation, 12 h has been chosen. The pseudocode for the algorithm is presented in Algorithm 1.  
```
// Initialisation lowPowerMode = true; lastRecording = time.now(); /* 1-65 mG */ stationaryRecordingTime = 43,200 ; /* 43200 s = 12 h. */ deviceFrequency = 300 ; whiledevicePoweredOndo \(Accel\gets readAccel()\); \(currentTime\gets time.now()\); \(lastReading\gets lastRecording\); \(timeElapsed=currentTime-lastReading\); ifAccel\(\leq\)jostleThresholdthen iftimeElapsed\(\geq\)stationaryRecordingTimethen getSensorData(); lastRecording = time.now();  end if else lowPowerMode = false ; /* Interrupt triggered by Accelerometer to wake device from sleep state. */  end while whilelowPowerMode\(==false\)do \(GPSfix\gets readGPS()\); \(device\_sleep(deviceFrequency)\); \(updatedGPS\gets readGPS()\); ifupdatedGPS!= GPSfix\(\pm\)thresholdthen trip = true; getSensorData(); lastRecording = time.now(); else trip = false; getSensorData(); lastRecording = time.now(); lowPowerMode = true; end if end
```  
**Algorithm 1**Device trip detection