1
votes

I am having a bit of a struggle with parsing values between these def functions. The base code getSensorData() and main() works fine, values are read from the sensor and sent to thingspeak. However, I wanted to add a calculation function called calcDewPoint() to the original code to calculate dewpoint from the sensor values (RH, T) but it seems that the code gets stuck :/

Output from terminal when I run the code as is:

starting...
sense1
sense2
dewpoint1
gamma1
exiting.

Temperature/Humidity monitor using Raspberry Pi and DHT22. Data is displayed at thingspeak.com Change list 02/06/2020 Add dewpoint calculations

import sys 
import RPi.GPIO as GPIO 
from time import sleep 
import Adafruit_DHT 
import urllib2 
import math #(using numPy instead of math lib)
#import numPy as np
#import constant (Create a *.py file to be able to use constant accross multiple projects) 

#Constant Variables - Magnus Parameters
m = 17.62 #Mass Constant (Water Vapour)
Tn = 243.12 #Tempreture Constant
Ah = 6.112 #hPa Pressure Constant
Ak = 0.611 #kPa Pressure Constant
K = 273.15 #Kelvin constant
Ta = 216.7 #Tempreture Constant

myAPI = 'FZZHL7N2R2AXXXXX'

def getSensorData():
   print 'sense1'
   RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 17)
   print 'sense2'
   return (str(RH), str(T))

def calcDewPoint(RH, T):
   print 'dewpoint1'
   Td = (K * gamma(RH, T)) / (m - gamma(RH, T))
   print 'dewpoint2' 
   return str(Td)

def gamma(RH, T):
   print 'gamma1'
   g = math.log(RH/100.0) + ((m * T) / (c + T))
   print 'gamma2'
   return str(g)

def main(): 
   print 'starting...'
   baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI 
   while True: 
       try: 
           RH, T = getSensorData() #Call function to Read DHT22 sensor conencted to RaspberryPi
           Td = calcDewPoint(RH, T) #Call function to calculate dewpoint
           f = urllib2.urlopen(baseURL + "&field1=%s&field2=%s&field3=%s" % (RH, T, Td)) 
           print f.read() 
           f.close() 
           sleep(60) #uploads DHT22 sensor values every 1 minutes 
       except: 
           print 'exiting.' 
           break 
# call main 
if __name__ == '__main__': 
   main()  
2
g = math.log(RH/100.0) + ((m * T) / (c + T)) is throwing an exception. We can't tell what the exception is, because your except block throws it away. Try editing the except block so it at least prints what the exception was.John Gordon

2 Answers

0
votes

Your variable c is not defined. When you call calcDewPoint, it then calls gamma, which tries to run your math expression. However, c does not exist at that point.

0
votes

Everything is running smooth, Code works with the odd exception in the debugger,

  File "/usr/lib/python3/dist-packages/thonny/backend.py", line 305, in _custom_import
    module = self._original_import(*args, **kw)
ImportError: No module named 'urllib2'

but overall it functions well. We can close up the thread.

"""
dht22.py 
Temperature/Humidity monitor using Raspberry Pi and DHT22. 
Data is displayed at thingspeak.com
Original author: Mahesh Venkitachalam at electronut.in 
Modified by Jacques Kleynhans  06062020 
Return rounded string values
Created Temporary variables Hum and Tair
"""

#import sys
import math
#import RPi.GPIO as GPIO 
from time import sleep 
import Adafruit_DHT 
import urllib2

#Constant Variables - Magnus Parameters Max 0.083 Tair Range -20C + 50C
m = 17.62 # g/mol Mass Constant (Water Vapour)
Tn = 240.7263 #Tempreture Constant
Ah = 6.116441 #hPa Pressure Constant
Ak = 0.611 #kPa Pressure Constant
K = 273.15 #Kelvin constant
Ta = 216.7 #Tempreture Constant

#constant for leaf Temperature
Tc = 2.0

myAPI = 'FZZHL7N2R2AXXXXX' 

def getSensorData(): 
   RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 17) 
   return (round(RH,3), round(T,3)) 

# Define functions to calculate dewpoint, return a rounded string)

def calcDewPoint(Hum,Tair):
   #print('dewpoint1')
   Td = (Tn * gamma(Hum,Tair)) / (m - gamma(Hum,Tair))
   #print('dewpoint2') 
   return round(Td,3)

def gamma(Hum,Tair):
   #print ('gamma1')
   g = math.log(Hum/100.0) + ((m * Tair) / (Tn + Tair))
   #print('gamma2')
   return round(g,3) 

# Define functions to calculate VPD, return a rounded integer)  
#Vapor pressure deficit control strategies for plant production
#IFAC Control applications and Ergonomics in Agriculture, Athens, Greece, 1998

def satVaporPres(Tair):
    es = 6.1078*math.exp((Tair*17.2694)/(Tair+237.3)) #mPa
    return round(es,3)

def actVaporPres(Hum, es):
    ea = (Hum/100)*es
    return round(ea,3)

def leafEs(Tair):
    leafTemp = Tair - Tc
    les = 6.1078*math.exp((leafTemp*17.2694)/(leafTemp+237.3))
    return round(les, 3)

#Partial pressure of water vapour Pw in the air surrounding the plant
def parPressureW(Hum, es, Tair):
    Pw = Hum*es*Tair
    return(Pw)

def VPD(les, ea): #VPD air / crop
    VPD = (les - ea)/10
    return round(VPD,3)

def main(): 
   print('starting...')
   baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI 
   while True: 
       try: 
           RH, T = getSensorData()
           Hum = RH
           Tair = T
           Td = calcDewPoint(Hum,Tair)

           es = satVaporPres(Tair)
           ea = actVaporPres(Hum, es)
           les = leafEs(Tair)
           TempVPD = VPD(les,ea)           

           print(Hum, Tair, Td, TempVPD)
           f = urllib2.urlopen(baseURL + "&field1=%s&field2=%s&field3=%s&field4=%s" % (RH, T, Td, TempVPD)) 
           print f.read() 
           f.close() 
           sleep(60) #uploads DHT22 sensor values every 1 minutes 
       except: 
           print('exiting.') 
           break 
# call main 
if __name__ == '__main__': 
   main()