# An Energy-Efficient and Secure Routing Protocol for Intrusion Avoidance in IoT-Based WSN
## 3 Proposed Intrusion Avoidance Protocol
### Updating of Cluster Heads  
As WSNs have restricted resources, thus ESR protocol re-formulate the role of cluster heads in a dynamic manner. The main aim behind updating of cluster heads component is to achieve uniform load balancing and energy consumption. ESR protocol observes the subsequent to evaluate the network measure.  
1. When any cluster head \(j\) receives the data packet then firstly it verifies whether it already received the same data packet or not. If yes, then cluster heads simply drop the duplicate data packet to reduce network congestion and energy consumption.
2. It might be a case that the cluster head receives a new data packet, but it has no enough energy resource, i.e., \(e_{j}<\)_threshold_, then it quits from data forwarding and initiates re-election mechanism inside a particular cluster boundary. Furthermore, the ESR protocol also computes the congestion rate \(C_{r}\) of each cluster based on the function, which signifies the normalized congestion value in the range of [0,1] as shown in Equation (8).  
\[C_{r}=\frac{ADR}{ARR} \tag{8}\]  
where ADR represents the average delay ratio among data packets and ARR represents the average reception rate of data packets.  
ESR protocol continuously checks the \(C_{r}\) value, and if it does not fall in the range of [0,1], ESR protocol assumes that a particular cluster head has extended to congestion limit and needs to initiates the re-election process as described in Section 3.1.  
In Algorithm 1, all the main modules of the proposed ESR protocol are lightened.  
```
0: Optimized clusters
1. Procedure clusters generation (\(K\))
2. Evalutes next-hop and generate local table
3. for each nodes \(i\in[1:n]\)
4. do
5. \(C_{v}=\text{w1}*e_{i}+\text{w2}*RSSI_{i}+\text{w3}*\left(\frac{1}{d_{i}\text{ w3}}\right)+\text{w4}*QL_{i}\)
6. endfor
7 if\(C_{v}\) []! = Null
8. set the highest \(C_{v}\) node as a primary cluster head
9. for each node \(i\in\)_clusterhead_ (\(i\))
10. do
11. normal_node response to \(\textit{clusterhead}_{i}\)
12. endfor
13. \(\textit{clusterhead}_{i}\) generates clusters and announces channel access schedules based on TDMA
14. endprocedure
```  
_Secure Data Routing_  
```
1. procedure secure-routing
2. BS generates secret key \(S\)
3. The secret key \(S\) is partitioned into a set of \(t\) cluster heads
4. Share of key \(S_{i}\) transmits to the set of \(t\) cluster heads
5. Node \(n_{i}\) transmits the data \(D_{i}\) to the cluster head
6. Data is encrypted using XOR operation with the key \(S_{i}\): \(E_{i}\ =S_{i}\ \oplus D_{i}\)
7. BS decrypt the received data packets from a set of t cluster heads using the decryption key \(S\)
8. endprocedure
```  
_Updating cluster heads_  
```
1. procedure updating of cluster head
2. for each node \(\in\)_clusterhead_
3. do
4. if\(n_{i}\)_energy_ < threshold OR
5. \(C_{r}=\frac{ADR}{ARR}\)
6. then
7. compute \(C_{v}\)_value_ re-elect new cluster head_
8. update TDMA channel access scheme
9. endif
10. endfor
11. endprocedure
```  
**Algorithm 1**Energy-efficient and secure routing protocol