/
palm_tree_measurement.py
90 lines (69 loc) · 2.29 KB
/
palm_tree_measurement.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
#!/usr/bin/python3
from ABE_ADCPi import ADCPi
from ABE_helpers import ABEHelpers
import sqlite3
import datetime
import time
import sys
import os.path
"""
================================================
ABElectronics ADC Pi 8-Channel ADC data-logger demo
Version 1.0 Created 29/02/2015
Requires python 3 smbus to be installed
run with: python3 demo-read_voltage.py
================================================
Initialise the ADC device using the default addresses and sample rate, change
this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""
db_name = 'measurements.db'
def createdb():
conn = sqlite3.connect(db_name)
c = conn.cursor()
try:
c.execute("CREATE TABLE 'measurements' ('date' TEXT NOT NULL, 'temperature' TEXT NOT NULL, 'light' TEXT NOT NULL, 'moisture' TEXT NOT NULL)")
conn.commit()
except sqlite3.OperationalError:
print("Failed to create database!")
conn.close()
def writetodb(temperature, light, moisture):
conn = sqlite3.connect(db_name)
c = conn.cursor()
try:
c.execute("INSERT INTO measurements VALUES ('" + str(datetime.datetime.now()) + "','" + temperature + "','" + light + "','" + moisture + "')")
conn.commit()
except sqlite3.OperationalError:
print("Failed to commit to database!")
conn.close()
def main():
i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCPi(bus, 0x68, 0x69, 18)
if not os.path.isfile(db_name):
createdb()
while (True):
temp = 0
light = 0
moisture = 0
for x in range(0, 60):
# read from adc channels and write to the log file
temp = temp + (adc.read_voltage(1) - 0.5) * 100
light = light + adc.read_voltage(2)
moisture = moisture + adc.read_voltage(3)
time.sleep(0.4)
# Average temp
temp = temp / 60
# Correct temp
temp = temp - 1.2
# Average light
light = light / 60
# Average moisture
moisture = moisture / 60
print("temp,light,moisture")
print(temp)
print(light)
print(moisture)
writetodb("%02f" % temp, "%02f" % light, "%02f" % moisture)
if __name__ == "__main__":
sys.exit(main())