# An IoT-Based System for Efficient Detection of Cotton Pest
## 2 Materials and Methods
### Detection Algorithm  
The Zigbee/Lora Console utility continuously monitored the sensor and its transmitter output. It compared the signal received from the sensor with computed defined criteria. The default standard of such a signal meant that the signal received should have been neither low enough to make sense nor high enough to cross a default standard (stated in the pseudocode below). It was also noise-free. If the compared value was in the middle of the set touchstone or readings went above the minimum set detection level, it was considered that an insect had been detected. The signal value was set heuristically to determine the peak signal of good quality. During the experiment, the amplitude of detecting signals is measured by summing the values as well as by individual values over the defined level. Detection was not considered if the measured signal remained under the predefined detection benchmarks. The following requirements were concentrated for the targeted algorithm:  
* The low-powered electronic circuitry in sensors was used for optimal energy conservation;
* The smallest change for auto-tuning was made for dealing with inconsistency in sensor electronics; with the mentioned necessities, the algorithm had two segments:
* Programmed tuning: during the initialization period, some samples must be obtained from the sensor to be aware of its status. * Motion Analysis: at the manufacturer-defined intervals, the LED emits light, and the PSD reads echelons. Any change occurring in the smooth pulse was noticed and considered the reflection of light, as shown in Figure 2B. If the PSD received any reflected signal within its range, it meant the presence of an object/insect. Figure 2: (**A**) Operational schematic of implementation. (**B**) Operating principle of the sensor. The following algorithm [29] shows the pseudocode for our sensing detector. Pseudocode of the detection algorithm. Main parameters. Serial Zigbee = Receiver, Transmitter  
Sensor Pin (1-6) = A 0, 1, 2, 3, 4, 5 (every pin represents a separate sensor)  
Sensor Value = 1 (Threshold Value in between 1 to 4 as per manufacturer specs, '1' means high sensitivity, (low values signal can be sensed) and '4' means low sensitivity only high value can be detected by the sensor. Distance > 1 && < 4 (it is defined as an object distance with sensors)  
Sensor Coordinates for Localization  
Drone GPS paths/route  
Here, the distance means the closer a worm is to our sensor, the better the received signal will be. For example, it will have a maximum value of four (4), while further away from the worm, the signal would be weaker. It can be reduced to one or even zero but we are not counting the values which are less than one and this is the standard we have stated in the code. In the detection algorithm, important parameters such as sensitivity and delay could be fixed according to precise body dimensions and the movement of target species. It will make our sensor (prototype) suitable for more crops and flying species. From the experiment point of view, the detection threshold (sensor value; SV, and delay) is the most crucial parameter. Our algorithm does not require much energy or resources to run, it can run on a simple computer and provide reasonable results in less time. We ran the algorithm and observed that it functions correctly. As per the time complexity, the device synchronization required n seconds and it is assumed that any sensor takes data from a field in time t. Therefore, it takes (n*t) seconds. The total time of data collection will be t2 and it would take m seconds to send all data to the sink node. Therefore, if ignoring the non-iterative task, then Algorithm 1 may take O(n*t2*m) seconds. As the value of n is very low, we can ignore it and one may write this as O(m*t2).