# Energy-Efficient IoT-Based Light Control System in Smart Indoor Agriculture
## 4 Grow Lights Monitoring System
### Light Control
#### 4.3.2 Distributed LED Brightness Control  
In the proposed scenario, the LEDs in each chamber of the plant factory or vertical farm are connected to a microcontroller that is responsible for controlling the growth conditions and collecting sensor data in that chamber. Each microcontroller is a node in the network presented earlier, where its role is sending sensor data to the server, receiving back control values from it, and using them to directly adjust the actuators. The microcontroller reads the light intensity value measured by the sensor and sends it to the server for processing.  
Then, it applies the control value which is the duty-cycle computed by the server and sent back to it in the downward traffic. In the server, a learning algorithm based on the Newton optimization method is implemented to compute the duty-cycles needed to obtain the desired light intensities from the sensor measurements. The cost function minimized in this learning algorithm is given by:  
\[J(\theta)=\frac{1}{2}\sum_{i=1}^{N}(L_{t,i}-L_{t,i})^{2} \tag{1}\]  
where \(L_{t,i}=\theta L_{max,i}\) is the current illuminance at time \(t\) in growth chamber \(i\), \(\theta\) is the duty cycle, \(L_{max,i}\) is the maximum illuminance of the LEDs measured by the sensor, and \(\hat{L}_{t,i}\) is the desired illuminance in the growth chamber \(i\) at time \(t\) fixed by the user. Notice that all these variables (\(L_{t,i}\), \(L_{t,i}\), \(L_{max,i}\), and \(\theta\)) are (\(3\times 1\)) vectors with three components referring to PAR, far-red, and UV lights, respectively. The Newton method used to update the values of \(\theta\) and iteratively solve this minimization problem is given by:  
\[\theta_{k+1}\coloneqq\theta_{k}-\alpha\frac{J^{\prime}(\theta_{k})}{J^{\prime \prime}(\theta_{k})},\quad k=0,1,2,\ldots \tag{2}\]  
where \(\alpha\) is a learning rate; \(k\) is the iteration number; and \(J^{\prime}(\theta)=L_{max,i}(\theta L_{max,i}-\hat{L}_{t,i})\) and \(J^{\prime\prime}(\theta)=L_{max,i}^{2}\) are the first and the second derivatives of \(J(\theta)\) with respect to \(\theta\), respectively. A sensor calibration step is implemented in the server, before the learning algorithm, in which the control values of \(\theta_{i}=0\) and \(\theta_{i}=1\) are sent to each node to read the corresponding values of illuminance \(L_{t,i}=0\) and \(L_{t,i}=L_{max,i}\), respectively. The implementation of the sensor calibration and the learning method is given in Algorithm 1.  
Sensor measurements are noisy, and there is jitter in the packet sending rate. The learning algorithm is not affected by this; it is proven to converge independently of the initial conditions, and sensor noises are averaged out. In addition, updating the value of \(\theta\) in Equation (2) is performed whenever a new measurement value is received from the client. Thus, jitter does not affect the control method since this is already designed to treat each client separately and wait for packet transmission time that changes depending on the distance from different clients to the server, the route of the data packet, and jitter.  
```
1:Initialize \(\theta_{i}=0\) for \(i=1,\ldots,N\), \(\alpha=1\), and desired error tolerance \(\epsilon\)
2:\(N\): number of clients
3:Initialize \(\hat{L}_{t,i}\) according to user requirements, for \(i=1,\ldots,N\)
4:#sensor calibration:
5:For\(i=1,\ldots,N\), send \(\theta_{i}=0\) to client \(i\)
6:For\(i=1,\ldots,N\), receive \(L_{t,i}=0\) from client \(i\)
7:For\(i=1,\ldots,N\), send \(\theta_{i}=1\) to client \(i\)
8:For\(i=1,\ldots,N\), receive \(L_{t,i}=L_{max,i}\) from client \(i\)
9:#LED brightness control:
10:If a value \(L_{t,i}\) is received from a client \(i\):
11:While True:
12:\(\theta_{i}:=\theta_{i}-\alpha\frac{J^{\prime}(\theta_{i})}{J^{\prime}(\theta_ {i})}\)
13:If\(J^{\prime}(\theta_{i})<\epsilon\):
14:Break
15:Send\(\theta_{i}\) to client \(i\)
16:Else
17:Wait for \(L_{t,i}\) from a client \(i\)
```  
**Algorithm 1** Distributed LED brightness control algorithm