TechGuru
Published

Home Automation - DIY School Project

The objective of this project is to develop a home automation system using NodeMCU board with Internet being remotely controlled by mobile.

IntermediateShowcase (no instructions)4 hours2,848
Home Automation - DIY School Project

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
We got it from: https://roboindia.com/store/nodemcu-motor-driver-shield-with-nodemcu?search=NodeMCU%20Motor%20Driver%20Shield%20with%20NodeMCU%09
×1
LED (generic)
LED (generic)
×1
DC motor (generic)
×1

Story

Read more

Schematics

How to connect

Code

NodeMCU_ESPCompatible code

Arduino
Download dependencies libraries from: https://github.com/esp8266/Arduino
//This example will set up a static IP - in this case 192.168.1.99
//More information - mail us : projectsforall777@gmail.com
//Website: www.labsforall.com
 
#include <ESP8266WiFi.h>

//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "12345678";
 
int gas = D5;
int light = D6;

const int ANALOG_PIN = A0; // The only analog pin on the Thing

//for nodemcu
// Pump
const int IN1 = D0;
const int IN2 = D1;

// DC Fan
const int IN3 = D2;
const int IN4 = D3;

WiFiServer server(80);

IPAddress apIP(192, 168, 1, 99); // where xx is the desired IP Address

void setup() {
  Serial.begin(9600);
  delay(10);
 
  Serial.print(F("Setting static ip to : "));
  Serial.println(apIP);

  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00
  WiFi.softAP("sunshinelabz", WiFiAPPSK);
  
  // Connect to WiFi network
  Serial.println();
 
//  // Start the server
  server.begin();
  Serial.println("Server started");

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  pinMode(gas, OUTPUT);
  pinMode(light, OUTPUT);

  Fan_Off();
  Pump_Off();
  Gas_Off();
  Light_Off();
}

void Fan_On()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
}

void Fan_Off()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
}

void Pump_On()
{
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void Pump_Off()
{
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void Gas_On()
{
  digitalWrite(gas, HIGH);
}

void Gas_Off()
{
  digitalWrite(gas, LOW);
}

void Light_On()
{
  digitalWrite(light, HIGH);
}

void Light_Off()
{
  digitalWrite(light, LOW);
}
 
void loop() {
 
 // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  // Match the request
  int val = -1; // We'll use 'val' to keep track of both the
                // request type (read/set) and value if set.

  Serial.print("Full request: ");
  Serial.println(req);

  Serial.print("Index of /status1=1 is: ");
  Serial.println(req.indexOf("/status1=1"));

  
  Serial.print("Index of /status1=0 is: ");
  Serial.println(req.indexOf("/status1=0"));


  Serial.print("Index of ?cmd=0 is: ");
  Serial.println(req.indexOf("?cmd=0"));

  if (req.indexOf("/status1=1") == 4)
  {
    Serial.println("Fan On");
  
    Fan_On();
  }
  
  else if (req.indexOf("/status1=0") == 4)
  {
    Serial.println("Fan Off");
    
    Fan_Off();
  }

  else if (req.indexOf("/status2=1") == 4)
  {
    Serial.println("Pump On");
    
    Pump_On();
  }

  else if (req.indexOf("/status2=0") == 4)
  {
    Serial.println("Pump Off");
    
    Pump_Off();
  }

  else if (req.indexOf("/status3=1") == 4)
  {
    Serial.println("gas On");
    
    Gas_On();
  }

  else if (req.indexOf("/status3=0") == 4)
  {
    Serial.println("gas off");
    
    Gas_Off();
  }

  else if (req.indexOf("/status4=1") == 4)
  {
    Serial.println("light On");
    
    Light_On();
  }

  else if (req.indexOf("/status4=0") == 4)
  {
    Serial.println("light off");
    
    Light_Off();
  }

  client.flush();

  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"on":"off";
  }
  else if (val == -2)
  { // If we're reading pins, print out those values:
    s += "Analog Pin = ";
    s += String(analogRead(ANALOG_PIN));
    s += "<br>"; // Go to the next line.
    //s += "Digital Pin 12 = ";
    //s += String(digitalRead(DIGITAL_PIN));
  }
  else
  {
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
  }
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
 
}

Credits

TechGuru

TechGuru

8 projects • 13 followers
We provide Science,School and Technology projects(Working Real Time Models) for your requirements. YOU HAVE A BUDGET, WE HAVE A PROJECT !
Thanks to roboindia.com.

Comments