This is an old revision of the document!
Dinca Laura-Aida - SSA
Smart Garden project aims to be a plant environment monitoring system. Thanks to a temperature and humidity sensor, we have the data on the basis of which we can implement a logic for opening the irrigation system when needed. Being a proof of concept implementation, it has not yet been used in a real garden and the condition for opening the valve is chosen for testing purposes.
This part is for creating the database and for defining its elements; in this case, an id and two integers, temperature and humidity for the values read from the sensor.
import sqlite3 import os database_path = "./database.db" conn = sqlite3.connect(database_path) cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS sensorData ( id INTEGER PRIMARY KEY, temperature INTEGER, humidity INTEGER ) """) conn.commit() conn.close()
For displaying the desired values when making a GET request, we include the following:
<!DOCTYPE html> <head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> <title>ESP32 Weather Report</title> <style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;} body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} p {font-size: 24px;color: #444444;margin-bottom: 10px;} </style> </head> <body> <div id=\"webpage\"> <h1>ESP32 Weather Report</h1> <p>Temperature: {{temperature}}° C</p> <p>Humidity: {{humidity}} %</p> </div> </body> </html>
from flask import Flask, send_file, request, render_template import sqlite3 app = Flask(__name__) database_path = "./database.db" @app.route('/insert_sensor_data', methods=['POST']) def insert_sensor_data(): # Connect to the database conn = sqlite3.connect(database_path) cursor = conn.cursor() data = request.get_json(); humidity = data['humidity'] temperature = data['temperature'] # Save the data in the database cursor.execute("INSERT INTO sensorData (temperature, humidity) VALUES (?, ?)", (temperature, humidity)) # Commit the changes and close the database connection conn.commit() conn.close() return "Sensor data uploaded" @app.route('/latestData') def latest_data(): # Connect to the database conn = sqlite3.connect(database_path) cursor = conn.cursor() # Retrieve the image from the database based on the image_id cursor.execute("SELECT temperature, humidity FROM sensorData WHERE id = (SELECT MAX(id) from sensorData)") result = cursor.fetchone() # Save the data to a temporary file temperature = result[0] humidity = result[1] # Close the database connection conn.close() # Send the data file in the response return render_template('./index.html',temperature=temperature, humidity=humidity) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
=== For