Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Techatronic
Published

WiFi Based Home Automation | NodeMCU esp8266

This is a WiFi-based home automation project in which we are controlling the appliances using nodemcu.

IntermediateFull instructions provided2 hours3,289
WiFi Based Home Automation | NodeMCU esp8266

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
4-CHANNEL RELAY CONTROLLER FOR I2C
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
×1
LED Light Bulb, Frosted GLS
LED Light Bulb, Frosted GLS
×3
Axial Fan, 12 VDC
Axial Fan, 12 VDC
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
9V battery (generic)
9V battery (generic)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Plain text
 //TECHATRONIC.COM  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #include <ESP8266WiFi.h>  
 const char* ssid = "DESKTOP"; // SSID i.e. Service Set Identifier is the name of your WIFI  
 const char* password = "asdfghjkl"; // Your Wifi password, in case you have open network comment the whole statement.  
 int R1=D0; // GPIO13 or for NodeMCU you can directly write D7   
 int R2=D1;  
 int R3=D2;  
 int R4=D3;  
 WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.  
 void setup() {  
  Serial.begin(115200);  
  delay(10);  
  pinMode(R1, OUTPUT);  
  pinMode(R2, OUTPUT);  
  pinMode(R3, OUTPUT);  
  pinMode(R4, OUTPUT);  
  digitalWrite(R1,HIGH);  
  digitalWrite(R2,HIGH);  
  digitalWrite(R3,HIGH);  
  digitalWrite(R4,HIGH);  
  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED) {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Start the server  
  server.begin();  
  Serial.println("Server started");  
  // Print the IP address  
  Serial.print("Use this URL to connect: ");  
  Serial.print("http://");  
  Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor  
  Serial.println("/");  
 }  
 void loop() {  
  // Check if a client has connected  
  WiFiClient client = server.available();  
  if (!client) {  
   return;  
  }  
  // Wait until the client sends some data  
  Serial.println("new client");  
  while(!client.available()){  
   delay(1);  
  }  
  // Read the first line of the request  
  String request = client.readStringUntil('\r');  
  Serial.println(request);  
  client.flush();  
  // Match the request  
  if (request.indexOf("/OFF1") != -1) {  
   digitalWrite(R1,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 1 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON1") != -1) {  
   digitalWrite(R1, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 1 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF2") != -1) {  
   digitalWrite(R2,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 2 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON2") != -1) {  
   digitalWrite(R2, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 2 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF3") != -1) {  
   digitalWrite(R3,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 3 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON3") != -1) {  
   digitalWrite(R3, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 3 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF4") != -1) {  
   digitalWrite(R4,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 4 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON4") != -1) {  
   digitalWrite(R4, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 4 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
 }  

Code snippet #2

Plain text
 //TECHATRONIC.COM  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #include <ESP8266WiFi.h>  
 const char* ssid = "DESKTOP"; // SSID i.e. Service Set Identifier is the name of your WIFI  
 const char* password = "asdfghjkl"; // Your Wifi password, in case you have open network comment the whole statement.  
 int R1=D0; // GPIO13 or for NodeMCU you can directly write D7   
 int R2=D1;  
 int R3=D2;  
 int R4=D3;  
 WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.  
 void setup() {  
  Serial.begin(115200);  
  delay(10);  
  pinMode(R1, OUTPUT);  
  pinMode(R2, OUTPUT);  
  pinMode(R3, OUTPUT);  
  pinMode(R4, OUTPUT);  
  digitalWrite(R1,HIGH);  
  digitalWrite(R2,HIGH);  
  digitalWrite(R3,HIGH);  
  digitalWrite(R4,HIGH);  
  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED) {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Start the server  
  server.begin();  
  Serial.println("Server started");  
  // Print the IP address  
  Serial.print("Use this URL to connect: ");  
  Serial.print("http://");  
  Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor  
  Serial.println("/");  
 }  
 void loop() {  
  // Check if a client has connected  
  WiFiClient client = server.available();  
  if (!client) {  
   return;  
  }  
  // Wait until the client sends some data  
  Serial.println("new client");  
  while(!client.available()){  
   delay(1);  
  }  
  // Read the first line of the request  
  String request = client.readStringUntil('\r');  
  Serial.println(request);  
  client.flush();  
  // Match the request  
  if (request.indexOf("/OFF1") != -1) {  
   digitalWrite(R1,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 1 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON1") != -1) {  
   digitalWrite(R1, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 1 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF2") != -1) {  
   digitalWrite(R2,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 2 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON2") != -1) {  
   digitalWrite(R2, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 2 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF3") != -1) {  
   digitalWrite(R3,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 3 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON3") != -1) {  
   digitalWrite(R3, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 3 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/OFF4") != -1) {  
   digitalWrite(R4,LOW);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 4 is ON");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
  if (request.indexOf("/ON4") != -1) {  
   digitalWrite(R4, HIGH);  
    client.println("HTTP/1.1 200 OK");  
    client.println("Content-Type: text/html");  
    client.println("");  
    client.println("<!DOCTYPE HTML>");  
    client.println("<html>");  
    client.println("Relay 4 is OFF");  
    client.println("</html>");  
    client.stop();  
    delay(1);  
  }  
 }  

Github

https://github.com/ekstrand/ESP8266wifi

Credits

Techatronic

Techatronic

72 projects • 127 followers
Electronic engineer

Comments