0

I have a few parts of a weather station together and am running into an issue with functions. the humair fuction returns two values hum and airtemp. The problem is when the sendpost function runs it doesn't know that output. I get an error that airtemp is not defined. I assume I need to somehow pass the output of a function to another function. I read about global variables and this link https://stackoverflow.com/questions/16043797/python-passing-variables-between-functions I guess I am looking for the right way to do this so when I do more complex things in the future I am set.

import Adafruit_DHT
import os
import requests
import time
import serial
from Adafruit_BME280 import *

sensor = Adafruit_DHT.DHT22
sensor2 = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8,)
serialport = serial.Serial("/dev/ttyAMA0", 115200, timeout=960)


def takevideo():
    if os.path.exists("/home/pi/Videos/tenseconds.h264"):
        os.remove("/home/pi/Videos/tenseconds.h264")
    if os.path.exists("/home/pi/Videos/tensec.mp4"):
        os.remove("/home/pi/Videos/tensec.mp4")
    os.system ("raspivid -t 10000 -w 640 -h 480 -fps 25 -b 120000 -p 
0,0,640,480 -o /home/pi/Videos/tenseconds.h264")
    os.system ("MP4Box -add /home/pi//Videos/tenseconds.h264 /home/pi/Videos/tensec.mp4")

def humair():
    hum, airtemp = Adafruit_DHT.read_retry(sensor, 17)
    return (str(hum), str(airtemp))

def barometric():
    degrees = sensor2.read_temperature()
    pascals = sensor2.read_pressure()
    hectopascals = pascals / 100
    return ('{0:0.2f}'.format(hectopascals))

def sendpost():
    url = 'http://the site name'
    files = {'file': open('/home/pi/Videos/tensec.mp4', 'rb')}
    values = {'action': 'log', 'siteid': '001','airtemp': airtemp, 'hum': hum}
    r = requests.post(url, files=files, data=values)
    print (r.content)

def main():
    while True:
        takevideo()
        hum, airtemp = humair()
        print (hum)
        bar = barometric()
        sendpost()
        time.sleep(900)

main()
  • This is a general programming question and has nothing to do with Raspberry Pi. – Ingo Jul 9 '18 at 20:59
0

To pass values from main() to sendpost, define senpost()

def sendpost(hum, airtemp):

With the rest being the same in that function.

Then modify the main function so that when it calls sendpost() it calls it like:

sendpost(hum, airtemp)

Again, not modifying anything else.

This will send the current values of hum and airtemp from main() to sendpost().

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.