# Implementing Greenhouse Climate Control in Haskell

2016-04-11

The benefit of using the greenhouse or indoor farming for doing hydroponic is we can control the climate inside it regardless the weather outside. Two of the relevant parameters to control the climate are temperature and humidity. Humidity itself is a huge factor when growing plants. If we can understand how the plants respond to it, we can manage and manipulate it to get better crops.

#### Temperature and Humidity

Humidity is the amount of water in the air as the gas form or we can say it is “water vapour.” Temperature relates to it. The warmer the air, the more water vapour it can potentially hold. It is tough to measure the humidity in the air since the amount of water that air can hold will always change with temperature.

Fortunately, there is a concept called “relative humidity.” It is a measurement regarding the percentage of the water vapour in the air compared to the total water vapour potential that the air could hold at a given temperature. So, when we say the relative humidity is 50%, it means, “at the particular temperature, the air is carrying half the potential water vapour possible”.

#### Vapour Pressure Deficit

Now after we understand about temperature and humidity, let’s move to Vapour Pressure Deficit (VPD). The VPD regards to how plants feel and react to the humidity in a growing environment. The word “deficit” in VPD regards to the difference between SVP (Saturated Vapour Pressure or 100% RH at a given temperature) and the pressure exerted by the water vapour that is held in the air (actual vapour pressure).

Here is the step-by-step guide to measuring VPD:

1. Measure leaf temperature by putting a sensor close to the crop canopy
2. Calculate the leaf vapour pressure at 100% RH
3. Measure the air temperature and RH
4. Calculate the actual vapour pressure
5. Subtract leaf VP from actual VP

According to Wikipedia , most plants grow well at VPD between 0.8 kPa and 0.95 kPa.

#### Let’s computerise it!

I want to start to automate my greenhouse (you can check Tanibox Indiegogo campaign ), and after thinking a few times, I chose to develop it in Haskell. I got the formula to calculate saturated vapour pressure, actual vapour pressure, and vapour pressure deficit from here .

```-- Saturated Vapour Pressure formula
calculateSVP t = (610.7 * 10 ** (7.5 * t / (237.3 + t)))

-- Actual Vapour Pressure formula
calculateAVP rh svp = (rh * svp) / 100

-- VPD formula
calculateVPD svp avp = svp - avp```

Define the category of the VPD. It is also in Pascal.

```data VPD = Ideal | Acceptable | TooHumid | TooDry
deriving (Show, Enum)

vpdCategory :: (RealFloat a) => a -> VPD
vpdCategory vpd
| vpd >= 800.0 && vpd <= 950.0 = Ideal
| vpd <= 450.0 = TooHumid
| vpd >= 1250.0 = TooDry
| otherwise = Acceptable```

So, here is the complete example code how to use it.

```-- | Temperature control for hydroponics
import Text.Printf

-- Category of the VPD value
data VPD = Ideal | Acceptable | TooHumid | TooDry
deriving (Show, Enum)

-- SVP (Saturated Vapour Pressure) should be calculated first
-- t is given temperature at 100% RH
calculateSVP t = (610.7 * 10 ** (7.5 * t / (237.3 + t)))

-- Calculating the actual vapour pressure (Pa)
-- rh is relative humidity
-- svp is saturated vapour pressure
calculateAVP rh svp = (rh * svp) / 100

-- VPD (Vapour Pressure Deficit) is the difference between SVP and APV
calculateVPD svp avp  = svp - avp

-- Define the category of given VPD
vpdCategory :: (RealFloat a) => a -> VPD
vpdCategory vpd
| vpd >= 800.0 && vpd <= 950.0 = Ideal
| vpd <= 450.0 = TooHumid
| vpd >= 1250.0 = TooDry
| otherwise = Acceptable

main :: IO()
main = do
putStrLn ("Leaf temperature in Celcius: ")
leafTemperature <- getLine
putStrLn ("Air temperature in Celcius: ")
airTemperature <- getLine
putStrLn ("Relative humidity in %: ")
humidity <- getLine

let rh = read humidity :: Double
lt = read leafTemperature :: Double
at = read airTemperature :: Double
svp = calculateSVP lt
avp = calculateAVP rh \$ calculateSVP at
vpd = calculateVPD svp avp
in putStrLn \$ "Leaf temperature: " ++ leafTemperature ++ " C, Leaf VP: " ++ printf "%.2f" svp ++ " Pan"
++ "Air Temperature: " ++ airTemperature ++ " C, Relative Humidity: "
++ humidity ++ "%, Air VP: " ++ printf "%.2f" avp ++ " Pan"
++ "VPD: " ++ printf "%.2f" vpd ++ " Pan"
++ "Category: " ++ (show . vpdCategory) vpd```

Run the programme. We can use this calculation for controlling the fan and humidifier to make our crops happy and healthy. Happy farming!