This shows you the differences between two versions of the page.
iothings:proiecte:2022sric:smart-garden [2023/06/02 05:37] laura_aida.dinca [Software] |
iothings:proiecte:2022sric:smart-garden [2023/06/02 06:23] (current) laura_aida.dinca [Software] |
||
---|---|---|---|
Line 28: | Line 28: | ||
{{:iothings:proiecte:2022sric:schema.jpeg?700|}} | {{:iothings:proiecte:2022sric:schema.jpeg?700|}} | ||
+ | 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 ===== | ===== Software ===== | ||
Line 55: | Line 58: | ||
</code> | </code> | ||
- | For displaying the desired values when making a GET request, we include the following: | + | For displaying the latest data when making a GET request, we include the following: |
<code> | <code> | ||
Line 77: | Line 80: | ||
</html> | </html> | ||
</code> | </code> | ||
+ | |||
+ | Result: | ||
{{:iothings:proiecte:2022sric:html_page.png?700|}} | {{:iothings:proiecte:2022sric:html_page.png?700|}} | ||
+ | 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> | <code python> | ||
from flask import Flask, send_file, request, render_template | from flask import Flask, send_file, request, render_template | ||
Line 116: | Line 122: | ||
cursor = conn.cursor() | 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)") | cursor.execute("SELECT temperature, humidity FROM sensorData WHERE id = (SELECT MAX(id) from sensorData)") | ||
result = cursor.fetchone() | result = cursor.fetchone() | ||
Line 135: | Line 140: | ||
</code> | </code> | ||
- | === For | + | === 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]] | ||