Middleware to Operate Smart Photovoltaic Irrigation Systems in Real Time

: Climate change, water scarcity and higher energy requirements and electric tari ﬀ compromises the continuity of the irrigated agriculture. Precision agriculture (PA) or renewable energy sources which are based on communication and information technologies and a large amount of data are key to ensuring this economic activity and guaranteeing food security at the global level. Several works which are based on the use of PA and renewable energy sources have been developed in order to optimize di ﬀ erent variables of irrigated agriculture such as irrigation scheduling. However, the large amount of technologies and sensors that these models need to be implemented are still far from being easily accessible and usable by farmers. In this way, a middleware called Real time Smart Solar Irrigation Manager (RESSIM) has been developed in this work and implemented in MATLAB TM with the aim to provide to farmers a user-friendly tool for the daily making decision process of irrigation scheduling using a smart photovoltaic irrigation management module. RESSIM middleware was successfully tested in a real ﬁeld during a full irrigation season of olive trees using a real smart photovoltaic irrigation system.


Introduction
The increase in energy requirements and water scarcity, has led to a rise in the operating costs of irrigated agriculture [1], aggravated by higher electricity tariff and fuel prices. Likewise, pressurized irrigation systems have greater environmental impact than gravity irrigation systems due to the use of energy [2,3]. To minimise these adverse impacts, the use of precision agriculture [4] and renewable energy sources is being promoted to improve the sustainability of irrigation agriculture. Both precision agriculture and the use of renewable energy sources require the use of a large amount of data from highly heterogeneous sensors as well as the use of information and communication technologies (ICTs) for their communication. In this way, several works have been developed, such as Reference [5], in which solar energy was applied to small-scale irrigation, covering daily water needs. The hybrid use of several renewable energy sources (wind/solar) have also been developed to meet irrigation energy needs and to increase the crop profitability [6,7] developed a Smart Photovoltaic Irrigation Manager (SPIM), which provides a daily irrigation scheduling for crops at plot scale, using photovoltaic (PV) solar energy to pump water to the irrigation network to meet crop irrigation requirements according to the available solar energy. This model developed in MATLAB TM synchronizes the photovoltaic solar energy to optimize the daily irrigation scheduling computing the daily irrigation requirements, the hydraulic behaviour of the irrigation network establishing the optimum irradiance threshold per irrigation sector, the instantaneous photovoltaic power production from pyranometer sensor and the daily soil water balance. Bhattacharjee et al. [8] developed a model to collect underground water using a submersible pump by a solar photovoltaic system in remote areas. The model included a solar charge controller connected to an inverter for the pump motor operation. Bouchakour et al. [9] developed an algorithm based on fuzzy logic to improve photovoltaic water pumping system performance. All these models are based on data from a wide range of sensors, various web platforms such as climatic web platforms and commercial irrigation controllers [7]. However, the volume and nature of the data used by these models introduces a significant challenge in the implementation of solar irrigation management tools in commercial fields which must operate in real time. In addition, a development trend in precision agriculture is standardisation [10], in order to achieve software sharing and interoperability of computer programs [11] and provide to farmers a robust, scalable and adaptable tool to help the daily making decision process. Peres et al. [12] worked in this context developing an autonomous intelligent gateway infrastructure for in-field processing in precision viticulture. However, this model implements only the hardware, communication capabilities and software architecture of an intelligent autonomous gateway, designed to provide the necessary middleware between locally deployed sensor networks and a remote location within the whole farm without considering the implementations of smart photovoltaic irrigation models developed by other researchers.
In this work, a new middleware called Real time Smart Solar Irrigation Manager (RESSIM) has been developed to control the operation of a smart photovoltaic irrigation system in real time. The core of this middleware is a smart photovoltaic irrigation management module, developed in MATLAB TM , that handles the information received from the irrigation controller and climate web platforms. RESSIM has been developed under a user-friendly graphical user interface (GUI) with MATLAB TM GUIDE and was applied for irrigation management of a commercial olive grove in southern Spain.

Model Description
RESSIM is a platform which connect software modules, graphical user interface (GUI), databases (building and management) and remote connections with different data sources (irrigation controller and its linked sensors and web platforms). RESSIM links six modules ( Figure 1): smart PV irrigation management, database, irrigation control, field sensing, agroclimatic information and GUI to make RESSIM a user-friendly middleware. These modules are described next. The core of RESSIM is the smart photovoltaic irrigation management module known as Smart Photovoltaic Irrigation Manager (SPIM) [7]. This model synchronizes the availability of photovoltaic energy with the energy demand of the different irrigation sectors to meet the daily crop irrigation needs. SPIM integrates several calculation modules to obtain the decision variables required for  The core of RESSIM is the smart photovoltaic irrigation management module known as Smart Photovoltaic Irrigation Manager (SPIM) [7]. This model synchronizes the availability of photovoltaic energy with the energy demand of the different irrigation sectors to meet the daily crop irrigation needs. SPIM integrates several calculation modules to obtain the decision variables required for irrigation management. One of these variables is the daily crop irrigation needs, calculated from evapotranspiration and precipitation data and crop water requirements. The irrigation system is operated in sectors, thus hydraulic modelling techniques are used to estimate the power requirements of each one. The supplied electric power depends on the energy production of the photovoltaic plant, estimated according to Reference [13].
Additionally, in the SPIM model the soil is considered as a water storage tank, so the soil water balance is computed daily. Using this information and daily priority rules, the optimum operating sequence of the irrigation sectors can be established in real time. As only photovoltaic energy is used to power the system, the presence of clouds may prevent the system from operating long enough to meet the daily crop irrigation needs. When this is the case, soil water content is checked, and if the moisture content is enough to fill the irrigation deficit, soil water is assumed to satisfy the irrigation needs of that day. Otherwise, the irrigation time of the following day is increased to compensate for the irrigation deficit.
Real-time information about climate type, soil moisture content, solar installation and the status of the system's hydraulic devices (e.g., open valve, pressure head at the pumping station) is used to calculate all the elements described above. The information required by SPIM is obtained from the database module and the communication module with the climate web platform. A detailed description of the SPIM model can be found in [7].

Irrigation Controller
In most farms, commercial irrigation controllers are used to apply the crop water requirements according to a predefined irrigation scheduling plan. Most commercial irrigation controllers are built over a database, "commercial database", which is used to operate them (reading sensor measurements and sending orders to actuators). Remote sensors are also included in the commercial irrigation controller as an external device and their measurements are recorded in the same database. Thus, commercial irrigation controllers are just an interpreter of its database through create, read, update and delete (CRUD) queries. These CRUD queries enable the irrigation controller to read the external sensors and give any orders to filters, electro valves, etc., which control the irrigation process. Likewise, the opening or closing process of all irrigation system elements (electro valves, filters . . . ) are also managed by the irrigation controller through its database. Consequently, irrigation controller is just a listener of its database, where the status of each field sets its behaviour.
Thus, managing the commercial database allows the irrigation controller and all their connected devices to be handled. Hence, in this work, the commercial irrigation controller was managed by RESSIM to target the commercial database through Structured Query Language (SQL) queries.

Field Sensing Module and Remote Agroclimatic Data Module
Two different ways to get and save information from field sensors were implemented in RESSIM. Most of the commercial irrigation controllers enable the integration of multiple sensors regardless of the transmission protocol. Measures from these sensors can be saved in its commercial database. Thus, RESSIM could read and operate these sensors through its database. For example, RESSIM managed in real time a pyranometer which was installed in the studied field. By RESSIM and the real time processing of pyranometer data, the smart PV irrigation management module could compute the instantaneous power available for irrigation sectors. Alternatively, RESSIM could integrate other Water 2019, 11, 1508 4 of 11 sensors, different from those initially included in the database. Thus, any other sensor, that can send information to a cloud repository, may be managed by the system.
The remote agroclimatic data module allowed agro-climatic information required by the smart PV irrigation management module (SPIM, in this work) to be obtained. These data are collected from a public weather stations platform. This module, developed in MATLAB TM , gets information from public websites by HTTP callbacks and sent it to RESSIM as JavaScript Object Notation (JSON) format. Then, this information was managed by RESSIM and sent to the smart PV irrigation management module.

Database Module
The database module was made up by two databases: commercial and open databases. The commercial one was a relational database which resides on a separate machine from the irrigation controller. This commercial database was accessible by TCP/IP connection and SQL server. Frequently, the irrigation controller database is private. Therefore, information about username, password, database name and port where the database was running was necessary for RESSIM. In this work, the commercial database was managed by CRUD functions from RESSIM.
The open database was automatically created by RESSIM at the first-time execution. This database consisted of five relational tables and was developed in MATLAB TM , using SQL server ( Figure 2). The first table, "Farm's Owner Table", relates each user to their farm(s). Information about location and the number of sectors that make up the irrigation network is required by SPIM. Thus, farm description is recorded in "Farm Description Table".
RESSIM. Most of the commercial irrigation controllers enable the integration of multiple sensors regardless of the transmission protocol. Measures from these sensors can be saved in its commercial database. Thus, RESSIM could read and operate these sensors through its database. For example, RESSIM managed in real time a pyranometer which was installed in the studied field. By RESSIM and the real time processing of pyranometer data, the smart PV irrigation management module could compute the instantaneous power available for irrigation sectors. Alternatively, RESSIM could integrate other sensors, different from those initially included in the database. Thus, any other sensor, that can send information to a cloud repository, may be managed by the system.
The remote agroclimatic data module allowed agro-climatic information required by the smart PV irrigation management module (SPIM, in this work) to be obtained. These data are collected from a public weather stations platform. This module, developed in MATLAB TM , gets information from public websites by HTTP callbacks and sent it to RESSIM as JavaScript Object Notation (JSON) format. Then, this information was managed by RESSIM and sent to the smart PV irrigation management module.

Database Module
The database module was made up by two databases: commercial and open databases. The commercial one was a relational database which resides on a separate machine from the irrigation controller. This commercial database was accessible by TCP/IP connection and SQL server. Frequently, the irrigation controller database is private. Therefore, information about username, password, database name and port where the database was running was necessary for RESSIM. In this work, the commercial database was managed by CRUD functions from RESSIM.
The open database was automatically created by RESSIM at the first-time execution. This database consisted of five relational tables and was developed in MATLAB TM , using SQL server ( Figure 2). The first table, "Farm's Owner Table", relates each user to their farm(s). Information about location and the number of sectors that make up the irrigation network is required by SPIM. Thus, farm description is recorded in "Farm Description Table".  Several agroclimatic parameters, such as reference evapotranspiration, rainfall, temperature and solar radiation are required by SPIM. For each farm, these data are stored in "Agroclimatic Data Table". On the other hand, the power demand of each sector of the irrigation network computed by SPIM is saved in "Farm PV Power Table". Finally, "Operating Sector Table"    After the initial setting, RESSIM determines the inputs/outputs, as well as SPIM's variables, which are shown in the main screen of the GUI. At this point, RESSIM remains in a "listening mode" to both the GUI and SPIM model. When SPIM requires climatic information, it sends a request through RESSIM (Figure 3, dotted line). Via HTTP callbacks, RESSIM obtains the climatic data from open websites and this information is sent back to SPIM. By the same token, the information received by field sensors, such as pressure head at pumping station or irradiance values, is obtained by RESSIM through SQL queries, by the connection between RESSIM software, the commercial database, RESSIM GUI and SPIM (Figure 3, dash line).
Once the sequence of operating sectors (irrigation scheduling) is determined as described by [7], this information is sent to the database module by RESSIM and stored in the open database, parallelly modifying the commercial database. Thus, the irrigation controller, which is connected to the After the initial setting, RESSIM determines the inputs/outputs, as well as SPIM's variables, which are shown in the main screen of the GUI. At this point, RESSIM remains in a "listening mode" to both the GUI and SPIM model. When SPIM requires climatic information, it sends a request through RESSIM (Figure 3, dotted line). Via HTTP callbacks, RESSIM obtains the climatic data from open websites and this information is sent back to SPIM. By the same token, the information received by field sensors, such as pressure head at pumping station or irradiance values, is obtained by RESSIM through SQL queries, by the connection between RESSIM software, the commercial database, RESSIM GUI and SPIM (Figure 3, dash line).
Once the sequence of operating sectors (irrigation scheduling) is determined as described by [7], this information is sent to the database module by RESSIM and stored in the open database, parallelly modifying the commercial database. Thus, the irrigation controller, which is connected to the commercial database, receives the length of the irrigation event of each sector in real time (Figure 3, dot and dash line).
Finally, a friendly GUI (Figure 4) was designed to visualize the values of the main decision variables in real time and to interact with SPIM and the irrigation controller. Any information shown or managed by RESSIM GUI follows the path shown in Figure 3 with the bold dash line. The GUI was divided into seven containers: experimental farm, pumping station-operating sector, photovoltaic plant, irrigation season, water volume records, soil water balance and irradiance threshold per sector. The experimental farm container shows the farm's layout highlighting the operating sector and the spatial hydraulic simulation of this sector in real time (pressure and flow for each emitter of the irrigation sector as well as for hydrant). Pressure head, in m, and flow applied, in m 3 h −1 , by the pumping station is shown in the container labelled "Pumping station-operating sector". Information related to the photovoltaic plant as irradiance, in W m −2 , and photovoltaic power, in W, is shown in the "Photovoltaic plant" container. The "Irrigation season" container provides the beginning date of the irrigation season, the estimated flowering date and the real flowering date. When estimated and real flowering dates do not match, SPIM recomputes the irrigation scheduling. The "Water volume records" container provides the total amount of water applied to each irrigation sector, in m 3 , the irrigation target volume in this irrigation season per irrigation sector, in m 3 , the percentage of irrigation requirements which have been satisfied, in %, the date of the last irrigation event per irrigation sector and the estimated irrigation requirements for the following day. The real time value of the soil water content in each sector is displayed in the "Soil water balance container". The range of irradiance in which each sector operates, determined by SPIM, is shown in the "Irradiance threshold per sector container". Finally, the current date, and start/stop button were in the upper-right corner of the GUI. threshold per sector. The experimental farm container shows the farm's layout highlighting the operating sector and the spatial hydraulic simulation of this sector in real time (pressure and flow for each emitter of the irrigation sector as well as for hydrant). Pressure head, in m, and flow applied, in m 3 h −1 , by the pumping station is shown in the container labelled "Pumping station-operating sector". Information related to the photovoltaic plant as irradiance, in W m −2 , and photovoltaic power, in W, is shown in the "Photovoltaic plant" container. The "Irrigation season" container provides the beginning date of the irrigation season, the estimated flowering date and the real flowering date. When estimated and real flowering dates do not match, SPIM recomputes the irrigation scheduling. The "Water volume records" container provides the total amount of water applied to each irrigation sector, in m 3 , the irrigation target volume in this irrigation season per irrigation sector, in m 3 , the percentage of irrigation requirements which have been satisfied, in %, the date of the last irrigation event per irrigation sector and the estimated irrigation requirements for the following day. The real time value of the soil water content in each sector is displayed in the "Soil water balance container". The range of irradiance in which each sector operates, determined by SPIM, is shown in the "Irradiance threshold per sector container". Finally, the current date, and start/stop button were in the upper-right corner of the GUI.

Implementation of RESSIM in a Real Case Study
RESSIM has been designed to be implemented to the most irrigation controllers which are based on a SQL database and most smart photovoltaic irrigation managers. The implementation of RESSIM just requires a PC with an internet connection to get the full potential of the tool.
A test field of 13.4 ha located on the experimental farm of Cordoba University (Southern Spain) was selected to test the proposed RESSIM middleware ( Figure 5). The irrigation system of the experimental farm is fitted with an irradiance sensor, a flow meter, a pressure meter and a soil

Implementation of RESSIM in a Real Case Study
RESSIM has been designed to be implemented to the most irrigation controllers which are based on a SQL database and most smart photovoltaic irrigation managers. The implementation of RESSIM just requires a PC with an internet connection to get the full potential of the tool.
A test field of 13.4 ha located on the experimental farm of Cordoba University (Southern Spain) was selected to test the proposed RESSIM middleware ( Figure 5). The irrigation system of the experimental farm is fitted with an irradiance sensor, a flow meter, a pressure meter and a soil moisture sensor, which are connected to a commercial irrigation controller, AGRONIC 4000 (https://www.progres.es/es/agronic4000).
Water 2019, 11, x FOR PEER REVIEW 7 of 11 moisture sensor, which are connected to a commercial irrigation controller, AGRONIC 4000 (https://www.progres.es/es/agronic4000). RESSIM was applied for an entire irrigation season. Thus, Figure 6 shows the applied irrigation depth, the required irrigation depth and the irrigation correction by SPIM model during the irrigation season. Two days (16 April and 28 July, Julian days of the year 106 and 209, respectively) were selected and marked in Figure 6 by asterisk in order to discuss the results with RESSIM management. The irrigation scheduling for the two selected days is shown in Figure 7a,b respectively. Both figures show the synchronization of the operating sectors of the irrigation network and the photovoltaic power generation determined by SPIM and managed by RESSIM. Figure 7a (Julian day of the year 106) shows the synchronization of the operating sector in a typical cloudy day while RESSIM management for a sunny day is shown in Figure 7b. The hard task to manage in real time (manually operate) the synchronization of the irrigation operation with the photovoltaic power generation highlights the utility of RESSIM in daily irrigation scheduling.  RESSIM was applied for an entire irrigation season. Thus, Figure 6 shows the applied irrigation depth, the required irrigation depth and the irrigation correction by SPIM model during the irrigation season. Two days (16 April and 28 July, Julian days of the year 106 and 209, respectively) were selected and marked in Figure 6 by asterisk in order to discuss the results with RESSIM management. The irrigation scheduling for the two selected days is shown in Figure 7a,b respectively. Both figures show the synchronization of the operating sectors of the irrigation network and the photovoltaic power generation determined by SPIM and managed by RESSIM. Figure 7a (Julian day of the year 106) shows the synchronization of the operating sector in a typical cloudy day while RESSIM management for a sunny day is shown in Figure 7b. The hard task to manage in real time (manually operate) the synchronization of the irrigation operation with the photovoltaic power generation highlights the utility of RESSIM in daily irrigation scheduling.
(https://www.progres.es/es/agronic4000). RESSIM was applied for an entire irrigation season. Thus, Figure 6 shows the applied irrigation depth, the required irrigation depth and the irrigation correction by SPIM model during the irrigation season. Two days (16 April and 28 July, Julian days of the year 106 and 209, respectively) were selected and marked in Figure 6 by asterisk in order to discuss the results with RESSIM management. The irrigation scheduling for the two selected days is shown in Figure 7a,b respectively. Both figures show the synchronization of the operating sectors of the irrigation network and the photovoltaic power generation determined by SPIM and managed by RESSIM. Figure 7a (Julian day of the year 106) shows the synchronization of the operating sector in a typical cloudy day while RESSIM management for a sunny day is shown in Figure 7b. The hard task to manage in real time (manually operate) the synchronization of the irrigation operation with the photovoltaic power generation highlights the utility of RESSIM in daily irrigation scheduling.  Two different times of the day were selected in Figure 7a,b (red asterisk), 11 am and 12 am, respectively. For both selected time periods, RESSIM GUI is shown in Figure 8a,b respectively. S3 and S2 irrigation sectors were working in the selected instant, respectively. Figure 8a highlights the first day of the irrigation season in which S2 irrigation sector was working at 12:00 h. Thus, the total water volume applied until that day was 0.3%, 0.4% and 0.5% for the three irrigation sectors, respectively. The volume applied at the studied hour on that day (Julian day of the year 106) was 15.9 m 3 , 20.8 m 3 and 39.6 m 3 for S1, S2 and S3, respectively, and the total water volume required on that day was 48.7 m 3 , Two different times of the day were selected in Figure 7a,b (red asterisk), 11 am and 12 am, respectively. For both selected time periods, RESSIM GUI is shown in Figure 8a,b respectively. S3 and S2 irrigation sectors were working in the selected instant, respectively. Figure 8a highlights the first day of the irrigation season in which S2 irrigation sector was working at 12:00 h. Thus, the total water volume applied until that day was 0.3%, 0.4% and 0.5% for the three irrigation sectors, respectively. The volume applied at the studied hour on that day (Julian day of the year 106) was 15.9 m 3 , 20.8 m 3 and 39.6 m 3 for S1, S2 and S3, respectively, and the total water volume required on that day was 48.7 m 3 , 41.8 m 3 and 60.7 m 3 , respectively. The estimated water volume for the following day (day of the year 107) was 52.3 m 3 , 44.9 m 3 and 65.1 m 3 , for the three irrigation sectors, respectively. For the day of the year 209 (Figure 8b), the percentage of water applied until that day was 73.2%, 73.3%, and 72.1% of the total water requirements of the irrigation season, for the three irrigation sectors, respectively. At that moment, the daily applied water volume for S1, S2 and S3 was 4479 m 3 , 3710 m 3 and 5450 m 3 , respectively. According to SPIM model, the estimated water volume for the following day (Julian day of the year 210) was 28.3 m 3 , 24.9 m 3 and 37.0 m 3 , for the three irrigation sectors, respectively. Both scenarios (Figure 8a,b) show the high synchronization determined by SPIM and carried out by RESSIM between the operation of the irrigation sectors and photovoltaic power generation linked to irrigation requirements.
Although the SPIM model could be run manually, the daily synchronization and the daily irrigation scheduling based on solar irrigation would be impossible without RESSIM middleware. For the day of the year 209 (Figure 8b), the percentage of water applied until that day was 73.2%, 73.3%, and 72.1% of the total water requirements of the irrigation season, for the three irrigation sectors, respectively. At that moment, the daily applied water volume for S1, S2 and S3 was 4479 m 3 , 3710 m 3 and 5450 m 3 , respectively. According to SPIM model, the estimated water volume for the following day (Julian day of the year 210) was 28.3 m 3 , 24.9 m 3 and 37.0 m 3 , for the three irrigation sectors, respectively. Both scenarios (Figure 8a,b) show the high synchronization determined by SPIM and carried out by RESSIM between the operation of the irrigation sectors and photovoltaic power generation linked to irrigation requirements.
Although the SPIM model could be run manually, the daily synchronization and the daily irrigation scheduling based on solar irrigation would be impossible without RESSIM middleware.

Conclusions
Due to the variability of power production during the day, the irrigation management with water pumped with PV energy is a complex task. As power production is variable, heterogeneous

Conclusions
Due to the variability of power production during the day, the irrigation management with water pumped with PV energy is a complex task. As power production is variable, heterogeneous elements like sensors, countless web platforms and commercial irrigation controllers must be used co-ordinately. The integration of all these elements is essential to ensure the success of this technology, which is more complicated to manage than systems based on conventional energies where the availability of power is constant.
Thus, RESSIM is a friendly tool that facilitates the management of PV irrigation systems, since it gathers information from different data sources and models and automatically apply the previously calculated irrigation depths to all the sectors depending on the instant power production of the solar panels. RESSIM was successfully tested in an experimental field for olive trees irrigation. The system managed automatically the solar irrigation system during one full irrigation season, without any major incident.
The first version of the RESSIM model have allowed the optimum synchronization between the solar energy production and the irrigation requirements and the operation with the pumping station. By this optimal synchronization, the investment cost of the photovoltaic plant can also be reduced, optimizing the size and consequently reducing the greenhouse gases emissions. In future versions of the tool, dynamic models of crop growth, predictive models of daily water demand at farm level as well as the possibility of fitting the pumping station with variable speed drives will be implemented in order to improve the synchronization between the photovoltaic energy production and water demand both at farm and at water user association levels.