-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
4885 lines (4632 loc) · 178 KB
/
main.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
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
void ICACHE_FLASH_ATTR mqttpublish(int i)
{
// forma auxdesc=topic
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,"/");
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
if (i<15) strcat(auxdesc,idpin[i]);
else if (i>100) { strcat(auxdesc,letrar); strcat(auxdesc,itoa(i-100,buff,10)); }
// forma auxchar=valor
if (i<=2) { strcpy(auxchar,ftoa(MbR[i]/10,1)); } // sondas temperatura y consignas
else if (i==3) { strcpy(auxchar,ftoa((MbR[i]*10*conf.factorA[0])+conf.offsetA[0],2)); } // Ent. anal.
else if (i<=5)
if (conf.tipoED[i-4]==2) // DHT
{ strcpy(auxchar,ftoa(dhtdata[i-4][0]*10,1)); strcat(auxchar,grados);strcat(auxchar,barra);strcat(auxchar,ftoa(dhtdata[i-4][1]*10,2)); strcat(auxchar,porcen); }
else
{ strcpy(auxchar,itoa(getbit8(conf.MbC8,i-2),buff,10)); } // entradas digitales ON/OFF u OFF/ON
else if (i<=7) { strcpy(auxchar,itoa(getbit8(conf.MbC8,i-6),buff,10)); } // salidas digitales
else if (i==8) { strcpy(auxchar,itoa(conf.iddevice,buff,10)); } // iddevice
else if (i==9) { // IP privada
strcat(auxchar,itoa(WiFi.localIP()[0],buff,10));
for (byte j=1;j<4;j++) { strcat(auxchar,"."); strcat(auxchar,itoa(WiFi.localIP()[j],buff,10)); } }
else if (i==10) { strcpy(auxchar,conf.myippub); } // IP pública
else if ((i>=11)&&(i<=13)) { strcpy(auxchar,ftoa(conf.setpoint[i-11]*10,1)); } // consignas
else if (i>=100)
{
strcpy(auxchar,ftoa(bmp085.readTemperature()*10,1)); strcat(auxchar,grados);strcat(auxchar,barra);
strcat(auxchar,ftoa(bmp085.readPressure()/10,1)); strcat(auxchar,mbar);
}
PSclient.publish(auxdesc, auxchar);
strcpy(auxdesc,"");strcpy(auxchar,"");
}
void ICACHE_FLASH_ATTR pinVAL(byte n, byte value, byte ori)
{
if ((n==sdPin[0]) || (n==sdPin[1]))
if (getbit8(conf.MbC8,n-12)!=value)
{
digitalWrite(n, valorpin[value]);
setbit8(conf.MbC8,n-12,value);
setbit8(MbC8ant,n-12,value);
saveconf();
if (value) tempact[n-sdPin[0]]=millis()/1000;
else tempdes[n-sdPin[0]]=millis()/1000;
setbit8(iftttchange, n-12,1);
if (ori==conf.iddevice) { statusChange=true; mqttpublish(n-6); }
}
}
int ICACHE_FLASH_ATTR pinvalR(byte ip, int port, byte pin, byte valor) // ejecuta comando remoto
{
createhost(ip);
clearmsg();
msg+=barra;msg+=valor?on:off;msg+=interr;msg+=letrap;msg+=ig;msg+=itoa(pin+12,buff,10);
msg+=amper;msg+=letrar;msg+=ig;msg+=itoa(conf.iddevice, buff, 10);
return callhttpGET(host,port,false,conf.timeoutrem);
clearmsg();
}
int ICACHE_FLASH_ATTR mqttextraepin(char* topic, String command)
{
clearmsg();
for (byte i=0;i<strlen(topic);i++) msg+=topic[i];
String auxS="";
byte i=0; boolean encontrado=false;
while ((i<15) && (!encontrado))
{
auxS="/"; for (byte j=0;j<strlen(idpin[i]);j++) auxS+=idpin[i][j]; auxS+="/"; auxS+=command;
encontrado=(msg.indexOf(auxS)>0);
if (!encontrado) i++;
}
return encontrado?i:-1;
}
void ICACHE_FLASH_ATTR mqttpublishallvalues()
{
for (byte i=0;i<8;i++)
if (getbit8(conf.mqttsalenable,i)==1)
{ mqttpublish(i);
if (i<=2) mqttpublish(i+11);}
for (byte i=8;i<10;i++) { mqttpublish(i); }
for (byte i=0;i<maxsalrem;i++) { if (conf.idsalremote[i]>167) mqttpublish(i+100); } // BMP085/BMP180 T/P
}
void mqttcallback(char* topic, byte* payload, unsigned int length)
{
int auxb=mqttextraepin(topic,"set"); // tratamiento de "set"
if ((auxb>=0) && (auxb<=2)) // set a tn, cambia la consigna
{
clearmsg();
for (byte j=0; j<length;j++) msg+=(char)payload[j];
conf.setpoint[auxb]=msg.toFloat();
saveconf();
mqttpublish(auxb);
mqttpublish(auxb+11);
}
else if ((auxb>=6) && (auxb<=7)) // salidas relé
{
if ((char)payload[0]=='0') { pinVAL(auxb+6,0,0); }
if ((char)payload[0]=='1') { pinVAL(auxb+6,1,0); }
mqttpublish(auxb);
}
else if ((auxb>=11) && (auxb<=13)) // consigna
{
clearmsg();
for (byte j=0; j<length;j++) msg+=(char)payload[j];
conf.setpoint[auxb-11]=msg.toFloat();
saveconf();
mqttpublish(auxb-11);
mqttpublish(auxb);
}
////////////////////////////////////////
auxb=mqttextraepin(topic,"state"); // tratamiento de "state"
if ((auxb>=0) && (auxb<=13)) { mqttpublish(auxb); }
else if (auxb==14) { mqttpublishallvalues(); }
clearmsg();
}
void initMqtt()
{
PSclient.setServer(conf.mqttserver, 1883);
PSclient.setCallback(mqttcallback);
}
boolean ICACHE_FLASH_ATTR mqttreconnect()
{
mqttclientID="conuco-";
for (byte i=0;i<6;i++) mqttclientID += conf.EEmac[i];
if (PSclient.connect(mqttclientID.c_str()))
{ PSclient.publish("conuco/g","conectado"); }
return PSclient.connected();
}
void ICACHE_FLASH_ATTR mqttsubscribe(char* topic)
{
PSclient.subscribe(topic);
}
void ICACHE_FLASH_ATTR mqttsubscribevalues()
{
long tini=millis();
for (byte i=0;i<15;i++) // subscribe "state" para todos los valores
{
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,"/");
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
strcat(auxdesc,idpin[i]);
strcat(auxdesc,tstate);
PSclient.subscribe(auxdesc);
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,"/");
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
strcat(auxdesc,idpin[i]);
strcat(auxdesc,tset);
PSclient.subscribe(auxdesc);
}
strcpy(auxdesc,"");
}
void senddashtag(File f, int tag)
{ f.print(comillas); f.print(c(tag)); f.print(comillas); f.print(dp); }
void senddashint(File f, long data, boolean wcoma)
{ f.print(data); if (wcoma) f.print(coma); }
void senddashfloat(File f, float data, boolean wcoma)
{ f.print(comillas); f.print(data); f.print(comillas); if (wcoma) f.print(coma); }
void senddashbool(File f, boolean data, boolean wcoma)
{ if (data) f.print(c(ttrue)); else f.print(c(tfalse)); if (wcoma) f.print(coma); }
void senddashtext(File f, int ntext, boolean wcoma)
{ f.print(comillas); f.print(c(ntext)); f.print(comillas); if (wcoma) f.print(coma); }
void senddashtext(File f, PGM_P data, boolean wcoma)
{ f.print(comillas); f.print(data); f.print(comillas); if (wcoma) f.print(coma); }
void senddashlocal(File f, int descr, boolean wcoma)
{ f.print(comillas); f.print(readdescr(filedesclocal, descr, 20)); f.print(comillas); if (wcoma) f.print(coma); }
void senddashrem(File f, int descr, boolean wcoma)
{ f.print(comillas); f.print(readdescr(filesalrem, descr, 20)); f.print(comillas); if (wcoma) f.print(coma); }
void senddashpub(File f, int npin, boolean wcoma, PGM_P suf)
{
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,barra);
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
strcat(auxdesc,idpin[npin]); strcat(auxdesc,suf);
f.print(comillas); f.print(auxdesc); f.print(comillas); if (wcoma) f.print(coma);
}
void senddashpubrem(File f, byte rem, int npin, boolean wcoma, PGM_P suf)
{
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,barra);
for (byte j=1;j<6;j++)
{
if (strlen(conf.mqttpath[j])>0)
{
if (j==2) strcat(auxdesc,itoa(conf.idsalremote[rem],buff,10));
else strcat(auxdesc,conf.mqttpath[j]);
strcat(auxdesc,"/");
}
}
strcat(auxdesc,idpin[npin]); strcat(auxdesc,suf);
f.print(comillas); f.print(auxdesc); f.print(comillas); if (wcoma) f.print(coma);
}
// senddashi2c(f, i-11, conf.senalrem[i-11], true, vacio);
void senddashi2c(File f, byte rem, int npin, boolean wcoma, PGM_P suf)
{
strcpy(auxdesc,conf.mqttpath[0]); strcat(auxdesc,barra);
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
strcat(auxdesc,letrar); strcat(auxdesc,itoa(rem,buff,10)); strcat(auxdesc,suf);
f.print(comillas); f.print(auxdesc); f.print(comillas); if (wcoma) f.print(coma);
}
void senddashtap(File f, int npin, boolean wcoma, PGM_P suf)
{
strcpy(auxdesc,comillas);
strcat(auxdesc,"app.publish");
strcat(auxdesc,paren_i);strcat(auxdesc,comilla);
strcat(auxdesc,conf.mqttpath[0]); strcat(auxdesc,barra);
for (byte j=1;j<6;j++) { if (strlen(conf.mqttpath[j])>0) { strcat(auxdesc,conf.mqttpath[j]); strcat(auxdesc,"/"); } }
strcat(auxdesc,idpin[npin]); strcat(auxdesc,suf);
f.print(auxdesc);
f.print(comilla);f.print(coma);
f.print(c(eventdatalast01));f.print(comillas);
if (wcoma) f.print(coma);
}
void senddashrec(File f)
{
f.print(comillas);
f.print(c(ifeventdata));
f.print(c(evendatalast));
f.print(comillas);f.print(coma);
}
boolean primero=true;
void sendcomunes(File f, byte i)
{
if (!primero) f.print(coma);
primero=false;
f.print(llave_i);
senddashtag(f, dashenableIntermediateState); senddashbool(f, true, true);
senddashtag(f, dashenteredIntermediateStateAt); senddashint(f, 1, true);
senddashtag(f, dashintermediateStateTimeout); senddashint(f, 1, true);
senddashtag(f, dashqos); senddashint(f, 0, true);
senddashtag(f, dashretained); senddashbool(f, false, true);
senddashtag(f, dashjsOnReceive); senddashtext(f, vacio, true);
senddashtag(f, dashjsonPath); senddashtext(f, vacio, true);
senddashtag(f, dashlastActivity); senddashint(f, 0, true);
senddashtag(f, dashupdateLastPayloadOnPub); senddashbool(f, true, true);
senddashtag(f, dashjsBlinkExpression); senddashtext(f, vacio, true);
senddashtag(f, dashenablePub); senddashbool(f, true, true);
senddashtag(f, dashjsOnTap); senddashtext(f, vacio, true);
senddashtag(f, dashjsOnDisplay); senddashtext(f, vacio, true);
senddashtag(f, dashid); senddashint(f, i+1, true);
senddashtag(f, dashlongId); senddashint(f, i+1 , true);
}
void createdashfile()
{
/** File f=SPIFFS.open(filedash,letraw);
if (f)
{
primero=true;
f.print(corchete_i);
for (int i=0;i<maxsalrem+14;i++)
{
if (i<=3) { if (getbit8(conf.bshowbypanel[0],i)==1) sendcomunes(f,i); }
else if (i<=5) { if (conf.modo45==0) if (getbit8(conf.bshowbypanel[0],i)) sendcomunes(f,i); }
else if (i<=7) { if (getbit8(conf.bshowbypanel[0],i)) sendcomunes(f,i); }
else if (i<11) { sendcomunes(f,i); }
else if (i<14) { sendcomunes(f,i); }
else if ((conf.idsalremote[i-14]>=1) && (conf.idsalremote[i-14]<=31)) { } // modbus
else if ((conf.idsalremote[i-14]>=151) && (conf.idsalremote[i-14]<=167)) { sendcomunes(f,i); } // conucos
else if (conf.idsalremote[i-14]>0) { sendcomunes(f,i);}
if (i<8) // señales locales
{
if (i<=3)
{ if (getbit8(conf.bshowbypanel[0],i)==1) // sondas + ent. analógica
{
senddashtag(f, dashlastPayload); senddashfloat(f, 0.0, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, 0x334CFF, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true,(i<=2)?tset:vacio);
senddashtag(f, dashpostfix); senddashtext(f, i<=2?grados:vacio, true);
senddashtag(f, dashtype); senddashint(f, 1, true);
senddashtag(f, dashtopic); senddashpub(f, i, true, vacio);
senddashtag(f, dashname); senddashlocal(f, i, false);
f.print(llave_f);
}
}
else if (i<=5)
{
if (getbit8(conf.bshowbypanel[0],i)) // entradas digitales
{
if (conf.modo45==0)
{
if (conf.tipoED[i-4]>1) // DHT
{
senddashtag(f, dashlastPayload); senddashtext(f, cero, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, -192, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashpostfix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true, i<=5?tstate:tset);
senddashtag(f, dashtype); senddashint(f, 1, false);
f.print(llave_f);
}
else
{
senddashtag(f, dashlastPayload); senddashtext(f, cero, true);
senddashtag(f, dashoffcolor); senddashint(f, -1,true);
senddashtag(f, dashoncolor); senddashint(f, -192,true);
senddashtag(f, dashpayloadoff); senddashtext(f, cero,true);
senddashtag(f, dashpayloadon); senddashtext(f, uno,true);
senddashtag(f, dashiconoff); senddashtext(f, i<=5?ic_radio_button_unchecked:ic_settings_poweroff,true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true, i<=5?tstate:tset);
senddashtag(f, dashiconon); senddashtext(f, ic_radio_button_checked,true);
senddashtag(f, dashtype); senddashint(f, 2, true);
senddashtag(f, dashtopic); senddashpub(f, i, true, vacio);
senddashtag(f, dashname); senddashlocal(f, i, false);
f.print(llave_f);
}
}
}
}
else if (getbit8(conf.bshowbypanel[0],i)) // salidas digitales
{
senddashtag(f, dashlastPayload); senddashtext(f, cero, true);
senddashtag(f, dashoffcolor); senddashint(f, -1,true);
senddashtag(f, dashoncolor); senddashint(f, -192,true);
senddashtag(f, dashpayloadoff); senddashtext(f, cero,true);
senddashtag(f, dashpayloadon); senddashtext(f, uno,true);
senddashtag(f, dashiconoff); senddashtext(f, i<=5?ic_radio_button_unchecked:ic_settings_poweroff,true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true, i<=5?tstate:tset);
senddashtag(f, dashiconon); senddashtext(f, ic_settings_poweron,true);
senddashtag(f, dashtype); senddashint(f, 2, true);
senddashtag(f, dashtopic); senddashpub(f, i, true, vacio);
senddashtag(f, dashname); senddashlocal(f, i, false);
f.print(llave_f);
}
}
else if (i<11) // id, ip, ipp
{
senddashtag(f, dashlastPayload); senddashint(f, 0, true);
senddashtag(f, dashmainTextSize); senddashtext(f, small,true);
senddashtag(f, dashtextcolor); senddashint(f, -192, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true, tstate);
senddashtag(f, dashpostfix); senddashtext(f, vacio,true);
senddashtag(f, dashtype); senddashint(f, 1, true);
senddashtag(f, dashname); senddashtext(f, i==8?maindevice:i==9?localip:publicip,true);
senddashtag(f, dashtopic); senddashpub(f, i, false, vacio);
f.print(llave_f);
}
else if (i<14) // consignas
{
senddashtag(f, dashlastPayload); senddashfloat(f, 0.0, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, 233, true);
senddashtag(f, dashprefix); senddashtext(f, sphtm, true);
senddashtag(f, dashtopicPub); senddashpub(f, i, true,(i-1<=2)?tset:vacio);
senddashtag(f, dashpostfix); senddashtext(f, grados, true);
senddashtag(f, dashtype); senddashint(f, 1, true);
senddashtag(f, dashtopic); senddashpub(f, i, true, vacio);
senddashtag(f, dashname); senddashlocal(f, i-11, false);
f.print(llave_f);
}
else // señales remotas
{
if (conf.idsalremote[i-14] <=31) // modbus
{
}
else if ((conf.idsalremote[i-14] >=151) && (conf.idsalremote[i-14] <=167))
{
if (conf.senalrem[i-14]<=3)
{
senddashtag(f, dashlastPayload); senddashfloat(f, 0.0, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, -192, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashpubrem(f, i-14, conf.senalrem[i-14], true,(conf.senalrem[i-14]<=2)?tset:vacio); /////////////////////////// modificar
senddashtag(f, dashpostfix); senddashtext(f, vacio,true);
}
else if (conf.senalrem[i-14]<=5)
{
senddashtag(f, dashlastPayload); senddashfloat(f, 0.0, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, -192, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashpubrem(f, i-14, conf.senalrem[i-14], true,tstate); /////////////////////////// modificar
senddashtag(f, dashpostfix); senddashtext(f, vacio,true);
}
else
{
senddashtag(f, dashlastPayload); senddashtext(f, cero, true);
senddashtag(f, dashoffcolor); senddashint(f, -1,true);
senddashtag(f, dashoncolor); senddashint(f, -192,true);
senddashtag(f, dashpayloadoff); senddashtext(f, cero,true);
senddashtag(f, dashpayloadon); senddashtext(f, uno,true);
senddashtag(f, dashiconoff); senddashtext(f, conf.senalrem[i-14]<=5?ic_radio_button_unchecked:ic_settings_poweroff,true);
senddashtag(f, dashtopicPub); senddashpubrem(f, i-14, conf.senalrem[i-14], true, tset);
senddashtag(f, dashiconon); senddashtext(f, conf.senalrem[i-14]<=5?ic_radio_button_checked:ic_settings_poweron,true);
}
senddashtag(f, dashname); senddashrem(f, i-14, true);
senddashtag(f, dashtopic); senddashpubrem(f, i-14, conf.senalrem[i-14], true, vacio);
senddashtag(f, dashtype);
if (conf.senalrem[i-14]<=3) senddashint(f, 1, false);
else if (conf.senalrem[i-14]<=5) senddashint(f, tipoedremote[i-14]<=1?2:1, false);
else senddashint(f, 2, false);
f.print(llave_f);
}
else // resto, I2C, BMP085,etc
{
senddashtag(f, dashlastPayload); senddashtext(f, cero, true);
senddashtag(f, dashmainTextSize); senddashtext(f, medium,true);
senddashtag(f, dashtextcolor); senddashint(f, -192, true);
senddashtag(f, dashprefix); senddashtext(f, vacio, true);
senddashtag(f, dashpostfix); senddashtext(f, vacio, true);
senddashtag(f, dashtopicPub); senddashtext(f, vacio, true);
senddashtag(f, dashname); senddashrem(f, i-14, true);
senddashtag(f, dashtopic); senddashi2c(f, i-14, conf.senalrem[i-14], true, vacio);
senddashtag(f, dashtype); senddashint(f, 1, false);
f.print(llave_f);
}
}
}
f.print(corchete_f);
f.close();
}
if (conf.mqttenable)
{
File f=SPIFFS.open(filedash,"r");
if (f)
{
if (!PSclient.connected())
mqttreconnect();
if (PSclient.beginPublish("conuco/dash", f.size(), false))
{
char auxb[1];
for (int i=0;i<f.size();i++)
{
f.readBytes(auxb,1);
PSclient.write(auxb[0]);
}
PSclient.endPublish();
}
f.close();
}
} **/
}
void ICACHE_FLASH_ATTR testTX433()
{
mySwitch.send(conf.rfkeys.code[0], 24);
}
void ICACHE_FLASH_ATTR leevaloresDIG()
{
for (byte i=0;i<maxSD;i++) setbit8(conf.MbC8,i,digitalRead(sdPin[i]));
if (conf.modo45 == 0)
{
for (byte i=0;i<maxED;i++)
{
byte valaux=digitalRead(edPin[i]);
if (conf.tipoED[i]==0)
setbit8(conf.MbC8,i+2,valaux);
else if (conf.tipoED[i]==1)
if (valaux==0)
setbit8(conf.MbC8,i+2,1);
else
setbit8(conf.MbC8,i+2,0);
statusChange=((getbit8(conf.MbC8,i+2) != getbit8(MbC8ant,i+2)));
if (statusChange)
{
setbit8(iftttchange,i+2,1);
if (getbit8(conf.MbC8,i+2)==1) conf.contadores[i]++;
mqttpublish(i+4);
}
}
MbC8ant[0]=conf.MbC8[0];
}
}
void ICACHE_FLASH_ATTR leevaloresAN() { MbR[baseAna]=analogRead(A0); MbRant[baseAna]=MbR[baseAna]; }
void ICACHE_FLASH_ATTR leevaloresOW()
{
sensors1.requestTemperatures();
for (byte i=0; i<nTemp1; i++) {
int auxI=sensors1.getTempC(addr1Wire[i])*100;
// if ((auxI<=-10000) || (auxI=8500)) { addlog(3,auxI,"Temp error:"); return; }
MbR[i]=auxI;
MbRant[i]=MbR[i];
}
}
void ICACHE_FLASH_ATTR leevaloresDHT()
{
for (byte i=0;i<2;i++)
{
// delay(dht[i].getMinimumSamplingPeriod());
dhtdata[i][0]=dht[i].getTemperature();
dhtdata[i][1]=dht[i].getHumidity();
}
}
void ICACHE_FLASH_ATTR loginHTML()
{
printremote();
clearmsg();
if (server.method()==HTTP_POST)
{
if (server.hasArg("0") && server.hasArg("1"))
{
if ((server.arg(0)==conf.userDev) && (server.arg(1)==conf.passDev))
{ setCookie(1); return; }
}
}
if (server.hasArg("DISCONNECT")) { setCookie(0); return; }
writeHeader(false,false);
pc(body_i);
pc(form_action);
printP(loghtm,comillas,b);
pc(Form_post);
printP(menor,table,b);
printP(c(tclass), ig, tnormal, mayor);
printparCP(t(usuario), 0, conf.userDev, 20, false);
printparCP(t(contrasena), 1, (char *)"", 20, true);
printP(menor, barra, table, mayor);
printP(menor, c(tinput), b, type, ig, comillas);
printP(submit, comillas, b, tvalue, ig, comillas);
pt(guardar);
printP(comillas, mayor);
printP(menor, barra, c(tinput), mayor);
pc(form_f);
pc(body_f);
printP(menor, barra, thtml, mayor);
serversend200();
}
void ICACHE_FLASH_ATTR actualizamasters() // envia datos a masters, normalmente 1 pero podrían ser varios
{
statusChange=false;
for (byte i=0; i<maxdevrem; i++) { if (ListOri[i]>0) { sendJson(ListOri[i],88); } }
}
////////////////// DWEET ///////////////////
int ICACHE_FLASH_ATTR getdweet(char* key)
{
if (conf.mododweet==0) return 0;
clearmsg();
msg+=c(getlastdweett); msg+=conucochar;msg+=key;
return callhttpGET((char *)"Dweet.io",80,true,conf.timeoutNTP);
}
int ICACHE_FLASH_ATTR postIoTweet()
{
if (conf.iottweetenable==0) return 0;
float data[4]; //Your sending data variable.
data[0]=MbR[0]*0.01;
data[1]=sondaremote[3];
data[2]=sondaremote[5];
data[3]=getbit8(bstatremote, 2);
myiot.WriteDashboard(conf.iottweetuser, conf.iottweetkey, data[0], data[1], data[2], data[3], private_tweet, public_tweet);
}
int ICACHE_FLASH_ATTR postdweet(char* key)
{
if (conf.mododweet==0) return 0;
clearmsg();
buildjsonext();
strcpy(auxchar, c(dweetfor)); strcat(auxchar, conucochar); strcat(auxchar, key);
HTTPClient http;
http.begin(c(dweetio), 80, auxchar);
http.addHeader(type, tPOST);
http.addHeader(contenttype, applicationjson);
http.addHeader(dataType, json);
http.setTimeout(conf.timeoutNTP);
int httpCode = http.POST(msg);
http.end();
clearmsg();
return httpCode;
}
////////////////// IFTTT ///////////////////
int ICACHE_FLASH_ATTR ifttttrigger(char *evento, char* key, char* value1, char* value2, char* value3)
{
if (conf.iftttenable == 0) return 0;
strcpy(auxchar, "/trigger/"); strcat(auxchar, evento); // value1, 2 y 3 tamaño máximo = 20
strcat(auxchar, "/with/key/"); strcat(auxchar, key); strcat(auxchar, interr);
strcat(auxchar, tvalue); strcat(auxchar, uno); strcat(auxchar, ig); strcat(auxchar, value1); strcat(auxchar, ampersand);
strcat(auxchar, tvalue); strcat(auxchar, dos); strcat(auxchar, ig); strcat(auxchar, value2); strcat(auxchar, ampersand);
strcat(auxchar, tvalue); strcat(auxchar, tres); strcat(auxchar, ig); strcat(auxchar, value3);
byte i=0;
while (auxchar[i]!='\0') { if (auxchar[i]==' ') auxchar[i]='_'; i++; }
HTTPClient http;
// Serial.print("ifttttrigger:"); Serial.print(c(makeriftttcom)); Serial.print(barra); Serial.print(auxchar);
http.begin(c(makeriftttcom), 80, auxchar);
http.setTimeout(conf.timeoutNTP);
int httpCode=http.GET();
// Serial.print(" "); Serial.println(httpCode>=0?"OK":"ERROR");
if (httpCode<0) addlog(2, httpCode, c(ifttt));
http.end();
return httpCode;
}
////////////////// END IFTTT ///////////////////
int ICACHE_FLASH_ATTR getMyIP()
{
clearmsg();
printP(barra);
HTTPClient http;
http.begin(conf.hostmyip, 80, msg);
http.setTimeout(conf.timeoutNTP);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) { msg=http.getString(); msg.toCharArray(conf.myippub, msg.length()); } }
http.end();
clearmsg();
return httpCode;
}
int ICACHE_FLASH_ATTR checkMyIP()
{
char auxip[16];
strcpy(auxip, conf.myippub);
getMyIP(); // busca actual y la guarda en conf.txt
if (strcmp(conf.myippub, auxip) != 0) // son diferentes
{
saveconf();
if (conf.iftttenable) ifttttrigger(conucochar, conf.iftttkey, conf.aliasdevice, (char *)"NewIP", conf.myippub);
}
}
void ICACHE_FLASH_ATTR setactivarem(byte num, boolean estado)
{
actirem[num] = estado;
for (byte j=0; j<maxsalrem; j++) if (conf.idsalremote[j]==conf.idremote[num]) actisenal[j] = estado;
}
int ICACHE_FLASH_ATTR actualizaremotos() // pide datos a remotos
{
int auxerr = 0;
for (byte i=0; i<maxdevrem; i++)
{
if ((conf.idremote[i] < 150) || (conf.idremote[i] > 166))
{
strcpy (auxdesc, vacio); savedescr(fileidmyjsonrem, auxdesc, i, 10);
strcpy (auxdesc, vacio); savedescr(filemacdevrem, auxdesc, i, 14);
}
else
{
if (actirem[i])
{
auxerr = ReqJson(conf.idremote[i], 88);
if (auxerr >= 0) { parseJsonremoto(); } // pone los valores en la variable "datosremoto" y en el remoto correspondiente
else { timerem[i]++; if (timerem[i] >= maxerrorrem) { setactivarem(i, false); timerem[i] = 0; } }
}
else { timerem[i]++; if (timerem[i] >= 1) { setactivarem(i, true); timerem[i] = 9; } }
}
}
return auxerr;
}
void ICACHE_FLASH_ATTR onescena(byte nesc)
{
int auxerr;
for (byte j = 0; j < maxSD; j++) // Salidas digitales locales
if (getbit8(conf.bescenaact[nesc], j))
pinVAL(sdPin[j], getbit8(conf.bescena[nesc], j), conf.iddevice);
for (byte i = 0; i < maxsalrem; i++) // para cada salida remota
{
onescenaact = true;
if (conf.idsalremote[i] > 0)
if (conf.senalrem[i] >= 6)
if (getbit8(conf.bescenaact[nesc], i + 2) == 1)
{
byte auxest = getbit8(conf.bescena[nesc], i + 2);
auxerr = pinvalR(conf.idsalremote[i], 88, conf.senalrem[i]-6, auxest);
if ((auxerr == 200) || (auxerr == 303) || (auxerr == (-11))) {
setbit8(bstatremote, i, auxest);
contaremote[i] = 0; }
}
}
}
void ICACHE_FLASH_ATTR onCmd()
{
if (autOK())
if (server.args() > 0)
{
byte auxi=server.arg(0).toInt();
if ((auxi==sdPin[0]) || (auxi==sdPin[1]))
pinVAL(auxi, 1, server.arg(1).toInt()); // tercer parámetro es el Id ori que lo pide
else if (auxi <= 200)
onescena(auxi - 100);
}
sendOther(panelhtm, panelact);
}
void ICACHE_FLASH_ATTR offCmd()
{
if (autOK())
if (server.args() > 0)
{
byte auxi=server.arg(0).toInt();
if ((auxi==sdPin[0]) || (auxi==sdPin[1])) pinVAL(auxi, 0, server.arg(1).toInt());
}
sendOther(panelhtm, panelact);
}
void ICACHE_FLASH_ATTR htmlNotFound()
{
clearmsg();
printP(c(HTTP11), b);
printP(c(t404), b);
printP(c(pagenotfound), crlf);
serversend404();
clearmsg();
}
void procesaeventos()
{
for (byte i=0; i<nEVE; i++) // número de condición, de 0 a 7
{
if (((conf.actPrg[0]) && (getbit8(conf.bPRGeve[i], 0))) ||
((conf.actPrg[1]) && (getbit8(conf.bPRGeve[i], 1)))) // si algún programa activo en la condición
{
if (conf.condact[i] < 150) // activadora entrada o salida digital (0-1)
{
boolean cumple=false;
if (conf.condact[i]<=1) cumple=(getbit8(conf.MbC8, conf.condact[i]+ 2)==conf.condvalD[i]);
// if ((!modo45) && (conf.condact[i]<=1)) cumple=(getbit8(conf.MbC8,conf.condact[i]+2) == condvalD[i]);
if ((conf.condact[i] >= 2) && (conf.condact[i] <= 3)) cumple = (valorpin[getbit8(conf.MbC8, conf.condact[i] - 2)] == conf.condvalD[i]);
if (conf.condact[i] >= 100) cumple=getbit8(bstatremote, conf.condact[i] - 100);
if (cumple) // si valor pin = valor en condición // CUMPLE
{
if (conf.evensal[i] <= 3) // señal local o escena
{
pinVAL(sdPin[conf.evensal[i]], getbit8(conf.bevenniv, i), conf.iddevice); // señal local
}
else if (conf.evensal[i] <= 200) // señal remota
{
if (getbit8(conf.bconactmode, i) == 0) // modo ESTADO
{
int auxerr = pinvalR(conf.idsalremote[conf.evensal[i] - 4], 88, conf.pinremote[conf.evensal[i] - 4], getbit8(conf.bevenniv, i));
if ((auxerr == 200) || (auxerr == 303) || (auxerr == (-11)))
{
setbit8(bstatremote, conf.evensal[i] - 4, getbit8(conf.bevenniv, i));
contaremote[conf.evensal[i] - 4] = 0;
}
else
{ Serial.print(c(tpinvalr)); Serial.println(auxerr); }
}
else // modo CAMBIO
{
if (getbit8(bconactcumple, i) == 0) // antes no cumplía
pinVAL(sdPin[conf.evensal[i]], getbit8(conf.bevenniv, i), conf.iddevice); // señal local
}
}
else if (conf.evensal[i]==despIFTTT) // mandar notificación
{
if (conf.iftttenable)
if (getbit8(bevenENABLE[0],i)==1)
{
if (ifttttrigger(conucochar, conf.iftttkey, conf.aliasdevice, itoa(conf.condact[i],buff,10), (char *)"testdig")==200)
{ setbit8(bevenENABLE[0],i,0); }
}
}
setbit8(bconactcumple, i, 1);
} // fin CUMPLE
else // no CUMPLE
{
setbit8(bconactcumple, i, 0);
} // fin no CUMPLE
} // fin activadora digital Local
else // activadora analógica o sonda (>=150)
{
float vact;
if (conf.condact[i] == 150) vact = float(MbR[3]); // entradas analógicas locales
else if (conf.condact[i] < 180) vact = float(sondaremote[conf.condact[i] - 160]); // entradas analógicas remotas
else if (conf.condact[i] < 200) vact = float(MbR[conf.condact[i] - 180] / 100); // temperaturas locales
else if (conf.condact[i] < 220) vact = float(sondaremote[conf.condact[i] - 200] / 100); // temperaturas remotas
else if (conf.condact[i] < 250) vact = float(mbtemp[conf.condact[i] - 220] / 100); // temperaturas modbus
// else if (condact[i]==254)
float tcomp = float(conf.evenvalA[i]);
if (((conf.evencomp[i] == 0) && (vact >= tcomp)) || ((conf.evencomp[i] != 0) && (vact <= tcomp)))
{ // CUMPLE
if (getbit8(conf.bconsaltipo, i) == 0) // señal de salida local
{
if (conf.evensal[i] <= 3)
{
pinVAL(sdPin[conf.evensal[i]], getbit8(conf.bevenniv, i), conf.iddevice); // señal local
}
else if (conf.evensal[i] <= 200)
{
int auxerr = pinvalR(conf.idsalremote[conf.evensal[i] - 4], 88, conf.pinremote[conf.evensal[i] - 4], getbit8(conf.bevenniv, i));
if ((auxerr == 200) || (auxerr == 303) || (auxerr == (-11)))
{
setbit8(bstatremote, conf.evensal[i] - 4, getbit8(conf.bevenniv, i));
contaremote[conf.evensal[i] - 4] = 0;
}
else
{ Serial.print(c(tpinvalr)); Serial.println(auxerr); }
}
else if (conf.evensal[i]==despIFTTT)
{
if (conf.iftttenable)
if (getbit8(bevenENABLE[1],i)==1)
{
if(ifttttrigger(conucochar, conf.iftttkey, conf.aliasdevice, itoa(conf.condact[i],buff,10), (char *)"testana")==200)
{ setbit8(bevenENABLE[1],i,0); }
}
}
}
setbit8(bconactcumple, i, 1);
}
else
{ // NO CUMPLE
setbit8(bconactcumple, i, 0);
}
}
}
}
}
void ICACHE_FLASH_ATTR termostatoHTML() {
printremote();
boolean conectado = autOK();
clearmsg();
if (server.args() > 0)
{
if (server.argName(0)=="up") {
conf.evenvalA[0] = constrain(conf.evenvalA[0] + 0.5, 5, 35);
conf.evenvalA[1] = conf.evenvalA[0] + 1.0;
}
else if (server.argName(0)=="dw") {
conf.evenvalA[0] = constrain(conf.evenvalA[0] - 0.5, 5, 35);
conf.evenvalA[1] = conf.evenvalA[0] + 1.0;
}
saveconf();
procesaeventos();
sendOther(termhtm,-1);
return;
}
writeHeader(false,false);
printP(c(body_i), menor, barra);
printP(table, mayor);
printP(menor, table, b);
printP(c(tclass), ig, tmenu);
printP(mayor);
// TEMPERATURA
byte val = getbit8(conf.MbC8, 0);
printP(tr);
printColspan(2);
pc(center_i);
printP(t(Modo), b);
pt(termostato);
printP(c(center_f), td_f);
printP(td, c(center_i));
printP(c(fontsize6), val == 0 ? "☼" : "☀");
pc(font_f);
pc(center_f);
printP(td_f, tr_f, tr);
printColspan(3);
pc(center_i);
printP(c(fontsize_i), ig,siete, mayor);
printF(MbR[0] * 0.01, 2);
pc(font_f);
printP(c(fontsize_i), ig,cinco, mayor, b, celsius);
pc(font_f);
printP(c(center_f), td_f, tr_f);
// SALIDA DIGITAL
printP(tr, td, c(center_i));
printP(href_i, comillas, termhtm);
printP(interr, letrad, letraw, ig, uno, comillas);
printP(href_f);
printP(c(fontsize_i), ig,cinco, mayor);
printP(downs);
pc(font_f);
pc(center_f);
printP(td_f, td, c(center_i));
printP(c(fontsize_i), ig,tres, mayor);
printF(conf.evenvalA[0], 1);
printP(c(font_f), b, celsius);
printP(c(center_f), td_f);
printP(td, c(center_i));
printP(href_i, comillas, termhtm);
printP(interr, letrau, letrap, ig, uno, comillas);
printP(href_f, c(fontsize_i), ig,cinco, mayor);
printP(ups);
pc(font_f);
printP(c(center_f), td_f, tr_f);
// final
printP(tr);
printColspan(2);
printI(day()); printPiP(barra, month(), barra); printI(year());
printP(b);
printhora();
printP(td_f, td, href_i, comillas);
printP(barra, panelhtm, comillas, href_f, c(panel), td_f);
printP(tr_f, menor, barra, table, mayor);
printP(c(body_f), menor, barra);
printP(thtml, mayor);
serversend200();
onescenaact = false;
}
void HtmlGetStateIn(byte ind)
{
colorea=(getbit8(conf.MbC8, ind+2)==1);
printP(colorea?th:td,c(resetcontp));
printI(ind);
printP(comillas,mayor,readdescr(filedesclocal,ind+4,20),href_f);
printP(colorea?th_f:td_f);
cell(conf.contadores[ind]);
}
void HtmlGetStateOut(byte ind)
{
colorea=(getbit8(conf.MbC8, ind) == 1);
unsigned long segundos = (millis()/1000)-(colorea?tempact[ind]:tempdes[ind]);
printP(colorea?th:td, href_i,comilla, colorea?off:on,interr, letrap);
printP(ig);
printI(ind+12);
printP(amper,letran,ig);
printI(conf.iddevice);
printP(comilla, mayor,readdescr(filedesclocal,ind+6,20),href_f,colorea?th_f:td_f,td);
printtiempo(segundos);
printP(td_f);
}
void handleStateTime() { clearmsg(); HtmlGetStateTime(); serversend200(); }
void handleStateIn(int ind) { clearmsg(); HtmlGetStateIn(ind); serversend200(); }
void handleState0In() { handleStateIn(0); }
void handleState1In() { handleStateIn(1); }
void handleStateOut(int ind) { clearmsg(); HtmlGetStateOut(ind); serversend200(); }
void handleState0Out() { handleStateOut(0); }
void handleState1Out() { handleStateOut(1); }
void HtmlGetStateR(byte rem)
{
colorea = getbit8(bstatremote, rem);
unsigned long segundos = (millis()/1000)-(contaremote[rem]);
printP(colorea?th:td, href_i, comilla);
printP(syhtm, interr, letrar, ig);
printI(rem);
printP(amper, colorea?c(roff):c(ron), ig, cero);
printP(comilla, mayor, readdescr(filesalrem, rem, 20));
printP(href_f, colorea?th_f:td_f, td);
printtiempo(contaremote[rem]);
printP(td_f);
}
void handleStater(int i) { clearmsg(); HtmlGetStateR(i); serversend200(); }
void handleStater0() { handleStater(0); }
void handleStater1() { handleStater(1); }
void handleStater2() { handleStater(2); }
void handleStater3() { handleStater(3); }
void handleStater4() { handleStater(4); }
void handleStater5() { handleStater(5); }
void handleStater6() { handleStater(6); }
void handleStater7() { handleStater(7); }
void handleStater8() { handleStater(8); }
void handleStater9() { handleStater(9); }
void handleStater10() { handleStater(10); }
void handleStater11() { handleStater(11); }
void handleStater12() { handleStater(12); }
void handleStater13() { handleStater(13); }
void handleStater14() { handleStater(14); }
void handleStater15() { handleStater(15); }
void voicecommandHTML()
{
printremote();
boolean conectado = (autOK());
clearmsg();
if (server.args()==2) // parámetro 1, a=0/1 apagar/encender, parámetro 2, t= nombre señal o escena
{
if ((server.argName(0).compareTo(letraa)==0) && (server.argName(1).compareTo(letrat)==0))
{
// server.arg(1).toLowerCase();
server.arg(1).toCharArray(auxchar, sizeof(auxchar));
byte i=6; boolean encontrado = false; // buscar en salidas locales
while ((i<=7) && (!encontrado))
{
clearmsg();