-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainfall.py
executable file
·43 lines (34 loc) · 1.44 KB
/
rainfall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#! /usr/bin/env python
# Author: Gopal Krishan Aggarwal and Piyush Mangtani
# Contact: gopalkriagg@gmail.com
# Desc: This script must run at boot time. It continously coutns number of bucket tips inside rain gauge and after every 3 mins stores the amountof rainfall in rainReading.txt file and resets the rainfall counter. i.e. it stores the amount of rainfall in last 3 mins in rainRading.txt file.
from time import sleep
import Adafruit_BBIO.GPIO as GPIO
import os
cwd = os.path.dirname(os.path.realpath(__file__))
rainGaugePin = "P9_11"
bucketTips = 0 #Maintains the count of how many times bucket inside rain gauge tipped
GPIO.setup(rainGaugePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#IncBucketTipsCount() increments the count of bucketTips
def IncBucketTipsCount(y):
global bucketTips
bucketTips += 1
print "Bucket tipped"
try:
GPIO.add_event_detect(rainGaugePin, GPIO.FALLING, callback = IncBucketTipsCount, bouncetime = 100)
except:
GPIO.add_event_detect(rainGaugePin, GPIO.FALLING, callback = IncBucketTipsCount, bouncetime = 100)
def resetRainfallReading():
global bucketTips
bucketTips = 0
def getRainfallReading():
rainfall = bucketTips * 0.2794
return rainfall #Rainfall in millimeters in last 3 minutes
def chorus():
f = open(cwd+'/rainReading.txt', 'w', 0)
f.write(str(getRainfallReading()))
resetRainfallReading()
f.close()
while True:
sleep(3*60) #After every 3 minutes
chorus() #perform this chorus