#include <SPI.h>
#include <WiFi.h> // Use the appropriate Wi-Fi library for your module, like WiFi101 or WiFi.h
#include <DHT.h>
// Wi-Fi credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
// Define pin connections
#define LED_PIN 5 // Define the LED pin
#define DHT_PIN 4 // Define the DHT sensor pin
#define DHT_TYPE DHT11 // Define the DHT sensor type
DHT dht(DHT_PIN, DHT_TYPE);
// Web Server Setup
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Start the web server
server.begin();
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
String request = client.readStringUntil('\r'); // Read the HTTP request
client.flush();
// Process the request and respond accordingly
if (request.indexOf("/led/on") != -1) {
digitalWrite(LED_PIN, HIGH); // Turn LED on
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body><h1>LED is ON</h1><a href='/'>Back</a></body></html>");
}
else if (request.indexOf("/led/off") != -1) {
digitalWrite(LED_PIN, LOW); // Turn LED off
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body><h1>LED is OFF</h1><a href='/'>Back</a></body></html>");
}
else {
// Serve temperature and humidity data
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body><h1>Home Automation Control</h1>");
client.println("<p>Temperature: " + String(temperature) + " ℃</p>");
client.println("<p>Humidity: " + String(humidity) + " %</p>");
client.println("<a href='/led/on'>Turn LED On</a><br>");
client.println("<a href='/led/off'>Turn LED Off</a>");
client.println("</body></html>");
}
delay(1); // Give the web browser time to receive the data
client.stop(); // Close the connection
}
}