-
Notifications
You must be signed in to change notification settings - Fork 119
/
p1reader_base.h
210 lines (163 loc) · 8.88 KB
/
p1reader_base.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//-------------------------------------------------------------------------------------
// ESPHome P1 Electricity Meter custom sensor
// Copyright 2020 Pär Svanström
//
// History
// 0.1.0 2020-11-05: Initial release
// 0.x.0 2023-04-18: Added HDLC support
// 0.2.0 2023-08-14: Some optimizations when ESPHome started to complain about execution times
// 0.3.0 2023-08-23: Rework structure to merge above changes with HDLC stuff
//
// 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.
//-------------------------------------------------------------------------------------
#ifndef P1READER_BASE_H
#define P1READER_BASE_H
#include "esphome.h"
#include "parsed_message.h"
#define BUF_SIZE 60
class P1ReaderBase : public PollingComponent, public UARTDevice {
int _pollingIntervalMs;
int _pollingIntervalLog = 30;
protected:
ParsedMessage _parsedMessage = ParsedMessage();
char _buffer[BUF_SIZE];
uint8_t _bufferLen;
int _uSecondsPerByte;
public:
Sensor *cumulativeActiveImport = new Sensor();
Sensor *cumulativeActiveExport = new Sensor();
Sensor *cumulativeReactiveImport = new Sensor();
Sensor *cumulativeReactiveExport = new Sensor();
Sensor *momentaryActiveImport = new Sensor();
Sensor *momentaryActiveExport = new Sensor();
Sensor *momentaryReactiveImport = new Sensor();
Sensor *momentaryReactiveExport = new Sensor();
Sensor *momentaryActiveImportL1 = new Sensor();
Sensor *momentaryActiveExportL1 = new Sensor();
Sensor *momentaryActiveImportL2 = new Sensor();
Sensor *momentaryActiveExportL2 = new Sensor();
Sensor *momentaryActiveImportL3 = new Sensor();
Sensor *momentaryActiveExportL3 = new Sensor();
Sensor *momentaryReactiveImportL1 = new Sensor();
Sensor *momentaryReactiveExportL1 = new Sensor();
Sensor *momentaryReactiveImportL2 = new Sensor();
Sensor *momentaryReactiveExportL2 = new Sensor();
Sensor *momentaryReactiveImportL3 = new Sensor();
Sensor *momentaryReactiveExportL3 = new Sensor();
Sensor *voltageL1 = new Sensor();
Sensor *voltageL2 = new Sensor();
Sensor *voltageL3 = new Sensor();
Sensor *currentL1 = new Sensor();
Sensor *currentL2 = new Sensor();
Sensor *currentL3 = new Sensor();
P1ReaderBase(UARTComponent *parent) : PollingComponent(), UARTDevice(parent) {
// Calculate pollingInterval for Component given our uart buffer size and the rest
size_t rxBufferSize = parent->get_rx_buffer_size();
uint8_t bits = parent->get_data_bits() + parent->get_stop_bits() + (parent->get_parity() != UART_CONFIG_PARITY_NONE ? 1 : 0) + 1;
float secondsPerByte = (float)bits * (1.0f / (float) parent->get_baud_rate());
ESP_LOGD("setup", "secondsPerByte calculated as: %f s", secondsPerByte);
_uSecondsPerByte = (int) (secondsPerByte * 1000000.0f);
// Keep a margin of 20%
_pollingIntervalMs = (int)((float)rxBufferSize * secondsPerByte * 800.0f);
set_update_interval(_pollingIntervalMs);
}
float get_setup_priority() const override { return esphome::setup_priority::LATE; }
void setup() override {
// Start with a clean buffer
memset(_buffer, 0, BUF_SIZE);
_bufferLen = 0;
_parsedMessage.initNewTelegram();
}
void update() override {
// Defer setup logging to here (and repeat a few times) as OTA logs do not pick up initial logs otherwise
if (_pollingIntervalLog >= 0) {
if (_pollingIntervalMs < 20) {
ESP_LOGE("setup", "Polling interval is too low: %d ms (rx_buffer_size %d, uSecondsPerByte %d)",
_pollingIntervalMs, parent_->get_rx_buffer_size(), _uSecondsPerByte);
} else if (_pollingIntervalMs < 100) {
ESP_LOGW("setup", "Polling interval is low: %d ms (rx_buffer_size %d, uSecondsPerByte %d)",
_pollingIntervalMs, parent_->get_rx_buffer_size(), _uSecondsPerByte);
} else {
ESP_LOGI("setup", "Polling interval calculated as: %d ms (rx_buffer_size %d, uSecondsPerByte %d)",
_pollingIntervalMs, parent_->get_rx_buffer_size(), _uSecondsPerByte);
}
_pollingIntervalLog--;
}
// Deliver a parsed and crc ok message in the calls _after_ actually reading it so we
// split the work over more scheduler slices since publish_state is slow (and logging is slow
// so set log level INFO to avoid all teh debug logging slowing things down)
if (_parsedMessage.telegramComplete) {
publishSensors(&_parsedMessage);
if (!_parsedMessage.telegramComplete) {
readP1Message();
}
} else {
readP1Message();
}
}
protected:
void publishSensors(ParsedMessage* parsedMessage) {
if (parsedMessage->crcOk && parsedMessage->telegramComplete)
{
if (parsedMessage->sendBatchOne) {
cumulativeActiveImport->publish_state(parsedMessage->cumulativeActiveImport);
cumulativeActiveExport->publish_state(parsedMessage->cumulativeActiveExport);
momentaryActiveImport->publish_state(parsedMessage->momentaryActiveImport);
momentaryActiveExport->publish_state(parsedMessage->momentaryActiveExport);
momentaryActiveImportL1->publish_state(parsedMessage->momentaryActiveImportL1);
momentaryActiveExportL1->publish_state(parsedMessage->momentaryActiveExportL1);
momentaryActiveImportL2->publish_state(parsedMessage->momentaryActiveImportL2);
momentaryActiveExportL2->publish_state(parsedMessage->momentaryActiveExportL2);
momentaryActiveImportL3->publish_state(parsedMessage->momentaryActiveImportL3);
momentaryActiveExportL3->publish_state(parsedMessage->momentaryActiveExportL3);
voltageL1->publish_state(parsedMessage->voltageL1);
voltageL2->publish_state(parsedMessage->voltageL2);
voltageL3->publish_state(parsedMessage->voltageL3);
parsedMessage->sendBatchOne = false;
ESP_LOGD("publish", "Sensors published (part one). CRC: %04X", parsedMessage->crc);
} else {
currentL1->publish_state(parsedMessage->currentL1);
currentL2->publish_state(parsedMessage->currentL2);
currentL3->publish_state(parsedMessage->currentL3);
cumulativeReactiveImport->publish_state(parsedMessage->cumulativeReactiveImport);
cumulativeReactiveExport->publish_state(parsedMessage->cumulativeReactiveExport);
momentaryReactiveImport->publish_state(parsedMessage->momentaryReactiveImport);
momentaryReactiveExport->publish_state(parsedMessage->momentaryReactiveExport);
momentaryReactiveImportL1->publish_state(parsedMessage->momentaryReactiveImportL1);
momentaryReactiveExportL1->publish_state(parsedMessage->momentaryReactiveExportL1);
momentaryReactiveImportL2->publish_state(parsedMessage->momentaryReactiveImportL2);
momentaryReactiveExportL2->publish_state(parsedMessage->momentaryReactiveExportL2);
momentaryReactiveImportL3->publish_state(parsedMessage->momentaryReactiveImportL3);
momentaryReactiveExportL3->publish_state(parsedMessage->momentaryReactiveExportL3);
ESP_LOGI("publish", "Sensors published (complete). CRC: %04X", parsedMessage->crc);
parsedMessage->initNewTelegram();
}
} else if (!parsedMessage->crcOk && parsedMessage->telegramComplete) {
parsedMessage->initNewTelegram();
}
}
size_t readBytesUntilAndIncluding(char terminator, char *buffer, size_t length) {
size_t index = 0;
while (index < length) {
uint8_t c;
bool hasData = read_byte(&c);
if (!hasData) break;
*buffer++ = (char)c;
index++;
if (c == terminator) break;
}
return index; // return number of characters, not including null terminator
}
virtual void readP1Message() = 0;
};
#endif