Jump to content

Banner.jpg.b89429c566825f6ab32bcafbada449c9.jpg

Help with Python code generally, but specifically Dew Point calculation


Jonk

Recommended Posts

I am fairly good at hacking up bits of code, but not fluent in writing from scratch or even understanding the rules, but I have a go!

Anyway, I appreciate that this is not the best place to ask, but I don't want to sign up for another forum to be bombarded with "you're doing this wrong...." and "learn the basics..."

I've turned to python for my rpi allsky, and want to conrtol relays based on temperature, humidity and probably more importantly, dew point. This is for inside and outside the camera enclosure, for various reasons.

I have managed to connect a BME280 sensor and can output T, H and P (pressure) automatically to a csv file - great! I've even set it to run as a service. Get me!

I have cobbled together a few bits of code, to try and calculate the dew point from the T and H values, in order for the program to decide whether to switch the internal heating or cooling on (yes cooling!) or to switch the external dew heater on.

This is where I'm stuck.

The function defined to a calculate the dew point uses the basic forumla

round((((humidity / 100) ** 0.125) * (112 + 0.9 * temperature) + (0.1 * temperature) - 112),1)

This should use the returned values for T and H to calculate and give me an answer, in this case:

return dew_point

Trying to print this value in the shell does not work, it prints a memory address.

So, having read a bit more, I learnt I could use print (type()) to see what's going on. It's not a number that it's returning, it's the whole function. And now I'm a bit stuck.

<class 'float'>
The temperature on Friday, 17-09-2021 at 21:20:58 was 20.6 Deg C
<class 'int'>
The humidity was 63 %
<class 'function'>
The dew point is <function calc_dew_point at 0xb6681ae0>

Can anyone cast their eye over this and tell me what I might be doing wrong please?

I 'think' that the

def calc_dew_point

function is not using the returned values of T and H as numbers, or it's spitting out something that's not a number.

I know I'm doing something wrong, but I don't know what.

Any help appreciated.

Here's my code:

#!/usr/bin/env python
#Dew point calculator for allsky climate and dew control
# initial set up of imports
import math
import time
import datetime

# imports the modules for the sensor
from bme280 import BME280
try:
    from smbus2 import SMBus
except ImportError:
    from smbus import SMBus
	
# sets up the variables for the sensor
bus=SMBus(1)
bme280 = BME280(i2c_dev=bus)

# define functions to use
	
def get_temp():
	temperature = bme280.get_temperature()
	temperature = round((temperature),2)
	temperature = temperature -2
	temperature = float(temperature)
	return (temperature)
def get_pressure():
	pressure = bme280.get_pressure()
	pressure = round((pressure),2)
	pressure = pressure -2
	pressure = str(pressure)
	return(pressure)
def get_humidity():
	humidity = bme280.get_humidity()
	humidity = round(humidity)
	humidity = int(humidity)
	return(humidity)
def date_now():
	today = datetime.datetime.now().strftime("%A, %d-%m-%Y")
	today = str(today)
	return(today)
def time_now():
	now = datetime.datetime.now().strftime("%H:%M:%S")
	now = str(now)
	return(now)
def calc_dew_point(temperature, humidity):
    dew_point = round((((humidity / 100) ** 0.125) * (112 + 0.9 * temperature) + (0.1 * temperature) - 112),1)
    dew_point = float(dew_point)
    return(dew_point)
	
while True:
    time_now()
    date_now()
    get_temp
    get_humidity
    calc_dew_point
    print (type(get_temp()))
    print ("The temperature on" ,date_now() ,"at" ,time_now() ,"was" ,get_temp() ,"Deg C")
    print (type(get_humidity()))
    print ("The humidity was" ,get_humidity() ,"%")
    print (type(calc_dew_point))
    print ("The dew point is" ,calc_dew_point)
       
    time.sleep(5) # wait x seconds until next reading

 

Link to comment
Share on other sites

I agree with Mike. All function calls need parentheses.This means that the earlier statements in the while loop need parentheses too. To make things more explicit you might say

temp = get_temp()

humidity = get_humidity()

dew_point = calc_dew_point(temp, humidity)

BTW The reason you need the parentheses is to distinguish the act of calling the function (which is what you want and what is the most normal case), from the use of the function name itself. Python allows functions as entities in themselves e.g. in a generalised sort algorithm you can pass the name of the function used to compare pairs of elements, allowing any kind of sorting to be implemented easily.

cheers

Martin

 

 

  • Thanks 1
Link to comment
Share on other sites

Gents, thanks very much - it worked!

while True:
    time_now()
    date_now()
    temperature = get_temp()
    humidity = get_humidity()
    dew_point = calc_dew_point(temperature, humidity)
    print (type(get_temp()))
    print ("The temperature on" ,date_now() ,"at" ,time_now() ,"was" ,get_temp() ,"Deg C")
    print (type(get_humidity()))
    print ("The humidity was" ,get_humidity() ,"%")
    print (type(calc_dew_point))
    print ("The dew point is" ,calc_dew_point(get_temp(), get_humidity()))
       
    time.sleep(5) # wait x seconds until next reading

<class 'float'>
The temperature on Saturday, 18-09-2021 at 09:19:49 was 19.85 Deg C
<class 'float'>
The humidity was 64.0 %
<class 'function'>
The dew point is 12.8

The class still shows the actual dew point number as a function - please explain?

I also thought that once a function had been called and 'executed', the 'return' remained in memory and you just had to use it, for example:

temperature = get_temp()

you could just put 'temperature' in a calc or print, rather than call it agin. So in this case, rather than:

print (temperature)

I would have to say

print (get_temp())

Most things I've read say that a return statement puts the value (string, int, float, another function whatever) into memory (as seen above with the memory location) to use again and again later. This is why I was confused.

Have I understood this correctly?

Link to comment
Share on other sites

You can indeed use the return value without having to call the function again. Does a statement of the form

print ("The temperature on" ,date_now() ,"at" ,time_now() ,"was" ,temperature ,"Deg C")

not work?

A Python function's return statement returns a value rather than placing it in a memory location. It is perfectly legal not to use the return value, in which case it is lost (not placed anywhere in memory). The only way to use the value is either to use it directly (as you've done in the print statement), or assign it to a variable such as 'temperature'. 

I would avoid thinking in terms of memory locations. Python is not like C, for example. Everything in Python is an object, which is roughly equivalent to the value of the object as well as its type, and the concept doesn't map on to memory locations in a way that it might do in a more primitive language like C.

cheers

Martin

Link to comment
Share on other sites

10 hours ago, Martin Meredith said:

You can indeed use the return value without having to call the function again. Does a statement of the form

print ("The temperature on" ,date_now() ,"at" ,time_now() ,"was" ,temperature ,"Deg C")

not work?

Yes it does, this is my <while True> loop

while True:
    day = day_today()
    date = date_today()
    the_time = time_now()
    temperature = get_temp()
    humidity = get_humidity()
    dew_point = calc_dew_point(temperature, humidity)
    print ("The temperature on" ,date ,"at" ,the_time ,"was" ,temperature ,"Deg C")
    print ("The humidity was" ,humidity ,"%")
    print ("The dew point was" ,dew_point ,"Deg C")
    write_to_csv()
    time.sleep(5) # wait x seconds until next reading

The csv file is written correctly and the output is printed as

The temperature on 18-09-2021 at 21:10:22 was 21.57 Deg C
The humidity was 64.0 %
The dew point was 14.4 Deg C

I think I now understand more how it works, so thanks very much!

Next step is to compare the values to a set point or another sensor and operate a relay. Hopefully not too much trouble.

I may be back!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.