forked from SzczepanLeon/wmbus-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_amiplus.h
executable file
·133 lines (122 loc) · 4.54 KB
/
driver_amiplus.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Based on: https://github.com/wmbusmeters/wmbusmeters/blob/master/src/driver_amiplus.cc
Copyright (C) 2019-2022 Fredrik Öhrström (gpl-3.0-or-later)
*/
#pragma once
#include "driver.h"
#include <vector>
#include <string>
struct Amiplus: Driver
{
Amiplus(std::string key = "") : Driver(std::string("amiplus"), key) {};
virtual esphome::optional<std::map<std::string, double>> get_values(std::vector<unsigned char> &telegram) override {
std::map<std::string, double> ret_val{};
add_to_map(ret_val, "total_energy_consumption_kwh", this->get_total_energy_consumption_kwh(telegram));
add_to_map(ret_val, "current_power_consumption_kw", this->get_current_power_consumption_kw(telegram));
add_to_map(ret_val, "total_energy_production_kwh", this->get_total_energy_production_kwh(telegram));
add_to_map(ret_val, "current_power_production_kw", this->get_current_power_production_kw(telegram));
add_to_map(ret_val, "voltage_at_phase_1_v", this->get_voltage_at_phase_v(1, telegram));
add_to_map(ret_val, "voltage_at_phase_2_v", this->get_voltage_at_phase_v(2, telegram));
add_to_map(ret_val, "voltage_at_phase_3_v", this->get_voltage_at_phase_v(3, telegram));
if (ret_val.size() > 0) {
return ret_val;
}
else {
return {};
}
};
private:
esphome::optional<double> get_voltage_at_phase_v(uint8_t phase, std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x0AFDC9FC;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 24) | ((uint32_t)telegram[i+1] << 16) |
((uint32_t)telegram[i+2] << 8) | ((uint32_t)telegram[i+3]));
if (c == total_register) {
i += 5;
if ((uint8_t)telegram[i-1] == phase) {
usage = bcd_2_int(telegram, i, 2);
ret_val = usage / 1.0;
ESP_LOGVV(TAG, "Found register '0AFDC9FC%d' with '%d'->'%f'", phase, usage, ret_val.value());
break;
}
}
i++;
}
return ret_val;
};
esphome::optional<double> get_total_energy_consumption_kwh(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x0E03;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 8) | ((uint32_t)telegram[i+1]));
if (c == total_register) {
i += 2;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '0E03' with '%d'->'%f'", usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};
esphome::optional<double> get_current_power_consumption_kw(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x0B2B;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 8) | ((uint32_t)telegram[i+1]));
if (c == total_register) {
i += 2;
usage = bcd_2_int(telegram, i, 3);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '0B2B' with '%d'->'%f'", usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};
esphome::optional<double> get_total_energy_production_kwh(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x0E833C;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 16) | ((uint32_t)telegram[i+1] << 8) | ((uint32_t)telegram[i+2]));
if (c == total_register) {
i += 3;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '0E833C' with '%d'->'%f'", usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};
esphome::optional<double> get_current_power_production_kw(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = 0x0BAB3C;
while (i < telegram.size()) {
uint32_t c = (((uint32_t)telegram[i+0] << 16) | ((uint32_t)telegram[i+1] << 8) | ((uint32_t)telegram[i+2]));
if (c == total_register) {
i += 3;
usage = bcd_2_int(telegram, i, 3);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '0BAB3C' with '%d'->'%f'", usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};
};