forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P165_SerSwitch.ino
753 lines (686 loc) · 30.5 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*##########################################################################################
######################### Plugin 165: Serial MCU controlled switch #######################
##########################################################################################
Features :
- make it possible to control serial linked devices through ESP8266
- exactly only ONE P165 plugin can be used one time on one device!
- serial have to be ENABLED, and serial logging level set to 0 at ESPEasy settings!
Compatible device list:
1/ Tuya Wifi Touch wall switch (originally controlled by Tuya Smart/Smart Life app)
2/ Tuya Wifi Dimmer Switch (originally controlled by Tuya Smart/Smart Life app)
3/ Sonoff Dual - v1 only! (R2 has no serial MCU!)
4/ LCTECH compatible 5V WiFi relay 1,2 and 4 relay versions also supported.
Relay states can be read from plugin values, the LCTech communication is only 1way, so the last stored state seen.
Tuya can report states and can be queried the actual state, Sonoff Dual may report its state, when it's hardware buttons pushed.
Support forum thread: https://www.letscontrolit.com/forum/viewtopic.php?f=6&t=3245
!!! For some reasons the serial 2way communication only works with Arduino ESP8266 core 2.4.0 !!!
List of commands :
- relay,[relay_number],[status] Set specific relay (0-3) to status (0/1)
- relaypulse,[relay_number],[status],[delay] Pulse specific relay for DELAY millisec with STATUS state,
than return to inverse state
- ydim,[DIM_VALUE] Set DIM_VALUE to Tuya dimmer switch (value can be 0-255, no range check!)
Of course, only the Tuya dimmer can do it... dim value can be read from plugin values.
There are no checks for is it state on or off.
Command Examples :
- /control?cmd=relay,0,1 Switch on first relay
- /control?cmd=relay,0,0 Switch off first relay
- /control?cmd=relay,1,1 Switch on second relay
- /control?cmd=relay,1,0 Switch off second relay
- /control?cmd=relaypulse,0,1,500 Set first relay to ON for 500ms, than stay OFF
- /control?cmd=relaypulse,0,0,1000 Set first relay to OFF for 1s, than stay ON
- /control?cmd=ydim,255 Set dimmer to MAX value
- /control?cmd=ydim,25 Set dimmer to ~10%
------------------------------------------------------------------------------------------
Copyleft Nagy Sándor 2018 - https://bitekmindenhol.blog.hu/
------------------------------------------------------------------------------------------
*/
#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 PLUGIN_VALUENAME4_165 "Relay3"
#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[4];
static byte Plugin_165_ostate[4];
byte Plugin_165_commandstate = 0; // 0:no,1:inprogress,2:finished
byte Plugin_165_type;
byte Plugin_165_numrelay = 1;
byte Plugin_165_ownindex;
byte Plugin_165_globalpar0;
byte Plugin_165_globalpar1;
byte Plugin_165_cmddbl = false;
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_QUAD;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 4;
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));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_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(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/Dimmer#2");
buttonOptions[2] = F("3/Dimmer#3");
int buttonoptionValues[3] = { 1, 2, 3 };
addFormSelector(F("Number of relays"), F("plugin_165_button"), 3, buttonOptions, buttonoptionValues, 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(F("Relay working mode"), F("plugin_165_mode"), 3, modeoptions, modeoptionValues, choice);
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_LCTECH)
{
choice = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String buttonOptions[4];
buttonOptions[0] = F("1");
buttonOptions[1] = F("2");
buttonOptions[2] = F("3");
buttonOptions[3] = F("4");
int buttonoptionValues[4] = { 1, 2, 3, 4 };
addFormSelector(F("Number of relays"), F("plugin_165_button"), 4, buttonOptions, buttonoptionValues, choice);
choice = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
String speedOptions[8];
speedOptions[0] = F("9600");
speedOptions[1] = F("19200");
speedOptions[2] = F("115200");
speedOptions[3] = F("1200");
speedOptions[4] = F("2400");
speedOptions[5] = F("4800");
speedOptions[6] = F("38400");
speedOptions[7] = F("57600");
addFormSelector(F("Serial speed"), F("plugin_165_speed"), 8, speedOptions, NULL, choice);
addFormCheckBox(F("Use command doubling"), F("plugin_165_dbl"), Settings.TaskDevicePluginConfig[event->TaskIndex][3]);
}
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"));
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_LCTECH)
{
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = getFormItemInt(F("plugin_165_button"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = getFormItemInt(F("plugin_165_speed"));
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = isFormItemChecked(F("plugin_165_dbl"));
Plugin_165_cmddbl = Settings.TaskDevicePluginConfig[event->TaskIndex][3];
}
Plugin_165_globalpar0 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_165_globalpar1 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
success = true;
break;
}
case PLUGIN_INIT:
{
String log = "";
LoadTaskSettings(event->TaskIndex);
Plugin_165_ownindex = event->TaskIndex;
Settings.UseSerial = true; // make sure that serial enabled
Settings.SerialLogLevel = 0; // and logging disabled
Serial.setDebugOutput(false); // really, disable it!
Serial.setRxBufferSize(BUFFER_SIZE); // Arduino core for ESP8266 WiFi chip 2.4.0
log = F("SerSW : Init ");
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_YEWE)
{
Plugin_165_numrelay = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
Serial.begin(9600, SERIAL_8N1);
Serial.setRxBufferSize(BUFFER_SIZE); // Arduino core for ESP8266 WiFi chip 2.4.0
delay(1);
getmcustate(); // request status on startup
log += F(" Yewe ");
log += Plugin_165_numrelay;
log += F(" btn");
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_SONOFFDUAL)
{
Plugin_165_numrelay = 3; // 3rd button is the "wifi" button
Serial.begin(19230, SERIAL_8N1);
log += F(" Sonoff Dual");
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == SER_SWITCH_LCTECH)
{
Plugin_165_numrelay = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
Plugin_165_cmddbl = Settings.TaskDevicePluginConfig[event->TaskIndex][3];
unsigned long Plugin_165_speed = 9600;
switch (Settings.TaskDevicePluginConfig[event->TaskIndex][2]) {
case 1: {
Plugin_165_speed = 19200;
break;
}
case 2: {
Plugin_165_speed = 115200;
break;
}
case 3: {
Plugin_165_speed = 1200;
break;
}
case 4: {
Plugin_165_speed = 2400;
break;
}
case 5: {
Plugin_165_speed = 4800;
break;
}
case 6: {
Plugin_165_speed = 38400;
break;
}
case 7: {
Plugin_165_speed = 57600;
break;
}
}
Serial.begin(Plugin_165_speed, SERIAL_8N1);
log += F(" LCTech ");
log += Plugin_165_speed;
log += F(" baud ");
log += Plugin_165_numrelay;
log += F(" btn");
}
Plugin_165_globalpar0 = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_165_globalpar1 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
switch (Plugin_165_numrelay)
{
case 1:
Plugin_165_type = SENSOR_TYPE_SWITCH;
break;
case 2:
Plugin_165_type = SENSOR_TYPE_DUAL;
break;
case 3:
Plugin_165_type = SENSOR_TYPE_TRIPLE;
break;
case 4:
Plugin_165_type = SENSOR_TYPE_QUAD;
break;
}
addLog(LOG_LEVEL_INFO, log);
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("SerSW : 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 = Plugin_165_type;
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) {
if (serial_buf[5] == 5) {
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("SerSW : 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];
}
break;
}
case 1: {
if (Plugin_165_numrelay > 1) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" r1:");
log += Plugin_165_switchstate[btnnum];
}
break;
}
case 2: {
if (Plugin_165_numrelay > 2) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" r2:");
log += Plugin_165_switchstate[btnnum];
}
break;
}
}
event->sensorType = Plugin_165_type;
addLog(LOG_LEVEL_INFO, log);
sendData(event);
}
}
} //10th byte end (Tuya switch)
if (bytes_read == 13) {
if (serial_buf[5] == 8) {
byte btnnum = (serial_buf[6] - 1);
Plugin_165_ostate[btnnum] = Plugin_165_switchstate[btnnum];
Plugin_165_switchstate[btnnum] = serial_buf[13];
Plugin_165_commandstate = 2; bytes_read = 0;
if (Plugin_165_ostate[btnnum] != Plugin_165_switchstate[btnnum]) {
log = F("SerSW : Dimmer");
switch (btnnum) {
case 1: {
if (Plugin_165_numrelay > 1) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" d1:");
log += Plugin_165_switchstate[btnnum];
}
break;
}
case 2: {
if (Plugin_165_numrelay > 2) {
UserVar[event->BaseVarIndex + btnnum] = Plugin_165_switchstate[btnnum];
log += F(" d2:");
log += Plugin_165_switchstate[btnnum];
}
break;
}
}
event->sensorType = Plugin_165_type;
addLog(LOG_LEVEL_INFO, log);
sendData(event);
}
}
} //13th byte end (Tuya dimmer)
} // 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("SerSW : 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 = Plugin_165_type;
sendData(event);
}
}
String log = F("SerSW : SetSwitch r");
log += rnum;
log += F(":");
log += rcmd;
addLog(LOG_LEVEL_INFO, log);
log = F("\nOk");
SendStatus(event->Source, log);
}
if ( command == F("relaypulse") )
{
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); // init state
delay(event->Par3);
sendmcucommand(rnum, !rcmd, Plugin_165_globalpar0, par3); // invert state
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 = Plugin_165_type;
sendData(event);
}
}
String log = F("SerSW : SetSwitchPulse r");
log += rnum;
log += F(":");
log += rcmd;
log += F(" Pulsed for ");
log += String(event->Par3);
log += F(" mS");
addLog(LOG_LEVEL_INFO, log);
log = F("\nOk");
SendStatus(event->Source, log);
}
if ( command == F("ydim") ) // deal with dimmer command
{
String log = F("SerSW : SetDim ");
if ( (Plugin_165_globalpar0 == SER_SWITCH_YEWE) && (Plugin_165_numrelay > 1)) { // only on tuya dimmer
success = true;
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;
sendmcudim(event->Par1);
log += event->Par1;
addLog(LOG_LEVEL_INFO, log);
log = F("\nOk");
} else {
log = F("\nNot supported");
}
SendStatus(event->Source, 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();
break;
}
case SER_SWITCH_LCTECH:
{
byte c_d = 1;
if (Plugin_165_cmddbl) {
c_d = 2;
}
Plugin_165_switchstate[btnnum] = state;
for (byte x = 0; x < c_d; x++) // try twice to be sure
{
if (x > 0) {
delay(1);
}
Serial.write(0xA0);
Serial.write((0x01 + btnnum));
Serial.write((0x00 + state));
Serial.write((0xA1 + state + btnnum));
Serial.flush();
}
break;
}
}
}
void sendmcudim(byte dimvalue)
{
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(0x08); // following data length 0x08
Serial.write(Plugin_165_numrelay); // dimmer order-id? select it at plugin settings 2/3!!!
Serial.write(0x02); // type=value
Serial.write(0x00); // length hi
Serial.write(0x04); // length low
Serial.write(0x00); // ?
Serial.write(0x00); // ?
Serial.write(0x00); // ?
Serial.write( dimvalue ); // dim value (0-255)
Serial.write( byte(19 + Plugin_165_numrelay + dimvalue) ); // checksum:sum of all bytes in packet mod 256
Serial.flush();
}