[SOLVED] Comparing a variable against itself? | Python 2.7.10

Oct 26, 2020
15
0
20
Thank you in advance for reading and taking the time to troubleshoot this with me!
Let me first start off with the important stuff:
Target Device:
  • Raspberry Pi 4B
  • Electronic-Salon Relay Hat
Development Environment:
  • macOS Catalina
  • PyCharm
  • Python 2.7.10
At my home I have a spring that serves my home with water. The solution I came up with to prevent dirty water caused by bad rainy weather loosening up the ground soil from entering my cistern is closing a valve and waiting for about 12 hours for the water to clear back up. Then I open the valve and clear water flows into my cistern providing my home with water, and that solution works really well.

I recently came up with the conclusion that I want to automate this process with a normally open solenoid. I purchased a Raspberry Pi, a Relay Hat, and an Ambient Weather weather station.

What I'm looking to do with Python 2.7.10 is check the same variable against itself after an allotted time. In this example, I'm checking the relative barometric pressure against itself and I'm wanting to look for a significant negative change in that variable.

i.e "What does variable A have? Okay, now wait 3 seconds. What does A have now? How much has it changed?"

This is what I've bodged together so far, how can I improve? Thank you.

At first I was thinking maybe I should plot a chart with the data and compare the difference between the two plot points, but I wasn't sure how to use Matplotlib.

Code:
# This is the executing script for the Smart Valve.
# This project is powered by raspberry pi and 120 angry pixies.
import time,imp
from ambient_api.ambientapi import AmbientAPI

# This code will pull the data from the weather computer

api = AmbientAPI()
devices = api.get_devices()
device = devices[0]
time.sleep(1) #pause for a second to avoid API limits


# The code below is meant for telling the python interpreter to behave normally whether or not it's in a RPi env or a
# developer env

try:
    imp.find_module('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO

# this code compares the rate of change for the barometric pressure over time and checks if rate is negative
a1 = None
a2 = None

while True:
    weatherData = device.get_data()
    data = dict(weatherData[0])
    pressure = data[u'baromrelin']
    wind = data[u'windspeedmph']
    rain = data[u'hourlyrainin']
    a1 = pressure
    time.sleep(30)
    a2 = pressure
    print("A1 is equal to " + str(a1))
    print("A2 is equal to " + str(a2))
    if a1 > a2:
        print("we should close the valve, it'll rain soon")
        continue
    elif a1 == a2:
        print("It's all hunky dory up here!")
        break
 
Solution
The code below was suggested by MikeCPT from StackExchange and it's is the best solution for my needs. Thanks everyone who contributed!

Code:
prev = None

# Pass 1

weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 123.4

if prev is None or curr > prev:
    print("Pressure is rising!")
# Current iteration's current pressure will become the next iteration's previous pressure. You could even then see curr = None because we about to overwrite on next iteration.
prev = curr # 123.4

time.sleep(30)

# Pass 2

weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 234.5

if prev is None or curr > prev:
    print("Pressure...

kanewolf

Titan
Moderator
Thank you in advance for reading and taking the time to troubleshoot this with me!
Let me first start off with the important stuff:
Target Device:
  • Raspberry Pi 4B
  • Electronic-Salon Relay Hat
Development Environment:
  • macOS Catalina
  • PyCharm
  • Python 2.7.10
At my home I have a spring that serves my home with water. The solution I came up with to prevent dirty water caused by bad rainy weather loosening up the ground soil from entering my cistern is closing a valve and waiting for about 12 hours for the water to clear back up. Then I open the valve and clear water flows into my cistern providing my home with water, and that solution works really well.

I recently came up with the conclusion that I want to automate this process with a normally open solenoid. I purchased a Raspberry Pi, a Relay Hat, and an Ambient Weather weather station.

What I'm looking to do with Python 2.7.10 is check the same variable against itself after an allotted time. In this example, I'm checking the relative barometric pressure against itself and I'm wanting to look for a significant negative change in that variable.

i.e "What does variable A have? Okay, now wait 3 seconds. What does A have now? How much has it changed?"

This is what I've bodged together so far, how can I improve? Thank you.

At first I was thinking maybe I should plot a chart with the data and compare the difference between the two plot points, but I wasn't sure how to use Matplotlib.

Code:
# This is the executing script for the Smart Valve.
# This project is powered by raspberry pi and 120 angry pixies.
import time,imp
from ambient_api.ambientapi import AmbientAPI

# This code will pull the data from the weather computer

api = AmbientAPI()
devices = api.get_devices()
device = devices[0]
time.sleep(1) #pause for a second to avoid API limits


# The code below is meant for telling the python interpreter to behave normally whether or not it's in a RPi env or a
# developer env

try:
    imp.find_module('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO

# this code compares the rate of change for the barometric pressure over time and checks if rate is negative
a1 = None
a2 = None

while True:
    weatherData = device.get_data()
    data = dict(weatherData[0])
    pressure = data[u'baromrelin']
    wind = data[u'windspeedmph']
    rain = data[u'hourlyrainin']
    a1 = pressure
    time.sleep(30)
    a2 = pressure
    print("A1 is equal to " + str(a1))
    print("A2 is equal to " + str(a2))
    if a1 > a2:
        print("we should close the valve, it'll rain soon")
        continue
    elif a1 == a2:
        print("It's all hunky dory up here!")
        break
If it was me, I would run standard weather station software on the PI. Something like weewx. Then use the database of data that the weatherstation software already archives. Sum the rain sensor over the last 24 or 36 hours. If that sum is greater than some amount don't open the valve.
To close the valve, you could again use the rain sensor data. If the valve is open and there has been some amount of rain in the last hour, close the valve.
Then it is just a cron job to run this. Check the state of the valve and run the proper database query.
 
Oct 26, 2020
15
0
20
If it was me, I would run standard weather station software on the PI. Something like weewx. Then use the database of data that the weatherstation software already archives. Sum the rain sensor over the last 24 or 36 hours. If that sum is greater than some amount don't open the valve.
To close the valve, you could again use the rain sensor data. If the valve is open and there has been some amount of rain in the last hour, close the valve.
Then it is just a cron job to run this. Check the state of the valve and run the proper database query.

Thanks, but I think I figured it out. Would you like to know what I chose to do?
 
Oct 26, 2020
15
0
20
The code below was suggested by MikeCPT from StackExchange and it's is the best solution for my needs. Thanks everyone who contributed!

Code:
prev = None

# Pass 1

weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 123.4

if prev is None or curr > prev:
    print("Pressure is rising!")
# Current iteration's current pressure will become the next iteration's previous pressure. You could even then see curr = None because we about to overwrite on next iteration.
prev = curr # 123.4

time.sleep(30)

# Pass 2

weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 234.5

if prev is None or curr > prev:
    print("Pressure is rising!")
    # 234.5 > 123.4

time.sleep(30)
 
Solution

kanewolf

Titan
Moderator
Why use the barometer rather than the rain sensor ? You can measure actual rainfall rather than a secondary indicator. I used the database method I described to prompt me to water my lawn. You can have significant barometer changes without rain.
 
Oct 26, 2020
15
0
20
Why use the barometer rather than the rain sensor ? You can measure actual rainfall rather than a secondary indicator. I used the database method I described to prompt me to water my lawn. You can have significant barometer changes without rain.
The idea is to predict buckets of rain otherwise known as torrential downpour before the water gets contaminated. the barometric reading isn't the only variable used. Now I'm using it as a points system, if closeScore == 2 close the valve or something like that. The whole source code isn't here. I live in a pretty tropical region where it can be sunny one minute and thunder and lighting with a ridiculous amount of rain the next without very many visible tells, and by the time I've measured the rate of downfall the water is already contaminated.
 
Last edited: