Professional Documents
Culture Documents
Study For Presentation
Study For Presentation
Study For Presentation
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
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 {
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 {
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
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.
xml
Copy
<!DOCTYPE html>
xml
Copy
<html>
<head>
<style>
</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
<table id="data-table">
<tr>
<th>Timestamp</th>
<th>Temperature</th>
<th>Humidity</th>
<th>Soil Moisture</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sensor_db";
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
if (mysqli_num_rows($result) > 0) {
} else {
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
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
These lines define the pin number and type of the DHT22 sensor and create a DHT object called "dht22".
Copy
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
This line defines the URL to which the data will be sent using an HTTP POST request.
Copy
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();
"&humidity=" + String(humidity) +
"&moisture=" + String(soilMoisture);
HTTPClient http;
http.begin(URL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
} else {
} else {
http.end();
Serial.println("--------------------------------------------");
if (isSoilDry) {
} else if (isSoilWet) {
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)) {
temperature = 0;
humidity = 0;
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() {
}
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");
delay(1000);
Serial.print(".");
Serial.println("");
Serial.println("WiFi connected");
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.