-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartgarden.py
More file actions
136 lines (109 loc) · 3.33 KB
/
smartgarden.py
File metadata and controls
136 lines (109 loc) · 3.33 KB
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
#!/usr/bin/python3
import smbus
import Adafruit_DHT
import time
import spidev
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
import IPMA
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000
# GPIO pins setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Create sensor object
DHT_sensor = Adafruit_DHT.DHT11
dht_pin = 16 # data pin on the raspberry pi
# global variables
humidity = 0
temperature = 0
# Define sensor channels
light_channel = 0
temp_channel = 1
# Water pump GPIO
pump = 23
#initialize water pump status
pumpSts = 0
# define as output and turn off
GPIO.setup(pump, GPIO.OUT)
GPIO.output(pump, GPIO.LOW)
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
def ConvertTemp(data):
# ADC Value
# (approx) Temp Volts
# 0 -50 0.00
# 78 -25 0.25
# 155 0 0.50
# 233 25 0.75
# 310 50 1.00
# 465 100 1.50
# 775 200 2.50
# 1023 280 3.30
temp = ((data * 330)/float(1023))-50
temp = round(temp,2)
return temp
app = Flask(__name__)
@app.route('/') # this tells the program what url triggers the function when a request is made
def index():
# global temperature, humidity
try: #check to see if the DHT sensor is connected
humidity, temperature = Adafruit_DHT.read_retry(DHT_sensor, dht_pin) #get the values from the sensor
if humidity is not None and temperature is not None:
tVal = '{:.1f}'.format(temperature)
hVal = '{:.1f}'.format(humidity)
humidity ='{:.1f}'.format(humidity) #convert value to one decimal place
temperature ='{:.1f}'.format(temperature) #convert value to one decimal place
except: # If the sensor is not connected send null values
humidity = 0
temperature = 0
pass
try:
# Read the light sensor data
light_level = ReadChannel(light_channel)
except: # If the sensor is not connected send null values
light_level = 0
pass
try:
# Read the temperature sensor data
temp_level = ReadChannel(temp_channel)
temp = ConvertTemp(temp_level)
except:
temp = 0
pass
try:
# Read water pump status
pumpSts = GPIO.input(pump)
except:
pumpSts = 0
pass
#variables to pass through to the web page
templateData = {
'title': 'Smart Garden',
'humidity' : humidity,
'temperature' : temperature,
'light' : light_level,
'temp' : temp,
'pump': pumpSts
}
return render_template('index.html', **templateData) #when a html request has been made return these values
@app.route("/<deviceName>/<action>")
def action(deviceName, action):
if action == "on":
GPIO.output(pump, GPIO.HIGH)
if action == "off":
GPIO.output(pump, GPIO.LOW)
pumpSts = GPIO.input(pump)
templateData = {
'temperature': temperature,
'humidity': humidity,
'pump' : pumpSts
}
return render_template('index.html', **templateData)
if __name__ == '__main__':
app.jinja_env.cache = {}
app.run(debug=True, host='0.0.0.0', port=5000, threaded=True)