-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
64 lines (55 loc) · 2.12 KB
/
test.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
import Adafruit_DHT
import time
import RPi.GPIO as gpio
import board
import pyrebase
import serial
config = {
"apiKey": "GLYZ3rfUD8H3xUxO9KcN34s1rTZ55JOtgE0FETSU",
"authDomain": "iot-soil-moisure-monitoring.firebaseapp.com",
"databaseURL": "https://iot-soil-moisure-monitoring-default-rtdb.firebaseio.com/",
"storageBucket": "iot-soil-moisure-monitoring.appspot.com"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0',9600, timeout = 1)
ser.flush()
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor1 = Adafruit_DHT.DHT11
# Set GPIO sensor is connected to
gpio = 4
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temp = Adafruit_DHT.read_retry(sensor1, gpio)
while True:
soiltemp = sensor.get_temperature()
# print(" The temperature is %s celsius" % temperature)
n = ser.readline().decode('utf-8').rstrip()
p = ser.readline().decode('utf-8').rstrip()
k = ser.readline().decode('utf-8').rstrip()
moisture = ser.readline().decode('utf-8').rstrip()
irrigation = ser.readline().decode('utf-8').rstrip()
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temp is not None:
print('Temp={0:0.1f} Humidity={1:0.1f}'.format(temp, humidity))
time.sleep(2)
# else:
# print('Failed to get reading. Try again!')
print(f'Nitrogen: {n}, Phosphorus: {p}, Potassium: {k}, Soil Moisture: {moisture}, Irrigation Status: {irrigation}, Soil Temp: {soiltemp:.2f}')
data = {
"Temperature" : temp,
"Humidity" : humidity,
"SoilTemperature" : soiltemp,
"Nitrogen" : n,
"Phosphorus" : p,
"Potassium" : k,
"SoilMoisture" : moisture,
"Irrigation" : irrigation
}
db.child("Status").push(data)
db.update(data)
print("Sent to Firebase")