## 4 Energy Aware Software Defined Network (EASDN) Routing Algorithm  
In a WSN, each sensor node has a different communication range to target and send data periodically. In a conventional WSN, sensors broadcast the control messages and data information periodically which leads to energy waste. The control plane is separated from the data plane through SDWSN. In the initial phase of SDWSN, the controller needs to collect the topology information from the underlying network because the control plane is responsible for generating the topology and resource allocation in SDWSN. Software defined networking facilitates to the user, but it has some cost that can be manageable during the topology changing phase as compared to a traditional WSN. SDWSN manages the network resources efficiently by the controller due to its global view, which reduces the sensor nodes' energy consumption significantly. This paper presents the design of an energy aware software defined network (EASDN) routing algorithm that is used to balance the energy consumption of sensor nodes to prolong network lifetime. In our energy aware approach, three phases are included: Neighbor discovery phase, status data collection phase, and the operational phase. The detailed algorithm is shown in Algorithms 1-3. In the neighbor discovery phase, each node discovers its neighbor through broadcasting. As shown in Algorithm 1, each node broadcasts a 'Hello' message and also receives the 'Hello' message from parallel neighboring nodes. If a sensor node finds any new neighbor (which neighbor ID does not exist into the neighbor list), it adds into the neighbor list. Otherwise, it discards the neighbor's 'Hello' message. The first phase stops after the time threshold or when the maximum number of nodes is reached. After the discovery phase, the status data collection phase starts. The whole procedure is given in Algorithm 2. In the status data collection phase, each node shares its own data with neighboring nodes. When neighbor node receives the status data, it forwards to neighbors excluding that neighbor from which the node receives data and the whole procedure continues up to controller neighbor nodes. Hence, the controller collects the data of the whole network through multi-hopcommunication up to a specific time threshold or up to a maximum number of nodes reached. Figure 3 describes the phase one and phase two. After collecting the whole network data, the controller operates the energy-aware algorithm and generates the flow table according to the distance from a controller and residual energy. The example of flow table is shown in Figure 4 and the procedure of the controller operational phase is given in Algorithm 3. The controller sends the control flow table to neighboring nodes (that are reachable to the controller) which extracts the desired data from the flow table and forwards the rest to neighboring nodes. Therefore, the whole network nodes receive the flow table through multi-hop communication. Figure 5 shows the forwarding of the flow table scenario. Figure 4: An example of a flow table. Figure 3: Neighbor discovery and status collection phase. After receiving the flow table from the controller, the operational phase starts. Each node follows the controller flow table instruction and establishes the routing path according to the flow table, which is shown in Figure 6.  
Figure 5: Received flow table. Figure 6: Routing path established. During the operational phase, the network nodes follow the same flow table instructions for sending node data (e.g., after each second generate packet) to the controller until any node dies or the node energy level is lower than the defined threshold. When the controller observes that any node energy goes less than a defined threshold, then it generates a new list according to the residual energy and distance. First, it will check the shortest distance between neighboring nodes to select the forwarder node from the short distance neighbor list, which has high residual energy.