Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views6 pages

Code HomeAutomation Esp8266

The document contains an Arduino sketch for an ESP8266-based IoT home automation system that controls a water pump, bedroom light, buzzer, and kitchen fan via a web interface. It sets up a WiFi connection, initializes device pins, and handles HTTP requests to turn devices on or off. The code also includes styling for the web interface and functions to manage device states based on user input.

Uploaded by

Varad Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Code HomeAutomation Esp8266

The document contains an Arduino sketch for an ESP8266-based IoT home automation system that controls a water pump, bedroom light, buzzer, and kitchen fan via a web interface. It sets up a WiFi connection, initializes device pins, and handles HTTP requests to turn devices on or off. The code also includes styling for the web interface and functions to manage device states based on user input.

Uploaded by

Varad Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code:-

#include <ESP8266WiFi.h>
#include <ThingESP.h>

// ThingESP setup
ThingESP8266 thing("Vaibhavsul", "HOMEAUTOMATION", "VAIBHAVSUL");

// Device pins
const int WATERPUMP = D1; // Water Pump
const int BEDROOMLIGHT = D2; // Bedroom Light
const int KITCHENFAN = D3; // Buzzer
const int BUZZER = D4; // Kitchen Fan

// HTTP server setup


WiFiServer server(80);

// HTTP request handling


String header;
String outputWaterpumpState = "off";
String outputBedroomLightState = "off";
String outputBuzzerState = "off";
String outputKitchenFanState = "off";

// Timing
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;

void setup() {
Serial.begin(115200);

// Initialize device pins


pinMode(WATERPUMP, OUTPUT);
pinMode(BEDROOMLIGHT, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(KITCHENFAN, OUTPUT);

// Set initial state of devices


digitalWrite(WATERPUMP, HIGH);
digitalWrite(BEDROOMLIGHT, HIGH);
digitalWrite(BUZZER, HIGH);
digitalWrite(KITCHENFAN, HIGH);
// Connect to WiFi
WiFi.begin("IOT", "123456789");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.print("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());

// Start HTTP server


server.begin();

// Initialize ThingESP
thing.SetWiFi("IOT", "123456789");
thing.initDevice();
}

void loop() {
// Handle HTTP requests
WiFiClient client = server.available();

if (client) {
Serial.println("New Client.");
String currentLine = "";
currentTime = millis();
previousTime = currentTime;

while (client.connected() && currentTime - previousTime <=


timeoutTime) {
currentTime = millis();

if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;

if (c == '\n') {
if (currentLine.length() == 0) {
sendHeader(client);

// Handle HTTP device control


handleDevice(client, WATERPUMP, outputWaterpumpState, 1,
"Water Pump", "#FF5733");
handleDevice(client, BEDROOMLIGHT, outputBedroomLightState,
2, "Bedroom Light", "#45B39D");
handleDevice(client, BUZZER, outputBuzzerState, 3, "Buzzer",
"#5499C7");
handleDevice(client, KITCHENFAN, outputKitchenFanState, 4,
"Kitchen Fan", "#F4D03F");

sendFooter(client);
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}

header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}

// Handle ThingESP commands


thing.Handle();
}

void sendHeader(WiFiClient& client) {


client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width,
initial-scale=1\">");
client.println("<title>ESP8266 IoT Control</title>");
client.println("<style>");
client.println("body { font-family: Arial, Helvetica, sans-serif;
background-color: #f0f0f0; text-align: center; margin: 0; padding: 0;}");
client.println(".container { max-width: 600px; margin: auto; padding:
20px;}");
client.println(".device-box { background-color: #ffffff; padding: 20px;
margin-bottom: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,
0, 0, 0.1); transition: transform 0.3s;}");
client.println(".device-box:hover { transform: scale(1.05); }");
client.println(".device-title { font-size: 24px; color: #333; margin-
bottom: 10px; }");
client.println(".device-state { font-size: 18px; color: #666; margin-
bottom: 15px; }");
client.println(".button { display: inline-block; padding: 10px 20px;
font-size: 16px; cursor: pointer; border: none; border-radius: 5px;
margin-right: 10px; transition: background-color 0.3s, color 0.3s; text-
decoration: none;}");
client.println(".button:hover { filter: brightness(90%); }");
client.println(".buttonOn { background-color: #2ecc71; color:
#fff; }");
client.println(".buttonOff { background-color: #e74c3c; color:
#fff; }");
client.println("</style></head>");
client.println("<body>");
client.println("<div class=\"container\">");
}

void sendFooter(WiFiClient& client) {


client.println("</div>");
client.println("</body></html>");
}

void handleDevice(WiFiClient& client, int devicePin, String& outputState,


int deviceNumber, const char* deviceName, const char* color) {
client.println("<div class=\"device-box\" style=\"border-left: 6px
solid " + String(color) + "\">");
client.println("<div class=\"device-title\">" + String(deviceName) +
"</div>");
client.println("<div class=\"device-state\">State: " + outputState +
"</div>");

if (header.indexOf("/" + String(deviceNumber) + "/on") >= 0) {


if (outputState == "off") {
Serial.println(String(deviceName) + " is turned on");
outputState = "on";
digitalWrite(devicePin, LOW); // Turn the device on
}
} else if (header.indexOf("/" + String(deviceNumber) + "/off") >= 0) {
if (outputState == "on") {
Serial.println(String(deviceName) + " is turned off");
outputState = "off";
digitalWrite(devicePin, HIGH); // Turn the device off
}
}

client.println("<a class=\"button buttonOn\" href=\"/" +


String(deviceNumber) + "/on\">Turn On</a>");
client.println("<a class=\"button buttonOff\" href=\"/" +
String(deviceNumber) + "/off\">Turn Off</a>");

client.println("</div>");
}

// Handle ThingESP commands


String HandleResponse(String query) {
if (query == "waterpump off") {
digitalWrite(WATERPUMP, HIGH);
return "Done: Waterpump Turned OFF";
}
else if (query == "waterpump on") {
digitalWrite(WATERPUMP, LOW);
return "Done: Waterpump Turned ON";
}

if (query == "bedroomlight off") {


digitalWrite(BEDROOMLIGHT, HIGH);
return "Done: Bedroom Light Turned OFF";
}
else if (query == "bedroomlight on") {
digitalWrite(BEDROOMLIGHT, LOW);
return "Done: Bedroom Light Turned ON";
}

if (query == "buzzer off") {


digitalWrite(BUZZER, HIGH);
return "Done: Buzzer Turned OFF";
}
else if (query == "buzzer on") {
digitalWrite(BUZZER, LOW);
return "Done: Buzzer Turned ON";
}

if (query == "kitchenfan off") {


digitalWrite(KITCHENFAN, HIGH);
return "Done: Kitchen Fan Turned OFF";
}
else if (query == "kitchenfan on") {
digitalWrite(KITCHENFAN, LOW);
return "Done: Kitchen Fan Turned ON";
}

if (query == "all on") {


digitalWrite(WATERPUMP, LOW);
digitalWrite(BEDROOMLIGHT, LOW);
digitalWrite(BUZZER, LOW);
digitalWrite(KITCHENFAN, LOW);
return "Done: All Lights, Buzzer, Kitchen Fan, and Waterpump Turned
ON";
}
else if (query == "all off") {
digitalWrite(WATERPUMP, HIGH);
digitalWrite(BEDROOMLIGHT, HIGH);
digitalWrite(BUZZER, HIGH);
digitalWrite(KITCHENFAN, HIGH);
return "Done: All Lights, Buzzer, Kitchen Fan, and Waterpump Turned
OFF";
}

return "Your query was invalid..";


}

For any queries regarding the kit, get in touch with us through our forum: -
https://forum.robu.in/

You might also like