-
Notifications
You must be signed in to change notification settings - Fork 0
/
stadsverwarming.h
193 lines (162 loc) · 4.8 KB
/
stadsverwarming.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
#include "esphome.h"
class Multical602 : public Component, public Switch, public UARTDevice
{
public:
Sensor *sensor_energy{nullptr};
Sensor *sensor_volume{nullptr};
Sensor *sensor_tempin{nullptr};
Sensor *sensor_tempout{nullptr};
Sensor *sensor_tempdiff{nullptr};
Sensor *sensor_power{nullptr};
Sensor *sensor_flow{nullptr};
Sensor *sensor_poweract{nullptr};
TextSensor *sensor_info{nullptr};
TextSensor *textsensor_status{nullptr};
std::string m_status;
UARTDevice *_tx;
UARTDevice *_rx;
Multical602(UARTComponent *uart_tx, UARTComponent *uart_rx, Sensor *m__energy, Sensor *m__volume, Sensor *m__tin, Sensor *m__tout,
Sensor *m__tdiff, Sensor *m__power, Sensor *m__flow, Sensor *m__poweract, TextSensor *m__info, TextSensor *m__status) : sensor_energy(m__energy), sensor_volume(m__volume), sensor_tempin(m__tin), sensor_tempout(m__tout),
sensor_tempdiff(m__tdiff), sensor_power(m__power), sensor_flow(m__flow), sensor_poweract(m__poweract), sensor_info(m__info), textsensor_status(m__status)
{
_tx = new UARTDevice(uart_tx);
_rx = new UARTDevice(uart_rx);
}
void setup() override
{
// This will be called by App.setup()
}
void write_state(bool state) override
{
// This will be called every time the user requests a state change.
run();
}
void run()
{
ESP_LOGD("multical", "Run");
byte sendmsg1[] = {175, 163, 177}; // /#1 with even parity
byte r = 0;
byte to = 0;
byte i;
byte j;
char message[255];
int parityerrors;
to = 0;
r = 0;
i = 0;
j = 0;
parityerrors = 0;
char *tmpstr;
float m_energy, m_volume, m_tempin, m_tempout, m_tempdiff, m_power;
long m_hours, m_flow, m_poweract;
std::string m_info;
for (int x = 0; x < 3; x++)
{
_tx->write(sendmsg1[x]);
}
delay(10);
bool gelukt = false;
while (_rx->available())
{
// receive byte
r = _rx->read();
if (r != 0 && r != 120)
{
message[i++] = char(r);
gelukt = true;
}
}
if (gelukt)
{
ESP_LOGD("multical", "OK");
Serial.println("OK: ");
Serial.println(message);
Serial.println("");
Serial.println("");
message[i] = 0;
tmpstr = strtok(message, " ");
if (tmpstr)
{
// Convert to kWh
m_energy = atol(tmpstr) / 1000.000;
// m_energy = m_energy / 1000.000;
}
else
{
m_energy = 0;
}
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_volume = atol(tmpstr) / 100.0;
else
m_volume = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_hours = atol(tmpstr);
else
m_hours = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempin = atol(tmpstr) / 100.0;
else
m_tempin = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempout = atol(tmpstr) / 100.0;
else
m_tempout = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_tempdiff = atol(tmpstr) / 100.0;
else
m_tempdiff = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_power = atol(tmpstr) / 10.0;
else
m_power = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_flow = atol(tmpstr);
else
m_flow = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_poweract = atol(tmpstr);
else
m_poweract = 0;
tmpstr = strtok(NULL, " ");
if (tmpstr)
m_info = atol(tmpstr);
else
m_info = "NONE";
ESP_LOGD("multical", "Energy: %f GJ", m_energy);
ESP_LOGD("multical", "Volume: %f m3", m_volume);
ESP_LOGD("multical", "Time: %ld hrs", m_hours);
ESP_LOGD("multical", "T_in: %f", m_tempin);
ESP_LOGD("multical", "T_out: %f", m_tempout);
ESP_LOGD("multical", "T_diff: %f", m_tempdiff);
ESP_LOGD("multical", "Power: %f", m_power);
ESP_LOGD("multical", "Flow: %ld l/h", m_flow);
ESP_LOGD("multical", "Power Act: %ld", m_poweract);
ESP_LOGD("multical", "Info: %ld", m_info);
m_status = "OK";
sensor_energy->publish_state(m_energy);
sensor_volume->publish_state(m_volume);
sensor_tempin->publish_state(m_tempin);
sensor_tempout->publish_state(m_tempout);
sensor_tempdiff->publish_state(m_tempdiff);
sensor_power->publish_state(m_power);
sensor_flow->publish_state(m_flow);
sensor_poweract->publish_state(m_poweract);
sensor_info->publish_state(m_info);
textsensor_status->publish_state(m_status);
}
else
{
ESP_LOGD("multical", "NIET OK");
m_status = "ERROR";
textsensor_status->publish_state(m_status);
}
}
};