Differences

This shows you the differences between two versions of the page.

Link to this comparison view

iothings:proiecte:2022sric:smart-garden [2023/06/02 04:14]
laura_aida.dinca [Hardware]
iothings:proiecte:2022sric:smart-garden [2023/06/02 06:23] (current)
laura_aida.dinca [Software]
Line 12: Line 12:
 ===== Hardware ===== ===== Hardware =====
  
-Components:+=== Components: ​===
  
-* NodeMCU 32S +  ​* NodeMCU 32S 
-  +  * Raspberry pi 4 
-* Raspberry pi 4+  * DHT22 - temperature and humidity sensor  
 +  * Solenoid valve - 12V, normally closed 
 +  * H-Bridge - model L298N, 12V 
 +  * Resistor 10kOhm 
 +  * Power supply adapter - 12V 
 +  * Breadboard 
 +  * Connection wires 
 +   
 +=== Electric circuit === 
 +   
 +{{:​iothings:​proiecte:​2022sric:​schema.jpeg?​700|}}
  
-* DHT22 - temperature and humidity sensor+To ensure the control of the motor (the solenoid valve), we need a driver (H-bridge) with the role of providing the voltage and current necessary for good operation. Thus, a driver acts as a connection interface. The motors require a high level of current, while the rest of the circuit, a lower one, so it is recommended to isolate the power supply of the motors from the rest of the components. 
 + 
 +To maintain the data line HIGH and ensure good communication between the sensor and the ESP32, I added a 10K pull-up resistor between VCC and the data line. 
 +===== Software ===== 
 + 
 +=== Raspberry Pi server === 
 + 
 +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.  
 + 
 +<code python>​ 
 +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() 
 +</​code>​ 
 + 
 +For displaying the latest data when making a GET request, we include the following:​ 
 + 
 +<​code>​ 
 +<​!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>​
    
-* Solenoid valve - 12V, normally closed+  <​p>​Temperature:​ {{temperature}}&​deg C</​p>​ 
 +  <​p>​Humidity:​ {{humidity}} %</​p>​ 
 +  
 +  </​div>​ 
 +  </​body>​ 
 +  </​html>​ 
 +</​code>​
  
-* H-Bridge - model L298N, 12V+Result:
  
-* Resistor 10kOhm+{{:​iothings:​proiecte:​2022sric:​html_page.png?​700|}}
  
-* Power supply adapter - 12V+We will insert sensor data into the database when receiving a POST request. The second endpoint is for GET request and will display the latest data from the database.  
 +<code python>​ 
 +from flask import Flask, send_file, request, render_template 
 +import sqlite3
  
-* Breadboard 
  
-* Connection wires+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() 
 + 
 +    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]
   ​   ​
-  ==Electric circuit+    # 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) 
 +</​code>​ 
 + 
 +=== ESP32 environment === 
 +Initializing the pin connections and DHT sensor, including the necessary libraries and the variables for connecting to the network (both ESP32 and Raspberry pi must be connected to the same WI-FI): 
 +<code c> 
 +#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; 
 +</​code>​ 
 + 
 +<code c> 
 +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());​ 
 +        
 +
 +</​code>​ 
 + 
 +In the loop() function, we read the values and send them using the created http client as json: 
 +<code c> 
 +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);​ 
 +  } 
 +</​code>​ 
 + 
 +The condition for opening the valve is defined below: 
 +<code c> 
 +//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 
 +  } 
 +</​code>​ 
 + 
 + 
 +===== References: =====
  
 +  - [[https://​www.hackster.io/​mafzal/​temperature-monitoring-with-dht22-arduino-15b013|DHT22]]
 +  - [[https://​www.espressif.com/​sites/​default/​files/​documentation/​esp32_datasheet_en.pdf|ESP32 Datasheet]]
 +  - [[https://​randomnerdtutorials.com/​esp32-web-server-gauges/​|ESP32 Web Server]]
 +  - [[https://​www.sqlite.org/​docs.html|SQLlite]]
  
iothings/proiecte/2022sric/smart-garden.1685668440.txt.gz · Last modified: 2023/06/02 04:14 by laura_aida.dinca
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0