DIY GUY Chris
Published © Apache-2.0

NodeMCU Home Automation (ESP8266)

If you want a NodeMCU device that could help you to control all your house electrical devices, then this will be your best guide

BeginnerFull instructions provided24 hours4,635
NodeMCU Home Automation (ESP8266)

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1

Software apps and online services

Blynk
Blynk
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

STL files

Schematics

Schematic

GERBER files

Code

NodeMCU codes

Arduino
/************************************************************************************************************************************************************************                                              
 * - Author               : BELKHIR Mohamed                        *                                               
 * - Profession           : (Electrical Ingineer) MEGA DAS owner   *                                                                                              
 * - Main purpose         : Industrial Application                 *                                                                                 
 * - Copyright (c) holder : All rights reserved                    *
 * - License              : BSD 2-Clause License                   * 
 * - Date                 : 11/01/2019                             *
 * ***********************************************************************************************************************************************************************/
 
 /*********************************** NOTE **************************************/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:

// * Redistributions of source code must retain the above copyright notice, this
//  list of conditions and the following disclaimer.

// * Redistributions in binary form must reproduce the above copyright notice,
//  this list of conditions and the following disclaimer in the documentation
//  and/or other materials provided with the distribution.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED

                                                                               
//---------------------------------------Do not remove these lines
#define BLYNK_PRINT Serial
#define TCP_MSS whatever
#define LWIP_IPV6 whatever
#define LWIP_FEATURES whatever
#define LWIP_OPEN_SRC whatever
//---------------------------------------
#include <SPI.h>                                                           // Include library for SPI communication
#include <ESP8266WiFi.h>                                                   // Include library for ESP module to use the integrated ESP module of the NodeMCU board
#include <BlynkSimpleEsp8266.h>                                            // Import the Bylnk library to use its function which will facilitate the data transfer from the app
#include "DHT.h"                                                           // Needed library to interface the Temperature and humidity sensor
#include <Wire.h>                                                          // Include the IC communication module
#include <BH1750.h>                                                        // Include the light sensor library
#define DHTPIN D3                                                          // Define the digital pin use for Temperature and humidity sensor 
#define DHTTYPE DHT11                                                      // Define type of the Temperature and humidity sensor
DHT MyDHTSensor(DHTPIN, DHTTYPE);                                          // Create the Temperature and humidity sensor instance
BH1750 MyLightSensor;                                                      // Create the light sensor instance

char auth[] = "You will receive this in an Email from Blynk";              // You will receive the "auth" token in your E-mail address when you create the Bylnk app so just copy and past it

char ssid[] = "Name of your Wifi network";                                 // Write the name "SSID" of your Wifi network 
char pass[] = "Wifi password";                                             // Insert the password of your Wifi network

BlynkTimer timer;                                                          // Timer use to synchronise the data transfer to the app

//----------------------------------------------This function will receive the data from the app and update the status of the appropriate pin
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();                                            // assigning incoming value from pin V1 to a variable
  digitalWrite(D4,pinValue);                                               // Write the value to the appropriate pin
}
//----------------------------------------------
//----------------------------------------------This function will receive the data from the app and update the status of the appropriate pin
BLYNK_WRITE(V2)
{
  int pinValue = param.asInt();                                            // assigning incoming value from pin V1 to a variable
  digitalWrite(D5,pinValue);                                               // Write the value to the appropriate pin
}
//----------------------------------------------
//----------------------------------------------This function will receive the data from the app and update the status of the appropriate pin
BLYNK_WRITE(V3)
{
  int pinValue = param.asInt();                                            // assigning incoming value from pin V1 to a variable
  digitalWrite(D6,pinValue);                                               // Write the value to the appropriate pin
}
//----------------------------------------------
//----------------------------------------------This function will receive the data from the app and update the status of the appropriate pin
BLYNK_WRITE(V4)
{
  int pinValue = param.asInt();                                            // assigning incoming value from pin V1 to a variable
  digitalWrite(D7,pinValue);                                               // Write the value to the appropriate pin
}
//----------------------------------------------
//----------------------------------------------This function will receive the data from the app and update the status of the appropriate pin
BLYNK_WRITE(V7)
{
  int pinValue = param.asInt();                                            // assigning incoming value from pin V1 to a variable
  analogWrite(D8,pinValue);                                                // Write the value to the appropriate pin
  Serial.println(pinValue);
}
//----------------------------------------------

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);                                           // Start the Wifi connction
  MyDHTSensor.begin();                                                     // Start the Temperature and Humidity sensor
  Wire.begin();                                                            // Start the IC bus connection
  MyLightSensor.begin();                                                   // Start the Light sensor
  //----------------------------- Define the pin mode of each used pin of your NodeMCU
  pinMode(D0,INPUT);
  pinMode(D4,OUTPUT);
  pinMode(D5,OUTPUT);
  pinMode(D6,OUTPUT);
  pinMode(D7,OUTPUT);
  pinMode(D8,OUTPUT);
  //-----------------------------
  digitalWrite(D4,HIGH);
  digitalWrite(D5,HIGH);
  digitalWrite(D6,HIGH);
  digitalWrite(D7,HIGH);
  digitalWrite(D8,LOW);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  delay(500);
  uint16_t lux = MyLightSensor.readLightLevel();                         // Read Light sensor value (Lux)
  float h = MyDHTSensor.readHumidity();                                  // Read Humidity in (%)
  float t = MyDHTSensor.readTemperature();                               // Read temperature as Celsius (the default)
//  if (isnan(h) || isnan(t)) 
//  {
//    Serial.println("Failed to read from DHT sensor!");
//    return;
//  }
//-------------------------------------- Send data to the Bylnk app
  Blynk.virtualWrite(V5, t);
  Blynk.virtualWrite(V6, h);
  Blynk.virtualWrite(V8, lux);
//--------------------------------------
Serial.println(lux);
}

Credits

DIY GUY Chris

DIY GUY Chris

51 projects • 340 followers
I'm having fun while making electronics projects, I am an electrical engineer in love with "Do It Yourself" tasks.
Thanks to JLCPCB.

Comments