## 3 Smart Irrigation System Considering Optimal Energy Management with Predictive Control
### Microgrid Controlled by Energy Management System (EMS) for Irrigation System (Second Stage)  
The energy consumption to achieve smart irrigation can be supplied by a microgrid, which requires optimal management. When different isolated energy sources can be managed to supply energy in a coordinated and reliable way, the microgrid is composed by a diesel generator, solar panels and a battery energy storage system (BESS). The second stage consists of the integration of smart irrigation with optimal energy management for the proposed microgrid. The operation of pumps are driven by generation units such as batteries, photovoltaic power or diesel generator. The control is constituted by an optimization problem that, by means of the objective function of Equation (11), minimizes the diesel generation cost (\(Cost_{GD}\)), cost for energy not supplied (\(Cost_{NS}\)) and cost for solar discharge (\(Cost_{VS}\)) in order to take advantage of the solar energy and always preserve the useful life of the batteries. The total costs are obtained by multiplying the cost of generation by technology by the power generated by each unit as the diesel generator power \((P_{GD})\), which is presented when the optimizer detects that the available energy from the batteries or solar generation is not able to supply energy to the system. The power not supplied \((P_{NS})\) occurs when the three energy sources present in the system are not able to meet the required demand. The solar curtailment power \((P_{VS})\) occurs when the solar power is greater than the electrical demand. \[min\sum_{t=1}^{T}\left(Cost_{GD}\cdot P_{GD}(t)+Cost_{NS}\cdot P_{NS}(t)+Cost_{ VS}\cdot P_{VS}(t)\right) \tag{11}\]  
The optimization problem is subject to equality and inequality constraints, referring to the generation units involved in the energy system. The energy system consists of a battery energy storage system (BESS), which operates in discharge mode \((P_{BD})\) when it provides energy to the system and in charge mode \((P_{BC})\) when the batteries are recharged, being part of the energy consumption. The first equality constraint is the energy balance, which is represented in Equation (12). The energy system needs to satisfy the energy balance, where the sum of the energy generated by the solar panels \((P_{GS})\), the diesel generator \((P_{GD})\) and the battery power in discharge mode \((P_{BD})\), is equal to the sum of the electrical demand of electronics devices \((D)\), electrical demand to water pump \((D_{B})\), the solar dump power \((P_{VS})\), the unsupplied power and the battery power in charge mode \((P_{BC})\). \[P_{GD}(t)+P_{GS}(t)+P_{BD}(t)=D(t)+D_{B}(t)+P_{VS}(t)-P_{NS}(t)-P_{BC}(t) \tag{12}\]  
The diesel generator has min power, \(P_{GD_{min}}\), and max power, \(P_{GD_{max}}\), operating limits, which are multiplied by a binary variable \((X_{GD})\). The binary variable decides whether to start the diesel generator or not and the diesel generator limit constraints are expressed in Equation (13). \[P_{GD_{min}}(t)\cdot X_{GD}(t)\leq P_{GD}(t)\leq P_{GD_{max}}(t)\cdot X_{GD}(t) \tag{13}\]  
The BESS model considers two operating modes: loading or unloading. For each mode, an equal loading and unloading efficiency coefficient \((n_{BD}=n_{BC})\) is considered. Equation (14) gives the available energy of the BESS at the first instant \(E(t=1)\), which is equal to the initially available energy \((E_{0})\). The available energy of the BESS can start at a certain percentage, then the energy contributed by the BESS when in charging mode is subtracted and finally the energy of the BESS when in discharging mode is added, which is shown in Equation (14). \[E(t=1)=E_{0}-n_{BC}\cdot P_{BC}(t=1)-\frac{1}{n_{BD}}P_{BD}(t=1) \tag{14}\]  
Equation (15) represents the point when the energy of the BESS starts to respond dynamically after the initial condition.