forked from arnemauer/Ducobox-ESPEasy-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P156_DucoSerialFanSpeed.ino
276 lines (236 loc) · 7.98 KB
/
_P156_DucoSerialFanSpeed.ino
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//############################### Plugin 156: DUCO Serial GW - FanSpeed #################################
//
// DUCO Serial Gateway to read out fanspeed
//
// https://github.com/arnemauer/Ducobox-ESPEasy-Plugin
// http://arnemauer.nl/ducobox-gateway/
//#######################################################################################################
#include "_Plugin_Helper.h"
#define PLUGIN_156
#define PLUGIN_ID_156 156
#define PLUGIN_NAME_156 "DUCO Serial GW - FanSpeed"
#define PLUGIN_READ_TIMEOUT_156 1000 // DUCOBOX askes "live" CO2 sensor info, so answer takes sometimes a second.
#define PLUGIN_LOG_PREFIX_156 String("[P156] DUCO FanSpeed: ")
#define PLUGIN_VALUENAME1_156 "FanSpeed"
boolean Plugin_156_init = false;
// when calling 'PLUGIN_READ', if serial port is in use set this flag and check in PLUGIN_ONCE_A_SECOND if serial port is free.
bool P156_waitingForSerialPort = false;
typedef enum {
P156_CONFIG_LOG_SERIAL = 0,
} P156PluginConfigs;
boolean Plugin_156(byte function, struct EventStruct *event, String& string){
boolean success = false;
switch (function){
case PLUGIN_DEVICE_ADD: {
Device[++deviceCount].Number = PLUGIN_ID_156;
Device[deviceCount].Type = DEVICE_TYPE_DUMMY;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].DecimalsOnly = true;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:{
string = F(PLUGIN_NAME_156);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_156));
break;
}
case PLUGIN_WEBFORM_LOAD:{
addFormCheckBox(F("Log serial messages to syslog"), F("Plugin156_log_serial"), PCONFIG(P156_CONFIG_LOG_SERIAL));
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:{
PCONFIG(P156_CONFIG_LOG_SERIAL) = isFormItemChecked(F("Plugin156_log_serial"));
success = true;
break;
}
case PLUGIN_EXIT: {
if(serialPortInUseByTask == event->TaskIndex){
serialPortInUseByTask = 255;
}
String log = PLUGIN_LOG_PREFIX_156;
log += F("EXIT PLUGIN_156");
addLogMove(LOG_LEVEL_INFO, log);
clearPluginTaskData(event->TaskIndex); // clear plugin taskdata
success = true;
break;
}
case PLUGIN_INIT:{
if(!Plugin_156_init && !ventilation_gateway_disable_serial){
Serial.begin(115200, SERIAL_8N1);
String log = PLUGIN_LOG_PREFIX_156;
log += F("Init plugin done.");
addLogMove(LOG_LEVEL_INFO, log);
Plugin_156_init = true;
}
success = true;
break;
}
case PLUGIN_READ:{
if (Plugin_156_init && !ventilation_gateway_disable_serial){
String log;
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
log = PLUGIN_LOG_PREFIX_156;
log += F("start read, eventid:");
log += event->TaskIndex;
addLogMove(LOG_LEVEL_DEBUG, log);
}
// check if serial port is in use by another task, otherwise set flag.
if(serialPortInUseByTask == 255){
serialPortInUseByTask = event->TaskIndex;
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
log = PLUGIN_LOG_PREFIX_156;
log += F("Start reading fanspeed");
addLogMove(LOG_LEVEL_DEBUG, log);
}
Plugin_156_startReadFanSpeed(PLUGIN_LOG_PREFIX_156);
}else{
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
char serialPortInUse[40];
snprintf(serialPortInUse, sizeof(serialPortInUse)," %u, set flag to read data later.", serialPortInUseByTask);
log = PLUGIN_LOG_PREFIX_156;
log += F("Serial port in use by taskid");
log += serialPortInUse;
addLogMove(LOG_LEVEL_DEBUG, log);
}
P156_waitingForSerialPort = true;
}
}
success = true;
break;
}
case PLUGIN_ONCE_A_SECOND:{
if(!ventilation_gateway_disable_serial){
if(P156_waitingForSerialPort){
if(serialPortInUseByTask == 255){
Plugin_156(PLUGIN_READ, event, string);
P156_waitingForSerialPort = false;
}
}
if(serialPortInUseByTask == event->TaskIndex){
if( (millis() - ducoSerialStartReading) > PLUGIN_READ_TIMEOUT_156){
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
String log = PLUGIN_LOG_PREFIX_156;
log += F("Serial reading timeout");
addLogMove(LOG_LEVEL_DEBUG, log);
}
DucoTaskStopSerial(PLUGIN_LOG_PREFIX_156);
serialPortInUseByTask = 255;
}
}
}
success = true;
break;
}
case PLUGIN_SERIAL_IN: {
// if we unexpectedly receive data we need to flush and return success=true so espeasy won't interpret it as an serial command.
if(serialPortInUseByTask == 255){
DucoSerialFlush();
success = true;
}
if(serialPortInUseByTask == event->TaskIndex){
uint8_t result = 0;
bool stop = false;
bool receivedNewValue = false;
while( (result = DucoSerialInterrupt()) != DUCO_MESSAGE_FIFO_EMPTY && stop == false){
switch(result){
case DUCO_MESSAGE_ROW_END: {
receivedNewValue = Plugin_156_readFanSpeedProcessRow(PLUGIN_LOG_PREFIX_156, event->BaseVarIndex, PCONFIG(P156_CONFIG_LOG_SERIAL));
if(receivedNewValue) sendData(event);
duco_serial_bytes_read = 0; // reset bytes read counter
break;
}
case DUCO_MESSAGE_END: {
DucoThrowErrorMessage(PLUGIN_LOG_PREFIX_156, result);
DucoTaskStopSerial(PLUGIN_LOG_PREFIX_156);
serialPortInUseByTask = 255;
stop = true;
break;
}
}
}
success = true;
}
break;
}
case PLUGIN_FIFTY_PER_SECOND: {
if(serialPortInUseByTask == event->TaskIndex){
if(serialSendCommandInProgress){
DucoSerialSendCommand(PLUGIN_LOG_PREFIX_156);
}
}
success = true;
break;
}
}
return success;
}
void Plugin_156_startReadFanSpeed(String logPrefix){
// set this variables before sending command
ducoSerialStartReading = millis();
duco_serial_bytes_read = 0; // reset bytes read counter
duco_serial_rowCounter = 0; // reset row counter
// SEND COMMAND
char command[] = "fanspeed\r\n";
DucoSerialStartSendCommand(command);
}
/* FanSpeed
.fanspeed
FanSpeed: Actual 389 [rpm] - Filtered 391 [rpm]
Done
>
*/
bool Plugin_156_readFanSpeedProcessRow(String logPrefix, uint8_t userVarIndex, bool serialLoggingEnabled){
String log;
if(serialLoggingEnabled && loglevelActiveFor(LOG_LEVEL_DEBUG)){
log = logPrefix;
log += F("ROW: ");
log += duco_serial_rowCounter;
log += F(" bytes read:");
log += duco_serial_bytes_read;
addLogMove(LOG_LEVEL_DEBUG, log);
DucoSerialLogArray(logPrefix, duco_serial_buf, duco_serial_bytes_read, 0);
}
// get the first row to check for command
if(duco_serial_rowCounter == 1){
char command[] = "fanspeed";
if (DucoSerialCheckCommandInResponse(logPrefix, command) ) {
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
log = logPrefix;
log += F("Received correct response on fanspeed");
addLogMove(LOG_LEVEL_DEBUG, log);
}
} else {
if(loglevelActiveFor(LOG_LEVEL_DEBUG)){
log = logPrefix;
log += F("Received invalid response on fanspeed");
addLogMove(LOG_LEVEL_DEBUG, log);
}
DucoTaskStopSerial(logPrefix);
return false;
}
}else if( duco_serial_rowCounter > 1){
duco_serial_buf[duco_serial_bytes_read] = '\0';
unsigned int fanSpeedFiltered = 0;
if (sscanf((const char*)duco_serial_buf, " FanSpeed: Actual %*u %*s %*s %*s %u", &fanSpeedFiltered) == 1) {
UserVar[userVarIndex] = fanSpeedFiltered;
char logBuf[55];
snprintf(logBuf, sizeof(logBuf), "Fanspeed: %u RPM", fanSpeedFiltered);
log = logPrefix;
log += logBuf;
addLogMove(LOG_LEVEL_INFO, log);
return true;
}
}
return false;
}