Python Forum
Rain sensor output only on change
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rain sensor output only on change
#1
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
Reply
#2
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".
Reply
#3
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.
Reply
#4
I worked out a solution myself and it works.

Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output values change akbarza 3 539 Oct-18-2023, 12:30 PM
Last Post: deanhystad
  Find a specific keyword after another keyword and change the output sgtmcc 5 855 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Change Text Color Output in Python bluethundr 2 8,744 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  Send The output of Gyroscope sensor to node red jenkins43 1 2,123 Feb-07-2019, 11:00 AM
Last Post: Larz60+
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,841 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 2,203 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  Working with rain time series dmildem 3 3,199 Nov-28-2018, 11:27 PM
Last Post: Larz60+
  Change output of cells. Hoogoo 2 2,627 Nov-19-2018, 12:47 AM
Last Post: Larz60+
  RPi with IR Sensor pimlicosnail 1 2,208 Jun-24-2018, 08:53 AM
Last Post: Larz60+
  Change the output window size and display area issac_n 0 2,275 Apr-13-2018, 04:41 AM
Last Post: issac_n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020