# Energy and Delay Aware Data Aggregation in Routing Protocol for Internet of Things
## 5 Energy and Delay Aware Data Aggregation in RPL
### Data Aggregation in Parent Node Using CS Theory  
In the data aggregation process, the sensor nodes transmit the data \(d=\{d1,d2\ldots dn\}^{T}\) to the parent node. The parent node \(PN_{1}\) performs the data collection and aggregation operation and it transmits the aggregated data packets to the DODAG root [35]. Each parent node gets the weighted sum of the random number multiply by the sensor data '\(d\)', called a sparse matrix. In path 1, the node \(PN_{1}\) generates the values are \(r_{11}\times d_{1}\), given in Equation (8).  
\[PN_{1}=r_{11}\times d_{1} \tag{8}\]  
where \(r_{11}\) indicates the random values of parent node 1 (\(PN_{1}\)) and \(d_{1}\) indicates the aggregated data of \(PN_{1}\).  
First, the parent node \(PN_{1}\) sends the aggregated data \(r_{11}\times d_{1}\) to \(PN_{2}\). Second, the parent node \(PN_{2}\) collects the data from \(PN_{1}\) and also it collects and aggregates the data from its child nodes. The mathematical representation is given in Equation (9).  
\[PN_{2}=r_{11}\times d_{1}+r_{12}\times d_{2} \tag{9}\]where \(r_{11}\times d_{1}\) indicates the aggregated data in \(PN_{1}\) and \(r_{12}\times d_{2}\) indicates the aggregated data in \(PN_{2}\).  
Third, the parent node \(PN_{2}\) sends the aggregated data to node \(PN_{3}\). The node \(PN_{3}\) performs the data aggregation operation, given in Equation (10).  
\[PN_{3}=\sum_{i=1}^{k}r_{1i}\times d_{i} \tag{10}\]  
where k indicates the number of parent nodes in each path, \(r_{1i}\) indicates random number of each parent node in path 1.  
In path 1, the parent nodes transmit the data from \(PN_{1}\) to DODAG root. The DODAG root collects the aggregated data in path 1 is called the observation matrix and it is given in Equation (11).  
\[y_{1}=\sum_{i=1}^{k}r_{1i}\times d_{i} \tag{11}\]  
where \(y_{1}\) indicates the DODAG root collects the aggregated data in path 1.  
Similarly, the DODAG root collects the aggregated data from M paths and the DODAG root receives the data packets \(y_{i}\), where i = 1,2,.... M. So, we can represent the data aggregation process mathematically, given in Equation (12).  
\[\left[\begin{array}{c}y_{1}\\ y_{2}\\.\\.\\ y_{M}\end{array}\right]=\left[\begin{array}{cccccc}r_{11}&r_{12}&..&..&r_{1N} \\ r_{21}&r_{22}&..&..&r_{2N}\\.&.&..&..&.\\.&.&..&..&.\\ r_{M1}&r_{M2}&..&..&r_{MN}\end{array}\right]\left[\begin{array}{c}d_{1}\\ d_{2}\\.\\.\\ d_{N}\end{array}\right] \tag{12}\]  
Finally, we can reconstruct the weighted sum of aggregated data from the M path to N node's original data using CS theory. Thus, we can reduce the number of data transmission (M<N) in the network.  
```
Input: Sensor data \(d=\{d1,d2,\ldots,dn\}\) Output: Compressed data \(y\)
1:Compute the data aggregation fromPN1 to DODAG root \(y_{1}=\sum\limits_{i=1}^{k}r_{1i}\times d_{i}\)
2:Compute the data aggregation in DODAG root from the M paths \(\left[\begin{array}{c}y_{1}\\ y_{2}\\.\\ y_{M}\end{array}\right]=\left[\begin{array}{cccccc}r_{11}&r_{12}&..&..&r_{1N} \\ r_{21}&r_{22}&..&..&r_{2N}\\.&.&..&..&.\\.&.&..&..&.\\ r_{M1}&r_{M2}&..&..&r_{MN}\end{array}\right]\) \(d_{N}\)
```  
**Algorithm 2**Data Aggregation Using Compressed Sensing Theory