Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import cayenne.client
- import time
- import logging
- import board
- import busio
- import digitalio
- import adafruit_bme280
- import math
- import scipy
- # Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
- MQTT_USERNAME = "19cfb8a0-82e7-11e8-9464-a9a522c157c5"
- MQTT_PASSWORD = "cc4752158e665ff01ab325271ff06017c200a457"
- MQTT_CLIENT_ID = "489fe300-d6ea-11e9-a38a-d57172a4b4d4"
- client = cayenne.client.CayenneMQTTClient()
- client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
- # For a secure connection use port 8883 when calling client.begin:
- # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)
- #Defining VPD calculation function
- def ea_calc(airtemp= scipy.array([]),\
- rh= scipy.array([])):
- '''
- Function to calculate actual vapour pressure from relative humidity:
- .. math::
- e_a = \\frac{rh \\cdot e_s}{100}
- where es is the saturated vapour pressure at temperature T.
- Parameters:
- - airtemp: array of measured air temperatures [Celsius].
- - rh: Relative humidity [%].
- Returns:
- - ea: array of actual vapour pressure [Pa].
- Examples
- --------
- >>> ea_calc(25,60)
- 1900.0946514729308
- '''
- # Test input array/value
- airtemp,rh = _arraytest(airtemp, rh)
- # Calculate saturation vapour pressures
- es = es_calc(airtemp)
- # Calculate actual vapour pressure
- eact = rh / 100.0 * es
- return eact # in Pa
- def es_calc(airtemp= scipy.array([])):
- '''
- Function to calculate saturated vapour pressure from temperature.
- For T<0 C the saturation vapour pressure equation for ice is used
- accoring to Goff and Gratch (1946), whereas for T>=0 C that of
- Goff (1957) is used.
- Parameters:
- - airtemp : (data-type) measured air temperature [Celsius].
- Returns:
- - es : (data-type) saturated vapour pressure [Pa].
- References
- ----------
- - Goff, J.A.,and S. Gratch, Low-pressure properties of water from -160 \
- to 212 F. Transactions of the American society of heating and \
- ventilating engineers, p. 95-122, presented at the 52nd annual \
- meeting of the American society of \
- heating and ventilating engineers, New York, 1946.
- - Goff, J. A. Saturation pressure of water on the new Kelvin \
- temperature scale, Transactions of the American \
- society of heating and ventilating engineers, pp 347-354, \
- presented at the semi-annual meeting of the American \
- society of heating and ventilating engineers, Murray Bay, \
- Quebec. Canada, 1957.
- Examples
- --------
- >>> es_calc(30.0)
- 4242.725994656632
- >>> x = [20, 25]
- >>> es_calc(x)
- array([ 2337.08019792, 3166.82441912])
- '''
- # Test input array/value
- airtemp = _arraytest(airtemp)
- # Determine length of array
- n = scipy.size(airtemp)
- # Check if we have a single (array) value or an array
- if n < 2:
- # Calculate saturated vapour pressures, distinguish between water/ice
- if airtemp < 0:
- # Calculate saturation vapour pressure for ice
- log_pi = - 9.09718 * (273.16 / (airtemp + 273.15) - 1.0) \
- - 3.56654 * math.log10(273.16 / (airtemp + 273.15)) \
- + 0.876793 * (1.0 - (airtemp + 273.15) / 273.16) \
- + math.log10(6.1071)
- es = math.pow(10, log_pi)
- else:
- # Calculate saturation vapour pressure for water
- log_pw = 10.79574 * (1.0 - 273.16 / (airtemp + 273.15)) \
- - 5.02800 * math.log10((airtemp + 273.15) / 273.16) \
- + 1.50475E-4 * (1 - math.pow(10, (-8.2969 * ((airtemp +\
- 273.15) / 273.16 - 1.0)))) + 0.42873E-3 * \
- (math.pow(10, (+4.76955 * (1.0 - 273.16\
- / (airtemp + 273.15)))) - 1) + 0.78614
- es = math.pow(10, log_pw)
- else: # Dealing with an array
- # Initiate the output array
- es = scipy.zeros(n)
- # Calculate saturated vapour pressures, distinguish between water/ice
- for i in range(0, n):
- if airtemp[i] < 0:
- # Saturation vapour pressure equation for ice
- log_pi = - 9.09718 * (273.16 / (airtemp[i] + 273.15) - 1.0) \
- - 3.56654 * math.log10(273.16 / (airtemp[i] + 273.15)) \
- + 0.876793 * (1.0 - (airtemp[i] + 273.15) / 273.16) \
- + math.log10(6.1071)
- es[i] = math.pow(10, log_pi)
- else:
- # Calculate saturation vapour pressure for water
- log_pw = 10.79574 * (1.0 - 273.16 / (airtemp[i] + 273.15)) \
- - 5.02800 * math.log10((airtemp[i] + 273.15) / 273.16) \
- + 1.50475E-4 * (1 - math.pow(10, (-8.2969\
- * ((airtemp[i] + 273.15) / 273.16 - 1.0)))) + 0.42873E-3\
- * (math.pow(10, (+4.76955 * (1.0 - 273.16\
- / (airtemp[i] + 273.15)))) - 1) + 0.78614
- es[i] = pow(10, log_pw)
- # Convert from hPa to Pa
- es = es * 100.0
- return es # in Pa
- def _arraytest(*args):
- '''
- Function to convert input parameters in as lists or tuples to
- arrays, while leaving single values intact.
- Test function for single values or valid array parameter input
- (J. Delsman).
- Parameters:
- args (array, list, tuple, int, float): Input values for functions.
- Returns:
- rargs (array, int, float): Valid single value or array function input.
- Examples
- --------
- >>> _arraytest(12.76)
- 12.76
- >>> _arraytest([(1,2,3,4,5),(6,7,8,9)])
- array([(1, 2, 3, 4, 5), (6, 7, 8, 9)], dtype=object)
- >>> x=[1.2,3.6,0.8,1.7]
- >>> _arraytest(x)
- array([ 1.2, 3.6, 0.8, 1.7])
- >>> _arraytest('This is a string')
- 'This is a string'
- '''
- rargs=[]
- for a in args:
- if isinstance(a, (list, tuple)):
- rargs.append(scipy.array(a))
- else:
- rargs.append(a)
- if len(rargs) == 1:
- return rargs[0] # no unpacking if single value, return value i/o list
- else:
- return rargs
- def vpd_calc(airtemp= scipy.array([]),\
- rh= scipy.array([])):
- '''
- Function to calculate vapour pressure deficit.
- Parameters:
- - airtemp: measured air temperatures [Celsius].
- - rh: (array of) rRelative humidity [%].
- Returns:
- - vpd: (array of) vapour pressure deficits [Pa].
- Examples
- --------
- >>> vpd_calc(30,60)
- 1697.090397862653
- >>> T=[20,25]
- >>> RH=[50,100]
- >>> vpd_calc(T,RH)
- array([ 1168.54009896, 0. ])
- '''
- # Test input array/value
- airtemp,rh = _arraytest(airtemp, rh)
- # Calculate saturation vapour pressures
- es = es_calc(airtemp)
- eact = ea_calc(airtemp, rh)
- # Calculate vapour pressure deficit
- vpd = es - eact
- return vpd # in hPa
- #initiate SPI bus
- spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
- cs = digitalio.DigitalInOut(board.D5)
- bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)
- i=0
- timestamp = 0
- #calculating vpd
- #vpd = vpd_calc(bme280.temperature, bme280.humidity)/100
- while True:
- client.loop()
- if (time.time() > timestamp + 10):
- client.celsiusWrite(1, bme280.temperature)
- client.virtualWrite(2, bme280.humidity, "rel_hum", "p")
- client.hectoPascalWrite(4, (vpd_calc(bme280.temperature, bme280.humidity)/100))
- client.hectoPascalWrite(3, bme280.pressure)
- timestamp = time.time()
- i = i+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement