-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP_sonoff_Schaltuhr.ino
1246 lines (1039 loc) · 34.7 KB
/
ESP_sonoff_Schaltuhr.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
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
/*
getestet mit: Sonoff, WIFI Smart Socket, horsky
115200 Baud
ESP8266, DOUT, 115200, 1M(64k SPIFFS), 80MHz
VID: 10C4
PID: EA60
*/
/*
TODO:
-PowerOnState ->per Datei?
-wlan/ssid-Setup per AP-Mode ->siehe esp32-32x32rgb-matrix/setINI (SSL ?)
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <time.h>
//https://github.com/xoseperez/hlw8012
//or CSE7759
#include <HLW8012.h>
#include <JeVe_EasyOTA.h> // https://github.com/jeroenvermeulen/JeVe_EasyOTA/blob/master/JeVe_EasyOTA.h
#include "FS.h"
#include "myNTP.h"
myNTP oNtp;
#include "data.h" //index.htm
const char* progversion = "WLan-Timer V2.5";//ota fs ntp ti getpin HLW8012
//-----------------------------default------------------------------------------------------
#define ARDUINO_HOSTNAME "Schalter"
#define buttMode INPUT
#define pin_ledinvert true //false=on
#define pin_buttoninvert true
#define pin_relaisinvert false //true=on
#define pin_led2 -1
#define pin_led2invert true //false=on
#define pin_ledWLAN -1
#define pin_ledWLANinvert true //false=on
//für HLW8012 current sensor Sonoff POW
#define SEL_PIN -1 //-= kein
#define CF1_PIN -1
#define CF_PIN -1
//hlw8012 / CSE7759 setup
#define CURRENT_MODE HIGH
#define CURRENT_RESISTOR 0.001
#define VOLTAGE_RESISTOR_UPSTREAM ( 5 * 470000 ) // Real: 2280k
#define VOLTAGE_RESISTOR_DOWNSTREAM ( 1000 ) // Real 1.009k
#define hlw_current_multiplier 144844.93
#define hlw_voltage_multiplier 408636.51
#define hlw_power_multiplier 10343611.74
//----------------------------------------------------------------------------------------------
/*
#define ARDUINO_HOSTNAME "sonoffpow" //http://sonoffpow.wg
#define pin_relais 12 //red+relais true=on
#define pin_led 15 //blue
#define pin_Button 0 //Button LOW=down
#define pin_ledinvert false // true=on
//https://github.com/erniberni/ESP_pow/blob/master/ESP_pow_with_OTA.ino
//https://github.com/xoseperez/hlw8012
#define SEL_PIN 5
#define CF1_PIN 13
#define CF_PIN 14
*/
/*
#define ARDUINO_HOSTNAME "sonoffs20" //V2.5 http://sonoffs20.wg ->anlage
//#define ARDUINO_HOSTNAME "dose2" //V2.5 http://dose2.wg 32x32
//#define ARDUINO_HOSTNAME "lampe" //V2.5 http://lampe.wg (dose3)
#define pin_relais 12 //blue+relais true=on
#define pin_led 13 //green false=on
#define pin_Button 0 //Button LOW=down
*/
/*
#define ARDUINO_HOSTNAME "horsky"// V2.5 ->usb-Strom
#define pin_relais 5 //red+relais true=on
#define pin_led 4 //blue false=on
#define pin_Button 13 //Button LOW=down
#define buttMode INPUT_PULLUP //!
*/
/*
#define ARDUINO_HOSTNAME "luminea"//pearl V2.5
#define pin_relais 15 //relais
#define pin_led 2 //green
#define pin_Button 13 //Button
*/
/**/
#define ARDUINO_HOSTNAME "oxaoxe"// V2.5
#define pin_relais 12 //relais
#define pin_led 0 //LED rot
#define pin_Button 13 //Button
#define pin_led2 15 //LED blau
#define pin_led2invert true //false=on
#define pin_ledWLAN 4 //LED blau WLAN
#define pin_ledWLANinvert true //false=on
#define SEL_PIN 3 //strom/voltsensor
#define CF1_PIN 14
#define CF_PIN 5
#define hlw_current_multiplier 555.35 //571.90 ->7.73A x->8.130A(8) A=W/V
#define hlw_voltage_multiplier 17066.45 //16101.83 -> 218V x->231 x=17066.45 V=W/A
#define hlw_power_multiplier 129437.79 //122724.46 -> 1879W x->1849.1 x=129437,79 W=V*A
//----------------------------------------------------------------------------------------------
//#define WIFI_SSID ""
//#define WIFI_PASSWORD ""
#include "wifisetup.h" //fest eingebunden
//----------------------------------------------------------------------------------------------
#define TIMERtxt "/timer.txt"
unsigned long butt_zeitchecker= 120;//ms min presstime
unsigned long butt_previousMillis=0;
unsigned long buttpresstime=0;
unsigned long tim_zeitchecker= 15*1000;//alle 15sec Timer checken
unsigned long tim_previousMillis=0;
byte last_minute;
#define UPDATE_TIMEHLW8012 2000 //alle 2 Sekunden messen
unsigned long UPDATE_TIMEHLW8012_previousMillis=0;
#define check_wlanasclient 30000 //alle 30 Sekunden*2 gucken ob noch verbunden, wenn nicht neuer Versuch
//zwischen 30 und 60 Sekunden
unsigned long check_wlanasclient_previousMillis=0;
#define anzahlVersuche 10 //nach 10 Versuchen im AP-Modus bleiben
#define keinAPModus true //true=immer wieder versuchen ins WLAN zu kommen
#define actionheader "HTTP/1.1 303 OK\r\nLocation:/index.htm\r\nCache-Control: no-cache\r\n\r\n"
uint8_t MAC_array[6];
char MAC_char[18];
String macadresse="";
EasyOTA OTA;
ESP8266WebServer server(80);
File fsUploadFile; //Haelt den aktuellen Upload
HLW8012 hlw8012;
bool isAPmode=false;
int anzahlVerbindungsversuche=0;
//---------------------------------------------
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
//---------------------------------------------
void setLED(bool an){
if(pin_ledinvert)an=!an;
digitalWrite(pin_led, an);
}
void toogleLED(){
digitalWrite(pin_led, !digitalRead(pin_led));
}
bool getLED(){
if(pin_ledinvert){
return digitalRead(pin_led)==LOW;
}
else{
return digitalRead(pin_led)==HIGH;
}
}
void setLED2(bool an){
if(pin_led2>-1){
if(pin_led2invert)an=!an;
digitalWrite(pin_led2, an);
}
}
void toogleLED2(){
if(pin_led2>-1)
digitalWrite(pin_led2, !digitalRead(pin_led2));
}
bool getLED2(){
if(pin_led2invert){
return digitalRead(pin_led2)==LOW;
}
else{
return digitalRead(pin_led2)==HIGH;
}
}
void setLEDWLAN(bool an){
if(pin_ledWLAN>-1){
if(pin_ledWLANinvert)an=!an;
digitalWrite(pin_ledWLAN, an);
}
}
void toogleLEDWLAN(){
if(pin_ledWLAN>-1)
digitalWrite(pin_ledWLAN, !digitalRead(pin_ledWLAN));
}
bool getLEDWLAN(){
if(pin_ledWLANinvert){
return digitalRead(pin_ledWLAN)==LOW;
}
else{
return digitalRead(pin_ledWLAN)==HIGH;
}
}
void setRelais(bool an){
if(pin_relaisinvert)an=!an;
digitalWrite(pin_relais, an);
setLED2(an);
}
void toogleRelais(){
digitalWrite(pin_relais, !digitalRead(pin_relais));
setLED2(digitalRead(pin_relais));
}
bool getRelay(){
if(pin_relaisinvert){
return digitalRead(pin_relais)==LOW;
}
else{
return digitalRead(pin_relais)==HIGH;
}
}
bool getButton(){
if(pin_buttoninvert){
return digitalRead(pin_Button)==LOW;
}
else{
return digitalRead(pin_Button)==HIGH;
}
}
//---------------------------------------------
void connectWLAN(){
setLED(true);
setLED2(false);
setLEDWLAN(false);
anzahlVerbindungsversuche++;
OTA.setup(WIFI_SSID, WIFI_PASSWORD, ARDUINO_HOSTNAME);//connect to WLAN
isAPmode=!(WiFi.status() == WL_CONNECTED);
Serial.print("mode: ");
if(isAPmode)
Serial.println("AP");
else
Serial.println("client");
macadresse="";
WiFi.macAddress(MAC_array);
for (int i = 0; i < sizeof(MAC_array); ++i) {
if(i>0) macadresse+=":";
macadresse+= String(MAC_array[i], HEX);
}
Serial.print("MAC: ");
Serial.println(macadresse);
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if(isAPmode){
setLED(true);
setLEDWLAN(true);
delay(500);
setLED(false);
setLEDWLAN(false);
delay(250);
setLED(true);
setLEDWLAN(true);
delay(500);
setLED(false);
setLEDWLAN(false);
delay(250);
setLED(true);
setLEDWLAN(true);
delay(500);
setLED(false);
setLEDWLAN(false);
delay(250);
}
else{
anzahlVerbindungsversuche=0;//erfolgreich verbunden, Zaehler auf 0 setzen
setLED(false);
setLED2(false);
setLEDWLAN(false);
}
}
void setup() {
//serial
Serial.begin(115200);
Serial.println("");
Serial.println("");
Serial.println(progversion);
//SPIFFS
SPIFFS.begin();
//Pins initialisieren
pinMode(pin_relais, OUTPUT);
pinMode(pin_led, OUTPUT);
pinMode(pin_Button, buttMode);
//oxaoxe
if(pin_led2>-1){
pinMode(pin_led2, OUTPUT);
}
if(pin_ledWLAN>-1){
pinMode(pin_ledWLAN, OUTPUT);
}
//Power ON:
setRelais(false);//relay off
setLED(true); //LED on
setLED2(false);
setLEDWLAN(false);
//Initialize HLW8012/ CSE7759
if(SEL_PIN>-1){
hlw8012.begin(CF_PIN, CF1_PIN, SEL_PIN, CURRENT_MODE, false, 500000); //500000microseconds
hlw8012.setCurrentMultiplier(hlw_current_multiplier);
hlw8012.setVoltageMultiplier(hlw_voltage_multiplier);
hlw8012.setPowerMultiplier(hlw_power_multiplier);
//hlw8012.setResistors(CURRENT_RESISTOR, VOLTAGE_RESISTOR_UPSTREAM, VOLTAGE_RESISTOR_DOWNSTREAM);
//0.001,( 5 * 470000 ),( 1000 )
//_current_multiplier = 144844.93
//_voltage_multiplier = 408636.51
//_power_multiplier = 10343611.74
}
//OTA
OTA.onMessage([](char *message, int line) {
toogleLED();
setLED2(!getLED());
toogleLEDWLAN();
Serial.println(message);
});
connectWLAN();
server.on("/action", handleAction);//daten&befehle
server.on("/",handleIndex);
server.on("/index.htm", handleIndex);
server.on("/index.html", handleIndex);
server.on("/data.json", handleData);//aktueller Status+Dateiliste
server.on("/upload", HTTP_POST, []() {
server.send(200, "text/plain", "");
}, handleFileUpload); //Dateiupload
server.onNotFound(handleNotFound);//Datei oder 404
server.begin();
Serial.println("HTTP server started");
//info-LED aus
setLED(false);
setLED2(false);
setLEDWLAN(false);
//digitalWrite(pin_led, true);
Serial.println("ready.");
//NTP start
oNtp.begin();
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
OTA.loop();
oNtp.update();
isAPmode=!(WiFi.status() == WL_CONNECTED);
unsigned long currentMillis = millis();//milliseconds since the Arduino board began running
//Button
if( currentMillis - butt_previousMillis > butt_zeitchecker){//alle 120ms
butt_previousMillis=currentMillis;
if (!getButton()) {//up
if( buttpresstime>0){
setLED(false);
//digitalWrite(pin_led, true);//LED off
Serial.println(buttpresstime);
if(buttpresstime>6000){//ca. 6 sec
Serial.println("Restart ESP");
//restart
setLED(true);
delay(500);
setLED(false);
delay(500);
setLED(true);
delay(500);
setLED(false);
delay(500);
setLED(true);
delay(500);
setLED(false);
delay(500);
ESP.restart();
}
else
{
//toogle Relais
toogleRelais();
Serial.println("toogle Relay");
}
buttpresstime=0;
}
}
else
{//down
buttpresstime+=butt_zeitchecker;//Zeit merken +=120ms
if(buttpresstime>6000)
toogleLED();
else
setLED(true);//LED an
}
}
if(oNtp.hatTime() && currentMillis - tim_previousMillis > tim_zeitchecker){//Timer checken
tim_previousMillis = currentMillis;
if(last_minute!=oNtp.getminute()){//nur 1x pro min
checktimer();
last_minute=oNtp.getminute();
}
}
//HLW8012
if(SEL_PIN>-1){
if(currentMillis - UPDATE_TIMEHLW8012_previousMillis > UPDATE_TIMEHLW8012){
UPDATE_TIMEHLW8012_previousMillis = currentMillis;
/*
Serial.print("[HLW] Active Power (W) : "); Serial.println(hlw8012.getActivePower());
Serial.print("[HLW] Voltage (V) : "); Serial.println(hlw8012.getVoltage());
Serial.print("[HLW] Current (A) : "); Serial.println(hlw8012.getCurrent());
Serial.print("[HLW] Apparent Power (VA) : "); Serial.println(hlw8012.getApparentPower());
Serial.print("[HLW] Power Factor (%) : "); Serial.println((int) (100 * hlw8012.getPowerFactor()));
Serial.println();
"typ":"hlw8012",
"currentmultiplier":14484.49,
"voltagemultiplier":408636.51, //
"powermultiplier":10343611.74,
"activepower":147765,
"voltage":5238, 226V
"current":185.70,
"apparentpower":972689,
"powerfactor":13
*/
// When not using interrupts we have to manually switch to current or voltage monitor
hlw8012.toggleMode();
}
}
//WLAN-ceck
unsigned long cwl=random(check_wlanasclient, check_wlanasclient+check_wlanasclient);//x..x+15sec sonst zu viele Anfragen am AP
if(currentMillis - check_wlanasclient_previousMillis > cwl){
//zeit abgelaufen
check_wlanasclient_previousMillis = currentMillis;
if(isAPmode){//apmode
//neuer Verbindengsaufbauversuch
if(anzahlVerbindungsversuche<anzahlVersuche || keinAPModus){//nur x-mal, dann im AP-Mode bleiben
connectWLAN();
}
}
}
/*//TODO ?
if ( WiFi.status() != WL_CONNECTED ){
//Serial.println("no connect");
OTA.setup(WIFI_SSID, WIFI_PASSWORD, ARDUINO_HOSTNAME);//re-connect to WLAN
}
*/
}
//-------------------Timer---------------
const int line_BUFFER_SIZE = 128;
char line_buffer[line_BUFFER_SIZE];
void checktimer(){
if (SPIFFS.exists(TIMERtxt)) {
// time
File file = SPIFFS.open(TIMERtxt, "r");
String zeile;
char tmp[line_BUFFER_SIZE];
int tmpcounter;
bool onoff;
char zeit[5];
byte t_st=0;
byte t_min=0;
byte tage=0;
String befehl="";
String id="";
int anz=0;
int i;
int t;
int sepcounter;
if(file){
//Serial.println("opend timer.txt");
//Zeilenweise einlesen
//on/off|hh:mm|mo-so als bit|befehl|id
//on|07:05|31|ON|t1
while (file.available()){
zeile=file.readStringUntil('\n');
//Serial.println(zeile);
//Zeile zerlegen
anz= zeile.length();
zeile.toCharArray(line_buffer,anz);
tmpcounter=0;
sepcounter=0;
onoff=false;
for(i=0;i<anz;i++){
if(line_buffer[i]=='|'){
tmp[tmpcounter]='\0';
if(sepcounter==0){//on/off
onoff=( tmp[0]=='o' && tmp[1]=='n');
}
if(sepcounter==1){//hh:mm
zeit[0]=tmp[0];
zeit[1]=tmp[1];
zeit[2]='\0';
t_st=String(zeit).toInt();
zeit[0]=tmp[3];
zeit[1]=tmp[4];
t_min=String(zeit).toInt();
}
if(sepcounter==2){
tage=String(tmp).toInt();
}
if(sepcounter==3){
befehl=String(tmp);
}
if(sepcounter==4){
id=String(tmp);
}
sepcounter++;
tmpcounter=0;
}
else{
tmp[tmpcounter]=line_buffer[i];
tmpcounter++;
}
}
//Zeilendaten auswerten
if(onoff){
byte maske=0;//uint8_t
byte ntp_wochentag=oNtp.getwochentag();
byte ntp_stunde=oNtp.getstunde();
byte ntp_minute=oNtp.getminute();
if(ntp_wochentag==0)maske=1;//Serial.print(" Mo");
if(ntp_wochentag==1)maske=2;//Serial.print(" Di");
if(ntp_wochentag==2)maske=4;//Serial.print(" Mi");
if(ntp_wochentag==3)maske=8;//Serial.print(" Do");
if(ntp_wochentag==4)maske=16;//Serial.print(" Fr");
if(ntp_wochentag==5)maske=32;//Serial.print(" Sa");
if(ntp_wochentag==6)maske=64;//Serial.print(" So");
if(tage & maske ){//Serial.print(" isday, ");
if(ntp_stunde==t_st && ntp_minute==t_min){
if(befehl=="ON")handleAktion(1, 1);
else
if(befehl=="OFF")handleAktion(2, 1);
else
if(befehl=="LEDON")handleAktion(3, 1);
else
if(befehl=="LEDOFF")handleAktion(4, 1);
else
if(befehl=="LED2ON")handleAktion(5, 1);
else
if(befehl=="LED2OFF")handleAktion(6, 1);
else
if(befehl=="LEDWLANON")handleAktion(7, 1);
else
if(befehl=="LEDWLANOFF")handleAktion(8, 1);
}
}
}
}
}
file.close();
}
}
//--------------Power--------------------
void unblockingDelay(unsigned long mseconds) {
unsigned long timeout = millis();
while ((millis() - timeout) < mseconds) delay(1);
}
void calibrate(float watt, float volt) {
//setResistors & _calculateDefaultMultipliers
//hlw8012.setResistors(CURRENT_RESISTOR, VOLTAGE_RESISTOR_UPSTREAM, VOLTAGE_RESISTOR_DOWNSTREAM);
//0.001,( 5 * 470000 ),( 1000 )
//_current_multiplier = 144844.93 // A
//_voltage_multiplier = 408636.51 // V
//_power_multiplier = 10343611.74 // W
/*
0.001 ->_current_multiplier = ( 1000000.0 * 512 * V_REF / _current_resistor / 24.0 / F_OSC );
5 * 470000
1000
_current_resistor = 0.001;
_voltage_resistor = (5 * 470000 + 1000) / 1000; -> 2351
_current_multiplier = ( 1000000.0 * 512 * 2.43 / _current_resistor / 24.0 / 3579000 ); ->144844.93
_voltage_multiplier = ( 1000000.0 * 512 * 2.43 * _voltage_resistor / 2.0 / 3579000 ); ->408636.51
_power_multiplier = ( 1000000.0 * 128 * 2.43 * 2.43 * _voltage_resistor / _current_resistor / 48.0 / 3579000 ); ->10343611.74
*/
// Let's first read power, current and voltage
// with an interval in between to allow the signal to stabilise:
hlw8012.getActivePower();
hlw8012.setMode(MODE_CURRENT);
unblockingDelay(2000);
hlw8012.getCurrent();
hlw8012.setMode(MODE_VOLTAGE);
unblockingDelay(2000);
hlw8012.getVoltage();
// Calibrate using a 60W bulb (pure resistive) on a 230V line
hlw8012.expectedActivePower(watt); // 60.0W
hlw8012.expectedVoltage(volt); // 230.0V
hlw8012.expectedCurrent(watt / volt);// 60.0/230 -> 0.26 A
}
//------------Data IO--------------------
void handleData(){// data.json
String message = "{\r\n";
String aktionen = "";
//uebergabeparameter?
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "settimekorr") {
oNtp.setTimeDiff(server.arg(i).toInt());
aktionen +="set_timekorr ";
}
if (server.argName(i) == "led") {
if (server.arg(i) == "on" ){
handleAktion(3,1);
aktionen +="LED_ON ";
}
if (server.arg(i) == "off"){
handleAktion(4,1);
aktionen +="LED_OFF ";
}
}
if (server.argName(i) == "relais") {
if (server.arg(i) == "on" ){
handleAktion(1,1);
aktionen +="Relais_ON ";
}
if (server.arg(i) == "off"){
handleAktion(2,1);
aktionen +="Relais_OFF ";
}
}
if (server.argName(i) == "led2") {
if (server.arg(i) == "on" ){
handleAktion(5,1);
aktionen +="LED2_ON ";
}
if (server.arg(i) == "off"){
handleAktion(6,1);
aktionen +="LED2_OFF ";
}
}
if (server.argName(i) == "ledwlan") {
if (server.arg(i) == "on" ){
handleAktion(7,1);
aktionen +="LEDWLAN_ON ";
}
if (server.arg(i) == "off"){
handleAktion(8,1);
aktionen +="LEDWLAN_OFF ";
}
}
}
message +="\"hostname\":\""+String(ARDUINO_HOSTNAME)+"\",\r\n";
message +="\"aktionen\":\""+aktionen+"\",\r\n";
message +="\"progversion\":\""+String(progversion)+"\",\r\n";
message +="\"cpu_freq\":\""+String(ESP.getCpuFreqMHz())+"\",\r\n";
message +="\"chip_id\":\""+String(ESP.getChipId())+"\",\r\n";
message +="\"flashchiprealsize\":\""+String(ESP.getFlashChipRealSize())+"\",\r\n";
message +="\"isAPmode\":\"";
if(isAPmode)
message +="true";
else
message +="false";
message +="\",\r\n";
byte ntp_stunde =oNtp.getstunde();
byte ntp_minute =oNtp.getminute();
byte ntp_secunde =oNtp.getsecunde();
// ntp_stunde
message +="\"lokalzeit\":\"";
if(ntp_stunde<10)message+="0";
message+=String(ntp_stunde)+":";
if(ntp_minute<10)message+="0";
message+= String(ntp_minute)+":";
if(ntp_secunde<10)message+="0";
message+=String(ntp_secunde);
message +="\",\r\n";
message +="\"datum\":{\r\n";
message +=" \"tag\":"+String(oNtp.getwochentag())+",\r\n";
message +=" \"year\":"+String(oNtp.getyear())+",\r\n";
message +=" \"month\":"+String(oNtp.getmonth())+",\r\n";
message +=" \"day\":"+String(oNtp.getday())+",\r\n";
message +=" \"timekorr\":"+String(oNtp.getUTCtimediff())+",\r\n";
if(oNtp.isSummertime())
message +=" \"summertime\":true\r\n";
else
message +=" \"summertime\":false\r\n";
message +="},\r\n";
//led/relais-status
message +="\"portstatus\":{\r\n";
message +="\"relais\":";
if(getRelay())
message +="true";
else
message +="false";
message +=",\r\n";
/*message +="\"button\":"; nicht für www relevant, da taster - nur lokale Auswertung
if(getButton())
message +="true";
else
message +="false";
message +=",\r\n";*/
message +="\"led\":";
if(getLED())
message +="true";
else
message +="false";
//oxaoxe
if(pin_led2>-1){
message +=",\r\n\"led2\":";
if(getLED2())
message += "true";
else
message += "false";
}
if(pin_ledWLAN>-1){
message +=",\r\n\"ledWLAN\":";
if(getLEDWLAN())
message += "true";
else
message += "false";
}
message +="\r\n";
message +="},\r\n";//Portstatus
if(SEL_PIN>-1){
message +="\"power\":{\r\n";
message +=" \"typ\":\"hlw8012\",\r\n";
message +=" \"currentmultiplier\":";
message +=String(hlw8012.getCurrentMultiplier());
message +=",\r\n";
message +=" \"voltagemultiplier\":";
message +=String(hlw8012.getVoltageMultiplier());
message +=",\r\n";
message +=" \"powermultiplier\":";
message +=String(hlw8012.getPowerMultiplier());
message +=",\r\n";
message +=" \"activepower\":";
message +=String(hlw8012.getActivePower());//W
message +=",\r\n";
message +=" \"voltage\":";
message +=String(hlw8012.getVoltage());//V
message +=",\r\n";
message +=" \"current\":";
message +=String(hlw8012.getCurrent());//A
message +=",\r\n";
message +=" \"apparentpower\":";
message +=String(hlw8012.getApparentPower());//VA
message +=",\r\n";
message +=" \"powerfactor\":";
message +=String((int) (100 * hlw8012.getPowerFactor()));//%
message +=",\r\n";
message +=" \"reactivepower\":";
message +=String((int) (hlw8012.getReactivePower()));
message +=",\r\n";
message +=" \"energy\":";
message +=String(hlw8012.getEnergy());//in Ws
message +="\r\n";
/* Serial.print("[HLW] Active Power (W) : "); Serial.println(hlw8012.getActivePower());
Serial.print("[HLW] Voltage (V) : "); Serial.println(hlw8012.getVoltage());
Serial.print("[HLW] Current (A) : "); Serial.println(hlw8012.getCurrent());
Serial.print("[HLW] Apparent Power (VA) : "); Serial.println(hlw8012.getApparentPower());
Serial.print("[HLW] Power Factor (%) : "); Serial.println((int) (100 * hlw8012.getPowerFactor()));
Serial.println();
*/
message +="},\r\n";
}
message +="\"macadresse\":\""+macadresse+"\",\r\n";
FSInfo fs_info;
if (SPIFFS.info(fs_info)) {
message +="\"fstotalBytes\":"+String(fs_info.totalBytes)+",\r\n";
message +="\"fsusedBytes\":"+String(fs_info.usedBytes)+",\r\n";
message +="\"fsused\":\"";
message +=float(int(100.0/fs_info.totalBytes*fs_info.usedBytes*100.0)/100.0);
message +="%\",\r\n";
}
//files
message +="\"files\":[\r\n";
String fileName;
Dir dir = SPIFFS.openDir("/");
uint8_t counter=0;
while (dir.next()) {
fileName = dir.fileName();
if(counter>0) message +=",\r\n";
message +=" {";
message +="\"fileName\":\""+fileName+"\", ";
message +="\"fileSize\":"+String(dir.fileSize());
message +="}";
counter++;
};
message +="\r\n]\r\n";
//--
message +="\r\n}";
server.sendHeader("Access-Control-Allow-Origin", "*");//wenn vom HTTPS-Seiten aufgerufen wird!
server.send(200, "text/plain", message );
Serial.println("send data.json");
}
void handleIndex() {//Rueckgabe HTML
//$h1gtag $info
int pos1 = 0;
int pos2 = 0;
String s;
String tmp;
String message = "";
while (indexHTM.indexOf("\r\n", pos2) > 0) {
pos1 = pos2;
pos2 = indexHTM.indexOf("\r\n", pos2) + 2;
s = indexHTM.substring(pos1, pos2);
//Tags gegen Daten ersetzen
if (s.indexOf("$h1gtag") != -1) {
s.replace("$h1gtag", progversion);//Ueberscherschrift=Prog-Version
}
//Liste der Dateien
if(s.indexOf("$filelist") != -1){
tmp="<table class=\"files\">\n";
String fileName;
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
fileName = dir.fileName();
Serial.print("getfilelist: ");
Serial.println(fileName);
tmp+="<tr>\n";
tmp+="\t<td><a target=\"_blank\" href =\"" + fileName + "\"" ;
tmp+= " >" + fileName.substring(1) + "</a></td>\n\t<td class=\"size\">" + formatBytes(dir.fileSize())+"</td>\n\t<td class=\"action\">";
tmp+="<a href =\"" + fileName + "?delete=" + fileName + "\" class=\"fl_del\"> löschen </a>\n";
tmp+="\t</td>\n</tr>\n";
};
FSInfo fs_info;
tmp += "<tr><td colspan=\"3\">";
if (SPIFFS.info(fs_info)) {
tmp += formatBytes(fs_info.usedBytes).c_str(); //502
tmp += " von ";
tmp += formatBytes(fs_info.totalBytes).c_str(); //2949250 (2.8MB) formatBytes(fileSize).c_str()
tmp += " (";
tmp += float(int(100.0/fs_info.totalBytes*fs_info.usedBytes*100.0)/100.0);
tmp += "%)";
/*tmp += "<br>\nblockSize:";
tmp += fs_info.blockSize; //8192
tmp += "<br>\npageSize:";
tmp += fs_info.pageSize; //256
tmp += "<br>\nmaxOpenFiles:";
tmp += fs_info.maxOpenFiles; //5
tmp += "<br>\nmaxPathLength:";
tmp += fs_info.maxPathLength; //32*/
}
tmp+="</td></tr></table>\n";
s.replace("$filelist", tmp);
}
message += s;
}
server.send(200, "text/html", message );
}
void handleAction() {//Rueckgabe JSON
/*
/action?sonoff=ON Relais einschalten
/action?sonoff=OFF Relais ausschalten