Energy Optimization For Building

Complete Documentation for ESP32 Master and ESP8266 Slave Devices

Project Overview

This project implements an energy optimization system using ESP32 and ESP8266 microcontrollers with various sensors to monitor or detect energy loss and optimize energy usage in a building or home environment.

The system consists of:

System Architecture

The system consists of 1 ESP32 master controller and 4 ESP8266 slave nodes with different sensors:

All devices communicate via ESP-NOW protocol for low-latency communication.

Installation and Setup

Required Libraries

Before uploading code to your devices, you need to install the following libraries through the Arduino Library Manager:

Library Installation Method Required For
ESP8266WiFi Built-in with ESP8266 Board Package All ESP8266 devices
ESP32WiFi Built-in with ESP32 Board Package ESP32 master
espnow.h / esp_now.h Built-in with ESP boards All devices for ESP-NOW communication
FS.h / SPIFFS.h Built-in with ESP boards All devices for filesystem operations
Updater.h Built-in with ESP boards All devices for OTA updates
ACS712 Install from Library Manager Current sensing ESP8266
DHT sensor library Install from Library Manager Temperature/Humidity ESP8266
ArduinoJson Install from Library Manager ESP32 master for JSON handling
WiFiClientSecure Built-in with ESP boards ESP32 master for secure connections
HTTPClient Built-in with ESP boards ESP32 master for HTTP requests

Note: To install libraries from the Arduino IDE, go to Sketch > Include Library > Manage Libraries and search for the library names.

Board Setup

Before uploading code, you need to install the board support packages:

  1. Open Arduino IDE
  2. Go to File > Preferences
  3. Add the following URLs to "Additional Boards Manager URLs":
    • For ESP8266: http://arduino.esp8266.com/stable/package_esp8266com_index.json
    • For ESP32: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
  4. Go to Tools > Board > Boards Manager
  5. Search for "esp8266" and install the package
  6. Search for "esp32" and install the package

1. ESP32 Master Controller

Master Device Responsibilities

Key Features

Master Device Code

Master Device Connections

ESP32 Pinout: - GPIO2: Built-in LED (for status indication) - No sensors connected directly to master - GPIOs available for expansion - 3.3V and GND for power if needed

Key Functionalities

Configuration

Parameter Default Value Description
BATCH_SIZE 5 Number of sensor readings to collect before sending to server
CHUNK_SIZE 240 Size of firmware chunks for OTA updates
DOWNLOAD_TIMEOUT 30000 (30s) Timeout for firmware downloads
serverUrl http://172.25.2.129:3001 Base URL for server communication

2. ESP8266 with LDR and DHT22

Environment Monitoring Node

Measures ambient light and temperature/humidity with OTA update capability.

Key Features

LDR/DHT22 Code

Connection Diagram

DHT22 Connections: - VCC: 3.3V - DATA: D4 (with 4.7K pull-up resistor) - GND: GND LDR Connections: - One leg: 3.3V - Other leg: A0 and to GND via 10K resistor

Configuration Parameters

Parameter Value Description
FIRMWARE_VERSION "1.0.0" Current firmware version
masterAddress 0x24,0xDC,0xC3,0xAE,0x8B,0x2C MAC address of master device
CHUNK_SIZE 240 Max payload size for ESP-NOW packets
Data Interval 15 seconds Time between sensor readings

Operation Flow

  1. Initializes DHT22 sensor and LDR
  2. Sets up SPIFFS for firmware storage
  3. Configures ESP-NOW communication with master
  4. Continuously reads and sends sensor data
  5. Handles OTA firmware updates when available

Note: The device will automatically check for firmware updates every cycle. When an update is received, it will be stored in SPIFFS and applied before restarting.

Warning: Ensure the master MAC address is correctly configured for proper communication. Incorrect MAC address will prevent data transmission and OTA updates.

3. ESP8266 with ACS712 Current Sensor

Current Monitoring Node

Measures AC current consumption and calculates power with OTA update capability.

Key Features

ACS712 Current Sensor Code

Connection Diagram

ACS712 Connections: - VCC: 5V - OUT: A0 - GND: GND Current Measurement: - IP+ (Input Positive): Connect to power source - IP- (Input Negative): Connect to load - GND: Common ground reference

Configuration Parameters

Parameter Value Description
ACS712 Configuration A0, 1.035, 1023, 100 Pin, Vref, ADC max, sensitivity (mV/A)
FIRMWARE_VERSION "1.0.0" Current firmware version
receiverAddress 0x24,0xDC,0xC3,0xAE,0x8B,0x2C MAC address of master device
Noise Threshold 20mA Current values below this are set to 0
Voltage 230V Assumed line voltage for power calculation
Power Factor 0.9 Assumed power factor for power calculation

Operation Flow

  1. Initializes and calibrates ACS712 sensor
  2. Sets up SPIFFS for firmware storage
  3. Configures ESP-NOW communication with master
  4. Continuously measures current and calculates power
  5. Sends data to master every 15 seconds
  6. Handles OTA firmware updates when available

Note: The ACS712 sensor requires calibration on first use. The autoMidPoint() function automatically calibrates the zero-current point.

Warning: When measuring AC mains current, ensure proper insulation and safety measures. The ACS712 provides galvanic isolation but still requires careful handling of high voltage connections.

4. ESP8266 with PIR Motion Sensor

Motion Detection Node

Detects human presence using infrared with OTA update capability.

Key Features

PIR Sensor Code

Connection Diagram

PIR Sensor Connections: - VCC: 5V - OUT: D1 (Digital Input) - GND: GND Adjustments: - Time Delay: Adjust potentiometer (typically labeled "Time") - Sensitivity: Adjust potentiometer (typically labeled "Sens") - Jumper Settings: Set to single trigger (H) or retrigger (L) mode

Configuration Parameters

Parameter Value Description
PIR Pin D1 Digital input pin for PIR sensor
FIRMWARE_VERSION "1.0.0" Current firmware version
receiverAddress 0x24,0xDC,0xC3,0xAE,0x8B,0x2C MAC address of master device
Detection Interval 15 seconds Time between motion checks
Sensor ID PIR100112202428 Unique identifier for this sensor

Operation Flow

  1. Initializes PIR sensor on digital pin D1
  2. Sets up SPIFFS for firmware storage
  3. Configures ESP-NOW communication with master
  4. Continuously checks for motion detection
  5. Sends HIGH (motion) or LOW (no motion) to master
  6. Handles OTA firmware updates when available

Note: The PIR sensor requires 30-60 seconds to stabilize after power-up. During this time, it may give false triggers.

Warning: For best performance, avoid pointing the PIR sensor at heat sources or windows where sunlight/temperature changes might cause false triggers.

5. ESP8266 with Door/Window Sensors

Contact Detection Node

Monitors door and window open/close status with magnetic contact sensors.

Key Features

Door/Window Sensor Code

Connection Diagram

Door Sensor Connections: - One side: GND - Other side: D1 (with internal pull-up enabled) Window Sensor Connections: - One side: GND - Other side: D3 (with internal pull-up enabled) Alternative Wiring (if using external resistors): - Sensor side 1: GND - Sensor side 2: GPIO pin - Add 10K resistor between GPIO pin and 3.3V

Configuration Parameters

Parameter Value Description
Door Sensor Pin D1 Digital input with pull-up
Window Sensor Pin D3 Digital input with pull-up
receiverAddress 0x24,0xDC,0xC3,0xAE,0x8B,0x2C MAC address of master device
Door Sensor ID DOORWIN100112202428 Unique identifier for door sensor
Window Sensor ID DOORWIN100212202428 Unique identifier for window sensor
Update Interval 15 seconds Time between status checks

Operation Flow

  1. Initializes door and window sensors with internal pull-up resistors
  2. Sets up ESP-NOW communication with master
  3. Continuously checks door and window status
  4. Sends separate messages for door and window status
  5. Reports HIGH (open) or LOW (closed) status for each
  6. Waits 15 seconds before next check

Sensor Behavior

State Digital Read Value Interpretation
Contact Closed LOW Door/Window is closed
Contact Open HIGH Door/Window is open

Note: Magnetic contact sensors work by closing a circuit when the magnet is near (closed position) and opening it when apart (open position). The internal pull-up resistors ensure a clean HIGH signal when the circuit is open.

Warning: For proper operation, align the magnet within 1/4 inch (6mm) of the sensor in the closed position. Test sensor alignment after installation.

System Integration

ESP-NOW Network Setup

  1. Program all devices with their respective code
  2. Update MAC addresses in master and slave codes
  3. Power all devices within range (recommended <50m)
  4. Verify communication in serial monitor

Troubleshooting

Common Issues and Solutions