|
1 | 1 | # SwitchBot |
2 | | -> In development |
| 2 | + |
| 3 | +A home automation system that turns electrical appliances into smart devices using ESP32 microcontrollers and a cloud-based WebSocket server. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +SwitchBot allows you to control up to 8 electrical relays remotely via a REST API or WebSocket connection. The system consists of two parts: |
| 8 | + |
| 9 | +1. **Hardware (ESP32 Firmware)** - Arduino sketch that runs on ESP32, handles relay control, manual switches, and WebSocket communication |
| 10 | +2. **Backend Server (Python)** - Async web server using aiohttp that manages device connections and provides REST API endpoints |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- Control up to 8 relays per device |
| 15 | +- Manual switch override support (physical buttons) |
| 16 | +- WiFi configuration via captive portal (WiFiManager) |
| 17 | +- Secure WebSocket communication (SSL/TLS) |
| 18 | +- REST API for integration with other services |
| 19 | +- Persistent state storage using ESP32 Preferences |
| 20 | +- Factory reset functionality |
| 21 | +- Real-time bidirectional communication |
| 22 | + |
| 23 | +## Tech Stack |
| 24 | + |
| 25 | +### Hardware |
| 26 | +- ESP32 microcontroller |
| 27 | +- Arduino framework |
| 28 | +- Libraries: |
| 29 | + - WiFiManager - Captive portal for WiFi setup |
| 30 | + - WebSocketsClient - WebSocket communication |
| 31 | + - ArduinoJson - JSON parsing |
| 32 | + - Preferences - Non-volatile storage |
| 33 | + |
| 34 | +### Backend |
| 35 | +- Python 3 |
| 36 | +- aiohttp - Async HTTP/WebSocket server |
| 37 | +- aiohttp-jinja2 - Template rendering |
| 38 | +- Cerberus - Data validation |
| 39 | + |
| 40 | +## Hardware Pin Configuration |
| 41 | + |
| 42 | +| Function | Pins | |
| 43 | +|----------|------| |
| 44 | +| Relay Outputs | GPIO 23, 22, 21, 19, 18, 5, 25, 26 | |
| 45 | +| Switch Inputs | GPIO 13, 12, 14, 27, 33, 32, 15, 4 | |
| 46 | +| Board LED | GPIO 2 | |
| 47 | +| Reset Button | GPIO 17 | |
| 48 | + |
| 49 | +## API Endpoints |
| 50 | + |
| 51 | +### GET `/api/{device_key}` |
| 52 | +Returns the current state of all relays for a device. |
| 53 | + |
| 54 | +**Response:** |
| 55 | +```json |
| 56 | +{ |
| 57 | + "relay_1": true, |
| 58 | + "relay_2": false, |
| 59 | + ... |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### POST `/api/{device_key}` |
| 64 | +Update relay states for a device. |
| 65 | + |
| 66 | +**Request Body:** |
| 67 | +```json |
| 68 | +{ |
| 69 | + "relay_1": true, |
| 70 | + "relay_2": false |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +**Response:** |
| 75 | +```json |
| 76 | +{ |
| 77 | + "status": true, |
| 78 | + "data": { "relay_1": true, "relay_2": false } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### WebSocket `/ws/{device_key}` |
| 83 | +Real-time bidirectional communication for device control and state updates. |
| 84 | + |
| 85 | +## Setup |
| 86 | + |
| 87 | +### Hardware Setup |
| 88 | + |
| 89 | +1. Flash the Arduino sketch to your ESP32 |
| 90 | +2. On first boot, connect to the "SwitchBot" WiFi network |
| 91 | +3. Configure your WiFi credentials and SwitchBot username/password |
| 92 | +4. The device will connect to the server automatically |
| 93 | + |
| 94 | +### Server Setup |
| 95 | + |
| 96 | +1. Install dependencies: |
| 97 | + ```bash |
| 98 | + pip install -r requirements.txt |
| 99 | + ``` |
| 100 | + |
| 101 | +2. Run the server: |
| 102 | + ```bash |
| 103 | + python -m app |
| 104 | + ``` |
| 105 | + |
| 106 | +## How It Works |
| 107 | + |
| 108 | +1. **Device Boot**: ESP32 loads stored credentials or starts captive portal for configuration |
| 109 | +2. **Connection**: Device establishes SSL WebSocket connection to the server |
| 110 | +3. **Registration**: Server registers the device in `ACTIVE_DEVICES` dictionary |
| 111 | +4. **Control**: Users send commands via REST API or WebSocket |
| 112 | +5. **Execution**: Server forwards commands to the device via WebSocket |
| 113 | +6. **Feedback**: Device reports state changes back to the server |
| 114 | +7. **Manual Override**: Physical switches can toggle relays locally, state syncs to server |
| 115 | + |
| 116 | +## Architecture |
| 117 | + |
| 118 | +``` |
| 119 | +┌─────────────┐ WebSocket ┌─────────────┐ REST API ┌─────────────┐ |
| 120 | +│ ESP32 │◄──────────────────►│ Server │◄─────────────────►│ Client │ |
| 121 | +│ (Device) │ (SSL) │ (aiohttp) │ │ (App/Web) │ |
| 122 | +└─────────────┘ └─────────────┘ └─────────────┘ |
| 123 | + │ │ |
| 124 | + │ Manual │ State |
| 125 | + │ Switches │ Storage |
| 126 | + ▼ ▼ |
| 127 | +┌─────────────┐ ┌─────────────┐ |
| 128 | +│ Relays │ │ Database │ |
| 129 | +│ (Appliances)│ │ │ |
| 130 | +└─────────────┘ └─────────────┘ |
| 131 | +``` |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +GPL-3.0 |
| 136 | + |
| 137 | +## Author |
| 138 | + |
| 139 | +[viperadnan](https://github.com/viperadnan-git) |
0 commit comments