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()
g = math.log(RH/100.0) + ((m * T) / (c + T))
is throwing an exception. We can't tell what the exception is, because yourexcept
block throws it away. Try editing the except block so it at least prints what the exception was. – John Gordon