-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.py
87 lines (73 loc) · 2.6 KB
/
monitor.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
import serial
import sys
import argparse
from statistics import mean
from twitter import Twitter
from thoughts import Thoughts
moisture_threshold = 415 # arbitrary
humidity_threshold = 35.0 # arbitrary
unearthed_reading = 550 # if the sensor is not in the soil
# Run this thing with --notweet to output the tweet to stdout
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--notweet', default=False, action='store_true')
parser.add_argument('--topic', default="moisture", action="store")
return parser.parse_args()
def parse_sample(sample_list):
readings = {
"temp": [],
"humidity": [],
"moisture": []
}
for reading in sample_list:
if '\r' not in reading:
break
reading = reading.strip()
reading = reading.split(':')
if len(reading) == 2:
key = reading[0]
val = reading[1]
if key == "moisture":
try:
readings["moisture"].append(int(val))
except:
print("we got a weird value or something")
elif key in ["humidity", "temp"]:
try:
readings[key].append(float(val))
except:
print("we got a weird value or something")
return readings
def output(args, msg):
if (args.notweet):
print(msg)
else:
Twitter.tweet(msg)
args = get_args()
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
sample = ser.read(200) # pull 200 bytes off serial
try:
decoded_sample = sample.decode("utf-8")
except:
print("we don't know why but there's something wrong")
sys.exit()
# turn the decoded sample string into a list of samples
sample_list = decoded_sample.split('\n')
readings_list = parse_sample(sample_list)
if args.topic == "moisture" and len(readings_list["moisture"]):
avg_reading = int(round(mean(readings_list["moisture"])))
if avg_reading > unearthed_reading:
# The sensor reading is high enough that it's probably not in soil
msg = Thoughts.get_unearthed_thought(avg_reading)
elif avg_reading > moisture_threshold:
# The reading is above the arbitrary dryness threshold
msg = Thoughts.get_thirsty_thought(avg_reading)
else:
# nicenice
msg = Thoughts.get_happy_thought(avg_reading)
output(args, msg)
if args.topic == "humidity" and len(readings_list["humidity"]):
avg_reading = round(mean(readings_list["humidity"]), 1)
if avg_reading < humidity_threshold:
msg = Thoughts.get_dry_thought(avg_reading)
output(args, msg)