Optimal Environment Control Mechanism Based on OCF Connectivity for Efficient Energy Consumption in Greenhouse

Greenhouses are a productive system that allows us to respond to the growing global demand for fresh and healthy food throughout the year, but the greenhouse environment is not easily controlled because its climate parameters are interrelated. However, the numbers of the actuator are operated parallelly to maintain the greenhouse environment; as a result, the energy consumption of greenhouses is high. In this study, we presented the optimization module by considering the outdoor environment with the aim of minimum energy consumption. Metaheuristic-based differential evaluation (DE) is used to optimize the climate parameters by considering indoor and outdoor environmental constraints. Furthermore, the long short-term memory (LSTM)-based inference model is offloaded on the Internet of Things (IoT) device to predict the next environmental situation. The objective function selects the optimal parameters within user preferences with minimum energy consumption based on the inferred parameter value. The open-source software framework IoTivity, implementing open connectivity foundation (OCF) technical standards, is used for the real-time connection between IoT devices and the IoT platform. Greenhouse owners can set the preferences based on the requirements of plants in the greenhouse by using a smart and remotely accessible Android-based interface. A fuzzy logic-based control module operates on an IoT device that maps the optimized parameters with the actuator and operates accordingly. The proposed model is analyzed, and the performance is evaluated in terms of energy consumption for each climate parameter and actuator in the greenhouse. The results show that the proposed mechanism saves 36% of energy.


I. INTRODUCTION
E NERGY saving is crucial for all sectors, especially the greenhouse, due to their unequivocal role in total energy consumption. Due to long-term temperature alterations, arable land reduction, and exploding population size, sustainable food production with optimal energy use has evolved from technological fringe to today's reality [1]. Since its inception greenhouse farming industry has played a key role in sustainable food production by providing a year-round production facility for fresh vegetables with around 50% alongside strengthening the global economy, ensuring food security and stability for replenishing the food needs across the globe [2]. Greenhouse tries to provide optimal conditions for cultivating crops to facilitate the growth cycle in a seasonal period of unsuitable plant growth, promoting sustainable food and energy provision. Planting crops requires special environmental conditions, such as temperature, humidity CO 2 , etc., which cannot be achieved through traditional manual control methods relying upon a nonscientific basis. Due to rapid urbanization, global climate changes, and water and energy resource shortages, traditional farming methods are unable to meet the growing global food needs in a sustainable manner. Recently, the farming industry has gradually developed into a largerscale planting sector. Thus, cultivating crops and plants in a greenhouse can be an effective solution for meeting environmental regulations along with optimizing energy use. However, greenhouse farming suffers from the problems of efficient operations, and resource management [3]. Some of the challenges involve extensive monitoring and optimal control of the greenhouse environment with minimum energy consumption and maximum crop yield and quality.
According to the findings of the study, [4], the appropriate planning for a conventional greenhouse can consume up to 80% of energy. Moreover, the facade material involved in the construction of greenhouses provides insufficient thermal resistance, which becomes a notable cause of heating and cooling energy demands. Alongside the physical structure and materials, the cultivation types also greatly influence energy consumption. Furthermore, the climatic region of the greenhouse greatly influences its energy consumption. Considering the rising energy prices, depletion of nonrenewable resources, carbon emissions, global warming, and climate change, energy saving has become a de facto necessity. Energy is a scarce resource; thus, strategies for optimizing energy consumption This work is licensed under a Creative Commons Attribution 4.0 License. For more information, see https://creativecommons.org/licenses/by/4.0/ in the greenhouse are being considered worldwide. Recent studies revealed that energy produced by renewable resources could fulfil 14% of total energy demand [5]. In this regard, optimizing the energy use in smart greenhouses can be treated as a potential energy-saving opportunity for effectively minimizing energy wastage and mitigating carbon emissions [6]. A reduction of the rapidly growing energy need for greenhouses is imperative for achieving sustainable cultivation [7].
To resolve the aforementioned challenges, the integration of agricultural Internet of Things (IoT) in the greenhouse can be a potential solution. In the era of technological advancements, the greenhouse has emerged as an active actor in the agricultural industry. It is a major contributor to precision agriculture based on advanced environmental control techniques, wireless sensor networks, management, communication and metering infrastructure. Smart greenhouses are equipped with heating, ventilation and air-conditioning components, supply air fans, CO 2 generators, physical sensors, actuators and a standard communication infrastructure constituting the agricultural IoT. The agricultural IoT-based greenhouse provides monitoring and ease of access to environmental resources for subsequent data acquisition through physical sensors [8].
IoT-empowered greenhouse facilitates farm owners in efficient decision-making and resource utilization with little human interventions. In terms of software implementation of optimal control in the smart greenhouse, technologies, such as Arduino and Raspberry Pi have received tremendous attention. However, constrained devices may suffer from challenges, such as privacy, storage, security and network communication abilities. To overcome such issues, open connectivity foundation (OCF) enables the resource-constrained devices deployed in the smart greenhouse to communicate securely and reliably with enhanced accessibility and seamless connectivity. The open-source software framework IoTivity implementing OCF provides a customized protocol for the constrained hardware, ensuring lower round trip time and smaller packet size employing Constrained Application Protocols (CoAPs) [9]. Hence, there is a dire need to develop an agricultural IoT system that can optimally control the greenhouse environment through efficient utilization of resources. As optimal resource utilization results in maximum crop yield along with cost reduction through optimal energy use. Furthermore, the collected environmental data based on OCF-IoTivity helps farmers in making accurate and timely decisions.
The study presents the optimization of energy consumption by using outdoor climate parameters. The application programming interface (API) from the Korean Meteorological Administration (KMA) is used to get the outdoor parameters, and the sensors are placed inside the greenhouse for indoor environment situation. The climate parameters for the time (t + 1) are predicted using the TFLite model based on long short-term memory (LSTM) artificial recurrent neural network (RNN) architecture. The model is first trained on the IoT platform and offloaded to the IoT device. Then, the values of temperature, humidity, and CO 2 are sent to the IoT platform to get the optimal combination of these parameters within user preferences based on the outdoor environment and energy price. The real-time price of energy is obtained from a smart  meter. The smart meter directly gets the cost of energy for each house from the grid station. Finally, based on optimal parameters selected by the optimization function, the fuzzy logic-based control module operates the actuators to set the indoor environment accordingly, as shown in Fig. 1.
The remainder of this article is formatted as follows. Section II presents the brief literature on IoT-based greenhouses and energy optimization techniques. Section III describes the optimal configuration mechanism with predictive optimization. Section IV shows the results of the prediction of climate parameters of LSTM and deep neural network (DNN); moreover, the section presents a brief comparison of energy consumption with and without optimization. Finally, Section V conclude the study with some future directions.

II. RELATED WORK
To control the climate parameters of greenhouse, multiple actuators have been used. Fig. 2 shows the basic structure of the greenhouse system. The environmental control of a greenhouse is a multivariable problem; there is a need for optimal coordination between heating, cooling, ventilation, fogging and CO 2 supply. Automated control systems (ACSs) are presented in greenhouses to control greenhouse climate remotely [10]. Some ACSs are developed to control the climate variable, such as temperature, humidity, and CO 2 concentration. Data processing systems and sensors are connected through communication protocols, including ModBus and RS-232 [11], [12]. These systems are used to control the greenhouse environment, but there is a lack of optimization techniques to minimize energy consumption and prefer the user-desired parameters. Energy management systems (EMSs) are deployed in residential and commercial sectors to minimize energy use [13], [14].
Tuning different actuators, including AI lights (Aqua Illumination LEDs), setting up the thermostat and activating security systems are different tasks executed in home automation systems (HASs). The tasks are executed to fulfil the requirements of the household owner. These systems are controlled by smartphones or Web applications to set the climate parameters automatically based on seasons and outdoor temperature. ZigBee, X10, Z-Wave, and Wi-Fi are protocols of HAS with the capability of a central controller that are commonly used for two-way communication to turn on and off actuators [15].
The core objective is to maintain the indoor climate (i.e., inside humidity, temperature and CO 2 ) within a predefined range. The indoor parameters can be affected by outdoor weather conditions and other operation time of the actuators. The indoor and outdoor climate parameters differ at a time (t) and time (t + 1). Actuators take time to change the environment of the greenhouse from the current parameter set to the desired range. In general, there is a lack of prediction of the next environmental parameters to tune the actuators as per the next climate situation. The thermal energy model by considering outdoor temperature, which affects the indoor temperature, is given in [16] where T in and T out are the indoor temperature at hour (h) and R io is thermal resistance. The thermal comfort level is given by the objective function where the thermal comfort level is given by where T set is the user-defined preference set and T ther is the threshold of the temperature difference, and β c and β h are the leading coefficients used to adjust the penalty values. This study considered user preferences, but the outdoor parameters also affect indoor comfort levels. Moreover, the operational time of other actuators also affects the indoor temperature; for instance, the fogging system can increase the humidity but also change the indoor temperature. Smart greenhouses are also controlled remotely by wireless sensors networks (WSNs). The centralized control with the aim of optimal energy consumption for greenhouse in a microgrid is presented [17]. The objective function was based on indoor climate parameters, storage system and water reservoir level of the greenhouse. Along with WSNs, some other control strategies, including fuzzy control [18], robust controller [19], and feedback controller [20] were presented. The Bayesian network was used for good approximation based on environmental conditions and set points of control signals [21]. The main focus was to control the air temperature within the greenhouse. At the same time, the inside air temperature is highly dependent on the outdoor temperature when air ventilation and forced ventilation are enabled.
IoT devices are recently developed for sensing and actuating services in different industries, including energy management, healthcare, and manufacturing. The devices are managed and connected by various protocols, including OCF, oneM2M (machine-to-machine) and open mobile alliance (OMA) [22], [23], [24]. The OMA is LightWeight M2M (LWM2M) and an international standard for mobile communication, which provides device management [25]. The management commands are executed on OMA nodes, and OMA DA manages the devices by using request and response transactions. The hybrid version of OMA with the European Telecommunications Standards Institute (ETSI) supports fault management and performance monitoring along with DM [26], [27]. Serviceoriented architecture (SOA) IoT systems use service management; for that purpose, the Arrowhead framework was proposed, which supports interconnected operations, and efficient development [28]. The services include information infrastructure service, system management, and information assurance services. These services provide various functions, including service registration and discovery, authentication and security, and orchestration in local cloud [29], [30]. The sensors and actuators are connected to IoT devices and pass the data through these different standards. Resource registration and accessibility are also done by using these communication standards.
OCF is another versatile standard (used in the proposed study) which supports various software platforms, including Tizen, Android, Linux, Windows, and iOS [31]. OCF can also work with any transport protocol, such as Bluetooth, Ethernet, Wi-Fi, bluetooth for low energy (BLE), and ZigBee. The default protocol of OCF is CoAP, which provides communication services in OCF-based networks using REST APIs. The energy consumption for the greenhouse is also minimized by using the Kalman filter-based ANN prediction model [32]. The authors used the Kalman filter and ANN to get the optimal value of K for prediction. The results are evaluated in terms of root means square error (RMSE), and the results show that the proposed model has better results than others.
Overall, there is a lack of consideration of outdoor parameters and the indirect effect of actuators with other parameters; for instance, ventilation fans decrease CO 2 level but can also change inside humidity and temperature. The energy consumption is highly dependent on outdoor parameters; by considering outdoor parameters, air ventilation can be preferred, which comparatively consumes low energy. Moreover, the actuators take some time to change the climate parameters within the desired range. To achieve this goal, actuators can be tuned on the value of climate parameters at the time (t + 1) by predicting the next temperature, humidity and CO 2 .

III. OPTIMAL CONFIGURATION MECHANISM WITH PREDICTIVE OPTIMIZATION
The IoT network consists of multiple devices which provide services by transferring the data from the environment to the intelligent platform. IoT devices can also be an AI-based platform to process and decide something based on the situation. These devices use different sensors to recognize the environmental parameters. IoT devices are not fast in terms of processing power, so in most cases, these devices send data to the central platform, or the trained models are deployed on the devices for inference. In the proposed model, a trained model is deployed on the IoT device, which intelligently infers climate parameters and sends the data to the OCF platform for optimization.
The experiments are conducted on a greenhouse with a size of 170 × 30 × 4 m. Raspberry pi 4 is used as an IoT device, and Ubuntu server 20.0.4 LTS is installed on the IoT device. Windows PC with 24 GB of RAM is used as an OCF platform. OCF IoTivity 2.2.2 is used as a communication standard between the OCF Platform and IoT Device. Furthermore, an Android device with Marshmallow OS is used to provide an interface to the owner of the greenhouse. Putty is installed on the OCF platform to access the device terminal using SSH, while WinSCP is used to transfer files using file transfer protocol (FTP). The greenhouse is divided into three equal compartments, where each zone is equipped with standard sensors and actuators. The continuous roof ventilation (area = 0.5 m 2 , equipped with anti-thrips netting), a fogging system with maximum capacity of 300g/m 2 /h were available. The rated power of the actuators in (kW) placed inside each zone is given as: (heating: 150, chiller: 170, CO 2 generator: 200, dehumidifier: 50, and forced ventilation: 15). The hardware devices, operating system, and libraries for implementation of self optimal control are listed in Table I.
In the first step of processing, data is collected from sensors and prediction models are trained on the collected data to infer climate parameters. The trained model predicts the next (at time t + 1) value of temperature, humidity, and CO 2 . Next, the collected data is sent to the platform using a request-response between the client and server. Finally, the OCF platform returns the optimized climate parameters to the IoT device using the OCF standard. The optimization of parameters is done by achieving minimum energy consumption within user preferences. The conceptual model of the proposed mechanism is shown in Fig. 3.

A. Prediction Mechanism for Environmental Sensors
The inference module based on LSTM is deployed on IoT Device to get the next temperature, humidity, and CO 2 value. LSTM and DNN models are first trained on server and the inference performance is analyzed. By considering certain parameters (Accuracy and loss) the best model (LSTM) is offloaded to IoT devices. The general disadvantage of LSTM is that it is computationally intensive but the proposed model analyzes the greenhouse environment on hourly basis and update the setting of actuators based on values computed by optimization module. Due to the delay-tolerant environment of greenhouse, the proposed model bears the lag (LSTM inference time, i.e., few seconds). Therefore, the accuracy of the model is prioritized over the inference time because the plants in the greenhouse are sensitive to the environment. LSTM is converted to a tf-lite model and deployed to the IoT device. The data used in this study is time series, where the previous values highly impact the next values. In addition, the LSTM model has a short memory, which helps the model to predict the next environmental parameters more accurately. Moreover, when the data contain long-, medium-, or shortterm trends, the LSTM model adds more parameters to be learned, which makes it a powerful forecasting model [33]. Four models for temperature, humidity, CO 2 and for energy prediction are trained and converted to tf-lite. These tf-lite models are deployed on raspberry-pi (IoT device). Three sensors for considered climate parameters are connected to the device to get the current value of temperature, humidity, and CO 2 as shown in Fig. 4. The ML models predict parameters for time (t+1) by getting the value at time (t). The energy consumption is also predicted by using future climate parameters. Lastly, IoT device send the data to the IoTivity client using the OCF IoTivity standard. Client stores the data in the database using JDBC for the optimization module.
The inference models send the data to the OCF platform when the request from the client is received. The client initiates the process by requesting climate parameters. IoTivity server on the IoT device is always ready to respond with climate parameters when the request is received. At the same time, the flask server is running on an IoT device to provide prediction services using the trained tf-lite model. The detailed flow of data will be explained in Section IV-B. The client first discovers the server by initiating a request using CoAP. The request is received by services running on IoT device. The request contains the number of climate parameters and resource information. The type of request can be a GET or a POST. For real-time climate parameters, the IoT device senses data from sensors and sends it to the flask library running on the same device. The trained inference models predict the next climate parameters and the energy consumption on these predicted parameters. All four parameters are returned to the server in the form of the JSON object. Finally, the server returns climate parameters to the client and the client store data in the database using JDBC. The IoTivity module receives the data sent from the flask server on IoT Device.
The data is in JSON format, which is easy to unpack on the client side.
Indoor climate parameters are sensed from sensors, and the outdoor parameters are obtained from KMA weather API. The API returns outdoor temperature and humidity values. Outdoor parameters are stored in the central database for the optimization module. The indoor and outdoor climate parameters (i.e., temperature, humidity, and CO 2 ) are used in the optimization function to minimize energy consumption. The energy returned from the tf lite model is used for the comparison with energy after optimization. Indoor and outdoor parameters are also visualized to the owner of greenhouse on their smartphone. Greenhouse owner can set the preferred climate parameters from smartphone and can also can check current environmental indoor situation of greenhouse. Smartphone initiates HTTP request using the volley library to connect with the central database. The preferred parameters are stored in the database and later used as upper and lower bounds for the optimization module. The flow of requests and overall architecture of connections and control is shown in Fig. 5.
Nowadays, most electricity companies are still using flat rate pricing (FPR) models for electricity pricing mechanisms. In FPR fixed price is charged for each time because of traditional electromechanical meters. Some companies are replacing FPR with real time pricing (RTP). In RTP the price of electricity changes for different time as per the requirement of end user. During high energy demand, the price of energy is increased while the price is decreased in other cases. In the proposed model smart meter is used to get the real-time energy price, and the parameters are selected based on the energy price at that specific time (t).   humidity) at (t + 1), user preferences range, and greenhouse attributes, including the state of actuates, size of zones, and natural ventilation area is extracted from the central database.

B. Mathematical Objective Function
The parameters are used in the cost function (28) given to the differential evaluation (DE) algorithm to minimize energy consumption. Based on the cost function, DE minimizes energy consumption and selects optimal parameters. The parameters are selected on the basis of energy cost. The real-time energy cost on a specific time is given by the smart meter. The energy price is stored in the database and retrieved along with other parameters while starting the optimization process. The energy consumption also depends on the outdoor parameters, indoor relative temperature, humidity, and CO 2 and the state of actuators. When the optimal global value is achieved by the algorithms, then these climate parameters are passed to the control module.
Indoor temperature can also be affected by the outdoor temperature and the state of the indoor actuators The equation shows that the indoor temperature at time t can be affected by the temperature at time (t − 1) and the volume of zone z with the following parameters: 1) the heat transferred from heating and chiller pipes; 2) the head loss through Natural and forced ventilation and walls; and 3) the heat produced by CO 2 generator and dehumidifier.
The temperature for each zone is depended on the multiple parameters as shown in the following equations [34].
Parameters for Heating: Parameters for Cooling: The above equations shows that the inside temperature at time t can be given as: 1) the temperature of chiller (heater) pipes at time (t−1); 2) the cooling (heating) absorbed from the operation of chilling (heating) system; and 3) cooling (heating) passed from the cooling (heating) pipes to the greenhouse.
The inside temperature must be within the given range of user preferences So, the relative temperature at time t should be within the user preferences Relative humidity of greenhouse is given by [34] R H = p par p wat (9) whereas and p par = wP atm θ 5 .
All the scenario makes the problem as mixed-integer linear programming (MILP) problem. The humidity inside the greenhouse is based on the mass balance principle The equation shows that the indoor humidity can be affected by multiple parameters, including the area of smart greenhouse, airflow rate of ventilation fans, outdoor humidity, and the state of both actuators mapped with humidity in a particular zone.
The desired humidity has a minimum and maximum range and the greenhouse user can set the range by considering the play type The relative humidity should be within the range of desired humidity Almost all types of plants need a certain level of CO 2 and the sunlight for photosynthesis. The plants consume CO 2 in day time so the CO 2 concentration will be low. So, there is a need to keep high level of photosynthesis by controlling CO 2 optimally. The level of CO 2 depends on the type of plants in the greenhouse. The user-preferred range for CO 2 can be given as The desired CO 2 is between the minimum and maximum levels of CO 2 . The concentration of CO 2 within the greenhouse can be modeled as The relative CO 2 within the greenhouse at time t is dependent on the CO 2 at time (t − 1), CO 2 generated by cg, nv, fv and C (pht,z) and state of all the actuators.
The owner sets the desired temperature D T , desired humidity D H , and desired D CO 2 by considering the plant type in the greenhouse.
The optimal climate parameters should be in the desired range set by the greenhouse owner The optimal state of the greenhouse will be [O T , O H , O CO 2 ] and the current state of the greenhouse at time (t) will be [T c , H c , CO 2 c ]. The greenhouse will be in an optimal state when the current parameters are within the desired range The temperature T, H, and CO 2 c show the difference between the current and desired upper and lower ranges of temperature, humidity, and CO 2 , respectively.
The higher difference shows higher energy consumption because the difference can be treated as a Euclidean distance between the current and desired parameter range.
If the unit energy consumption to increase temperature, humidity, and CO 2 by one degree is ET o , EH o , and ECO 2o (by excluding the external factors), respectively, then the minimum energy required to set the parameters within the user preferences will be The indoor environmental parameters can be affected by outdoor climate parameters. By involving outdoor parameters and assigning weights to each parameter, the above equations will become The estimated energy consumption for Temperature, Humidity, and CO 2 can be represented by the following equation: and the total energy consumption will be The objective is to minimize the energy consumed to set the climate parameters within the desired range by considering outdoor temperature and humidity. Therefore, the objective function is convex and has one optimum global point.
A common practice in optimization problems is to include the penalty function, which allows the optimization algorithm to move to the global optimum. DE is used as an optimization function which accepts the cost function to minimize it. DE is a real-valued multidimensional, population-based function that designs a search space by maintaining a population candidate solution [35]. The constraints limit the solution within a specific bound.
We need to minimize the energy consumed for each actuator in each zone.
Operational constraints are also included to ensure that two different types of actuators will not operate simultaneously. The control of forced ventilation air ventilation depends on outdoor parameter values. Suppose outdoor temperature or humidity helps to get the desired optimal values, then air ventilation and forced ventilation will be operated. Also, the dehumidification and fogging systems should not be operated simultaneously, and heating and cooling will not be injected into the greenhouse environment at the same time.
Algorithm 1 shows the self-optimal control mechanism for the greenhouse. Initially, the user-preferred parameter range is extracted from the database. The owner of the greenhouse sets the range by using an android-based greenhouse application. Next, the parameters of DE are initialized, and the population size is set. The self-optimal mechanism is activated each hour to optimize the parameters. The current energy price is obtained from the smart meter, and current indoor humidity, temperature and CO 2 are taken from the IoT device. By initializing the required parameters, the DE algorithm selects the optimal combination of parameters and returns optimal temperature, humidity, and CO 2 .

C. Actuator Control Based on Fuzzy Logic
The optimized values are passed from the OCF server to the IoT device to control actuators. The actuators are first scheduled and selected based on the combination of optimal climate parameters. Fig. 7 shows the fuzzy logic-based control structure to control the actuators. A chiller and heater are used to decrease and increase the temperature, respectively. Similarly, a dehumidifier, fogging system, CO 2 generator, and forced ventilation are used to control the humidity and CO 2 of the greenhouse. The heater will be operational if Tc < Td, and the air ventilation system will be turned on if the outdoor temperature will help to increase the temperature. The air and forced ventilation systems will be turned on if the temperature and humidity both agree to inject outdoor climate into the greenhouse; in other words, if outdoor temperature and humidity help to achieve desired values, then both actuators will be operational. Similarly, the chiller will be turned on if Tc > Td and Tc >> Td, and air ventilation and forced ventilation will be operated to inject outdoor temperature within the greenhouse based on the outdoor environmental situation. In case of humidity, the fogging system is used to increase, and a dehumidifier is used to decrease indoor humidity. Outdoor humidity is also considered to use fewer power actuators, i.e., air ventilation and forced ventilation. For CO 2 outdoor environment is not considered because the carbon level in natural air is 0.04%. Therefore, the outdoor CO 2 will always help to decrease the indoor CO 2 level. The forced ventilation system is used to decrease the level of CO 2 , and a CO 2 generator is used to inject carbon within the greenhouse. Plants also inject carbon within the environment, so usually, the CO 2 level within the greenhouse is high.

A. Implementation of Environment Evaluation Measures
To select the best model from LSTM and DNN for prediction of next climate parameters based on the current values, RMSE, mean absolute error (MAE), and R2 are selected as evaluation metrics. In addition, for the optimization module, energy consumption is compared with and without optimization for each parameter (i.e., temperature, humidity, and CO 2 ).
The RMSE is a square of the difference between actual and predicted values. In contrast, MAE is the absolute value of the difference between actual and predicted values. R2 coefficient shows the accuracy of prediction instead of an error. The higher value of R2 shows a better performance of the model. All three measures are used to evaluate the performance of models, and one best model is deployed on IoT devices to infer the next parameters

B. OCF Standard Connectivity
The connection between IoT devices and the IoT platform is established using the OCF standard. The research project named Campus as a Mashups Platform for IoT Experiments (CAMPIE) proposed a framework to deploy an IoT in a large area. The CAMPIE service discovery (CSD) based on OCF IoTivity is a versatile service discovery architecture. Fig. 8 shows the demonstration of connectivity between IoT device and platform using the OCF standard. The client initiates a request by discovering the server within the same network and request for climate parameters. When the server receives a request from a client, it requests the simulator running on the same device (IoT device) for climate parameters. The figure shows the CoAP request from coap://192.168.0.2:58284 (IP address and port) sent by the IoTivity client and received by the server. The server sends an HTTP request to the simulator, and the simulator gets the values from trained models. The simulator returns the JSON object to the server, and the server sends the data back to the client. Finally, the client sends the data to the database for the optimization module.

C. Predictive Optimization
The data set is collected from the sensors, including the temperature sensor, humidity sensor, and CO 2 sensor. The   data is used to train the DNN and LSTM model to predict the next value of climate parameters and energy consumption. The energy consumption is the dependent variable and changes based on the value of temperature, humidity, and CO 2 . Table III shows the results of prediction in terms of RMSE, MAE, and R2 coefficient for LSTM and DNN. The purpose of comparing the predicted value and actual measured value at time t + 1 is to select the best model for the IoT device that gives the best climate parameters based on current parameter values. It is clear from the table that the results of LSTM are reasonable compared to DNN. The testing and training results of LSTM and DNN are reported. The R2 coefficient shows the accurate prediction of models, while RMSE and MAE show the error rate. The error rate of LSTM for both the training and testing phase is less, while the R2 score is high compared to the DNN model for prediction and inference.
The migration of ML techniques on edge provides a faster process of inference. To migrate the models to IoT devices, LSTM is converted to a tflite model and deployed on IoT devices for future predictions. Three models for inference of temperature, humidity, and CO 2 and one for energy prediction on these three inferred climate parameters are deployed on edge. For humidity, the model outperforms and shows 99% R2 score in training and 98% in testing. Hold-out techniques are used to train the model. The sensor's history data of six months is used to train the model, while the energy consumption on these attributes is used to forecast energy consumption. The source codes of Energy optimization and OCF connectivity (for client and server) are online available. 1 Fig. 9(a) and (b) shows the inference results of temperature for each hour. The results show that the prediction of LSTM is better in comparison to DNN. Similarly for Humidity [ Fig. 10(a) and (b)] and for CO 2 [ Fig. 11(a) and (b)] LSTM performs well. For temperature, the value is taken from 18 to 21, which is the normal temperature for the broccoli in the greenhouse, and humidity is taken from 50 to 70, which is the normal humidity for broccoli. The user preferences are also considered, and the value of these climate parameters is adjusted by getting greenhouse experts' opinions. CO 2 is 1 https://github.com/atifrizwan91/Greenhouse/ measured in terms of parts-per-million (ppm), and the carbon dioxide level is selected from 400 to 650 ppm for indoor greenhouse parameters. CO 2 is also injected by plants into the environment of greenhouse. The level of CO 2 in the greenhouse is normally high in the evening and while it is low in the daytime.
After predicting all climate parameters, the energy consumption is computed by using a trained model. The results of the trained LSTM and DNN model are shown in Fig. 12(a) and (b). The illustration shows that the performance of LSTM is good compared to DNN. Next, the LSTM model is deployed on edge to forecast the energy consumption for inferred parameters. The consumption of energy is predicted in KW/H. The predicted energy is treated as energy consumption without optimization. After predicting climate parameters and energy  consumption, the values are passed to the OCF platform to optimize energy consumption.

D. Comparison of Optimization Results
The optimization function tries to select the optimal parameters between the user's preferred range. The optimization function aims to select minimum energy consumption within the specified constraints. The user preferences for temperature, humidity, and CO 2 are by using smartphone application as shown in Fig. 13. The user gives upper and lowers bounds of parameters, and the optimal value is selected between the bounds based on minimum energy consumption. Greenhouse owner can also view the comparison of indoor and outdoor temperature and humidity. Indoor parameters will always be within the user-preferred range. The current climate parameter values are also shown on the main screen of the application.
The LSTM model is used to predict energy consumption based on the predicted climate parameters. After that, the optimization function selects the optimal parameters by considering outdoor parameters. Finally, the energy consumption on the optimal parameters is compared with the predicted energy. Fig. 14 shows the comparison of energy consumption with and without optimization. The results are based on energy consumption for each hour for one month. It is clear from the figure that energy consumption is minimized by selecting the optimal parameters with DE by minimizing the cost function. Furthermore, the energy cost is also decreased, which is directly gotten from the smart meter.
The objective function minimizes energy consumption by considering outdoor temperature and humidity and considers the state of other actuators in the greenhouse. Broccoli is grown in the greenhouse, and the user preferences are set based on the type of plants. The energy consumption in the greenhouse is optimized by using: 1) indoor parameters; 2) outdoor parameters; and 3) user-preferred range. The level of actuators is set by the fuzzy logic control module. The greenhouse owner can set the range of temperature, humidity, and CO2 based on the crop grown in the greenhouse. The crop in the greenhouse affects the indoor parameters, but by allowing the greenhouse owner to set its preferred range, we ignored the effect of the crop. The study used broccoli as a test case, but the proposed model can be implemented with other crops in a greenhouse. Fig. 15 shows the comparison of optimal temperature with indoor and outdoor temperature. For each instance, a different level of the current and outdoor temperature is selected to apply the optimization algorithm to get the optimal climate parameter based on the price of energy obtained from the smart meter. The outdoor temperature does not always help to get the optimal temperature; sometimes, the outdoor temperature resists getting the optimal indoor temperature. The current temperature is also important because the actuators need to increase/decrease the temperature from the current to an optimal value obtained from the optimization function. The  distance between the current and the closely bound of user preference is directly proportional to the energy consumption. Similarly, for humidity, to gain the optimal humidity inside the greenhouse, the current and outdoor humidity is considered as shown in Fig. 16. The fogging system is used to increase the indoor humidity, and the dehumidifier is used to decrease the level of humidity. KMA API returns the level of outdoor humidity in %age, and the humidity sensor senses the indoor humidity in the same unit. The data shows high fluctuations in outdoor parameters because of uncertainty in the outdoor environment. Different levels of indoor and outdoor humidity are used to select the optimal humidity, same as temperature. The optimal humidity is always between the user preferences with minimum energy consumption. For the experiments, the user preferences for temperature are selected from 17 • C to 22 • C, and for humidity, the bounds are 55%-70%. CO 2 concentration is important for all types of plants. Different plants inject CO 2 into the environment at different times. The level of CO 2 can be increased in the evening, so the level should be optimized. By selecting the optimal CO 2 within user preferences, energy consumption is minimized. The outdoor CO 2 is not considered for the optimal values because the level of carbon in natural air is too low. For broccoli, the level of CO 2 is selected from 400 to 450 ppm. The comparison of indoor and optimal CO 2 is shown in Fig. 17.
The optimal temperature is achieved by considering the outdoor and current indoor temperature values. A heater and chiller are used to get the desired temperature and achieve optimal environmental temperature. The energy consumption  for the heater and chiller is given in Fig. 18. The energysaving for each day is not constant because of the change in outdoor temperature. When the ventilation fan or air ventilation is operated to achieve the optimal temperature, the energy consumption is low. The optimization function selects the best temperature, and then the fuzzy control selects the best actuator based on the value of indoor and outdoor climate parameters. The outdoor temperature on Jeju island normally lies between 15 and 25 in summer, so the energy consumption is low because the optimal temperature for broccoli can be achieved by operating forced and natural ventilation. The humidity level is usually high because of the high rain rate in the experimental area. To achieve the desired humidity level within the greenhouse, a fogging system and dehumidifier is used. Due to high outdoor humidity, the dehumidifier consumes more energy as compared to fogging system. Fig. 19 shows the energy consumption for the humidification and dehumidification process for one month (30 days). The figure shows that the energy consumption with optimization is comparatively low. When outdoor humidity allows for openair ventilation, the energy consumption is low. Broccoli needs a low level of humidity; because of that, the dehumidification process consumes more energy. Multiple plants inject a different level of CO 2 into the environment. Forced ventilation and CO 2 generator are used to increase or decrease the CO 2 level within the greenhouse. The forced ventilation is also used for temperature and humidity to inject fresh outdoor air into the greenhouse. Fig. 20 shows the comparison of energy consumption for CO 2 with and without optimization. It is clear that the energy consumption for optimal CO 2 is less as compared to without optimization.   The energy consumption for each actuator is given in Fig. 21. Six different actuators are used to control the environment of the smart greenhouse, including a chiller, heater, fogging system (humidifier), dehumidifier, CO 2 generator, and forced ventilation. Each actuator consumes different levels of energy on different levels. For example, dehumidifiers and forced ventilation take more energy compared to other actuators. The fuzzy control selects the best actuator according to the indoor climate environment. It is clear from the figure that when the actuators are controlled without optimization, the energy consumption is too high, while in another case, the consumption of energy is low.

V. CONCLUSION
In this study, a cost function is used to minimize energy consumption by considering outdoor parameters has been presented. The AIOT platform contributes an essential role in optimization in the self-optimization-based greenhouse. For optimization, user preferences are considered to select the best parameters within the specified range. The owner sets the range of climate parameters based on the plant type in the greenhouse. The LSTM-based inference model is offloaded on the smart edge device to predict the next climate parameters using current values. The energy consumption on current environmental parameters is also predicted; later, the energy is compared with the energy computed after optimization. The predicted climate parameters and energy consumption are passed to the IoT platform using the CoAP protocol. CoAP protocol is used by OCF IoTivity, which is used to connect the IoT Device and platform. The platform receives the current energy price from the smart meter and user preference from the database. The greenhouse owner can set the preferences (min and max values) from the android-based interface. By considering all these parameters, the platform uses DE to get the optimal parameters and send them to the IoT device by using the same protocol. The fuzzy logic-based mapping is applied, and the desired actuator is operated. The climate parameters of the greenhouse are interrelated, so the indirect effect of each actuator is considered along with outdoor parameters, which helps to decrease energy consumption. The results are reported in terms of energy consumption for each parameter and each actuator.

AUTHORS' CONTRIBUTIONS
Atif Rizwan contributed to study the conception and design of proposed work. Material preparation and data collection is performed by Atif Rizwan, Anam Nawaz Khan, and Rashid Ahmad. The experiments are conducted by Atif Rizwan and Anam Nawaz Khan. The first draft is written by Atif Rizwan and reviewed and edited by Rashid Ahmad and Do Hyeun Kim. All authors commented on previous versions of manuscript and approved the final version.