# Communication Protocol Design for IoT-Enabled Energy Management in a Smart Microgrid
## 3 Information Exchange between HEMSs
### \(k\) Odd, \(\ell\) Odd  
In this case, at the first time slot, the \(\bar{k}\)th HEM, where \(\bar{k}\in\mathcal{K},k\leq\bar{k}\leq\ell\) and \(\bar{k}\mod 2\
eq 0\), transmits and the \(\bar{\ell}\)th HEMS, where \(\bar{\ell}\in\mathcal{K},k\leq\bar{\ell}\leq\ell\) and \(\bar{\ell}\mod 2=0\) receives the messages. That is, the \(\bar{\ell}\)th HEMS receives the following:  
\[v_{\bar{\ell}}= \begin{array}{cc}h_{\bar{\ell}-1,\bar{\ell}}u_{\bar{\ell}-1 }+n_{\bar{\ell}}&\text{if}&\bar{\ell}=K\\ h_{\bar{\ell}-1,\bar{\ell}}u_{\bar{\ell}-1}+h_{\bar{\ell}+1,\bar{\ell}}u_{\bar {\ell}+1}+n_{\bar{\ell}}&\text{if}&\bar{\ell}\
eq K\end{array}  \tag{1}\]  
The signals are decoded as \(\hat{u}_{\bar{\ell}-1}\) and \(\hat{u}_{\bar{\ell}+1}\) at the \(\bar{\ell}\)th HEMS using the maximum likelihood (ML) algorithm. During the second time slot, the \(\bar{\ell}\)th HEMS adds up the decoded signals from the previous time slot and forwards the information to the \(\bar{k}\)th HEMS. The received signal at the \(k\)th HEMS is as follows:  
\[v_{\bar{k}}= \begin{array}{cc}h_{\bar{k}+1,\bar{k}}(\hat{u}_{\bar{k}}+\hat{u }_{\bar{k}+2})+n_{\bar{k}}&\text{if}&\bar{k}=1\\ h_{\bar{k}-1,\bar{k}}(\hat{u}_{\bar{k}-2}+\hat{u}_{\bar{k}})+h_{\bar{k}+1,\bar{ k}}(\hat{u}_{\bar{k}}+\hat{u}_{\bar{k}+2})+n_{\bar{k}}&\text{if}&\bar{k}\
eq 1\end{array}  \tag{2}\]  
The \(k\)th HEMS then decodes \(\hat{V}_{\bar{k},\bar{k}+2}\) and \(\hat{V}_{\bar{k},\bar{k}-2}\) as an estimate of \(\hat{u}_{\bar{k}}\oplus\hat{u}_{\bar{k}+2}\) and \(\hat{u}_{\bar{k}}\oplus\hat{u}_{\bar{k}-2}\), respectively, using ML decoding. Then, the \(k\)th HEMS performs the XOR operation between its own message and the estimates \(\hat{V}_{\bar{k},\bar{k}+2}\) and \(\hat{V}_{\bar{k},\bar{k}-2}\) to extract the signals from the \((\bar{k}-2)\)th and \((\bar{k}+2)\)th HEMSs, respectively. Next, the signals from \((\bar{k}-2)\)th and \((\bar{k}+2)\)th HEMSs are combined at the \(k\)th HEMS who then relays the sum of the signals to the \((\bar{k}-1)\)th and \((\bar{k}+1)\)th HEMSs. The receiving HEMSs perform the XOR operation between the known and received information and obtain the unknown information. The HEMSs continue the process until the \((\frac{|\ell-k|}{2}+1)\)th HEMS extracts the messages from the \(k\)th and \(\ell\)th HEMSs, computes the sum and forwards it to the \((\frac{|\ell-k|}{2})\)th HEMS and \((\frac{|\ell-k|}{2}+2)\)th HEMS. It takes up to \(\frac{|\ell-k|}{2}\) time slots to achieve this. Note that, \(|\ell-k|\) is an even number when both \(\ell\) and \(k\) are odd numbers. Finally, the \((\frac{|\ell-k|}{2})\)th HEMS receives the signal, as follows:  
\[v_{\frac{|\ell-k|}{2}}=h_{\frac{|\ell-k|}{2}+1,\frac{|\ell-k|}{2}}(\hat{u}_{ \bar{k}}+\hat{u}_{\ell})+n_{\frac{|\ell-k|}{2}} \tag{3}\]  
Similarly, the \((\frac{|\ell-k|}{2}+2)\)th HEMS receives the signal as follows:  
\[v_{\frac{|\ell-k|}{2}+2}=h_{\frac{|\ell-k|}{2}+3,\frac{|\ell-k|}{2}+2}(\hat{u }_{\bar{k}+1}+\hat{u}_{\ell-1})+n_{\frac{|\ell-k|}{2}+2} \tag{4}\]  
Both the \((\frac{|\ell-k|}{2})\)th and \((\frac{|\ell-k|}{2}+2)\)th HEMSs extract the XOR of the messages from the \(\ell\)th and \(k\)th HEMSs. The extracted messages are forwarded to the nearby HEMSs, who then continue to forward the sum of the messages to the adjacent HEMSs until the \(k\)th and the \(\ell\)th HEMSs receive the messages from the \((k+1)\)th and \((\ell-1)\)th HEMSs, respectively. Finally, the \(k\)th and \(\ell\)th HEMSs cancel their own signal to extract each other's messages. We explain the above mechanism with the following example:  
_Example:_ Let us consider a microgrid with seven HEMSs, A, B, C, D, E, F, G, where HEMS A wants to exchange information with HEMS G at a certain instant (to model the worst case scenario). At the first time slot, HEMSs A, C, E and G transmit and HEMSs B, D and F receive the sum of signals from HEMS A and C, HEMS C and E and HEMS Eand G, respectively.