A web-based circuit simulator viewer with real-time visualization using WebSocket communication.
-
Backend: C WebSocket server (
circuit_server)- Raw WebSocket implementation (no external libraries except OpenSSL for handshake)
- Binary protocol for efficient data transfer
- Real-time circuit simulation with adjustable speed
- Multi-threaded (simulation thread per client)
-
Frontend: HTML/CSS/JavaScript
- Canvas-based circuit visualization
- Live voltage and current display
- Interactive controls (start/stop/step/reset)
- Adjustable simulation speed and current visualization
All messages use binary format with network byte order (big-endian).
Sent by server when circuit is loaded.
[0x01][element_count:2][node_count:2][element_data...]
Each element:
[type:1][node0:2][node1:2][node2:2][value:8][current:8][v0:8][v1:8][v2:8]
Sent by server during simulation.
[0x02][time:8][step_count:4][element_updates...]
Each element update:
[current:8][v0:8][v1:8][v2:8]
Sent by client to control simulation.
[0x03][command:1][optional_data...]
Commands:
CMD_START (0x01): Start simulationCMD_STOP (0x02): Stop simulationCMD_STEP (0x03): Execute single stepCMD_RESET (0x04): Reset circuitCMD_SET_SPEED (0x05) + [speed:4]: Set speed multiplierCMD_SET_CURRENT_VIS (0x06) + [factor:4]: Set current visualization factor
Sent by client to load a circuit file.
[0x04][filename_string...]
Sent by server on error.
[0xFF][error_code:1]
Error codes:
0x01: Load error0x02: Simulation error
cd web/backend
makeRequirements:
- GCC
- OpenSSL development libraries (
libssl-dev) - pthread
cd web/backend
./circuit_server [port]Default port is 8080.
Open web/frontend/index.html in a web browser. Or serve with:
cd web/frontend
python3 -m http.server 8000Then navigate to http://localhost:8000
-
Start the server:
./circuit_server 8080 -
Open the web interface: Open
index.htmlin a browser -
Load a circuit:
- Enter the path to a circuit file (e.g.,
../../examples/test_comprehensive.txt) - Click "Load Circuit"
- The circuit topology will appear on the canvas
- Enter the path to a circuit file (e.g.,
-
Control simulation:
- Start: Begin continuous simulation
- Stop: Pause simulation
- Step: Execute single timestep
- Reset: Reset to initial state
-
Adjust visualization:
- Speed slider: Control simulation speed (0.1x to 5x)
- Current vis slider: Adjust wire thickness based on current (0.1x to 10x)
-
Wire color: Indicates voltage level
- Red: High positive voltage (>3V)
- Orange: Medium voltage (1-3V)
- Green: Near ground (-1 to 1V)
- Blue: Negative voltage (<-1V)
-
Wire thickness: Indicates current magnitude (scaled by current vis factor)
-
Element symbols:
- Resistor: Zigzag line
- Capacitor: Two parallel plates
- Inductor: Coiled line
- Voltage source: Circle with 'V'
- Current source: Circle with 'I'
- Diode: Triangle with line
- Transistor: NPN/PNP symbol with arrow
- Switch: Hinged contact
Available in examples/:
test_simple.txt: Basic RC circuittest_diode.txt: Diode rectifiertest_switch.txt: Switch circuittest_comprehensive.txt: Mixed components
You can also load CircuitJS format files (.txt files from CircuitJS).
web/backend/
├── websocket.h # WebSocket protocol definitions
├── websocket.c # WebSocket implementation
├── server.c # Main server and message handlers
└── Makefile # Build configuration
web/frontend/
├── index.html # Main page structure
├── style.css # Styling
└── circuit.js # Circuit viewer and WebSocket client
Connection refused: Ensure the server is running on the correct port.
Circuit not loading: Check the file path is correct relative to the server's working directory.
No visualization: Ensure WebSocket connection is established (status indicator shows "Connected").
Slow simulation: Reduce speed multiplier or decrease current visualization factor.