-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsesors.py
237 lines (203 loc) · 6.23 KB
/
sesors.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import grovepi
from grovepi import *
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
from threading import Thread
import random
import time
# Author Jordan May
# x15515673
#
# Code sourced from below, I used only code needed to activate the sensors.
# Temperature/Humidity Sensor ref : http://wiki.seeedstudio.com/Grove-Temperature_Sensor/
# Light Sensor ref : http://wiki.seeedstudio.com/Grove-Light_Sensor/
# Soil ref : https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_moisture_sensor.py.
# # Firebase code : https://firebase.google.com/docs/reference/admin/python/firebase_admin
# Set up variables
temp_humidity_port = 2
light_sensor_port = 0
soil_port = 1
grovepi.pinMode(light_sensor_port,"INPUT")
humidityState = False
temperatureState = False
# Set up Firebase
try:
fb_credentials = credentials.Certificate('smartplant-2fc4c-firebase-adminsdk-gjnsf-78fd7405b8.json')
firebase_admin.initialize_app(fb_credentials, {'databaseURL' : 'https://smartplant-2fc4c.firebaseio.com/'})
print("Success")
global node
# root
node = db.reference()
except IOError as e:
print("Something went wrong" + e)
# start listener
def listener():
firebase_admin.db.reference('/').listen(listener_fb)
# listener for firebase events
def listener_fb(event):
# Make a reference to our state
# Temperature
temperature_state = db.reference('Temperature/Information/state')
temperature_value = db.reference('Temperature/Information/message')
temperature_timer = db.reference('Temperature/Information/rateLimit')
# Humidity
humidity_state = db.reference('Humidity/Information/state')
humidity_value = db.reference('Humidity/Information/message')
humidity_timer = db.reference('Humidity/Information/rateLimit')
# Soil
soil_state = db.reference('Soil/Information/state')
soil_value = db.reference('Soil/Information/message')
soil_timer = db.reference('Soil/Information/rateLimit')
# Light
light_state = db.reference('Light/Information/state')
light_value = db.reference('Light/Information/message')
light_timer = db.reference('Light/Information/rateLimit')
# Check for our temperature
if temperature_state.get() == "ON":
print("Temperature is on")
# Define global state
# Turn this on]
global temperatureState
temperatureState = True
try:
# Start publish thread for temperature
temperature_publishing = Thread(target = temperature_publish_firebase)
# if the thread hasnt being started then start
if not temperature_publishing.isAlive():
temperature_publishing.start()
print("Startinng temperature thread")
except Exception as e:
pass
else:
print("Temperature is off")
temperatureState = False
# Check for our humidity
if humidity_state.get() == "ON":
print("Humidity is on")
# global state
global humidityState
humidityState = True
try:
# start thread for humidity
humidity_publishing = Thread(target = humidity_publish_firebase)
# if not started already
if not humidity_publishing.isAlive():
print("Starting humidity thread")
humidity_publishing.start()
except Exception as e:
pass
else:
humidityState = False
print("Humidity is turning off")
# Check for our Light
if light_state.get() == "ON":
print("Humidity is on")
# globals
global lightState
lightState = True
try:
# start thread
light_thread = Thread(target = light_publish_firebase)
if not light_thread.isAlive():
print("Starting up light thread")
light_thread.start()
except Exception as e:
pass
else:
lightState = False
# Check for soil
if soil_state.get() == "ON":
print("Soil is turning on")
global soilState
soilState = True
try:
# start thread
soil_thread = Thread(target = soil_publish_firebase)
if not soil_thread.isAlive():
print("Starting up the soil thread")
soil_thread.start()
except Exception as e:
pass
else:
soilState = False
# PUBLISHING TO FIREBASE THE VALUES
# Publishing temperature
def temperature_publish_firebase():
# get state of sensor atm
rate = db.reference('Temperature/Information/rateLimit')
rates = int(rate.get())
# As long as our temperature state is on, keep on reading from our sensors for temperature
while temperatureState:
temp = temperature_sensor(rates)
print(temp)
temp1 = str(temp)
update_fb = node.child("Temperature/Information/").update({'message' : temp})
# Publishing humidity
def humidity_publish_firebase():
# get state of sensor atm
rate = db.reference('Humidity/Information/rateLimit')
rates = int(rate.get())
# as long as our temperature state is on, keep reasding from our sensors
while humidityState:
hum = humidity_sensor(rates)
temp1 = str(hum)
update_fb = node.child("Humidity/Information/").update({'message' : hum})
# Publishing Light Sensor
def light_publish_firebase():
rate = db.reference('Light/Information/rateLimit')
rates = int(rate.get())
while lightState:
light = light_sensor(rates)
temp1 = str(light)
update_fb = node.child("Light/Information/").update({'message' : light})
# Publishing Soil Sensor
def soil_publish_firebase():
rate = db.reference('Soil/Information/rateLimit')
rates = int(rate.get())
while soilState:
soil = soil_sensor(rates)
temp1 = str(soil)
update_fb = node.child("Soil/Information/").update({'message' : soil})
# SINGLE GETTER CALLS TO OUR SENSORS
# Temperature Sensor
def temperature_sensor(rateLimit):
time.sleep(rateLimit)
try:
[temp, hum] = dht(temp_humidity_port, 0)
print("Current temperature is = ", temp)
temperature = temp
return temperature
except IOError:
return 0
# Humidity
def humidity_sensor(rateLimit):
try:
[temp, hum] = dht(temp_humidity_port, 0)
print("Current humidity is = ", hum)
humidity = hum
return humidity
except IOError:
return 0
# Light
def light_sensor(rateLimit):
time.sleep(rateLimit)
try:
sensor_value = grovepi.analogRead(light_sensor_port)
print("Current light is = ", sensor_value)
values = sensor_value
return values
except IOError:
return 0
# Soil
def soil_sensor(rateLimit):
time.sleep(rateLimit)
try:
soil_value = grovepi.analogRead(soil_port)
print("Current soil is = ", soil_value)
soil = soil_value
return soil
except IOError:
return 0
listener_thread = Thread(target = listener(), args=(temperature_publish_firebase,))
listener_thread.start()