echo '' ;

ESP8266 NodeMCU Project – Home Automation

Home automation is the process of controlling various devices and systems in your home through a centralized control system. With the advancement of technology, home automation has become more accessible and affordable for homeowners.The ESP8266 NodeMCU is a popular choice for home automation projects due to its built-in Wi-Fi capability and compatibility with various sensors and actuators. In this project, we will explore how to set up a basic home automation system using the ESP8266 NodeMCU.

Home Automation device feature

sta mode : change username and password

wifi.sta.config("NEW_SSID", "NEW_PASSWORD")

Replace “NEW_SSID” with the new Wi-Fi network name (SSID) you want to connect to, and “NEW_PASSWORD” with the new password for that network.

save old device state (on/off)

-- Define the file name where the state will be saved
local file = "device_state.txt"

-- Function to save the device state to a file
function saveDeviceState(state)
  file.open(file, "w+")
  file.writeline(state)
  file.close()
end

-- Function to read the device state from the file
function readDeviceState()
  if file.open(file, "r") then
    local state = file.readline()
    file.close()
    return state
  else
    return nil
  end
end

-- Example usage: saving the device state
local deviceState = "on" -- Assume the device is currently on
saveDeviceState(deviceState)

-- Example usage: reading the device state
local oldState = readDeviceState()
if oldState then
  print("Old device state: " .. oldState)
else
  print("No saved device state found.")
end

status monitor (device on time, off time log)

-- Define the file names for on and off time logs
local onLogFile = "on_time_log.txt"
local offLogFile = "off_time_log.txt"

-- Function to log device on time
function logDeviceOnTime()
  local currentTime = tmr.time()
  file.open(onLogFile, "a+")
  file.writeline(currentTime)
  file.close()
end

-- Function to log device off time
function logDeviceOffTime()
  local currentTime = tmr.time()
  file.open(offLogFile, "a+")
  file.writeline(currentTime)
  file.close()
end

-- Example usage: log device on time
logDeviceOnTime()

-- Example usage: log device off time
logDeviceOffTime()

In this Lua code snippet, we define functions to save and read the device state to and from a file (in this case, “device_state.txt”). The saveDeviceState function writes the device state (e.g., “on” or “off”) to the file, and the readDeviceState function reads the device state from the file.

device power consumption

-- Configure ADC pin for current sensor
adc.force_init_mode(adc.INIT_ADC)
adc.setmode(adc.MODE_VCC)
adc.setvref(adc.REF_INTERNAL, 0)

-- Define the current sensor sensitivity (mV/A), and the operating voltage
local sensitivity = 66  -- ACS712 sensitivity: 66mV/A
local operatingVoltage = 3.3  -- Operating voltage of ESP8266 NodeMCU

-- Function to measure device current and calculate power consumption
function measurePowerConsumption()
  local rawValue = adc.read(0)
  local current = ((rawValue / 1024) * operatingVoltage) / sensitivity  -- Calculate current in Amperes
  local power = current * operatingVoltage  -- Calculate power in Watts
  
  print("Current (A): " .. current)
  print("Power (W): " .. power)
end

-- Call the function to measure power consumption
measurePowerConsumption()

In this Lua script for the ESP8266 NodeMCU, we define functions to log device on and off times to separate files (on_time_log.txt and off_time_log.txt). The logDeviceOnTime and logDeviceOffTime functions record the current time (in seconds since boot) when the device is turned on and off, respectively.

change device name

-- Define the new device name
local newDeviceName = "MyNewDeviceName"

-- Change the hostname of the ESP8266 NodeMCU
wifi.sta.sethostname(newDeviceName)

time based on/off

-- Set the pin for the device control (e.g., relay, LED, etc.)
local devicePin = 1  -- Assuming the device is connected to GPIO pin 1

-- Function to turn the device on
local function turnDeviceOn()
  gpio.write(devicePin, gpio.HIGH)  -- Assuming HIGH activates the device
end

-- Function to turn the device off
local function turnDeviceOff()
  gpio.write(devicePin, gpio.LOW)  -- Assuming LOW deactivates the device
end

-- Schedule device on/off times
local onTime = "08:00:00"  -- Turn on at 8:00 AM
local offTime = "20:00:00"  -- Turn off at 8:00 PM

tmr.alarm(0, 1000, tmr.ALARM_AUTO, function()
  local time = rtctime.epoch2cal(rtctime.get())
  local currentTime = string.format("%02d:%02d:%02d", time.hour, time.min, time.sec)
  
  if currentTime == onTime then
    turnDeviceOn()
  elseif currentTime == offTime then
    turnDeviceOff()
  end
end)

Encrypt and decrypt

local crypto = require("crypto")

-- Encryption key and IV (Initialization Vector)
local key = "myEncryptionKey123"  -- 16 bytes key for AES-128
local iv = "initializationVec"    -- 16 bytes IV for AES-128

-- Data to encrypt
local plaintext = "Hello, this is a secret message."

-- Encrypt data
local cipher = crypto.encrypt("aes-128-cbc", key, iv, plaintext)

-- Print the encrypted data
print("Encrypted Data: " .. crypto.toHex(cipher))

-- Decrypt data
local decryptedText = crypto.decrypt("aes-128-cbc", key, iv, cipher)

-- Print the decrypted text
print("Decrypted Text: " .. decryptedText)

Steps to set up the home automation system:

  1. Connect the relay module to the ESP8266 NodeMCU. The relay module will act as a switch to control devices such as lights, fans, or appliances.
  2. Connect the temperature and humidity sensor to the ESP8266 NodeMCU. This sensor can be used to monitor the environmental conditions in your home.
  3. Upload the necessary code to the ESP8266 NodeMCU. You can use the Arduino IDE to program the ESP8266 NodeMCU. The code will include commands for turning the relay on and off based on sensor readings.
  4. Set up a central control system. You can create a web server on the ESP8266 NodeMCU to control the devices remotely using a smartphone or computer. Alternatively, you can use a third-party platform such as Blynk or Home Assistant for easier control and automation.
  5. Test the system by monitoring the sensor readings and controlling the relay module. You can set up automated routines such as turning on the lights when the temperature drops below a certain threshold.

With the ESP8266 NodeMCU, you can create a customizable and affordable home automation system to make your daily life more convenient and efficient. Experiment with different sensors and actuators to expand the capabilities of your home automation setup.

ESP8266 NodeMCU Project Home Automation: NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 NodeMCU Project – Home Automation
Others
NodeMCU All Post
Sitemap

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading