Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LightBox — Intelligent Power Changeover System

An automated multi-source power management system for environments with unreliable mains electricity. LightBox manages three power sources (mains/NEPA, inverter, and generator), automatically switching between them based on availability and voltage quality, with remote monitoring and control via a mobile app.

System Architecture

LightBox uses a multi-node, multi-MCU architecture with three physically separate hardware nodes communicating wirelessly:

┌─────────────────────────┐     Serial2      ┌──────────────┐    ESP-NOW     ┌──────────────────┐
│   CHANGEOVER UNIT       │    (9600 baud)    │  ESP8266      │  (wireless)   │  GENERATOR NODE  │
│   Arduino Mega 2560     │◄────────────────►│  Bridge       │◄────────────►│  ESP8266          │
│                         │                   │               │               │                   │
│  • 3-ch AC voltage      │     Serial1       │  MAC: 32:AE:  │               │  • Servo (choke)  │
│  • CT clamp current     │    (9600 baud)    │  A4:07:0D:65  │               │  • 2x relay       │
│  • 3x MOSFET relays     │                   └──────────────┘               │    (ignition +    │
│  • SSD1306 OLED         │                                                   │     starter)      │
│  • Buzzer (fault alarm) │                                                   │  • AC presence    │
│                         │                                                   │  • Battery monitor│
│  ┌─────────────┐        │                                                   │                   │
│  │  SIM800L    │        │                                                   │  MAC: 32:AE:     │
│  │  GSM Modem  │        │                                                   │  A4:07:0D:66     │
│  └──────┬──────┘        │                                                   └──────────────────┘
└─────────┼───────────────┘
          │ GPRS
          ▼
   ┌──────────────┐         WebSocket        ┌──────────────┐
   │  HiveMQ      │◄──────────────────────►│  React Native │
   │  MQTT Broker │        (port 8000)       │  Mobile App   │
   │  (port 1883) │                          └──────────────┘
   └──────────────┘

Node 1: Changeover Controller (Arduino Mega 2560)

The central brain of the system. Reads AC voltages from all three power sources, measures current draw, controls relay switching, displays status on an OLED, and communicates with the GSM modem and ESP8266 bridge.

Pin Function
A0 Mains (NEPA) AC voltage sense
A1 Inverter AC voltage sense
A7 Generator AC voltage sense
A2 Battery voltage (R1=10k, R2=965Ω)
A6 CT clamp current sensor
A3 Buzzer output (fault alarm)
D4 Inverter relay MOSFET
D5 Mains relay MOSFET
D6 Generator relay MOSFET
D8 Mode toggle button
D2 Auto/Manual interrupt
D3 Internet on/off interrupt

Node 2: ESP8266 Communication Bridge

Serial-to-ESP-NOW bridge. Receives commands from the Mega via UART and forwards them wirelessly to the generator node. Returns battery level and generator status data.

Node 3: Generator Starter Controller (ESP8266)

Physically controls the generator with a servo motor (choke), two relays (ignition + starter motor), and monitors AC presence and battery voltage.

Generator Start Sequence:

  1. Servo → 120° (engage choke)
  2. Ignition relay → HIGH
  3. Starter relay → HIGH for 2–3s (crank)
  4. Starter relay → LOW
  5. Servo → 0° (disengage choke)
  6. Check AC sensor — if LOW = success, else retry

Up to 4 retries with alternating choke positions and crank durations.

Communication Protocols

Protocol Path Details
Serial Mega ↔ ESP bridge Serial2, 9600 baud. "1" = gen on, "0" = gen off
Serial Mega ↔ SIM800L Serial1, 9600 baud. AT commands via TinyGSM
ESP-NOW Bridge ↔ Generator node Peer-to-peer WiFi, ~3.5s interval
MQTT Device → Cloud Publish to dan/in, subscribe to dan/out
GPRS SIM800L → Internet Airtel / MTN Nigeria APNs

MQTT Telemetry Payload (device → cloud)

{
  "Main": 220,
  "Inv": 215,
  "Gen": 0,
  "Curr": 2.5,
  "Mode": 1,
  "OpMode": 1,
  "BatV": 85,
  "GenV": 70
}

MQTT Command Payload (app → device)

{
  "phmode": 1,
  "phout": 2,
  "pshtdwn": 0
}
  • phmode: 1 = auto, 0 = manual
  • phout: 1 = mains, 2 = inverter, 3 = generator, 4 = off
  • pshtdwn: 1 = shutdown

Sensing & Measurement

AC Voltage (3 channels)

Resistive voltage divider → ADC → calibration factor:

AcVoltOut = (analogRead(pin) * (4.9 / 1023)) * calibrationFactor;
// calibrationFactor: 93.67 (mains/inverter), 570.67–790.67 (generator)
  • Available: ≥ 140V
  • Safe range: 130V – 255V

Current

CT clamp via EmonLib:

emon1.current(6, 48.0);          // pin A6, calibration 48.0
Irms = emon1.calcIrms(1480);     // 1480 samples per reading
wattage = Irms * AcVoltage;

Battery Voltage

Resistive dividers on both nodes:

// Changeover unit (A2): R1=10k, R2=965Ω → 12V battery
input_voltage = (analogRead(A2) * 4.8 / 1023.0) / (r2 / (r1 + r2));

// Generator node (A0): R1=10k, R2=1060Ω → 20-sample moving average

Operating Modes

Auto Mode

Evaluates all 8 possible power source combinations (3 binary inputs) with priority logic:

Mains > Inverter > Generator

If no sources available → sends genOn command to start the generator.

Manual Mode

Long-press button cycles through: MAINS → INV → GEN → OFF (2-second hold intervals).

Remote Mode (MQTT)

Mobile app can override both modes via JSON commands.

Fault Detection & Safety

Fault Threshold Action
Under-voltage < 130V Isolate source, switch to alternate, buzzer alarm
Over-voltage > 255V Isolate source, switch to alternate, buzzer alarm
Overload > 16,001W All relays off (emergency shutdown), buzzer alarm
Low battery < 11.5V Save state to EEPROM, prepare for power loss
Gen low battery < 3.2V Cut power to generator node to preserve battery

Faults auto-clear when voltage returns to the safe range.

OLED Display Layout (128×64)

┌────────────────────────────────┐
│  MAIN:220V  INV:215V  GEN:0V  │  ← Active source inverted
│                                │
│       OUT: 1500W               │  ← Or "MAIN: FAULT" / "OVERLOAD!!"
│                                │
│  MODE: AUTO    BAT: 85%       │
│  GEN: 70%      G+  16dB      │  ← G=GPRS, G+=MQTT, NC=offline
└────────────────────────────────┘

State Persistence (EEPROM)

Address Data
0 Output mode (1=Mains, 2=Inv, 3=Gen, 4=Off)
2 Operating mode (1=Auto, 2=Manual)

Read on startup via lastStateCheck() to restore previous state after power loss.

Mobile App (React Native)

  • Real-time voltage, current, and wattage display
  • Battery and generator fuel level indicators
  • Auto/Manual mode toggle
  • Source selection: Main, Inv, Gen, Idle, Shutdown
  • MQTT over WebSocket (HiveMQ, port 8000)

Libraries

Library Purpose
EmonLib AC current measurement via CT clamp
Adafruit_SSD1306 128×64 I2C OLED display driver
Adafruit_GFX Graphics primitives for OLED rendering
TinyGsmClient GSM modem abstraction (SIM800L AT commands)
PubSubClient MQTT publish/subscribe client
ArduinoJson JSON serialization/deserialization
ESP8266WiFi + espnow ESP-NOW peer-to-peer wireless protocol
Servo Generator choke control
EEPROM Non-volatile state persistence
react_native_mqtt Paho MQTT client for mobile app

Repository Structure

├── Final_code/
│   ├── Thelightbox_final.ino    # Final changeover controller firmware
│   └── genStarter.ino           # Final generator starter firmware
├── CHANGE OVER V2/              # Changeover firmware iterations
├── GEN STARTER V2/              # Generator starter iterations
├── display/                     # OLED display + MQTT integration
├── espBridge/                   # ESP-NOW bridge firmware
├── app/                         # React Native mobile app
├── acVoltTest.ino               # AC voltage calibration test
├── currentTest.ino              # CT clamp calibration test
├── communicationTest.ino        # ESP-NOW protocol test
├── GSMCode.ino                  # SIM800L connectivity test
└── sudocode.txt                 # System design pseudocode

Video Demo

LightBox Demo

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages