forked from enesbcs/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P165_SerSwitch.ino
493 lines (448 loc) · 19.3 KB
/
_P165_SerSwitch.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//############################# Plugin 165: Serial MCU controlled switch v1.6 ###########################
//
// Designed for TUYA/YEWELINK Wifi Touch Light switch with ESP8266 + PIC16F1829 MCU,
// the similar Sonoff Dual MCU controlled Wifi relay and LCTECH WIFI RELAY is also supported.
// Based on P020 Ser2Net and P001 Switch.
//
// Dummy device (Switch) can be used to control relays, one device per relay and/or rules Publish command
// for example.
// Support thread: https://www.letscontrolit.com/forum/viewtopic.php?f=6&t=3245
//
//#######################################################################################################
#define PLUGIN_165
#define PLUGIN_ID_165 165
#define PLUGIN_NAME_165 "Serial MCU controlled switch"
#define PLUGIN_VALUENAME1_165 "Relay0"
#define PLUGIN_VALUENAME2_165 "Relay1"
#define PLUGIN_VALUENAME3_165 "Relay2"
#define BUFFER_SIZE 128 // at least 3x33 byte serial buffer needed for Tuya
#define SER_SWITCH_YEWE 1
#define SER_SWITCH_SONOFFDUAL 2
#define SER_SWITCH_LCTECH 3
static byte Plugin_165_switchstate[3];
static byte Plugin_165_ostate[3];
byte Plugin_165_commandstate = 0; // 0:no,1:inprogress,2:finished
byte Plugin_165_numrelay = 1;
byte Plugin_165_ownindex;
byte Plugin_165_globalpar0;
byte Plugin_165_globalpar1;
boolean Plugin_165_init = false;
boolean Plugin_165(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_165;
Device[deviceCount].Type = DEVICE_TYPE_DUMMY;
Device[deviceCount].VType = SENSOR_TYPE_SWITCH;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_165);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_165));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_165));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_165));
success = true;
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[3];
options[0] = F("Yewelink/TUYA");
options[1] = F("Sonoff Dual");
options[2] = F("LC TECH");
int optionValues[3] = { SER_SWITCH_YEWE, SER_SWITCH_SONOFFDUAL, SER_SWITCH_LCTECH };
addFormSelector(string, F("Switch Type"), F("plugin_165_type"), 3, options, optionValues, choice);
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE)
{
choice = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String buttonOptions[3];
buttonOptions[0] = F("1");
buttonOptions[1] = F("2");
buttonOptions[2] = F("3");
addFormSelector(string, F("Number of relays"), F("plugin_165_button"), 3, buttonOptions, NULL, choice);
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_SONOFFDUAL)
{
choice = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String modeoptions[3];
modeoptions[0] = F("Normal");
modeoptions[1] = F("Exclude/Blinds mode");
modeoptions[2] = F("Simultaneous mode");
int modeoptionValues[3] = { 0, 1, 2 };
addFormSelector(string, F("Relay working mode"), F("plugin_165_mode"), 2, modeoptions, modeoptionValues, choice);
}
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("plugin_165_type"));
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE)
{
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_165_button"));
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_SONOFFDUAL)
{
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_165_mode"));
}
Plugin_165_globalpar0 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_165_globalpar1 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
success = true;
break;
}
case PLUGIN_INIT:
{
LoadTaskSettings(event->TaskIndex);
Plugin_165_ownindex = event->TaskIndex;
Serial.setDebugOutput(false);
Serial.setRxBufferSize(BUFFER_SIZE); // Arduino core for ESP8266 WiFi chip 2.4.0
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE)
{
Plugin_165_numrelay = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
Serial.begin(9600, SERIAL_8N1);
delay(1);
getmcustate(); // request status on startup
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_SONOFFDUAL)
{
Plugin_165_numrelay = 3; // 3rd button is the "wifi" button
Serial.begin(19230, SERIAL_8N1);
// on start we do not know the state of the relays...
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_LCTECH)
{
Plugin_165_numrelay = 1;
Serial.begin(9600, SERIAL_8N1);
// on start we do not know the state of the relays...
}
Plugin_165_globalpar0 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_165_globalpar1 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
success = true;
Plugin_165_init = true;
break;
}
case PLUGIN_SERIAL_IN:
{
int bytes_read = 0;
byte serial_buf[BUFFER_SIZE];
String log;
if (Plugin_165_init)
{
while (Serial.available() > 0) {
yield();
if (bytes_read < BUFFER_SIZE) {
serial_buf[bytes_read] = Serial.read();
if (bytes_read == 0) { // packet start
Plugin_165_commandstate = 0;
switch (Settings.TaskDevicePluginConfig[event->TaskIndex][0])
{
case SER_SWITCH_YEWE: //decode first byte of package
{
if (serial_buf[bytes_read] == 0x55) {
Plugin_165_commandstate = 1;
}
break;
}
case SER_SWITCH_SONOFFDUAL: //decode first byte of package
{
if (serial_buf[bytes_read] == 0xA0) {
Plugin_165_commandstate = 1;
}
break;
}
}
} else {
if (Plugin_165_commandstate == 1) {
if (bytes_read == 1) { // check if packet is valid
switch (Settings.TaskDevicePluginConfig[event->TaskIndex][0])
{
case SER_SWITCH_YEWE:
{
if (serial_buf[bytes_read] != 0xAA) {
Plugin_165_commandstate = 0;
bytes_read = 0;
}
break;
}
case SER_SWITCH_SONOFFDUAL:
{
if ((serial_buf[bytes_read] != 0x04) && (serial_buf[bytes_read] != 0x00)) {
Plugin_165_commandstate = 0;
bytes_read = 0;
}
break;
}
}
}
if ( (bytes_read == 2) && (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_SONOFFDUAL)) { // decode Sonoff Dual status changes
Plugin_165_ostate[0] = Plugin_165_switchstate[0]; Plugin_165_ostate[1] = Plugin_165_switchstate[1]; Plugin_165_ostate[2] = Plugin_165_switchstate[2];
Plugin_165_switchstate[0] = 0; Plugin_165_switchstate[1] = 0; Plugin_165_switchstate[2] = 0;
if ((serial_buf[bytes_read] & 1) == 1) {
Plugin_165_switchstate[0] = 1;
}
if ((serial_buf[bytes_read] & 2) == 2) {
Plugin_165_switchstate[1] = 1;
}
if ((serial_buf[bytes_read] & 4) == 4) {
Plugin_165_switchstate[2] = 1;
}
Plugin_165_commandstate = 2; bytes_read = 0;
if (Settings.TaskDevicePluginConfig[event->TaskIndex][1] == 1)
{ // exclusive on mode
if ((Plugin_165_ostate[0] == 1) && (Plugin_165_switchstate[1] == 1)) {
sendmcucommand(0, 0, Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
Plugin_165_switchstate[0] = 0;
}
if ((Plugin_165_ostate[1] == 1) && (Plugin_165_switchstate[0] == 1)) {
sendmcucommand(1, 0, Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
Plugin_165_switchstate[1] = 0;
}
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][1] == 2)
{ // simultaneous mode
if ((Plugin_165_ostate[0] + Plugin_165_switchstate[0]) == 1) {
sendmcucommand(1, Plugin_165_switchstate[0], Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
Plugin_165_switchstate[1] = Plugin_165_switchstate[0];
} else {
if ((Plugin_165_ostate[1] + Plugin_165_switchstate[1]) == 1) {
sendmcucommand(0, Plugin_165_switchstate[1], Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
Plugin_165_switchstate[0] = Plugin_165_switchstate[1];
}
}
}
log = F("SW : State ");
if (Plugin_165_ostate[0] != Plugin_165_switchstate[0]) {
UserVar[event->BaseVarIndex] = Plugin_165_switchstate[0];
log += F(" r0:");
log += Plugin_165_switchstate[0];
}
if (Plugin_165_ostate[1] != Plugin_165_switchstate[1]) {
UserVar[event->BaseVarIndex + 1] = Plugin_165_switchstate[1];
log += F(" r1:");
log += Plugin_165_switchstate[1];
}
if (Plugin_165_ostate[2] != Plugin_165_switchstate[2]) {
UserVar[event->BaseVarIndex + 2] = Plugin_165_switchstate[2];
log += F(" b2:");
log += Plugin_165_switchstate[1];
}
addLog(LOG_LEVEL_INFO, log);
if ( (Plugin_165_ostate[0] != Plugin_165_switchstate[0]) || (Plugin_165_ostate[1] != Plugin_165_switchstate[1]) || (Plugin_165_ostate[2] != Plugin_165_switchstate[2]) ) {
event->sensorType = SENSOR_TYPE_SWITCH;
sendData(event);
}
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE) { // decode Tuya/Yewelink status report package
if ((bytes_read == 3) && (serial_buf[bytes_read] != 7))
{
Plugin_165_commandstate = 0; // command code 7 means status reporting, we do not need other packets
bytes_read = 0;
}
if (bytes_read == 10) {
byte btnnum = (serial_buf[6] - 1);
Plugin_165_ostate[btnnum] = Plugin_165_switchstate[btnnum];
Plugin_165_switchstate[btnnum] = serial_buf[10];
Plugin_165_commandstate = 2; bytes_read = 0;
if (Plugin_165_ostate[btnnum] != Plugin_165_switchstate[btnnum]) {
log = F("SW : State");
switch (btnnum) {
case 0: {
if (Plugin_165_numrelay > 0) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" r0:");
log += Plugin_165_switchstate[btnnum];
}
}
case 1: {
if (Plugin_165_numrelay > 1) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" r1:");
log += Plugin_165_switchstate[btnnum];
}
}
case 2: {
if (Plugin_165_numrelay > 2) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" r2:");
log += Plugin_165_switchstate[btnnum];
}
}
}
event->sensorType = SENSOR_TYPE_SWITCH;
addLog(LOG_LEVEL_INFO, log);
sendData(event);
}
} //10th byte end (Tuya package)
} // yewe decode end
} // Plugin_165_commandstate 1 end
} // end of status decoding
if (Plugin_165_commandstate == 1) {
bytes_read++;
}
} else
Serial.read(); // if buffer full, dump incoming
}
} // plugin initialized end
success = true;
break;
}
case PLUGIN_READ:
{
if (Plugin_165_init)
{
if ((Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE) && (Plugin_165_commandstate != 1))
{ // check Tuya state if anybody ask for it
String log = F("SW : ReadState");
addLog(LOG_LEVEL_INFO, log);
getmcustate();
}
success = true;
}
break;
}
case PLUGIN_WRITE:
{
String log = "";
String command = parseString(string, 1);
byte rnum = 0;
byte rcmd = 0;
byte par3 = 0;
if (Plugin_165_init)
{
if ( command == F("relay") ) // deal with relay change command
{
success = true;
if ((event->Par1 >= 0) && (event->Par1 < Plugin_165_numrelay)) {
rnum = event->Par1;
}
if ((event->Par2 == 0) || (event->Par2 == 1)) {
rcmd = event->Par2;
}
LoadTaskSettings(Plugin_165_ownindex); // get our own task values please
event->TaskIndex = Plugin_165_ownindex;
byte varIndex = Plugin_165_ownindex * VARS_PER_TASK;
event->BaseVarIndex = varIndex;
if ( Plugin_165_globalpar0 < SER_SWITCH_LCTECH) {
par3 = Plugin_165_globalpar1;
}
sendmcucommand(rnum, rcmd, Plugin_165_globalpar0, par3);
if ( Plugin_165_globalpar0 > SER_SWITCH_YEWE) { // report state only if not Yewe
if (UserVar[(varIndex + rnum)] != Plugin_165_switchstate[rnum]) { // report only if state is really changed
UserVar[(varIndex + rnum)] = Plugin_165_switchstate[rnum];
if (( par3 == 1) && (rcmd == 1) && (rnum < 2))
{ // exclusive on mode for Dual
UserVar[(varIndex + 1 - rnum)] = 0;
}
if (par3 == 2) { // simultaneous mode for Dual
UserVar[(varIndex + 1 - rnum)] = Plugin_165_switchstate[1 - rnum];
}
event->sensorType = SENSOR_TYPE_SWITCH;
sendData(event);
}
}
String log = F("SW : SetSwitch r");
log += event->Par1;
log += F(":");
log += rcmd;
addLog(LOG_LEVEL_INFO, log);
}
}
break;
}
}
return success;
}
void getmcustate() {
Serial.write(0x55); // Tuya header 55AA
Serial.write(0xAA);
Serial.write(0x00); // version 00
Serial.write(0x08); // Tuya command 08 - request status
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x07);
Serial.flush();
}
void sendmcucommand(byte btnnum, byte state, byte swtype, byte btnum_mode) // btnnum=0,1,2, state=0/1
{
byte sstate;
switch (swtype)
{
case SER_SWITCH_YEWE:
{
Serial.write(0x55); // Tuya header 55AA
Serial.write(0xAA);
Serial.write(0x00); // version 00
Serial.write(0x06); // Tuya command 06 - send order
Serial.write(0x00);
Serial.write(0x05); // following data length 0x05
Serial.write( (btnnum + 1) ); // relay number 1,2,3
Serial.write(0x01); // ?
Serial.write(0x00); // ?
Serial.write(0x01); // ?
Serial.write( state ); // status
Serial.write((13 + btnnum + state)); // checksum:sum of all bytes in packet mod 256
Serial.flush();
break;
}
case SER_SWITCH_SONOFFDUAL:
{
Plugin_165_switchstate[btnnum] = state;
if (( btnum_mode == 1) && (state == 1) && (btnnum < 2))
{ // exclusive on mode
Plugin_165_switchstate[(1 - btnnum)] = 0;
}
if (btnum_mode == 2)
{ // simultaneous mode
Plugin_165_switchstate[0] = state;
Plugin_165_switchstate[1] = state;
}
sstate = Plugin_165_switchstate[0] + (Plugin_165_switchstate[1] << 1) + (Plugin_165_switchstate[2] << 2);
Serial.write(0xA0);
Serial.write(0x04);
Serial.write( sstate );
Serial.write(0xA1);
Serial.flush();
delay(1);
break;
}
case SER_SWITCH_LCTECH:
{
if (btnnum == 0) { // only one relay possible as i know
Plugin_165_switchstate[btnnum] = state;
for (byte x = 0; x < 2; x++) // try twice to be sure
{
Serial.write(0xA0);
Serial.write(0x01);
if (state == 0) { // off
Serial.write(0x00);
Serial.write(0xA1);
} else { // on
Serial.write(0x01);
Serial.write(0xA2);
}
Serial.flush();
delay(1);
}
break;
}
}
}
}