Using a Piicodev Button to sense Tipping Bucket Rain Gauge

So far my weather station has Piico BM 280 displaying on Oled screen (linked to Piicodev RPi adaptor and logging to file with a time stamp. I also have a Vaiasala ultrasonic anemometer logging using Minicom terminal to the USB port on the Raspberry pi

I have a tipping bucket rain gauge and have adapted a Piicodec Button and sample code to monitor the bucket tips.The rain gauge magnetic switch is wired parallel to the Button (see pic)

Here is the adaption of the sample tutorial code
Each tip is sensed as a “button press” To calibrate each button press represents 0.2mm of rain
The data are read to shell and logged to file every minute with a timestamp

# Get an accurate press-count, even when the sample-rate is very slow (3 seconds)
# Try clicking the button multiple times between updates. You ought to see that
# every click is accounted for!

from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices
from PiicoDev_Unified import sleep_ms
from datetime import datetime
 
button = PiicoDev_Switch()   # Initialise the module

total_presses = 0

while True:

    
    today = datetime.now()
    today_str = today.strftime("%Y-%m-%d %H:%M")
    
    text_file = open(r"/home/rgrif/Desktop/Rainfall.txt", "a") # a append
    
    total_presses += button.press_count * 0.2 # press_count resets to zero after being read
    print(today_str) 
    print('Total rainfall mm:', total_presses)
    sleep_ms(60000)
    
    #write the data to text file
    
    text_file.write ((today_str)+"," + str(total_presses)+ "," "\n")# \n is new line

    
    
    text_file.close()

The problem I need some help with is to stop and restart the script at a specific time (0900) each day or at least reset the count to 0 at 0900 each day
I have tried crontab but it will not run the script using the python(3) command with the the copied pathway appended. It always says the path is invalid

I also tried to use datetime and although it will run a simple command like print in the example below it also get hung up on the file paths. Is there a command to reset the count to zero at say 0900


import datetime
import time   
 
  
while True:
    time.sleep (1)
    if datetime.now().time().strftime("%H:%M") == '09:00':
       print ('you beauty')

I’d love to see an error message for this.
IMO crontab is the right tool for this application we just need to give it the right code, permissions, and ensure it’s in the path.

Would you mind copy and pasting the relevant crontab command you have tried and the error message output by the terminal?

Thanks Pimusix

I tested the crontab and works with a simple command say echo to display a phrase
When I use the python3/home/rgrif/rainfall.py there is no error message but it simly does nothing at the scheduled time see screen shot

the datetime script works when imbedded in the rainfall.py for a simple command eg print (‘you beauty’)
see the screen shot and the text returned in the Thonny shell

I think I dont understand the command line protocols to run the rainfall.py . Ive been reading up on making files executable and shebang, but I dont quite get it,yet
Maybe it is possible to command to set the count back to zero at 09:00

0 9 * * * python3/home/rgrif/rainfall.py seems off like your missing a space between python3 and /home

I think you want

#time --- exe --- path_to_script
0 9 * * * python3 /home/rgrif/rainfall.py

crontab needs

  1. when it should run the script (9am)
  2. what it needs to run the script (python3)
  3. where the script is (the rgrif directory)

Without that space you would only have two of the three args.
Does that fix your problem?

Hi Pixmusix,

I tried adding the space and also dropping the 3 . Crontab didnt respond at the the trigger time. I also added a space in the command line of the datetime routine and at the trigger time it returned a name error: name python3 not defined

1 Like

Oh. maybe you have an alias for python or are using python2 (not 3)

I suppose we want to use whatever version of python thorny has been using because thorny has been successful so far.

I actually have no idea what version of python thorny would be running by default.

Maybe a CE staff member knows?

1 Like

Hey!

Pretty sure thonny will use python3 by default if you are using an up to date version.

looking at this code 0 9 * * * python3 /home/rgrif/rainfall.py I think the issue is that this line will look for ‘python3’ in the current directory which it won’t find.

Try directing it to the default location like this 0 9 * * * /usr/bin/python3 /home/rgrif/rainfall.py

Hopefully, that fixes it for you. :slight_smile:

1 Like

Thanks Samuel for your reply. Unfortunately no success. The datetime routine returned a name error python3 not defined and the thonny assist analysed each dir of the command as invalid

Hey @Robert281517

One thing to check is that there has been permissions given to execute the file. Assuming your file is in the /home/rgrif directory you should be able to run:

cd /home/rgrif
chmod a+x rainfall.py

Ive given up on crontab as I still cant define the directory pathways to python3 and then execute the .py file.
Good news though, I persevered with the datetime module and the script below does the job :slightly_smiling_face:
With the tipping bucket NO magnetic switch in parallel with the piicodev button the script below lets the RPi report the calibrated (0.2ml per tip) rainfall to the Thonny shell with a time stamp, and log to a text file every 30 seconds.
The if datetime.now() routine resets the accumulated rain fall volume to zero at 09:00 every day. Just like the BOM The trick was to use this argument to sent button = PiicoDev_Switch() to re-initialise the module followed by total_presses = 0

from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices
from PiicoDev_Unified import sleep_ms
from datetime import datetime
import time 

 button = PiicoDev_Switch()   # Initialise the module

 total_presses = 0

 while True:
     time.sleep (1)
     if datetime.now().time().strftime("%H:%M") == ('09:00'):
         button = PiicoDev_Switch()   # Re - initialise module
         total_presses = 0
    
    today = datetime.now()
    today_str = today.strftime("%Y-%m-%d %H:%M")
    
    text_file = open(r"/home/rgrif/Desktop/Rainfall.txt", "a") # a append
    
    total_presses += button.press_count * 0.2 # press_count resets to zero after being read
    print(today_str) 
    print('Total rainfall mm:', total_presses)
    sleep_ms(30000)
    
    #write the data to text file
    
    text_file.write ((today_str)+"," + str(total_presses)+ "," "\n")# \n is new line

    
    
    text_file.close()


Edit Sorry but only part of the script was saved as script so the script outside the yellow has lost its indenture

Hi @Robert281517

Glad to hear that you managed to get it working!