NodeMCU ESP8266: Home Automation System With Sensors and MIT App Inventor 2

Introduction: NodeMCU ESP8266: Home Automation System With Sensors and MIT App Inventor 2

About: Just a student trying to help other students.

Hey guys, this is a project me and my friends make for our Final Year Project. We have gone through a lot of research from a lot of websites including this one to make this project worked as it should. So what I am trying to do is to give you guys my 2 cents of knowledge to you guys/students/girls/teacher/lecturer/robots or whatever you are.

So, in this "Instructable" I will share, how to create a useable and reliable Home Automation System or Smart Home for you to recreate at your own comfort. Also, I will share how to create your own simple web server for your NodeMCU ESP8266 and how to create your own MIT APP Inventor. Well, I will be putting links below.

In this project, we use 2 kinds of sensors; the HC-SR501 PIR Sensor and the LM35 Temperature Sensor. Both of these sensors have their own respectable usefulness. Exciting isn't it? Let's start making.

NOTE: THIS PROJECT IS CALLED ISAT, so if you see anything about the name "ISAT", it is a part of this project.

Step 1: Project Overview

This project is still under development, so I will be using LEDs to indicate the ON and OFF. Let's see how it works.

1. You can switch all the appliances on which is the Servo Motor, Fan, LEDs, PIR Sensor, and the LM35 using the web server built in the Arduino IDE or the mobile application created in MIT App Inventor 2.

2. The PIR Sensor HC-SR501 detects motion and sends signals to turn ON the LED. It will turn OFF as soon as motion is absent.

3. The Temperature Sensor LM35 will detect temperature and sends signals to the Fan. In this project below 30°C, the Fan will not move. 32°C - 34°C, the Fan speed will move but slow. 35°C - 37°C, the Fan speed is medium and 38°C - 40°C, the Fan speed is high. You can try to adjust the command for this part. Anything that suits you.

4. The Fan can also be turned on at various speeds - Slow, Medium, and Fast on the webserver and the mobile application.

So, I hope you guys understand what I am trying to tell you guys. But if not, do follow the instructions down below.

Step 2: Things Required

1. NodeMCU ESP8266 V2 Microcontroller // I recommend the V2 rather than the V3, but you can try if wanted.

2. Breadboard

3. 2 LEDs // Whatever colour you prefer.

4. SG90 Servo Motor //The blue one.

5. Fan or Motor //We use a small drone motor.

6. HC-SR501 PIR Sensor.

7. LM35 Temperature Sensor.

8. A Button

9. Bunch of cables - Female-To-Female, Male-to-Male, and Male-To-Female wires.

Step 3: Circuit Diagram

In this step, we will see how to connect the appliances to the NodeMCU ESP8266.

Well actually, I think the pictures above will already show how to connect, well picture speaks better than words can in this step.

But anyway, remember to try to avoid connecting anything on D3 or GPIO00, this will disturb the WiFi connection signal.

NOTE: Sorry for the messy wiring.


Step 4: NodeMCU ESP8266 and Web Server Coding.

In this part, I will give you the coding and the reasons why you need to put in the codes as it is and the libraries needed for this coding to work. Do forgive me if I missed adding some libraries.

First, download the Arduino IDE on your laptop.

https://www.arduino.cc/en/software

Next, open the Arduino IDE and go to Tools > Board > Boards Manager. On the search bar type "ESP8266" and install.

After install, go to Tools > Board > ESP8266 Board (Latest Version) > NodeMCU 1.0 (ESP-12E Module).

Step 5: Change the Preferences

Then, go to File > Preferences.

On the Additional Board Manager URLs bar, check if your URLs is:

http://arduino.esp8266.com/stable/package_esp8266c...

If yes, NOICE.

If not, type in the URL given up there or copy and paste.

Step 6: Download the Libraries

Now, onto the libraries.

Goto Tools > Manage Libraries.

On the search bar, search ArduinoJson by Benoit Blanchon and install the latest version.

Then, search Servo by Michael Mergolis and install the latest version.

Finally, search ThingSpeak by MathWorks. // This one will have to wait for its explanation but just install it.

Step 7: Codings

#include <ESP8266WiFi.h>
#include <Servo.h>
#include <ThingSpeak.h>

Servo servo;

const char* ssid = "Your SSID"; //Your WiFi Name/SSID
const char* password = "Your Password"; //Your WiFi Password

int val;
 
int LM35    = A0;        // ADC0---A0 of NodeMCU
int FAN     = 15;        // GPIO15---D8 of NodeMCU
int ledPin  = 12;        // GPIO12---D6 of NodeMCU
int ledPin2 = 14;        // GPIO14---D5 of NodeMCU
int PIR     = 13;        // GPIO13---D7 of NodeMCU

WiFiServer server(80);
WiFiClient client;

unsigned long myChannelNumber = ThinkSpeak Channel ID; //ThinkSpeak Channel Number
const char* myWriteAPIKey = "Write API Key"; //ThingSpeak WRITE API Key
 
void setup ()
{ 
  Serial.begin(115200);
  delay(10);

  servo.attach(2);                // GPIO02 of NodeMCU with PWM pin of servo motor

  pinMode (PIR,INPUT);            // declare PIR Sensor as input
  
  pinMode (LM35,INPUT);           // declare LM35 Sensor as input
  pinMode (FAN,OUTPUT);           // declare Fan as output
  
  pinMode (ledPin, OUTPUT);       // declare LED 1 as output
  digitalWrite(ledPin, LOW);

  pinMode(ledPin2, OUTPUT);       // declare LED 2 as output
  digitalWrite(ledPin2, LOW);

  // 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());
  Serial.println("/");

  ThingSpeak.begin(client);
}

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

  // Servo motor for door
  int value1 = 0;

  if (request.indexOf("/Req=0") != -1)
  {
    servo.write(2);       //Moving servo to 0 degree
    value1 = 0;
  }

  if (request.indexOf("/Req=90") != -1)
  {
    servo.write(104);     //Moving servo to 90 degree
    value1 = 90;
  }

  // LED 1, LED 2 and PIR sensor
  long state = digitalRead(PIR);
  int value2 = LOW;

  if (request.indexOf("/LED=ON") != -1)
  {
    digitalWrite(ledPin, HIGH);
    value2 = HIGH;
  }

  if (request.indexOf("/MOT=ON") != -1)
  {

    if (state == HIGH)
    {
      digitalWrite (ledPin, HIGH);
      Serial.println("Motion detected!");
      delay(500);
    }

    else
    {
      digitalWrite (ledPin, LOW);
      Serial.println("Motion absent!");
      delay(500);
    }
  }

  if (request.indexOf("/LED=OFF") != -1)
  {
    digitalWrite(ledPin, LOW);
    value2 = LOW;
  }

  int value3 = LOW;
  if (request.indexOf("/LED2=ON") != -1)
  {
    digitalWrite(ledPin2, HIGH);
    value3 = HIGH;
  }

  if (request.indexOf("/LED2=OFF") != -1)
  {
    digitalWrite(ledPin2, LOW);
    value3 = LOW;
  }
  // Set ledPin according to the request
  // digitalWrite(ledPin, value);


  // LM35 and Fan
  float temperature = analogRead(LM35);
  temperature = (temperature * (3.3 / 1023));
  float celsius = temperature * 100.0;
  Serial.print("Temperature = ");
  Serial.print(celsius);
  Serial.println("C  ");
  val = celsius;

  ThingSpeak.writeField(myChannelNumber, 1, val, myWriteAPIKey); //Update in ThingSpeak

  if (request.indexOf("/FAN=ON") != -1)
  {
    if (celsius < 24)
    {
      analogWrite(FAN, 0);
      Serial.println("Fan Off");
      delay(500);
    }

    else if (celsius < 34)
    {
      analogWrite(FAN, 410);
      Serial.println("Fan Speed : Slow");
      delay(500);
    }

    else if (celsius < 38)
    {
      analogWrite(FAN, 754);
      Serial.println("Fan Speed : Medium");
      delay(500);
    }

    else if (celsius < 42)
    {
      analogWrite(FAN, 1023);
      Serial.println("Fan Speed : Fast");
      delay(500);
    }
  }

  // Fan Speed
  if (request.indexOf("/FAN=OFF") != -1)
  {
    analogWrite(FAN, 0);
    Serial.println("Fan Off");
    delay(500);
  }

  if (request.indexOf("/FAN=ON1") != -1)
  {
    analogWrite(FAN, 410);
    Serial.println("Fan Speed : Slow");
    delay(500);
  }

  if (request.indexOf("/FAN=ON2") != -1)
  {
    analogWrite(FAN, 754);
    Serial.println("Fan Speed : Medium");
    delay(500);
  }

  if (request.indexOf("/FAN=ON3") != -1)
  {
    analogWrite(FAN, 1023);
    Serial.println("Fan Speed : Fast");
    delay(500);
  }

  if (request.indexOf("/TEMP") != -1)
  {
    Serial.print(val);
    delay(500);
  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Refresh: 5"); //Refresh every 5 seconds
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.println("<h1 align=center>ISAT Server Control</h1><br><br>");

  client.print("<p style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'>Temperature (*C)= ");
  client.println(celsius);

  client.println("<br><br>");

  client.print("Front Door");
  client.println("<br>");
  client.println("<a href=\"/Req=0\"\"><button>Close</button></a>");
  client.println("<a href=\"/Req=90\"\"><button>Open</button></a>");

  client.println("<br><br>");

  client.print("Living Room Light");
  client.println("<br>");
  client.println("<a href=\"/LED=OFF\"\"><button>Off </button></a>");
  client.println("<a href=\"/LED=ON\"\"><button>On </button></a>");
  client.println("<a href=\"/MOT=ON\"\"><button>MOn </button></a>");

  client.println("<br><br>");

  client.print("Bathroom Light");
  client.println("<br>");
  client.println("<a href=\"/LED2=OFF\"\"><button>Off </button></a>");
  client.println("<a href=\"/LED2=ON\"\"><button>On </button></a>");

  client.println("<br><br>");

  client.print("Living Room Fan");
  client.println("<br>");
  client.println("<a href=\"/FAN=OFF\"\"><button>Off </button></a>");
  client.println("<a href=\"/FAN=ON\"\"><button>On </button></a>");

  client.println("<br>");

  client.print("Living Room Fan Speed");
  client.println("<br>");
  client.println("<a href=\"/FAN=ON1\"\"><button>1 </button></a>");
  client.println("<a href=\"/FAN=ON2\"\"><button>2 </button></a>");
  client.println("<a href=\"/FAN=ON3\"\"><button>3 </button></a><br />");


  client.println("</html>");

  delay(100);
  Serial.println("Client disonnected");
  Serial.println("");

}

Step 8: Coding Download and Serial Monitor

In this code, it has already equipped you with the things you need to understand for yourself, and the webserver.

To go to the serial monitor, goto Tools > Serial Monitor. In this serial monitor, it will show you the IP Address of your NodeMCU ESP8266 and the Web Server URL.

NOTE: DO NOT RUN IT YET.

Step 9: Setting Up ThingSpeak

Now, the next step is for you to set your own ThingSpeak and then, put the Channel ID and Write API Keys in the code given to you.

Before that, what is ThingSpeak? ThingSpeak is one of the MathWorks tools. It stores the data you wanted and sends it to the MIT App Inventor with certain codings. It acts as a cloud to get data from the circuit and send it to the Mobile Application.

I will guide you step by step on this one.

Go to https://thingspeak.com/

If you do not have one, create an account.

Then after you have verified. Go to the main page and click the "Get Started" or simply go to the Channel Bar.

Step 10: Setting Up Channel

Next, go to click "New Channel".

After that, enter whatever name you wanted.

The description is optional.

Choose 1 field and name it Thermometer and Save the channel.

Step 11: Get Your Channel ID and API Keys

Write your channel ID given onto the code in Arduino IDE.

Then go to API Keys and copy the "Write API Key", then paste it on the codings.

Now, you are ready for the next step.

NOTE: You can test the circuit using the webserver URL given.

Step 12: Creating App Using MIT App Inventor

Use the link below:

https://appinventor.mit.edu/

Create your own account by clicking on Create App.

I tried to include the .aia files for you guys but Instructables does not allow it, so here goes.

Step 13: MIT App Inventor 2 File Download

Create your applications like this or with your own style, it would not affect the entirety of the codings.

Do not forget the barcode scanner, clocks, and webs.

Now, download the ISAT_3.rar and extract the ISAT_3.aia.

Step 14: How to Download Your MIT File to My Projects

Go to My Projects and import the aia file from your computer.

Step 15: Getting Your Read URL

Next, go back to your ThingSpeak Channel and copy the Read a Channel Feed at the API Keys.

Step 16: Putting Your Read URL to Block Code in MIT App Inventor 2

Then, paste it on the Clock4 Timer, Web2 text box. Without the GET.

Step 17: Download Apps on Your Phone

Now, you are done to run your application, circuit, and webserver to its fullest. But before that, you need to download MIT AI2 Companion and Barcode Scanner by ZXing Team on the PlayStore. Then you can go to the MIT App Inventor 2 website and Connect > AI Companion. After a while, then you can use the app on your phone or tablet.

Finally, your project is complete, you can try to play around with it, make your website better than mine and the application much beautiful. Do whatever you want.

GOOD LUCK CODING!

If you missed out on the files, here are all the files you needed below.

Be the First to Share

    Recommendations

    • Unusual Uses Contest

      Unusual Uses Contest
    • Tiny Things Speed Challenge

      Tiny Things Speed Challenge
    • Hide It Challenge

      Hide It Challenge

    2 Comments

    0
    V.P.R192
    V.P.R192

    9 months ago

    Thanks!!!

    0
    SyberProdigy
    SyberProdigy

    Question 10 months ago

    There seems to be a problem with isat_3.rar, when I download it and attempt to open it I get a corrupted file error. Can you give me another link?