· 5 years ago · Oct 14, 2020, 03:00 PM
1''''
2open terminal
3sudo apt update
4sudo apt install sense-hat
5sudo reboot'''''
6
7import sqlite3
8import time
9from datetime import datetime
10from sense_hat import SenseHat
11
12def main():
13 sense = SenseHat()
14 while True:
15 count = 0
16 sensorValue = 0
17 senses = ["t","h","g","p"]
18 sensorName = ""
19 for count in range(4):
20 if senses[count] == "t":
21 sensorValue = sense.get_temperature()
22 sensorID = 0
23 elif senses[count] == "h":
24 sensorValue = sense.get_humidity()
25 sensorID = 1
26 #elif senses[count] == "g":
27 #sensorValue = #need to acquire gas sensor first
28 #sensorID =
29 elif senses[count] == "p":
30 sensorValue = sense.get_pressure()
31 sensorID = 2
32 #sensorValue = float(input(""))
33 setValue(sensorValue,sensorID) #writes sensor data to database
34 checkExperiment(sensorID,sensorValue)
35 count+=1
36 time.sleep(1)
37 '''
38 is called detailing the storage of values to a database
39 checkExperiement subroutine is called detailing the alarm system
40 '''
41
42def setValue(sensorValue, sensorID):
43 #sensorName = str(input("Enter sensor name: "))
44 database = connectDatabase()
45 current_date = date.now()
46 current_time = time.now()
47 sql = "INSERT INTO Data VALUES (null, \"{}\", \"{}\", {}, {})".format(current_date, current_time, sensorID, sensorValue)
48 sendSQL(sql)
49
50def sendSQL(sql):
51 with sqlite3.connect("database.db")as db:
52 cursor = db.cursor()
53 cursor.execute(sql)
54
55def returnSQL(sql):
56 with sqlite3.connect("database.db")as db:
57 cursor = db.cursor()
58 cursor.execute(sql)
59 results = cursor.fetchall()
60 return results
61
62def connectDatabase():
63 sql = """CREATE IF NOT EXISTS Table SensorTable(
64 SensorID integer PRIMARY KEY AUTOINCREMENT,
65 SensorName string);"""
66 sendSQL(sql)
67
68 sql = """CREATE Table ExperimentTable(
69 ExperimentID integer PRIMARY KEY AUTOINCREMENT,
70 ExperimentName string,
71 Running boolean);"""
72 sendSQL(sql)
73
74 sql = """CREATE Table ExperimentSensorTable(
75 SensorExperiment integer PRIMARY KEY AUTOINCREMENT,
76 ExperimentID integer,
77 SensorID integer,
78 Condition string,
79 FOREIGN KEY (SensorID) REFERENCES SensorTable(SensorID),
80 FOREIGN KEY (ExperimentID) REFERENCES ExperimentTable(ExperimentID));"""
81 sendSQL(sql)
82
83 sql = """CREATE Table Data(
84 TestID integer PRIMARY KEY AUTOINCREMENT,
85 Date date,
86 Time time,
87 SensorID integer,
88 value float,
89 FOREIGN KEY (SensorID) REFERENCES SensorTable(SensorID));"""
90 sendSQL(sql)
91
92 sendSQL("INSERT INTO SensorTable VALUES (null, \"Temperature\")")
93 sendSQL("INSERT INTO SensorTable VALUES (null, \"Humidity\")")
94 sendSQL("INSERT INTO SensorTable VALUES (null, \"Pressure\")")
95
96
97def checkExperiment(sensorID,value):
98 '''
99 To check whether an experiment is running, SQL will be run.
100 If an experiment is running
101 construct the SQL to look for conditions
102 A condition to sensor check carried out
103 User will be alerted if sensor value exceeds condition otherwise terminating
104 If not, subroutine terminates
105 '''
106 #running in experiment table GOT sensor ID
107 sql = "SELECT ExperimentSensorTable.Condition FROM ExperimentSensorTable JOIN ExperimentTable ON ExperimentSensorTable.ExperimentID = ExperimentTable.ExperimentID WHERE ExperimentTable.Running = True AND ExperimentSensorTable.SensorID = {}".format(sensorID)
108 results = returnSQL(sql)
109 if len(results) > 0: #that means there is a running experiment with that sensor ID
110 for condition in results:
111 condition = condition.split("+")#separates the condition from the number
112 #condition[0] = symbol, condition[1] = number
113 condition[1] = int(condition[1])
114 if condition[0] == "=":
115 if value == condition[1]:
116 #set alarm off
117 pass
118 elif condition[0] == ">":
119 if value > condition[1]:
120 #set alarm off
121 pass
122 elif condition[0] == ">=":
123 if value >= condition[1]:
124 #set alarm off
125 pass
126 elif condition[0] == "<":
127 if value < condition[1]:
128 #set alarm off
129 pass
130 elif condition[0] == "<=":
131 if value< condition[1]:
132 #set alarm off
133 pass
134
135def addExperiment():
136 valid = False
137 while not valid: #not valid is the same as valid == False
138 valid = True
139 Ename = input("Enter experiment name: ")
140 for letter in name:
141 if not letter.isalpha():
142 if not letter == "-":
143 try:
144 int(letter)
145 except ValueError:
146 valid = False
147 if not valid:
148 print("That is not valid: letters, numbers and hyphens only")
149
150 while not valid:
151 valid = True
152 try:
153 running = bool("Enter experiment running status - TRUE/FALSE: ")
154 except ValueError:
155 value = False
156 if not valid:
157 print("That is not valid: only 'True' and 'False' values accepted")
158
159 sql = "INSERT INTO ExperimentTable VALUES (null, \"{}\", {})".format(ExperimentName,Running)
160 sendSQL(sql)
161 #ask if the experiment is running?- DONE
162 #add experiment name and running to experiment table
163 #get sensor ID
164
165 sensorNum = int(input("Enter the number of sensors used: "))
166 for i in range(0,sensorNum):
167 #need to know if sensor exists or new sensor
168 #ask if it's a new sensor?
169 #if yes ask for name and add to sensor Table and store new sensor ID - add it to the database and then return all results from sensor in acending order and take the first sensor ID
170 sensorName = input("Enter sensor: ")
171 sql = "INSERT INTO SensorTable VALUES (null, \"{}\")".format(sensorName)
172 sendSQL(sql)
173 #if no output all sensors in sensor table and ask for ID
174 #then ask for condition
175
176
177 conditions = [">",">=","=","<","<="]
178 valid = False
179 while not valid:
180 print("Available conditions:")
181 for condition in conditions:
182 print(" - ",condition)
183 condition = input("Enter condition: ")
184 if condition in conditions:
185 valid = True
186 else:
187 print("That is not a valid condition")
188
189 valid = False
190 while not valid:
191 try:
192 numberCondition = int(input("Enter the numerical value for your condition: "))
193 valid = True
194 except ValueError:
195 valid = False
196 print("That is not a valid entry, please enter an integer")
197
198 sql = "INSERT INTO ExperimentSensorTable (ExperimentID, SensorID, Condition) VALUE ({},{},\"{}\")".format(experimentID, sensorID, condition+"+"+str(numberCondition))
199 sendSQL(sql)
200
201 #https://pythonhosted.org/sense-hat/api/#environmental-sensors
202 #https://magpi.raspberrypi.org/articles/breadboard-tutorial
203 #https://www.youtube.com/watch?v=RCIL35e47mk
204
205 #try this: https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/7