Contents
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
- Download the code save it as .ino extension and put in folder with same name.
- Connect the sensor as per the diagram . The wires from DHT11 is connected to NODEMCU.
- Connect the NodeMCU to your system.
- Select the port in which NodeMCU is connected.
- Push the code to NodeMCU.
- Then after the code is pushed the NOdeMCU gets connected to your network and starts connection .
- 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:
Asking questions are in fact pleasant thing if you are
not understanding anything totally, but this article provides pleasant understanding yet.
Great site. Lots of helpful info here. I’m sending it
to several pals ans additionally sharing in delicious.
And naturally, thanks in your effort!
Yep. Mostly end up reading something else in the end.
This post provides clear idea in favor of the new viewers of
blogging, that genuinely how to do blogging and site-building.
For the reason that the admin of this web site is working, no doubt very quickly it will be
renowned, due to its feature contents.
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.
Everyone loves what you guys tend to be up too.
This kind of clever work and coverage! Keep up the wonderful works guys I’ve included you
guys to blogroll.
Everything is very open with a really clear
clarification of the challenges. It was really informative.
Your website is very helpful. Thanks for sharing!
Nice answers in return of this query with solid
arguments and explaining everything concerning that.