forked from malvern-code-club/auqa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsense.py
110 lines (87 loc) · 3.12 KB
/
sense.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
import os
if os.environ["TEST"] != "y": # Make sure we are not testing as this will import error
from sense_hat import SenseHat
import RPi.GPIO as GPIO
import sqlite3
import time
import datetime
import random
import camera
conn = sqlite3.connect("database.db", check_same_thread=False) #SQLite3 Setup
c = conn.cursor() #SQL Cursor
c.execute("""CREATE TABLE IF NOT EXISTS `data` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`timestamp` DATE NOT NULL DEFAULT (datetime('now','localtime')),
`humidity` REAL,
`temperature` REAL,
`pressure` REAL,
`soil_humidity` REAL,
`image`, TEXT
);""")
conn.commit()
c.execute("""CREATE TABLE IF NOT EXISTS `options` (
`name` TEXT,
`value` TEXT
);""")
conn.commit()
if os.environ["TEST"] != "y":
s = SenseHat() #Sensehat Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN) # Soil Sensor
GPIO.setup(18, GPIO.OUT) # Pump
else:
s = None
def get_data(): #Get humidity and temperature
if os.environ["TEST"] != "y":
humidity = s.get_humidity()
temp = s.get_temperature()
pressure = s.get_pressure()
soil_humidity = GPIO.input(17)
else:
# Generate Fake Data
humidity = random.uniform(30, 80) # 30% to 80%
temp = random.uniform(10, 30) # 10degC to 30degC
pressure = random.uniform(1000, 2000) # 1000mbar to 2000mbar
soil_humidity = random.uniform(0, 1) # 1 or 0
log("=========================================")
log("Current Humidity: " + str(humidity) + " %")
log("Current Temperature: " + str(temp) + " degC")
log("Current Air Pressure: " + str(pressure) + " mbar")
log("-----------------------------------------")
log("Current Soil Humidity: " + str(soil_humidity))
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%H-%M-%S-%d')
filename = st+".jpg"
camera.Capture(filename)
insert_data(humidity, temp, pressure, soil_humidity, filename)
def insert_data(humidity, temperature, pressure, soil_humidity, filename): # Put data into the database
c.execute("INSERT INTO `data` (humidity, temperature, pressure, soil_humidity, image) VALUES (?, ?, ?, ?, ?)", [humidity, temperature, pressure, soil_humidity, filename])
conn.commit()
return c.lastrowid
# TODO: Replace this with a CRON Job
def go():
while True:
get_data()
rowid = c.lastrowid
c.execute("SELECT * FROM data WHERE id=?", [rowid])
soil_humidity = c.fetchall()
soil_humidity = soil_humidity[0][5]
# Digital: 1 = water, 0 = Good
if soil_humidity >= 1: # Digital
water()
else:
log("Not watering plant because not over threshold")
time.sleep(10)
if __name__ == "__main__":
go()
def log(message):
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S %d/%m/%Y')
print("[" + st + "] ", message)
def water():
log("== Watering for 5s... ==")
if os.environ["TEST"] != "y":
GPIO.output(18, GPIO.HIGH)
time.sleep(5)
GPIO.output(18, GPIO.LOW)
log("== Watered Plant ==")