-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
473 lines (385 loc) · 14.4 KB
/
app.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import os
import base64
from flask import Flask, render_template, request, jsonify
import random
import datetime
import requests
import serial
import sqlite3
import threading
import csv
import io
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
from dotenv import load_dotenv
app = Flask(__name__)
# Load environment variables from .env file
load_dotenv()
# OpenWeatherMap API configuration
OPENWEATHERMAP_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
CITY = "Kanpur,IN"
# Database files
MOISTURE_DB = "moisture_data.db"
PUMP_DB = "pump_data.db"
# Serial port configuration
SERIAL_PORT = "COM6"
BAUD_RATE = 9600
# Global variable to track Arduino connection status
ard_status = "Connected"
# Global variable to store the plant name
identified_plant_name = None
PLANTID_API_KEY = os.getenv("PLANTID_API_KEY")
# Gemini API configuration
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)
# Configure the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{
"category": HarmCategory.HARM_CATEGORY_HARASSMENT,
"threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
"category": HarmCategory.HARM_CATEGORY_HATE_SPEECH,
"threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
"category": HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
"threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
"category": HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
"threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
]
model = genai.GenerativeModel(
model_name="gemini-1.0-pro",
generation_config=generation_config,
safety_settings=safety_settings,
)
def init_databases():
conn_moisture = sqlite3.connect(MOISTURE_DB)
c_moisture = conn_moisture.cursor()
c_moisture.execute(
"""CREATE TABLE IF NOT EXISTS moisture_data
(timestamp TEXT, moisture_level INTEGER)"""
)
conn_moisture.commit()
conn_moisture.close()
conn_pump = sqlite3.connect(PUMP_DB)
c_pump = conn_pump.cursor()
c_pump.execute(
"""CREATE TABLE IF NOT EXISTS pump_data
(timestamp TEXT, water_used REAL, temperature REAL, humidity REAL)"""
)
conn_pump.commit()
conn_pump.close()
def get_weather_data():
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": CITY,
"appid": OPENWEATHERMAP_API_KEY,
"units": "metric", # For Celsius
}
try:
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
return {
"temperature": round(data["main"]["temp"], 1),
"humidity": round(data["main"]["humidity"], 1),
}
except:
return {
"temperature": "N/A",
"humidity": "N/A",
}
def generate_data():
# Connect to the moisture database to get the last soil moisture level
conn_moisture = sqlite3.connect(MOISTURE_DB)
c_moisture = conn_moisture.cursor()
c_moisture.execute(
"SELECT moisture_level FROM moisture_data ORDER BY timestamp DESC LIMIT 1"
)
result = c_moisture.fetchone()
soil_moisture_level = (
result[0] if result else random.randint(0, 100)
) # Fallback to random if no data
conn_moisture.close()
# Connect to the pump database to get the sum of water used and the last watering event timestamp
conn_pump = sqlite3.connect(PUMP_DB)
c_pump = conn_pump.cursor()
# Sum of water used
c_pump.execute("SELECT SUM(water_used) FROM pump_data")
result = c_pump.fetchone()
water_usage = (
round(result[0], 2)
if result[0] is not None
else round(random.uniform(0.5, 2.0), 2)
) # Fallback to random if no data
# Last watering event timestamp
c_pump.execute("SELECT timestamp FROM pump_data ORDER BY timestamp DESC LIMIT 1")
result = c_pump.fetchone()
last_watering_event = (
result[0]
if result
else (
datetime.datetime.now() - datetime.timedelta(minutes=random.randint(5, 120))
).strftime("%Y-%m-%d %H:%M:%S")
)
conn_pump.close()
# Get weather data
weather_data = get_weather_data()
if weather_data:
temperature = weather_data["temperature"]
humidity = weather_data["humidity"]
else:
temperature = round(random.uniform(15.0, 30.0), 1)
humidity = round(random.uniform(40.0, 80.0), 1)
return {
"soil_moisture_level": soil_moisture_level,
"ard_status": ard_status,
"water_usage": water_usage,
"last_watering_event": last_watering_event,
"temperature": temperature,
"humidity": humidity,
"CITY": CITY,
}
def fetch_moisture_data():
conn = sqlite3.connect(MOISTURE_DB)
c = conn.cursor()
now = datetime.datetime.now()
twenty_four_hours_ago = now - datetime.timedelta(hours=24)
c.execute(
"""SELECT timestamp, moisture_level
FROM moisture_data
WHERE timestamp >= ?
ORDER BY timestamp ASC""",
(twenty_four_hours_ago.strftime("%Y-%m-%d %H:%M:%S"),),
)
data = c.fetchall()
conn.close()
return data
def fetch_pump_data():
conn = sqlite3.connect(PUMP_DB)
c = conn.cursor()
now = datetime.datetime.now()
twenty_four_hours_ago = now - datetime.timedelta(hours=24)
c.execute(
"""SELECT timestamp, water_used, temperature, humidity
FROM pump_data
WHERE timestamp >= ?
ORDER BY timestamp ASC""",
(twenty_four_hours_ago.strftime("%Y-%m-%d %H:%M:%S"),),
)
data = c.fetchall()
conn.close()
return data
def generate_csv_data():
moisture_data = fetch_moisture_data()
pump_data = fetch_pump_data()
csv_buffer = io.StringIO()
csv_writer = csv.writer(csv_buffer)
csv_writer.writerow(
["Timestamp", "Moisture Level", "Water Used", "Temperature", "Humidity"]
)
# Create dictionaries for quick lookup
moisture_dict = {row[0]: row[1] for row in moisture_data}
pump_dict = {row[0]: row[1:] for row in pump_data}
# Combine all timestamps
all_timestamps = sorted(set(moisture_dict.keys()) | set(pump_dict.keys()))
# Write rows
for timestamp in all_timestamps:
moisture_level = moisture_dict.get(timestamp, "")
pump_data = pump_dict.get(timestamp, ["", "", ""])
csv_writer.writerow([timestamp, moisture_level] + list(pump_data))
return csv_buffer.getvalue()
def get_ai_insights(csv_data):
prompt = f"""
You are an AI assistant for a plant watering system. The task is to analyze the following CSV data:
{csv_data}
**Instructions:**
- **If CSV data is provided**, please address the following aspects:
1. Overall moisture level trends
2. Watering frequency and amount
3. Temperature and humidity effects on watering
4. Any unusual patterns or anomalies
5. Recommendations for improving plant care based on the data
- **If CSV data is empty or lacks sufficient information**, do the following instead:
- Generate general information and tips about plant care, excluding watering tips and related topics.
- Use Markdown to format your response for better readability.
**Important Note:** Ensure that you follow the correct instruction based on the availability and quality of the CSV data.
"""
response = model.generate_content(prompt)
return response.text
def save_moisture_data(moisture_level):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
moisture_level = round(moisture_level)
conn = sqlite3.connect(MOISTURE_DB)
c = conn.cursor()
c.execute(
"INSERT INTO moisture_data (timestamp, moisture_level) VALUES (?, ?)",
(timestamp, moisture_level),
)
conn.commit()
conn.close()
def save_pump_data(water_used):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
weather_data = get_weather_data()
if weather_data:
conn = sqlite3.connect(PUMP_DB)
c = conn.cursor()
c.execute(
"INSERT INTO pump_data (timestamp, water_used, temperature, humidity) VALUES (?, ?, ?, ?)",
(
timestamp,
round(water_used, 2),
weather_data["temperature"],
weather_data["humidity"],
),
)
conn.commit()
conn.close()
def serial_communication():
global ard_status
try:
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
print("Arduino connected.")
while True:
if ser.in_waiting > 0:
line = ser.readline().decode("utf-8").strip()
# Process moisture level data
if "Moisture Level:" in line:
moisture_level = int(line.split(":")[1].strip())
moisture_percent = (moisture_level / 800.0) * 100
save_moisture_data(moisture_percent)
# Process water usage data
elif "Water Used:" in line:
water_used = float(line.split(":")[1].strip())
save_pump_data(water_used)
except serial.SerialException as e:
if (
"ClearCommError" in str(e)
or "FileNotFoundError" in str(e)
or "No such file or directory" in str(e)
):
ard_status = "Disconnected"
print("Arduino disconnected:", e)
except Exception as e:
print("Error:", e)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/dashboard")
def dashboard():
data = generate_data()
return render_template("dashboard.html", data=data)
@app.route("/graphs")
def graphs():
moisture_data = fetch_moisture_data()
pump_data = fetch_pump_data()
# Prepare data for the charts
moisture_time = [item[0] for item in moisture_data]
moisture_level = [item[1] for item in moisture_data]
pump_time = [item[0] for item in pump_data]
water_used = [item[1] for item in pump_data]
temperature = [item[2] for item in pump_data]
humidity = [item[3] for item in pump_data]
return render_template(
"graphs.html",
moisture_time=moisture_time,
moisture_level=moisture_level,
pump_time=pump_time,
water_used=water_used,
temperature=temperature,
humidity=humidity,
)
@app.route("/ai")
def ai():
csv_data = generate_csv_data()
insights = get_ai_insights(csv_data)
return render_template("ai.html", insights=insights)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json["message"]
# Check if the message is related to plants or the watering system
plant_related = (
model.generate_content(
f"Please determine if the following message is related to plants or a plant watering system. Answer with 'Yes' if it is related, or 'No' if it is not. Message: {user_message}"
)
.text.strip()
.lower()
)
if plant_related == "yes":
context = f"""
You are an AI assistant for a plant watering system. Your role is to provide information and answer questions
about plants, plant care, watering systems, and related topics. Use the data from the plant watering system
if relevant to the question.
**Important Instructions:**
- **Always include the plant name**: {identified_plant_name} in your responses to provide more accurate and tailored advice.
- **Exception**: If the plant name is "None", do not mention or include the plant name in your response.
Ensure that you follow these instructions carefully to provide the most relevant information.
"""
prompt = f"{context}\n\nHuman: {user_message}\nAI:"
response = model.generate_content(prompt)
return jsonify({"response": response.text})
else:
return jsonify(
{
"response": "I'm sorry, but I can only answer questions related to plants and the watering system."
}
)
@app.route("/identify", methods=["POST"])
def identify():
global identified_plant_name # Declare the global variable
# Check if plant name is provided
plant_name = request.form.get("plant_name")
if plant_name:
identified_plant_name = plant_name
return jsonify({"plant_name": plant_name})
# Otherwise, handle image upload
if "image" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
image = request.files["image"]
image_data = base64.b64encode(image.read()).decode("utf-8")
response = requests.post(
"https://plant.id/api/v3/identification",
headers={"Api-Key": PLANTID_API_KEY, "Content-Type": "application/json"},
json={
"images": ["data:image/jpg;base64," + image_data],
"latitude": 26.449,
"longitude": 80.331,
},
)
if response.status_code != 201:
return (
jsonify({"error": f"Error contacting Plant.ID API {response.text}"}),
response.status_code,
)
result = response.json()
try:
if (
result.get("result")
and result["result"].get("classification")
and result["result"]["classification"].get("suggestions")
):
plant_name = result["result"]["classification"]["suggestions"][0]["name"]
identified_plant_name = plant_name # Store in global variable
return jsonify({"plant_name": plant_name})
else:
return jsonify({"error": "Plant could not be identified"}), 400
except KeyError as e:
return jsonify({"error": f"Error processing API response: {str(e)}"}), 500
if __name__ == "__main__":
# Start the serial communication thread
serial_thread = threading.Thread(target=serial_communication)
serial_thread.start()
app.run(host="0.0.0.0", debug=True)