-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Peblar #16451
Merged
Merged
Add Peblar #16451
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e0270ab
Add peblar charger
PieVo 78a4f37
Run lint on peblar.go
PieVo e0f62f6
Simplify
andig 026c33c
Run lint-ui too
PieVo 5f96354
decorate peblar getPhases
PieVo 7605784
restore reading connected phases for current/voltage functions
PieVo b51dd7d
drop logging
PieVo 3472c80
Align
andig bcb3ba8
minor
andig 217fc01
supported statuses
andig e14a277
wip
andig 58fc86d
Merge branch 'master' into feature/add-peblar-charger
andig e826071
wip
andig fdb0e2c
sync wb.enabled
PieVo c316f67
Merge branch 'master' into feature/add-peblar-charger
PieVo e9086af
Drop last logging remnant
PieVo 6a5d7e7
Merge branch 'master' into feature/add-peblar-charger
PieVo c9bb229
Fix MaxCurrentMillis
PieVo 08ed595
Add sponsorship
andig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
package charger | ||
|
||
// LICENSE | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
// Details on the Peblar modbus server obtained from: https://developer.peblar.com/modbus-api | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/util" | ||
"github.com/evcc-io/evcc/util/modbus" | ||
"github.com/volkszaehler/mbmd/encoding" | ||
) | ||
|
||
// Peblar charger implementation | ||
type Peblar struct { | ||
conn *modbus.Connection | ||
curr uint32 | ||
enabled bool | ||
phases uint16 | ||
} | ||
|
||
const ( | ||
// Meter addresses | ||
peblarRegEnergyTotal = 30000 | ||
peblarRegSessionEnergy = 30004 | ||
peblarRegPowerPhase1 = 30008 | ||
peblarRegPowerPhase2 = 30010 | ||
peblarRegPowerPhase3 = 30012 | ||
peblarRegPowerTotal = 30014 | ||
peblarRegVoltages = 30016 | ||
peblarRegCurrents = 30022 | ||
|
||
// Config addresses | ||
peblarRegSerialNumber = 30050 | ||
peblarRegProductNumber = 30062 | ||
peblarRegFwIdentifier = 30074 | ||
peblarRegPhaseCount = 30092 | ||
peblarRegIndepRelay = 30093 | ||
|
||
// Control addresses | ||
peblarRegCurrentLimitSource = 30112 | ||
peblarRegCurrentLimitActual = 30113 | ||
peblarRegModbusCurrentLimit = 40000 | ||
peblarRegForce1Phase = 40002 | ||
|
||
// Diagnostic addresses | ||
peblarRegCpState = 30110 | ||
) | ||
|
||
func init() { | ||
registry.Add("peblar", NewPeblarFromConfig) | ||
} | ||
|
||
//go:generate go run ../cmd/tools/decorate.go -f decoratePeblar -b *Peblar -r api.Charger -t "api.PhaseSwitcher,Phases1p3p,func(int) error" -t "api.PhaseGetter,GetPhases,func() (int, error)" | ||
|
||
// NewPeblarFromConfig creates a Peblar charger from generic config | ||
func NewPeblarFromConfig(other map[string]interface{}) (api.Charger, error) { | ||
cc := modbus.TcpSettings{ | ||
ID: 255, | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewPeblar(cc.URI, cc.ID) | ||
} | ||
|
||
// NewPeblar creates Peblar charger | ||
func NewPeblar(uri string, id uint8) (api.Charger, error) { | ||
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Register contains the physically connected phases | ||
b, err := conn.ReadInputRegisters(peblarRegPhaseCount, 1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
wb := &Peblar{ | ||
conn: conn, | ||
curr: 6000, // assume min current | ||
phases: binary.BigEndian.Uint16(b), // required for retrieving the right amount of voltage/current registers | ||
} | ||
|
||
b, err = conn.ReadInputRegisters(peblarRegIndepRelay, 1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var phasesS func(int) error | ||
var phasesG func() (int, error) | ||
|
||
if binary.BigEndian.Uint16(b) == 1 { | ||
phasesS = wb.phases1p3p | ||
phasesG = wb.getPhases | ||
} | ||
|
||
return decoratePeblar(wb, phasesS, phasesG), err | ||
} | ||
|
||
// Status implements the api.Charger interface | ||
func (wb *Peblar) Status() (api.ChargeStatus, error) { | ||
b, err := wb.conn.ReadInputRegisters(peblarRegCpState, 1) | ||
if err != nil { | ||
return api.StatusNone, err | ||
} | ||
|
||
switch s := rune(encoding.Uint16(b)); s { | ||
case 'A', 'B', 'C': | ||
return api.ChargeStatus(s), nil | ||
default: | ||
return api.StatusNone, fmt.Errorf("invalid status: %d", s) | ||
} | ||
} | ||
|
||
// Enabled implements the api.Charger interface | ||
func (wb *Peblar) Enabled() (bool, error) { | ||
return verifyEnabled(wb, wb.enabled) | ||
} | ||
|
||
// Enable implements the api.Charger interface | ||
func (wb *Peblar) Enable(enable bool) error { | ||
var current uint32 | ||
if enable { | ||
current = wb.curr | ||
} | ||
|
||
err := wb.setCurrent(current) | ||
if err == nil { | ||
wb.enabled = enable | ||
} | ||
|
||
return err | ||
} | ||
|
||
// setCurrent writes the current limit in mA | ||
func (wb *Peblar) setCurrent(current uint32) error { | ||
b := make([]byte, 4) | ||
binary.BigEndian.PutUint32(b, current) | ||
|
||
_, err := wb.conn.WriteMultipleRegisters(peblarRegModbusCurrentLimit, 2, b) | ||
return err | ||
} | ||
|
||
// MaxCurrent implements the api.Charger interface | ||
func (wb *Peblar) MaxCurrent(current int64) error { | ||
return wb.MaxCurrentMillis(float64(current)) | ||
} | ||
|
||
var _ api.ChargerEx = (*Peblar)(nil) | ||
|
||
// MaxCurrent implements the api.ChargerEx interface | ||
func (wb *Peblar) MaxCurrentMillis(current float64) error { | ||
if current < 6 { | ||
return fmt.Errorf("invalid current %.1f", current) | ||
} | ||
|
||
err := wb.setCurrent(wb.curr) | ||
if err == nil { | ||
wb.curr = uint32(current * 1e3) | ||
} | ||
PieVo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return err | ||
} | ||
|
||
var _ api.Meter = (*Peblar)(nil) | ||
|
||
// CurrentPower implements the api.Meter interface | ||
func (wb *Peblar) CurrentPower() (float64, error) { | ||
b, err := wb.conn.ReadInputRegisters(peblarRegPowerTotal, 2) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return float64(binary.BigEndian.Uint32(b)), err | ||
} | ||
|
||
var _ api.ChargeRater = (*Peblar)(nil) | ||
|
||
// ChargedEnergy implements the api.MeterEnergy interface | ||
func (wb *Peblar) ChargedEnergy() (float64, error) { | ||
b, err := wb.conn.ReadInputRegisters(peblarRegSessionEnergy, 4) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return float64(encoding.Int64(b)) / 1e3, err | ||
} | ||
|
||
// TotalEnergy implements the api.MeterEnergy interface | ||
func (wb *Peblar) TotalEnergy() (float64, error) { | ||
b, err := wb.conn.ReadInputRegisters(peblarRegEnergyTotal, 4) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return float64(encoding.Int64(b)) / 1e3, nil | ||
} | ||
|
||
// getPhaseValues returns 1..3 sequential register values | ||
func (wb *Peblar) getPhaseValues(reg uint16, divider float64) (float64, float64, float64, error) { | ||
b, err := wb.conn.ReadInputRegisters(reg, wb.phases*2) | ||
if err != nil { | ||
return 0, 0, 0, err | ||
} | ||
|
||
var res [3]float64 | ||
for i := 0; i < int(wb.phases); i++ { | ||
res[i] = float64(binary.BigEndian.Uint32(b[4*i:])) / divider | ||
} | ||
|
||
return res[0], res[1], res[2], nil | ||
} | ||
|
||
var _ api.PhaseCurrents = (*Peblar)(nil) | ||
|
||
// Currents implements the api.PhaseCurrents interface | ||
func (wb *Peblar) Currents() (float64, float64, float64, error) { | ||
return wb.getPhaseValues(peblarRegCurrents, 1e3) | ||
} | ||
|
||
var _ api.PhaseVoltages = (*Peblar)(nil) | ||
|
||
// Voltages implements the api.PhaseVoltages interface | ||
func (wb *Peblar) Voltages() (float64, float64, float64, error) { | ||
return wb.getPhaseValues(peblarRegVoltages, 1) | ||
} | ||
|
||
// phases1p3p implements the api.PhaseSwitcher interface via the decorator | ||
func (wb *Peblar) phases1p3p(phases int) error { | ||
var b uint16 | ||
if phases == 1 { | ||
b = 1 | ||
} | ||
|
||
_, err := wb.conn.WriteSingleRegister(peblarRegForce1Phase, b) | ||
return err | ||
} | ||
|
||
// getPhases implements the api.PhaseGetter interface | ||
func (wb *Peblar) getPhases() (int, error) { | ||
b, err := wb.conn.ReadHoldingRegisters(peblarRegForce1Phase, 1) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
phases := 3 | ||
if binary.BigEndian.Uint16(b) == 1 { | ||
phases = 1 | ||
} | ||
|
||
return phases, nil | ||
} | ||
|
||
var _ api.Diagnosis = (*Peblar)(nil) | ||
|
||
// Diagnose implements the api.Diagnosis interface | ||
func (wb *Peblar) Diagnose() { | ||
if b, err := wb.conn.ReadInputRegisters(peblarRegSerialNumber, 12); err == nil { | ||
fmt.Printf("\tSN:\t%s\n", b) | ||
} | ||
if b, err := wb.conn.ReadInputRegisters(peblarRegProductNumber, 12); err == nil { | ||
fmt.Printf("\tPN:\t%s\n", b) | ||
} | ||
if b, err := wb.conn.ReadInputRegisters(peblarRegFwIdentifier, 12); err == nil { | ||
fmt.Printf("\tFirmware:\t%s\n", b) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
template: peblar | ||
products: | ||
- brand: Peblar | ||
description: | ||
generic: Home, Home Plus, Business | ||
capabilities: ["1p3p", "mA"] | ||
requirements: | ||
description: | ||
de: Peblar-Ladegeräte mit Firmware-Version 1.6 und höher unterstützen Modbus TCP über Port 502. Der Modbus-Server muss über die Weboberfläche des Ladegeräts aktiviert werden. Stellen Sie sicher, dass Smart-Charging-Strategien deaktiviert und auf Standard gesetzt sind. | ||
en: Peblar chargers with firmware version 1.6 and onwards support Modbus TCP via port 502. The Modbus server needs to be enabled via the charger web interface. Ensure that smart charging strategies are disabled and set to Default. | ||
params: | ||
- name: modbus | ||
choice: ["tcpip"] | ||
render: | | ||
type: peblar | ||
{{- include "modbus" . }} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doh :O
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well that resulted in unexpected behavior..