An implementation of the Aqara MagicCube using ESP32/ESP8266-based smart IoT cube with gesture recognition, orientation detection, and Home Assistant integration via MQTT.
- 12 gesture/action types — shake, tap, flip, rotate, vibration, face detection, and more
- 6-face orientation detection using MPU6050 IMU (top, bottom, left, right, front, back)
- Deep sleep power management — wakes on vibration or 5-minute timer (~10–50µA in sleep)
- Battery monitoring with voltage and percentage reporting
- Home Assistant MQTT integration — plug-and-play sensor/automation support
- Remote commands — reset, sleep, and status request via MQTT
| Component | Purpose |
|---|---|
| ESP32 DevKit (30-pin) | Main controller |
| MPU6050 (GY-521) | 6-axis IMU — gesture & orientation |
| SW-420 Vibration Sensor | Knock/vibration detection |
| LiPo Battery (3.7V, 500–2000mAh) | Power source |
| TP4056 Charger Module | Battery charging & protection |
Enclosure: 60×60×60mm cube (3D printable STL available)
| Topic | Direction | Description |
|---|---|---|
home/magiccube/status |
Publish | Sensor data & actions |
home/magiccube/command |
Subscribe | Remote commands (reset, sleep, status) |
{
"action": "shake",
"face": "top",
"battery_voltage": 4.28,
"battery_percentage": 100,
"pitch": 12.34,
"roll": 45.67,
"rssi": -45,
"boot_count": 6,
"cube_status": "active"
}| Action | Trigger |
|---|---|
boot |
Power on / wake from sleep |
shake |
3 shakes > 20 m/s² within 500ms |
flip |
Rapid 180° rotation |
rotate_clockwise |
Spin > 150°/s clockwise |
rotate_counterclockwise |
Spin > 150°/s counterclockwise |
tap |
Single impact > 12 m/s² |
vibration |
Hardware vibration sensor |
idle |
End of 5s active window |
The configuration.yaml snippet in the full documentation sets up all sensors, buttons, and a complete Lovelace dashboard. Automations are available for:
- Toggle lights on shake
- Brightness/volume control via rotation
- Scene switching by face orientation
- Bedtime routines, security alerts, climate control, and more
| Mode | Consumption | Duration |
|---|---|---|
| Active | ~80–120mA | 5 seconds per wake |
| Deep Sleep | ~10–50µA | Up to 5 minutes |
Typical battery life (1000mAh): 1–10 days depending on usage.
- Wire components per the schematic in the full documentation
- Flash firmware via Arduino IDE
- Update
secrets.hwith your WiFi and MQTT credentials - Add the MQTT sensor/button config to Home Assistant
- Place the cube and start automating!
MIT License — free for personal and commercial use.
Firmware Version: 1.0 | Author: Raghu