Python Forum
Rain sensor output only on change - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Rain sensor output only on change (/thread-37186.html)



Rain sensor output only on change - Pete6 - May-10-2022

Greeting all. First post here and a bit of a newbie to coding.

I have built a simple weather station. It samples a rain detector sensor once every 10 seconds in a try: loop. I am running this on a Raspberry Pi 4/8Gb running Buster which uses Python 3.7.2. I wish to output CSV data to a local file each time it rains or does not rain. I do not want o output data every 10 seconds. I want a new data point only on state change i.e. only when it starts raining or has stopped.

I am sampling every 10 seconds because I am sampling other sensors too but this one is giving my embryonic programming skills trouble.

If anyone can give me a code fragment that solves this problem I would be most grateful.

Thanks in advance
Pete


RE: Rain sensor output only on change - ndc85430 - May-10-2022

You're going to need to give more details, like showing your existing code and crucially show which library you're using to communicate with the sensor. Reacting to a state change is quite different to asking for data regularly. It'll be up to the library whether it provides both modes and the documentation should tell you. The terms to look up are "event driven" and "polling".


RE: Rain sensor output only on change - Pete6 - May-11-2022

Thanks for your reply. Here is my code as requested.

The application for this is my astronomy observatory which has a roll-off roof and two telescopes. The roof needs to be closed if it rains BUT I need to move the telescopes out of the roof closing path first. This last is done by the telescope controller (a Windows 10 PC) which is informed that it is raining by this piece of code. The telescope controller then parks the telescope(s) on a pre-determined position away from the roof close path and then sends the "close the roof" command. When it stops raining this piece of code tells the telescope controller and the roof is enabled for opening once more.

From my code you can see that the Rain / NoRain file is created or deleted with each loop iteration. I really only want to create or delet the file on rain event change.

#!/usr/bin/env python

import subprocess
import sys
import os
import time
import RPi.GPIO as GPIO
# BME280 Temp, RH & Pressure

raining_state = 0

# GPIO Pin assigns and names.

#Roof_Closed_NO = 18 # GPIO Pin detecting closed roof
#Roof_Closed_NC = 27   # GPIO Pin detecting open roof
#Roof_Open_NO = 15   # GPIO Pin detecting open roof
#Roof_Open_NC = 25   # GPIO Pin detecting open roof

Roof_Open = 27   # GPIO Pin detecting open roof
Roof_Closed = 25 # GPIO Pin detecting closed roo

Rain = 17 # GPIO Pin detecting Rain
Scope_1 = 7 # GPIO Pin detecting Pete's telescope Park Status
Scope_2 = 8 # GPIO Pin detecting Bill's telescope Park Status

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

GPIO.setup(Roof_Open, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Roof_Closed, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Rain, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Scope_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Scope_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#--------------------------------------------------------------

try:
   while 1:
       # Rain, Roof and telescope Park Status
       # RAIN GPIO 17
       rain_state = GPIO.input(Rain)

       if rain_state:
          Raining = ("Dry    ")
          raining_state = 0
          if os.path.exists("/home/pi/unsafe/raining"):
             os.remove("/home/pi/unsafe/raining")
       else:
          Raining = ("Raining")
          raining_state = 1
          f_rain = open('/home/pi/unsafe/raining','w')
          f_rain.write('Raining'+'\n')
          f_rain.close()

except KeyboardInterrupt:
    # Position the cursor and say bye-bye.
    print ("Program exiting...")
finally:
       print ("")
Below is a Google Photos image block diagram of the observatory controller. Not ethat the rain detector runs at 12VDC and provides a dry closed contact when rain is detected. It also has an internal heater that turns on when it rains so that when rain stops the excess water is quickly evaporated. The Raspberry Pi' GPIO pins are 3V3 so I needed to optocoupler the incoming 12V from the various sensors to something the Pi could digest.

System Overview

Thanks again for your help.


RE: Rain sensor output only on change - Pete6 - May-11-2022

I worked out a solution myself and it works.

Thank you.