A power monitoring system based on the INA228 current/voltage/power sensor and Raspberry Pi Pico (RP2040), with a custom UART communication protocol and PC-side software to control and collect data.
- INA228 sensor driver for RP2040
- Interactive command-based configuration of INA228 (set/get config)
- Raw data acquisition plus derived/processed values on the PC side
- Save collected and processed data to JSON for offline analysis
- Custom UART protocol with CRC error detection
- Retransmission and timeout handling
- Real-time data streaming with 1 kHz sample rates
- PC-side protocol simulator with fault injection capabilities
- Comprehensive test suite using Google Test
powermonitor/
├── device/ # Raspberry Pi Pico firmware
│ ├── INA228.cpp/hpp # INA228 sensor driver
│ ├── powermonitor.cpp # Main device firmware
│ └── CMakeLists.txt # Pico SDK build configuration
├── pc_client/ # PC-side serial client
├── pc_sim/ # PC-side simulator and tests
│ ├── pc_sim_main.cpp # Simulator demo entry
│ └── CMakeLists.txt # Host build configuration with GTest
├── protocol/ # Protocol implementation
│ ├── crc16_ccitt_false.cpp
│ ├── frame_builder.cpp
│ ├── parser.cpp
│ └── unpack.cpp
├── sim/ # Simulation infrastructure
│ ├── event_loop.cpp # Discrete event simulator
│ └── virtual_link.cpp # Virtual UART link with fault injection
├── node/ # Protocol node implementations
│ ├── pc_node.cpp # PC-side protocol logic
│ ├── device_node.cpp # Device-side protocol logic
│ └── ina228_model.cpp # INA228 behavior model
└── docs/ # Documentation
├── pc_client/
│ ├── overview.md
│ └── implementation_plan.md
├── pc_sim/
│ ├── simulator_tests.md
│ └── state_machine_tests.md
├── protocol/
│ ├── uart_protocol.md
│ └── parser_design.md
└── device/
└── time_sync.md
The workflow.ps1 PowerShell script provides a unified interface for building, testing, and flashing:
# Run tests
pwsh workflow.ps1
# Clean build and run tests
pwsh workflow.ps1 -Clean
# Build device firmware only
pwsh workflow.ps1 -BuildDevice
# Build and flash device firmware to Pico (Windows only)
pwsh workflow.ps1 -FlashPico
# Generate Visual Studio solution
pwsh workflow.ps1 -GenerateSolution
# Show all options
pwsh workflow.ps1 -HelpRequirements for device firmware:
- Set
PICO_SDK_PATHenvironment variable to your Pico SDK installation - For
-FlashPico: Windows OS with Pico in BOOTSEL mode (RPI-RP2 drive)
If you prefer using Visual Studio, generate a VS solution with CMake, then set the PC app as the startup project and press Run.
# Let CMake pick the default generator (often Visual Studio on Windows)
cmake -S . -B build_vs
- Open the generated solution in
build_vs/. - Set
powermonitoras the Startup Project (PC-side interactive client). - Build and run (F5).
Requires Pico SDK installation.
cd device
cmake -B build
cmake --build build
# Flash to Pico
# Copy build/powermonitor.uf2 to your Pico in BOOTSEL modeThere are two test tracks:
-
PC simulator / host-side tests (GoogleTest): protocol framing/parsing, state machines, and fault-injection scenarios.
- Recommended:
./test.sh - Or run directly:
./build/pc_sim/pc_sim_test - Details:
docs/pc_sim/simulator_tests.mdanddocs/pc_sim/state_machine_tests.md
- Recommended:
-
Real device tests (serial/on-device): step-by-step validation against actual hardware.
- Test plan:
docs/pc_client/device_serial_tests.md
- Test plan:
Edit pc_sim/pc_sim_main.cpp to adjust simulation parameters:
-
LinkConfig: Fault injection settings
drop_prob: Packet drop probability (0.0-1.0)flip_prob: Bit flip probability (0.0-1.0)min_delay_us,max_delay_us: Transmission delay rangemin_chunk,max_chunk: Byte chunking for fragmentation
-
INA228 Model: Edit
node/ina228_model.cppto change simulated voltage/current/temperature waveforms
a tool to test asio serial api
A lightweight browser viewer is available at web_viewer/.
It loads the JSON generated by powermonitor and renders voltage/current/power timeline tracks with logic-analyzer-style interactions:
- Zoom timeline with mouse wheel
- Pan timeline by dragging
- Hover to inspect point values
Run locally:
npm install
npm run dev
# opens http://localhost:5173/web_viewer/index.html
# optional auto-load:
# http://localhost:5173/web_viewer/index.html?data=/build/pc_client/output/powermonitor_20260218_005711.jsonValidation for web-only changes:
# workflow.ps1 is not required when only files under web_viewer/ are changed
npm run build:webThe communication protocol uses framed messages with the following features:
- Frame format:
SOF(0xAA 0x55) + VER + TYPE + FLAGS + SEQ + LEN(LE) + MSGID + DATA + CRC16(CCITT-FALSE) - CRC16-CCITT-FALSE error detection
- Sequence numbering for reliable delivery
- ACK/NACK responses with automatic retransmission
- Configurable timeout and retry mechanisms
See docs/protocol/uart_protocol.md for detailed protocol specification.
- MCU: Raspberry Pi Pico (RP2040) / Adafruit Feather RP2040
- Sensor: Texas Instruments INA228 (I2C power monitor)
- Interface: USB CDC (UART over USB)
See individual source files for license information.