0% found this document useful (0 votes)
2 views

_Home automation using own app

This document is an Arduino sketch for controlling relays using an ESP8266 WiFi module. It connects to a specified WiFi network and listens for commands to turn relays on or off based on incoming data from a client application. The code includes functions for setting up the WiFi connection, handling client requests, and controlling the relay pins accordingly.

Uploaded by

Insaaf Army
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)
2 views

_Home automation using own app

This document is an Arduino sketch for controlling relays using an ESP8266 WiFi module. It connects to a specified WiFi network and listens for commands to turn relays on or off based on incoming data from a client application. The code includes functions for setting up the WiFi connection, handling client requests, and controlling the relay pins accordingly.

Uploaded by

Insaaf Army
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/ 3

#include <ESP8266WiFi.

h>
WiFiClient client;
WiFiServer server(80);

/* WIFI settings */
const char* ssid = "---------"; //WIFI SSID
const char* password = "----------"; //WIFI PASSWORD

/* data received from application */


String data ="";

/* define L298N or L293D motor control pins */


int Relay1 = 12; //D6
int Relay2 = 16; //D0
int Relay3 = 4; //D2

void setup()
{
/* initialize motor control pins as output */
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);

digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
digitalWrite(Relay3,LOW);

/* start server communication */


Serial.begin(115200);
connectWiFi();
server.begin();
}

void loop()
{
/* If the server available, run the "checkClient" function */
client = server.available();
if (!client) return;
data = checkClient ();
Serial.print(data);
/************************ Run function according to incoming data from application
*************************/
if (data == "Relay1ON")
{
digitalWrite(Relay1,HIGH);
}

else if (data == "Relay1OFF")


{
digitalWrite(Relay1,LOW);
}

else if (data == "Relay2ON")


{
digitalWrite(Relay2,HIGH);
}

else if (data == "Relay2OFF")


{
digitalWrite(Relay2,LOW);
}

else if (data == "Relay3ON")


{
digitalWrite(Relay3,HIGH);
}

else if (data == "Relay3OFF")


{
digitalWrite(Relay3,LOW);
}

void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
/********************************** RECEIVE DATA FROM the APP
******************************************/
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}

You might also like