Study For Presentation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Certainly, let's break down the code step by step:

Copy

if(httpCode > 0) {

This line checks whether the HTTP response code is greater than zero, which indicates that the server
responded to the GET request.

Copy

if(httpCode == HTTP_CODE_OK) {

This line checks whether the HTTP response code is equal to HTTP_CODE_OK, which is a constant that
represents the HTTP status code for a successful response (typically 200). If the response
code is HTTP_CODE_OK, the rest of the code in this block will execute.

Copy

String payload = http.getString();

Serial.println(payload);

This block of code retrieves the response payload using the http.getString() method, which returns the
response body as a string. The payload is then printed to the serial monitor using Serial.println(). This is
useful for debugging and verifying that the data was received correctly.

Copy

} else {

Serial.printf("[HTTP] GET... code: %d\n", httpCode);

If the HTTP response code is not HTTP_CODE_OK, this block of code will execute. It prints an error
message to the serial monitor indicating the response code and that the GET request was unsuccessful.

Copy

} else {

Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());

If the HTTP response code is less than or equal to zero, this block of code will execute. It prints an error
message to the serial monitor indicating that the GET request failed, along with a description of the
error.
Copy

http.end(); // Close the HTTP connection

Finally, this line closes the HTTP connection using the http.end() method. This is important to free up
system resources and prevent memory leaks.

In summary, this code sends an HTTP GET request to a web server, retrieves the response payload if the
request was successful, and prints the payload or an error message to the serial monitor depending on
the response code. It then closes the HTTP connection to free up resources.

2. php and html

Certainly, here's a detailed breakdown of what each line in the HTML file does:

xml

Copy

<!DOCTYPE html>

This line declares the document type as HTML5.

xml

Copy

<html>

<head>

<meta http-equiv="refresh" content="5">

<title>Green House Sensor Data</title>

<style>

/* CSS styles for the page layout and table formatting */

</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

/* JavaScript code for updating the table with the latest sensor data */

</script>
</head>

<body>

These lines define the HTML document structure, including the head and body sections. The head
section contains a meta tag that refreshes the page every 5 seconds, a title tag that sets the page title to
"Green House Sensor Data", and a script tag that loads the jQuery library and defines a JavaScript
function for updating the data table.

php-template

Copy

<h1>Green House Sensor Data</h1>

<table id="data-table">

<!-- Table header row -->

<tr>

<th>Timestamp</th>

<th>Temperature</th>

<th>Humidity</th>

<th>Soil Moisture</th>

</tr>

<?php

// Connect to the sensor_db database on your XAMPP server

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "sensor_db";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check the connection

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

// Retrieve the latest sensor data from the sensor_data table

$sql = "SELECT * FROM dht22 ORDER BY datetime DESC LIMIT 5";

$result = mysqli_query($conn, $sql);

// Output the latest sensor data as an HTML table

if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_assoc($result)) {

echo "<tr><td>" . $row["datetime"] . "</td><td>" . $row["temperature"] .


"</td><td>" . $row["humidity"] . "</td><td>" . $row["soilMoisture"] . "</td></tr>";

} else {

echo "<tr><td colspan='4'>No data available</td></tr>";

// Close the database connection

mysqli_close($conn);

?>

</table>

These lines define the content of the HTML document, including an h1 heading that displays "Green
House Sensor Data" and a table with an id of "data-table" that displays the latest sensor data.

The PHP code in between the table tags connects to a MySQL database, retrieves the latest sensor data,
and outputs it as an HTML table. The code checks for a successful database connection, retrieves the
latest sensor data from the "dht22" table, and outputs each row of data as an HTML table row with four
columns for the timestamp, temperature, humidity, and soil moisture. If no data is available, the code
outputs a single table row with a "No data available" message.
Overall, this HTML file defines the layout and content of a web page that displays the latest sensor data
from a MySQL database in a formatted HTML table.

3. arduino ide

Certainly, here's a detailed breakdown of what each line in the Arduino sketch does:

Copy

#include <WiFi.h>

#include <HTTPClient.h>

#include <DHT.h>

These lines include the required libraries for the sketch: the WiFi library for connecting to Wi-Fi,
the HTTPClient library for sending HTTP requests, and the DHT library for reading data from
the DHT22 sensor.

Copy

#define DHTPIN 4

#define DHTTYPE DHT22

DHT dht22 (DHTPIN, DHTTYPE);

These lines define the pin number and type of the DHT22 sensor and create a DHT object called "dht22".

Copy

const int SOIL_MOISTURE_PIN = 36;

This line defines the analog input pin for the soil moisture sensor.

Copy

#define SOIL_DRY_THRESHOLD 40

This line defines the threshold value for the soil moisture sensor for the "soil is dry" condition using the
#define preprocessor directive.

Copy

#define SOIL_WET_THRESHOLD 95
This line defines the threshold value for the soil moisture sensor for the "soil is wet" condition using the
#define preprocessor directive.

Copy

String URL = "http://192.168.100.163/dht22_project/test_data.php";

This line defines the URL to which the data will be sent using an HTTP POST request.

Copy

const char* ssid = "Purge belle";

const char* password ="amdifferent17";

These lines define the Wi-Fi network name (SSID) and password for connecting to Wi-Fi.

Copy

int temperature = 0;

int humidity = 0;

int soilMoisture = 0;

These lines define the variables for storing the sensor data.

Copy

void setup() {

Serial.begin(115200);

dht22.begin();

connectWiFi();

pinMode(SOIL_MOISTURE_PIN, INPUT);

These lines define the setup function, which runs once when the Arduino is powered on or reset. The
function initializes the Serial communication, starts the DHT22 sensor, connects to Wi-Fi, and sets the
pin mode for the soil moisture sensor.

reasonml
Copy

void loop() {

if(WiFi.status() != WL_CONNECTED) {

connectWiFi();

Load_DHT22_Data();

Load_Soil_Moisture_Data();

String postData = "temperature=" + String(temperature) +

"&humidity=" + String(humidity) +

"&moisture=" + String(soilMoisture);

HTTPClient http;

http.begin(URL);

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

int httpCode = http.POST(postData);

String payload = http.getString();

bool isSoilDry = (soilMoisture < SOIL_DRY_THRESHOLD);

bool isSoilWet = (soilMoisture >= SOIL_WET_THRESHOLD);

if(httpCode > 0) {

if(httpCode == HTTP_CODE_OK) {

String payload = http.getString();


Serial.println(payload);

} else {

Serial.printf("[HTTP] GET... code: %d\n", httpCode);

} else {

Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());

http.end();

Serial.print("URL: "); Serial.print(URL);

Serial.print(" Data: "); Serial.print(postData);

Serial.print(" httpCode:"); Serial.println(httpCode);

Serial.print(" Payload: "); Serial.println(payload);

Serial.println("--------------------------------------------");

if (isSoilDry) {

Serial.println("Soil is too dry!");

} else if (isSoilWet) {

Serial.println("Soil is wet enough");

delay(5000);

These lines define the loop function, which runs continuously after the setup function. The function
checks if the Wi-Fi connection is established, loads data from the DHT22 and soil moisture sensors,
constructs the data to send to the server, sends the HTTP POST request, and checks the response code.
The function also determines if the soil is dry or wet based on the soil moisture reading and prints debug
information to the serial monitor. The function delays for 5 seconds before running again.

ini

Copy

void Load_DHT22_Data() {

temperature = dht22.readTemperature();

humidity = dht22.readHumidity();

if (isnan(temperature) || isnan(humidity)) {

Serial.println("Failed to read from DHT sensor!");

temperature = 0;

humidity = 0;

Serial.printf("Temperature: %d °C\n", temperature);

Serial.printf("Humidity: %d %%\n", humidity);

This function reads the temperature and humidity data from theDHT22 sensor using the DHT22 object
created earlier. It checks if the sensor readings are valid and sets the temperature and humidity
variables to zero if not. It then prints the temperature and humidity values to the serial monitor.

Copy

void Load_Soil_Moisture_Data() {

int moistureValue = analogRead(SOIL_MOISTURE_PIN);

soilMoisture = map(moistureValue, 0, 4095, 0, 100);

Serial.printf("Soil moisture: %d %%\n", soilMoisture);

}
This function reads the analog value from the soil moisture sensor using the analogRead() function and
maps it to a 0-100 range using the map() function. It then sets the soilMoisture variable to this mapped
value and prints it to the serial monitor.

reasonml

Copy

void connectWiFi() {

WiFi.mode(WIFI_OFF);

delay(1000);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);

Serial.println("Connecting to WiFi");

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.print(".");

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

This function connects to Wi-Fi using the network name (SSID) and password defined earlier. It sets the
Wi-Fi mode to station mode and begins the connection process. It waits for the connection to be
established by continuously printing dots to the serial monitor until the connection is established. It
then prints the local IP address to the serial monitor.

4.

You might also like