-
Notifications
You must be signed in to change notification settings - Fork 1
/
GTP.py
92 lines (77 loc) · 3.04 KB
/
GTP.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
# -*- coding: utf-8 -*-
#!/usr/bin/python
#import all required libraries
import pyrebase # For reading and writing data to the firebase database
import sys
import Adafruit_DHT # To read temperature and humidity data from DHT11 sensor
import json, requests
import RPi.GPIO as GPIO # Raspberry Pi library to read and write sensor ports
import time # To set manual override time for LED
#create function to check sensor reading
def LSval (LSpin):
reading = 0
GPIO.setup(LSpin,GPIO.OUT)
GPIO.output(LSpin,GPIO.LOW)
time.sleep(1)
GPIO.setup(LSpin,GPIO.IN)
while (GPIO.input(LSpin) == GPIO.LOW):
reading += 1
return reading
GPIO.setmode(GPIO.BCM)
#Light sensor =LS Light is LED
LED=21
LS=13
GPIO.setup(LED,GPIO.OUT)
config = {
"apiKey": "insertkeyhere",
"authDomain": "gtpdatabase.firebaseapp.com",
"databaseURL": "https://gtpdatabase.firebaseio.com",
"projectId": "gtpdatabase",
"storageBucket": "gtpdatabase.appspot.com",
"messagingSenderId": "791376230162"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
#url = "https://gtpdatabase.firebaseio.com/temperature.json"
while True:
humidity, temperature = Adafruit_DHT.read_retry(11,4)
print("Temp: {0:0.1f} C Humidity: {1:0.1f} %".format(temperature, humidity))
lightres = LSval(LS)
updatedata = {"Temperature": temperature,
"Humidity": humidity,
"Light Resistance": lightres}
db.child("plant1").child("Latest Readings").update(updatedata)
#data = {'humidity': humidity, 'temperature': temperature}
print(updatedata)
#Read the database for LED status
led = db.child("plant1").child("LED Switch").get().val()
#check the number returned from the function and turn LED on or off as needed
if led == 1:
GPIO.output(LED,True)
time.sleep(10)
elif led == 0.5:
if lightres>5000:
GPIO.output(LED,True)
else:
GPIO.output(LED,False)
else:
GPIO.output(LED,False)
time.sleep(10)
db.child("plant1").update({"LED Switch": 0.5})
#Update the database
humidity_readings = db.child("plant1").child("Readings").child("Humidity").get()
humidity_readings = humidity_readings.val()[1:] + [humidity]
temp_readings = db.child("plant1").child("Readings").child("Temperature").get()
temp_readings = temp_readings.val()[1:] + [temperature]
lightres_readings = db.child("plant1").child("Readings").child("Light Resistance").get()
lightres_readings = lightres_readings.val()[1:] + [lightres]
db.child("plant1").child("Latest Readings").update(updatedata)
update_readings = {"Humidity": humidity_readings,
"Temperature": temp_readings,
"Light Resistance": lightres_readings}
db.child("plant1").child("Readings").update(update_readings)
#requests.post(url, json.dumps(data))
#Python 3.5.3 (default, Jan 19 2017, 14:11:04)
#[GCC 6.3.0 20170124] on linux
#Type "copyright", "credits" or "license()" for more information.
GPIO.cleanup()