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 latest data 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)
Initializing the pin connections and DHT sensor, including the necessary libraries and the variables for connecting to the network (both ESP32 and Raspberry pi be connected to the same WI-FI):
#include <WiFi.h> #include <HTTPClient.h> #include <WebServer.h> #include "DHT.h" #define DHTTYPE DHT22 #define RELAY_PIN 16 const char* ssid = ""; // Enter SSID here const char* password = ""; //Enter Password here // DHT Sensor uint8_t DHTPin = 4; // Initialize DHT sensor. DHT dht(DHTPin, DHTTYPE); float temperature; float humidity;
void setup() { Serial.begin(115200); delay(100); pinMode(DHTPin, INPUT); dht.begin(); pinMode(RELAY_PIN, OUTPUT); //connect to your local wi-fi network WiFi.begin(ssid, password); //check wi-fi is connected to wi-fi network while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected..!"); Serial.print("Got IP: "); Serial.println(WiFi.localIP()); }
In the loop() function, we read the values and send them using the created http client as json:
HTTPClient http; // Prepare the data that will be send temperature = dht.readTemperature(); humidity = dht.readHumidity(); String data = "{\"humidity\":" + String(humidity) + ",\"temperature\":" + String(temperature) + "}"; // Send a POST request with JSON data http.begin("http://192.168.1.104:5000/insert_sensor_data"); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(data); // Check the response code if (httpResponseCode > 0) { String response = http.getString(); Serial.println(httpResponseCode); Serial.println(response); } else { Serial.print("Error sending request: "); Serial.println(httpResponseCode); }
The condition for opening the valve is defined below:
//condition for opening the valve if ((int(temperature) > 35) && (int(humidity) <60)) { digitalWrite(RELAY_PIN, HIGH); // open valve 20 seconds delay(20000); digitalWrite(RELAY_PIN, LOW); // close valve }