/*
* Secure Temperature and Humidity Monitoring for Agriculture
*
* Hardware:
* - ESP32 DevKit C V4
* - DHT22 sensor (Temperature & Humidity)
*
* Wiring:
* - DHT22 VCC -> ESP32 3.3V
* - DHT22 GND -> ESP32 GND
*
*
* Functionality:
* - Connects to a WiFi network using WPA2.
* - Reads temperature and humidity from the DHT22 sensor.
* - Securely transmits the sensor data via HTTPS to a remote server.
*
* Cybersecurity Features:
* - Uses TLS/SSL via WiFiClientSecure for encrypted data transmission.
*
*
* Note:
* - Update WiFi credentials and server details below.
*/
#include <WiFi.h>
#include "DHT.h"
// WiFi Credentials
const char* ssid = "WokwiSSID";
const char* password = "WokwiPassword";
// Server details (use HTTP instead of HTTPS)
const char* host = "example.com"; // Use a placeholder server or your own endpoint
const int httpPort = 80; // Standard HTTP port
// DHT Sensor Configuration
#define DHTPIN 4 // GPIO pin connected to the DHT22 sensor's data line
#define DHTTYPE DHT22 // DHT22 sensor type
DHT dht(DHTPIN, DHTTYPE);
// Create a WiFi client object (no need for WiFiClientSecure)
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\nSecure Temperature & Humidity Monitoring System");
dht.begin();
Serial.print("Connecting to WiFi network: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Optionally, you can also synchronize time using NTP if needed
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
}
void loop() {
// Wait 2 seconds between sensor readings
delay(2000);
// Read sensor values from the DHT22 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reading failed; if so, log an error message.
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Error: Failed to read from DHT sensor!");
return; // Skip this iteration if readings are invalid.
}
// Print the sensor readings to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// **********************************
// HTTP Data Transmission
// **********************************
// Try to connect to the remote server using HTTP
if (!client.connect(host, httpPort)) {
Serial.println("Error: Connection to server failed!");
return; // Skip transmission if connection fails
}
// Build the HTTP POST request
String url = "/api/sensor-data"; // Update this endpoint to your server's configuration
String postData = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
// Debug: Print the POST data
Serial.println("Preparing to send data:");
Serial.println(postData);
// Construct the HTTP request headers
client.println("POST " + url + " HTTP/1.1");
client.println("Host: " + String(host));
client.println("User-Agent: ESP32-Secure-Client/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println(); // End of headers
// Send the POST data (sensor readings) to the server
client.print(postData);
// Read and print the server's response (if any)
Serial.println("Server Response:");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
// End of HTTP response headers reached
break;
}
Serial.println(line);
}
// Optionally, read the remaining payload from the server:
String response = client.readString();
Serial.println(response);
// Close the connection
client.stop();
}