-
Notifications
You must be signed in to change notification settings - Fork 0
/
liikaalunta.py
142 lines (109 loc) · 2.92 KB
/
liikaalunta.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import RPi.GPIO as GPIO
import time
import httplib, urllib
import mysql.connector
import datetime
import config
updateDbTimer = 0
currentStreak = 0
valuesOnMinute = [0] * 60
ignoreCount = 0
minuteCounter = 60
#Setup Mysql settings
mydb = mysql.connector.connect(
host=config.dbHost,
user=config.dbUser,
password=config.dbPassword,
database=config.dbDatabase
)
GPIO.setmode(GPIO.BCM)
#Set pins
GPIO_TRIGGER = 14
GPIO_ECHO = 15
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
#Calculate distance in cm
def distance():
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
TimeElapsed = StopTime - StartTime
distance = (TimeElapsed * 34300) / 2
return 160 - distance
#Send notification to phone
def sendNotification(dist):
conn = httplib.HTTPSConnection("pushsafer.com:443")
conn.request("POST", "/api",
urllib.urlencode({
"k": config.notSecret,
"m": "%.1f cm" % dist,
"t": "Lunta",
"i": "63",
"s": "3",
"v": "",
"p": "",
}), { "Content-type": "application/x-www-form-urlencoded" })
response = conn.getresponse()
print (response.status, response.reason)
data = response.read()
print (data)
#Update values to database
def updateDatabase(lumiMaara):
global minuteCounter
mycursor = mydb.cursor()
sql = "UPDATE lumi SET lumimaara = '%s' WHERE paikka = 'koti'"
data = (lumiMaara,)
mycursor.execute(sql, data)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
print(minuteCounter)
if(minuteCounter >= 60):
minuteCounter = 0
print(minuteCounter)
now = datetime.datetime.now()
sql2 = "INSERT INTO historia (date, lumimaara) VALUES (%s, %s)"
data2 = (now, lumiMaara)
mycursor.execute(sql2, data2)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
#Get median value of distances over one minute
def getMedianOfValues(ignoreCount):
valuesOnMinute.sort()
ignoreCount = int(ignoreCount / 2)
medianValue = valuesOnMinute[30 - ignoreCount]
return medianValue
#Main loop
if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)
valuesOnMinute[updateDbTimer] = dist
if dist > 1000:
ignoreCount += 1
if(updateDbTimer >= 59):
updateDbTimer = 0
minuteCounter += 1
medianValue = getMedianOfValues(ignoreCount)
ignoreCount = 0
updateDatabase(medianValue * 10)
#If amount of snow is greater than x
if medianValue > 15:
currentStreak += 1
#If amount of snow have stayed over x for 5 minutes straight
if currentStreak >= 5:
currentStreak = 0
sendNotification(medianValue)
else:
currentStreak = 0
updateDbTimer += 1
time.sleep(1)
except KeyboardInterrupt:
print ("Measurement stopped by User")
GPIO.cleanup()