# A Node Placement Algorithm Utilizing Mobile Nodes in WSN and IoT Networks +
## 5 The Energy Node Placement Algorithm
### Mobile Node  
The mobile nodes are responsible for monitoring their energy and their usage in the network. This monitoring process is performed with the use of two functions: (a) the _network usage function_ (see Algorithm 3), which is called periodically, and (b) the _energy usage function_ (see Algorithm 4), which works as a push notification function.  
```
1:functioncheck_Usable
2:send(_FindAlternative_) to each \(n_{j}\in DN\)
3:wait until all \(n_{j}\in DN\) to reply
4:if\(\exists reply_{j}==FA\)-SearchFailedthen
5:broadcast(_Search_Failed)
6:returnFALSE // \(m_{i}\) needed
7:endifreturnTRUE // \(m_{i}\) not needed
8:endfunction
```  
**Algorithm 3** Check Usable Function for mobile node \(m_{i}\)  
The procedure starts by calling the two usage functions (Algorithm 5, lines 1-2). Based on their outcome, the mobile node decides its next action, as described below:  
* When the node is still needed, and its energy level is OK, the mobile node continues to act as a static node (Algorithm 5, lines 3-5).
* When the node is not needed, and its energy level is OK, the mobile node changes its status to _idle_ and informs the sink node with a _Becomeldle_ message, as well as its neighbors, about its new status. (Algorithm 5, lines 6-10).
* When the node is needed, but its energy level has reached the first threshold (warning threshold), the _replacement procedure_ is triggered: the mobile node requests from the sink a replacement and waits until either the timer (_Treplaced_) expires or its energy level reaches the second threshold (departure threshold). When the timer expires, which determines a time interval in which the sink node needs to send a replacement, the node informs its neighbors about its departure and leaves (by this time, a replacing node should have arrived). However, when the second energy threshold is reached, the mobile node informs the sink node and its neighbors about its departure, and then it returns to its initial position (at the sink) to get recharged (Algorithm 5, lines 11-23).  
When the node's energy level reaches the second threshold, and the node is not needed (possibly it is idle), it will inform the sink node and its neighbors about its return and move back to its initial position (Algorithm 5, lines 24-28).  
```
1:flag_mu = network_usable()
2:flag_eu = energy_usable()
3:if flag_nu == FALSE AND flag_eu == OK then
4:do nothing
5:endif
6:if flag_nu == TRUE AND flag_eu == OK then
7: status = idle
8:send(_Becomeldle_) to sink
9:send(_Idle_) to each \(n_{j}\in neighbor\_list\)
10:endif
11:if flag_nu == FALSE AND flag_eu == TH1 then
12:send(_ShouldBeRplaced_) to sink
13:wait until _Treplace_ OR _remain_energy == TH2
14:if remain_energy == TH2 then
15:send(_ComingBack_) to sink
16:send(_GoingBack_) to each \(n_{j}\in neighbor\_list\)
17:move to init_pos
18:endif
19:if Treplace expired then
20:send(_GoingBack_) to each \(n_{j}\in neighbor\_list\)
21:move to init_pos
22:endif
23:endif
24:if flag_nu == TRUE AND flag_eu == TH2 then
25:send(_ComingBack_) to sink
26:send(_GoingBack_) to each \(n_{j}\in neighbor\_list\)
27:move to init_pos
28:endif
29:upon receive (\"NewPosition\") from sink then
30:move to new_pos
```  
**Algorithm 5** Periodic check algorithm for mobile node \(m_{i}\)