Skip to content

ocpp: OCPP 1.6J Central System for EV chargers (EV Unit 4/5) - #37

Merged
frahlg merged 1 commit into
masterfrom
ev/unit-4-ocpp-cs
Apr 16, 2026
Merged

frahlg merged 1 commit into
masterfrom
ev/unit-4-ocpp-cs

Conversation

@frahlg

@frahlg frahlg commented Apr 15, 2026

Copy link
Copy Markdown
Member

Summary

42W now embeds an OCPP 1.6J Central System. Any OCPP-1.6J charger (Easee Native OCPP, Wallbox, Alfen, Schrack, Mennekes, Keba, go-e via OCPP) connects directly to `ws://<42w-ip>:8887/` and starts pushing telemetry. No per-vendor Lua driver needed.

This is the local-first answer to EV integration: the charger speaks to a local server on your LAN, not Easee Cloud.

What it does

  • New `go/internal/ocpp/` package with `Server`, `Handler`, and `Config`. ~290 LOC of our own code; `github.com/lorenzodonini/ocpp-go` handles the WebSocket + JSON layer (the same lib SteVe uses).
  • Each connected charger creates a `DerEV` reading in `telemetry.Store`, keyed by the chargerId from the URL. Status maps cleanly:
    • `Charging` → connected=true, charging=true
    • `Preparing` / `SuspendedEV` / `SuspendedEVSE` / `Finishing` / `Reserved` → connected=true, charging=false
    • `Available` / `Unavailable` → connected=false
    • `Faulted` → emits `ev_fault` metric
  • `MeterValues` reads `Power.Active.Import` (W or kW) into the DerEV `w`, and `Energy.Active.Import.Register` (Wh or kWh) into the running `session_wh`.
  • `StartTransaction` / `StopTransaction` track per-session energy and emit `ev_session_wh` to the long-format TSDB on stop.
  • Unit 3's dispatch clamp now picks up real-charger power automatically — home batteries will not discharge into an active EV charge.
  • Optional HTTP Basic auth (`ocpp.username` + `ocpp.password`).

Phase 1 boundaries (intentional)

  • Read-only. No `RemoteStartTransaction` / `RemoteStopTransaction` / `SetChargingProfile` yet — Phase 2.
  • Auto-authorize all idTags. RFID gating is Phase 2.
  • `ws://` only, no TLS. LAN-only deployments. `wss://` + cert story is Phase 2 / Zap material.
  • OCPP 1.6J only. Easee Native OCPP is 1.6J; revisit 2.0.1 when vendors catch up.

Config

```yaml
ocpp:
enabled: true
bind: 0.0.0.0
port: 8887
username: easee
password:
heartbeat_interval_s: 60
```

Disabled by default (`cfg.OCPP` nil → server doesn't start).

Test plan

  • `go test ./internal/ocpp/` passes — boot+meter pushes DerEV; session tracking accumulates Wh; basic-auth rejects wrong creds.
  • Full suite green (`go test ./...`).
  • ARM64 cross-build clean.
  • Deploy to homelab-rpi.
  • Configure Easee Native OCPP (firmware ≥328) to point at `ws://:8887/` with Basic auth.
  • Verify boot + heartbeat in journalctl, MeterValues hit `/api/status.ev_charging_w`, plug car in and watch ferroamp/sungrow stay non-negative.

Builds on the merged plumbing in #34, #35, #36. Unit 5 (UI card + HA sensors) ships next.

🤖 Generated with Claude Code

Embeds a WebSocket OCPP 1.6J central system into 42W, so any OCPP-1.6J
charger (Easee Native OCPP, Wallbox, Alfen, Schrack, Mennekes, Keba,
go-e via OCPP) can connect directly without a per-vendor Lua driver.

Per-charger MeterValues + StatusNotification are translated into DerEV
readings in telemetry.Store. The dispatch clamp added in Unit 3 picks
those up automatically — when the EV draws power, home batteries stop
discharging into the car. Read-only (Phase 1): RemoteStartTransaction
and SetChargingProfile are deferred to Phase 2.

Backed by github.com/lorenzodonini/ocpp-go (the same library SteVe
uses). Pure Go, no CGo. ~290 LOC of our own code; the library handles
the WebSocket + JSON layer.

Config (all optional, disabled by default):

  ocpp:
    enabled: true
    bind: 0.0.0.0
    port: 8887
    username: easee
    password: <secret>
    heartbeat_interval_s: 60

Charge points connect to ws://<bind>:<port>/<chargerId>. The chargerId
becomes the driver name in /api/devices and /api/status.drivers.

Tests:
  - TestBootAndMeterValuesPushDerEV — boot + status + meter → DerEV
  - TestStartStopTransactionTracksSession — session_wh accounting
  - TestBasicAuthRejectsWrongCredentials — auth gate works

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c3156aba2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"bind", cfg.Bind, "port", cfg.Port, "path", cfg.Path,
"basic_auth", cfg.Username != "")
// cs.Start blocks until cs.Stop is called.
s.cs.Start(cfg.Port, fmt.Sprintf("%s{ws}", cfg.Path))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Honor configured bind address for OCPP listener

Start advertises cfg.Bind as a runtime setting, but listener startup ignores it and only passes cfg.Port and path to s.cs.Start(...). As written, ocpp.bind cannot constrain where the socket is exposed, so operators who set a specific bind address (for example loopback/LAN-only) do not get the behavior they configured. Please wire the bind address into server startup (or reject/remove the option) so configuration and network exposure match.

Useful? React with 👍 / 👎.

Comment on lines +150 to +153
case core.ChargePointStatusAvailable, core.ChargePointStatusUnavailable:
s.connected = false
s.charging = false
case core.ChargePointStatusPreparing,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Zero EV power on Available/Unavailable status updates

In OnStatusNotification, the Available/Unavailable branch flips connected/charging but leaves lastPowerW untouched before calling pushReading. If the previous meter sample was non-zero and the charger does not send a subsequent zero-power sample (or StopTransaction is missed), the control loop keeps subtracting stale EV watts and can continue clamping batteries unnecessarily. Reset lastPowerW when entering these non-charging statuses.

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit c00d817 into master Apr 16, 2026
@frahlg
frahlg deleted the ev/unit-4-ocpp-cs branch April 16, 2026 04:23
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.0.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

frahlg added a commit that referenced this pull request Sep 27, 2026
* refactor(control): remove the priority and weighted modes

- #7 priority/weighted: remove ModePriority and ModeWeighted, State.PriorityOrder
  and Weights, distributePriority/distributeWeighted, their revision hashing,
  catalogue, contract registry and HA select entries, and the never-read
  batteries.<name>.weight setting and its Settings field. Priority held every
  battery at its measured power because nothing set its order. A stored
  priority or weighted mode boots as self_consumption, is saved back and
  logged once; the API, HA and the app refuse both. Their 60 golden records
  are dropped (611 -> 551, deletions only) and the e2e target-following step
  runs in self_consumption.
- #34 HA mode: haCallbacks.SetMode now calls appModes.SetMode, so it goes
  through control.ApplyMode, only logs a failed save, still applies the export
  preference and replans without blocking the MQTT handler.
- #36 UseCascade: delete the unread control.State field and its initializer.
- #37 use_energy_dispatch: drop the deprecated key. Loading YAML or stored
  settings turns it into planner.legacy_dispatch (the old key still wins),
  DropRetiredSettings deletes it and battery weights from state.db, and one
  energyDispatchEnabled helper serves boot and hot reload, which no longer
  keeps the legacy path after the planner section is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7

* chore: drop an accidental node_modules symlink

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MuerPFZFG88kgu8sWVHeq7

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant