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!

Tags:haskell, hydroponic , urban farming

责编内容by:Asep Bagja Priandana (源链)。感谢您的支持!


520究极进化 – 心跳心跳大作战 大家520快乐!祝各位都能跟心仪的另一半进行crossover,产生fitness更高的后代哦~ 蛤?你没有另一半?没关系,小编发现...
Authenticated Data Structures Generically Derive inclusion and membership proofs for arbitrary sum-of-product data...
Haskell developer experience in NixOS Posted in category nixos on 2017-06-05 Tags:haskell, nixos , ...
Queryparser,一款开源SQL解析工具 在2015年初,Uber开始将业务迁移到多个活跃的数据中心。作为初步迁移活动的一部分,Uber工程部门将其业务实体的ID从整数类型转变为UUID类型。为了实现这...
Eta in Practice: Working With Haskell Packages Eta in Practice: Working With Haskell Packages ...