Imported from bergmartin/batterymonitor (
AGENTS.md). Install upstream withnpx skills add bergmartin/batterymonitor. Copyright stays with the author.
Agents & AI Assistant Context
This document serves as a guide for AI agents and developers working on the batterymonitor project. It outlines best practices, workflows, and minimum requirements for the embedded system.
Best Practices
Code Quality & Structure
- Modular Design: Keep the
src/main.cpp(ormainfile) minimal. Move logic into libraries within thelib/directory or separate components insrc/. - Configuration: Do not hardcode credentials (WiFi, MQTT). Use build flags (
-D) inplatformio.inior a separatesecrets.hfile added to.gitignore. - Formatting: Adhere to the project's
.clang-formator standard C++ style guides. - Documentation: Comment complex logic. Maintain
README.mdfor high-level overview.
PlatformIO Specifics
- Dependency Management: Define libraries in
platformio.iniusinglib_deps. Pin versions to ensure reproducibility. - Environments: Use separate environments in
platformio.inifor different boards or build types (e.g.,debug,release,ota).
Git Branching Strategy
We use a simplified Feature Branch Workflow:
main: The stable, production-ready branch. Code here must compile and pass all tests.feature/<feature-name>: Created frommainfor new features. Merged back tomainvia Pull Request (PR).fix/<bug-name>: Created frommainto address bugs.chore/<task-name>: For maintenance, documentation, or dependency updates.
Rules:
- Never push directly to
main. - All PRs require a successful build check.
- Commit messages should be imperative and descriptive (e.g., "Add MQTT reconnection logic" not "added mqtt").
Basic Minimum for Embedded Project
For this project (and similar IoT nodes), the following features are mandatory foundation blocks:
1. Over-The-Air (OTA) Updates
- Mechanism: Must support updating firmware without physical access.
- Safety: Implement rollback or confirmation mechanisms if possible to prevent bricking remote devices.
- Security: Use password protection or signed binaries if exposed.
2. MQTT Publishing & Connectivity
- State Reporting: Periodically publish sensor data/state to topic
prefix/device_id/state. - Availability: Implement LWT (Last Will and Testament) on topic
prefix/device_id/status(payload:online/offline) to detect disconnects. - Resilience: Auto-reconnect logic for both WiFi and MQTT with exponential backoff.
3. Home Assistant (HA) Discovery
- Auto-Configuration: The device must self-register with Home Assistant via MQTT Discovery.
- Topic Structure: Publish config payloads to
homeassistant/<component>/<node_id>/<object_id>/config. - Payload: Include
name,state_topic,unique_id,device(manufacturer, model), andavailability_topic. - Benefit: No manual YAML configuration required in Home Assistant.