IOT SMART HOME USING NODEMCU AND SERVER WITH NODEJS

Introduction

Smart home refers to the ability of your home appliances making their own decision according to the different condition. It minimizes the human effort and is very important concept in this modern world. In this project we are going to build IOT based smart home where the sensors in different rooms make get and post request to server and the server responds to request as the code .

Component Required

Hardware

  • Nodemcu
  • dht11 temperature and humidity sensor
  • bread board
  • jumper wire

Software

  • Arduino IDE

API

The API is designed using

  • C++ code
  • Arduino IDE that helps in pushing the code to NODEMCU

The server is designed using

  • NODEJS 
  • EXPRESSJS  
  • MONGODB

The API developed must be:

  • light
  • user centric
  • scalable(in terms of types of data that can be sent)
  • Low cost(as sensors can be placed in all rooms and they can interact with cloud and act as the response of the cloud)

Features

  • Here in this project we are going to learn how to make Get and Post request from a client to server using the NODEMCU(ESP8266). It is mostly for IOT SMART HOMES where the sensors in different rooms make get and post request to server and the server responds to request as the code . Here the server our and the server is coded in NODEJS.
  • IOT SMART HOME using NodeMCU and Server with Nodejs
  • Instead of using api of google or any third party services . This api directly collects data from sensors and parse them in JSON format and sends to server. 
  • The server does rest all kind of processing of datas. 
  • The API can be modified to send of all kind of datas ranging from single variable data to whole array of values.
  • This API helps in designing custom payload that can be sent from the sensors to cloud and the data for further processing and respective response from the server.

Connection Diagram

Coding

STEP TO BE FOLLOWED TO RUN THE CODE

  1. Download the code save it as .ino extension and put in folder with same name.
  2. Connect the sensor as per the diagram . The wires from DHT11 is connected to NODEMCU.
  3. Connect the NodeMCU to your system.
  4. Select the port in which NodeMCU is connected.
  5. Push the code to NodeMCU.
  6. Then after the code is pushed the NOdeMCU gets connected to your network and starts connection .
  7. To see the working open the serial monitor but remember to keep the baud rate same.

IMPORTANT LIBRARIES

  • The Code uses some libraries download them from the following links:-
  • https://drive.google.com/file/d/1h_fPtKzZi35o8flxR5LI1xxgsMGQLG1h/view?usp=sharing
  • https://drive.google.com/file/d/1LxxoeMnuUJOepyokDTIEQ7dnjQyeQpz/view?usp=sharing

GET REQUEST FROM CLIENT TO SERVER

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

void setup() {
 
  Serial.begin(9600);                 //Serial connection
  WiFi.begin("Your wifi Name", "Your wifi Password");   //WiFi connection
 
  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    delay(500);
    Serial.println("Waiting for connection");

  }
 
}

void loop() {
 
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 Serial.println("Connected Working");

HTTPClient http;  //Declare an object of class HTTPClient
 http.useHTTP10(true);
 http.begin("your hosted server url");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request
  if (httpCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpCode);
      }
 
http.end();   //Close connection
}

delay(10000);    //Send a request every 30 seconds
 
}

POST REQUEST FROM CLIENT TO SERVER

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include "DHT.h"        // including the library of DHT11 temperature and humidity sensor
#define DHTTYPE DHT11   // DHT 11
#define dht_dpin D1  //here we are defining the pin number where we will recieve the datas
DHT dht(dht_dpin, DHTTYPE); 
float tmp=0,hum=0;
void getdata(){
    float h = dht.readHumidity();
    float t = dht.readTemperature();  
    tmp=t;
    hum=h;       
    Serial.print("Current humidity = ");
    Serial.print(h);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(t); 
    Serial.println("C  ");
}


void setup() {
  dht.begin();
  Serial.begin(9600);                 //Serial connection
//   WiFi.begin("Your Network Name", "network password");   //WiFi connection
WiFi.begin("Your Wifif Name", "sYour Wifi Password");  //WiFi 
  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    delay(500);
    Serial.println("Waiting for connection");

  }
 
}

void loop() {
 
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 Serial.println("Connected Working");
HTTPClient http;  //Declare an object of class HTTPClient
getdata();
 http.begin("Your server Link");  //Specify request destination
 http.addHeader("Content-Type", "application/json");
 String s1 = "\"living\"";
 String s2 = "\"ON\"";
String api="\"5f2d5f90bb61bb00171d2904\"";
String s4="\"status\":"+s2;
String api_key="\"id\":"+api;
String payload="{\"room\":"+s1+","+s4+","+api_key+" }";
String test="";
Serial.println(payload);
int httpCode = http.POST(payload);
  if (httpCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpCode);
      }
 
http.end();   //Close connection
}

delay(5000);    //Send a request every 30 seconds
 
}

Youtube Link

THE AUTHOR OF THE PROJECT IS :

KHITISH PANIGRAHI

LINKEDIN ID:

https://www.linkedin.com/in/babayaga9

Recommended For You

About the Author: admin

9 Comments

  1. A lot of of the things you state happens to be supprisingly appropriate and that makes me ponder why I hadn’t looked at this with this light previously. This piece really did turn the light on for me personally as far as this subject matter goes. However at this time there is actually one particular issue I am not really too cozy with so while I try to reconcile that with the actual central idea of your issue, permit me see what all the rest of your subscribers have to say.Very well done.

  2. Everything is very open with a really clear
    clarification of the challenges. It was really informative.
    Your website is very helpful. Thanks for sharing!

Comments are closed.