-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuh50reader.h
136 lines (114 loc) · 5.05 KB
/
uh50reader.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
134
135
136
//-------------------------------------------------------------------------------------
// ESPHome Landis+Gyr Ultraheat UH50 (T550) Heat Meter custom sensor
// Copyright 2020 Pär Svanström
//
// History
// 0.1.0 2021-02-03: Initial release
//
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// 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.
//-------------------------------------------------------------------------------------
#include "esphome.h"
#define BUF_SIZE 2500
#define WAIT_TIME 1
byte data_cmd[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
'/', '#' , '!', 0x0D, 0x0A
};
// Landis & Gyr EN 61107 mode B protocol
// http://manuals.lian98.biz/doc.en/html/u_iec62056_struct.htm
// https://www.ungelesen.net/protagWork/media/downloads/solar-steuerung/iec62056-21%7Bed1.0%7Den_.pdf
// http://tunn.us/arduino/landisgyr.php
// https://www.elvaco.se/Image/GetDocument/en/286/cmi4110-users-manual-english.pdf
// https://github.com/lvzon/dsmr-p1-parser/blob/master/doc/IEC-62056-21-notes.md
// http://womp3-14.vijfwal.nl/p2013c.html
class UH50Reader : public Component, public UARTDevice, public CustomAPIDevice {
const char* DELIMITERS = "(*";
UARTDevice uart_out;
unsigned long timeLastRun;
char buffer[BUF_SIZE];
public:
Sensor *cumulativeActiveImport = new Sensor();
Sensor *cumulativeVolume = new Sensor();
Sensor *currentPower = new Sensor();
Sensor *flowRate = new Sensor();
Sensor *temperatureFlow = new Sensor();
Sensor *temperatureReturn = new Sensor();
Sensor *temperatureDiff = new Sensor();
UH50Reader(UARTComponent *uart_in, UARTComponent *uart_out) : UARTDevice(uart_in), uart_out(uart_out) { }
void setup() override {
sendDataCmd();
timeLastRun = millis() - (WAIT_TIME - 1) * 60000;
//register the service which Home Assistant can call to read the meter
register_service(&UH50Reader::read_meter, "start_read_meter");
}
void loop() override {
if (millis() - timeLastRun > WAIT_TIME * 60000) {
sendDataCmd();
readTelegram();
timeLastRun = millis();
}
}
void read_meter() {
sendDataCmd();
readTelegram();
}
private:
void publishSensors(OBISData *od, int count) {
for (int i=0; i < count; i++) {
if (!strcmp(od[i].obis_code, "6.8"))
cumulativeActiveImport->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.26"))
cumulativeVolume->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.4"))
currentPower->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.27"))
flowRate->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.29"))
temperatureFlow->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.28"))
temperatureReturn->publish_state(atof(od[i].value));
else if (!strcmp(od[i].obis_code, "6.30"))
temperatureDiff->publish_state(atof(od[i].value));
}
}
void sendDataCmd() {
for (int i = 0; i < sizeof(data_cmd); i++) {
uart_out.write(data_cmd[i]);
}
ESP_LOGI("cmd", "data cmd sent");
}
void readTelegram() {
OBISData obisdata[MAX_OBIS_CODES];
bool publish=false;
// fast forward until we find the STX byte (start-of-text)
byte b = 0x00;
while (available() && b != 0x02) {
b = read();
}
while (int len = available()) {
ESP_LOGD("readTelegram", "Got %d bytes available to read", len);
if (!read_array((uint8_t *) buffer, len))
ESP_LOGW("readTelegram", "read_array() returned false, meter reading may be incomplete");
ESP_LOGD("readTelegram", "Read %s", buffer);
int count;
parse_obis(buffer, obisdata, &count);
print_parsed_data(obisdata, count);
publishSensors(obisdata, count);
// clean buffer
memset(buffer, 0, BUF_SIZE - 1);
}
}
};