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:
- 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.
- Connect the temperature and humidity sensor to the ESP8266 NodeMCU. This sensor can be used to monitor the environmental conditions in your home.
- 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.
- 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.
- 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.