-
Notifications
You must be signed in to change notification settings - Fork 0
/
STM_aurora_pvout_SPIx_v3.1.ino
3272 lines (3185 loc) · 120 KB
/
STM_aurora_pvout_SPIx_v3.1.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
/*
---------------------------------------
Aurora Power One to PVOUTPUT.ORG
---------------------------------------
*/
//======================================
//For testing delete the /* below to disable (production) for production please outcomment the next line definition with a /*
//======================================
/*
#define USE_TEST_LAN_IP true
#define OPTION_NO_PVOUTPUT true
#define OPTION_NO_WEATHER true
#define OPTION_NO_PLUVIO true
#define OPTION_NO_PVI_POLL true
#define OPTION_NO_ABUSEIPDB true
#define OPTION_NO_EMAIL true
/*
*/
#define PVIVersion "V3.1 "
#define Release "R125"
#define COMPILEDATE __DATE__
#define COMPILETIME __TIME__
#define OPTION_REPORT_ALL_2_ABUSEIPDB true // report every breath every 20 minutes one at a time
#define OPTION_FLASH true
#define USE_DHCP true
/*
COMPILE IN ARDUINO VERSION 1.8.19
v3.1 migration to wolfSSL
v3.0 temperature changed from class to tab
V2.8 and higher compile with 1.8.13 or 1.8.19 with modified ethernet library (in Arduino version library map)
adapted for 1.8.6 nightly 2018/09/07 03:25
tested with 1.8.7 with ethernet lib 1.1.2, version2 shows
8 sockets and hangs. test started on 30-sep-2018 ethernet 2 lib with max_num_sock defined in sketch
tested with 1.8.13
Creation Date: 25.8.2014
Author: Bert Havinga after an idea from Heinz Pieren
18-10-2015 peakpower calculation changed to unsigned int, update dns to first of month
21 aug 2020 adapted to STM32
FLASH on SPI 1
RS485 on Serial2(PA3, PA2)
RS485 direction pin is deprecated due to RS485 module with autodirection
Relay_fanA PB6
Relay_fanB PB7
Relay_fanC PB8
spi flash w25q32 is mounted together with ethernet on SPI1
On 411CE the SPIClass SPITwo(PB15, PB14, PB10); // MOSI, MISO, SCLK
But we use this as IO
*/
//===================================
#define WELCOME "\r\nAurora Power One STM32 PVOUTPUT.ORG uploader "
#define NAME "B. Havinga"
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_fanA PB13
#define Relay_fanB PB8
#define Relay_fanC PB6
#define Relay_Power PB9
#define Relay_C_DIST PB12
#define Relay_Power_R PB7
#define keyswitch PA0 // keyswitch is PA0, but is forced low at activation and usage of UART2 in testboard PB12 has extern switch (unreliable)
#define TX2 PA2
#define RX2 PA3
#define FLASH_SS PA4
#define ETHERNET_SS PA15
#define longRS485Wait 900
#define shortRS485Wait 300
#ifndef Buienradar_h
#define Buienradar_h
#endif
#include "Arduino.h"
#include "userdefs.h"
#include <Ethernet.h>
#include <Dns.h>
#include <FlashMini.h>
#include <wolfssl.h>
#include <wolfssl/ssl.h>
#include <TimeLib.h>
#include <SPIMemory.h>
#include <string.h>
//#ifdef WOLFSSL_TRACK_MEMORY
//#include <wolfssl/wolfcrypt/mem_track.h>
//#endif
#if defined (USE_LOGGING) || defined (USE_PWRLOGGING)
#include <SD.h>
#endif
#define MAX_SOCK_NUM 4
#if defined(ARCH_STM32)|| defined(ARDUINO_ARCH_ESP32) || defined(STM32F4xx)
#define SUSPCMDSIZE 384
#define SUSPARRAYDEPTH 25
char suspicious_string[SUSPARRAYDEPTH][SUSPCMDSIZE]; // the suspicious content
byte suspicious_ips[SUSPARRAYDEPTH][4]; // The IP of the sender
int suspicious_perc[SUSPARRAYDEPTH]; // The percentage of suspicion
int suspicious_length[SUSPARRAYDEPTH]; // the length of the suspicious content
time_t suspicious_time[SUSPARRAYDEPTH]; // the time the datagram is received
int suspicious_index = 0; // the actual entry in the array
int last_suspicious_index = 0; // the actual last reported array entry
int suspicious_total = 0;
#define UNKWNCMDARRAYDEPTH 10
char unknownCmd_string[UNKWNCMDARRAYDEPTH][SUSPCMDSIZE]; // the unknown command
int unknownCmd_index = 0; // the actual entry in the array
byte unknownCmd_ips[UNKWNCMDARRAYDEPTH][4]; // The IP of the sender
bool unknownCmd_wkwnips[UNKWNCMDARRAYDEPTH]; // from well known IP
int unknownCmd_perc[UNKWNCMDARRAYDEPTH]; // The percentage of suspicion
int unknownCmd_length[UNKWNCMDARRAYDEPTH]; // the length of the suspicious content
time_t unknownCmd_time[UNKWNCMDARRAYDEPTH]; // the time the datagram is received
bool unknownCmd_sema = false; // semaphore <> 0 is unknown command queued
byte unknownCmd_queued[UNKWNCMDARRAYDEPTH]; // 0= not queued, 1 = queued, 2 = submitted
int unknownCmd_total = 0;
#endif
unsigned long G_SocketConnectionTimers[MAX_SOCK_NUM]; //system millis
unsigned int Sock_DisCon[MAX_SOCK_NUM]; //aantal keren gerest
char lastSock_DisCon[MAX_SOCK_NUM][24];
// global variables
byte lastMonth;
byte lastDayReset = 0;
byte lastHour;
byte lastMinute;
byte lastSecond;
byte iMonth;
byte iDay;
byte iHour;
byte iMinute;
byte iSecond;
byte strd_iMonth = 0;
byte lastiMinuteShdw = 0;
byte lastiHourShdw = 0;
byte lanmode;
byte macBuffer[6]; // create a buffer to hold the MAC address
unsigned long wCLength = 0;
bool AlreadyAdded = false;
bool min_serial = true; // log messages for USB Serial port
bool ethrlog = false;
bool iplog = false;
bool maillogging = false;
bool pvoutputlog = false;
bool showbufferremaining = false;
bool wolfssllog = false;
bool pluvio_ena = true;
bool AlreadyDailySend = false;
bool AlreadyMonthSend = false;
bool udpdump = false;
bool HTTPlog = false;
bool WolfSSLrwmail = false;
bool exc_plv_err = false;
bool InExlog = false;
bool invlogging = false;
bool brlogging = false;
bool abuslogging = false;
bool mailSendErrorflag = false;
bool mailRcvErrorflag = false;
bool SerInpRcdv = false;
bool suspicious_mail = true;
bool unknownCmd_mail = true;
bool tftpReqEna = false;
unsigned long upTime = 0; // the amount of hours the Arduino is running
int ChkSumErrCnt = 0;
int errorwolfsslwantread = 0;
int errorwolfsslwantwrite = 0;
int errorHTMLnocharavlbl = 0;
int tmpChkSumErrCnt;
int nr_flsh_error = 0;
int PeakPowerObserved; // used to determine the moment of peakpower this day
int PeakPowerInObserved = 0;
//int uplPeakPowerObserved; //used to determine if time for peakpower to upload needs new time
int pwrPeak = 0;
int prevPwrPeak = 0;
int rprt2abuseipdb = 0;
int rprt2abuseipdberr = 0;
int shadow = 0;
int timeDiff;
int unkcmdfrmwhl = 0;
//int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx, EthernetClient wtlsclient);
//int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx, EthernetClient wtlsclient);
int pluvio_last_RXdata_size;
int pluvio_max_RXdata_size = 0; //max observed packet size received form pluvio buienradar
int pluvio_min_RXdata_size = 4096; //min observed packet size received form pluvio buienradar
int pluvio_last_HTTP_headersize;
int pluvio_min_HTTP_headersize = 4096; //min observed html header size received form pluvio buienradar
int pluvio_max_HTML_headersize = 0; //max observed html header size received form pluvio buienradar
int ab_last_HTML_headersize;
int ab_last_RXdata_size;
int lastsocket18close = 0;
int abuse_well_known_hits = 0;
int abuse_well_known_scans = 0;
unsigned int currentInvtime; // current time
uint16_t totalnets = 0;
uint16_t totalHTTPcmd = 0;
uint32_t dayYield[93];
unsigned long oldtime;
unsigned long newtime;
unsigned int peakpower5min = 0; // value for peakpower in 5 minute lap
unsigned long MaxcmltvPwr = 0; // used for uploading
float actTension = 0.0;
float prevTension = 0.0;
unsigned int GridPower = 0;
unsigned int PrevGridPower = 0;
unsigned int totalToday = 0;
uint32_t maxtotalToday = 0;
unsigned long cmltvPwr;
float actInvTemp = 0.0;
float startInvTemp = 0.0;
float maxInvTemp = 0.0;
float actBstTemp = 0.0;
float actDc1Voltage = 0.0;
float actDc1Current = 0.0;
float actDc2Voltage = 0.0;
float actDc2Current = 0.0;
float rendement = 0.0;
float Riso = 0.0;
float invRiso = 0.0;
float BrCorrFact = 1.0;
//int PowerGeneratedDayOfMonth[31];
int Power1;
int Power2;
int PVIPolled = 0;
int luchtvochtigheid;
int luchtDrukMin3 = 1020;
//int lastGridPower; // used to surpress comm errors in case the PVI is sleeping
unsigned long uploadsOk = 0;
unsigned long totalUploadsOk = 0;
unsigned long uploadErr = 0;
unsigned long totalUploadsErr = 0;
/*
int crcErrGridVoltage = 0;
int crcErrGridPower = 0;
int crcErrgetTempBst = 0;
int crcErrgetTempInv = 0;
int crcErrgetDc1Voltage = 0;
int crcErrgetDc1Current = 0;
int crcErrgetDc2Voltage = 0;
int crcErrgetDc2Current = 0;
int crcErrgetRiso = 0;
int crcErrgetTime = 0;
int crcErrgetEnergyDay = 0;
int crcErrgetEnergyTotal = 0;
int crcErrgetGridPowerPeakDay = 0;
*/
unsigned long minRxTime = 300;
unsigned long maxRxTime = 0;
bool rxTimeflag = false;
int cntrespTimeout = 0;
int tmpcntrespTimeout;
// int rxReadCnt = 0;
unsigned long receiveMaxRS485Wait = shortRS485Wait; //at start use the short. in the loop determine the long or short timeout for PVI packet receive
int todayIPCnt = 0;
int dynDNSCnt = 0;
unsigned int socket0x18close = 0;
unsigned int fanAOnCount = 0;
unsigned int fanBOnCount = 0;
unsigned int fanCOnCount = 0;
unsigned int fanAOnTotalCount;
unsigned int fanBOnTotalCount;
unsigned int fanCOnTotalCount;
unsigned long runTimefanA;
unsigned long runTimefanB;
unsigned long runTimefanC;
unsigned int runTimeThisDayfanA = 0;
unsigned int runTimeThisDayfanB = 0;
unsigned int runTimeThisDayfanC = 0;
unsigned int runTimeThisCyclefanA = 0;
unsigned int runTimeThisCyclefanB = 0;
unsigned int runTimeThisCyclefanC = 0;
const unsigned int fanApowerOn = 1200;
const unsigned int fanBpowerOn = 1800;
const unsigned int fanCpowerOn = 2000;
const unsigned int fanApowerOff = 1000;
const unsigned int fanBpowerOff = 1200;
const unsigned int fanCpowerOff = 1100;
float tempFanAOff = 36.5; // fan A off
float tempFanBOff = 38.0; // fan B off
float tempFanCOff = 37.0; // fan C off
float tempFanAOn = 38.0; // fan A on
float tempFanBOn = 39.0; // fan B on
float tempFanCOn = 40.0; // fan C on
char webData[96];
// char wc[66]; //temp array for writing to flash
time_t resetRxTime = 0;
time_t sketchStartTime = 0;
time_t pluvio_max_RXdata_time = 0;
time_t pluvio_min_RXdata_time = 0;
time_t pluvio_min_HTTP_header_time = 0;
time_t pluvio_max_HTTP_header_time = 0;
unsigned long abuse_array_scan_start_time = 0;
unsigned long abuse_array_scan_run_time = 0;
char hPaTrend = '-';
char TimestartInvTemp[6];
char TimePeakPower[6];
char TimePeakPowerIn[6];
//char uplTimePeakPower[6]; // the time used for addoutput rounded at 5 minutes, pvoutput rounds itself
char TimePeakTemp[6]; // time inverter temp max
char ab_lastresponse[32] = "None";
char maillog[512];
char dynDNSresponse[64];
char mailerrortype[64];
char ob[8];
char lastarrtime[8];
char looptime[32];
char msgchararray[64];
char lastsocket18closetime[24];
char firstShadowTime[] = "not today";
char lastShadowTime[] = "not today";
char flasherror[256];
char c_poll_tijd[12];
char mailsubj[128];
char temptxtbuff[1024];
char mailcontent[80];
char mailbrstate[80];
String weatherCondition1300 = ""; // Weathercondition at 13:00
String flash_error = "";
bool ab_rslt;
bool br_rslt;
bool GetTemp_Ok;
bool force_temperature;
// bool min_serial;
bool showbuffer;
bool resetmaxpluvio;
unsigned int tick = 0;
unsigned int nosockavail = 0;
unsigned int todayCnt;
unsigned int todayCnt_br;
unsigned int todayErrCnt;
unsigned int todayErrCnt_br;
unsigned int totalErrCnt_br = 0;
unsigned int todayErrCnt_ab = 0;
unsigned int totalErrCnt_ab = 0;
unsigned int MaxFailErrCnt_br;
unsigned int pluviosucc_br;
unsigned int visibility;
int lv_phi;
int hk;
int windkrachtBft;
float MinTemp; // Minimum Temperature
float MaxTemp; // Maximum Temperature
float prevActual; // the previous temperature from the metering meteo station
float last_neerslagintensiteit;
float max_neerslagintensiteit;
float gdFactor;
float pluvio_br;
int arrpluvio_br[24];
int arrhour_br[24];
int arrminute_br[24];
float totexpectedPluvio_br = 0.0;
float totPluvio_br;
float actual;
float windsnelheidMS;
float luchtdruk;
time_t tmp_tijd;
char abErrTime_ab[32];
char windrichting[8];
char c_tijd_Temp[16];
char c_tijd_Pluv[16];
char br_tijd[16];
char meetStation[16];
char weatherType[64];
char icon_temperature[16];
char TimeMinTemp[16];
char TimeMaxTemp[16];
char weatherProcTime[16];
char pluviosuccTime_br[16];
char weatherErrTime_br[24];
char pluvioProcTime_br[16];
char pluvioErrTime_br[32];
time_t lastpluvioTime_br;
time_t maxpluvioTime_br;
char nextpluvioTime_br[16];
char samenvatting[384];
// float average;
char* weatherStation;
#define FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
IPAddress UDPSendTo(192, 168, 0, 255);
#if defined (USE_LOGGING) || defined (USE_PWRLOGGING)
File logFile;
#endif
EthernetServer server(555); // port changed from 80 to 555
EthernetUDP Udp;
EthernetUDP UDPclient;
EthernetUDP TFTPclient;
//-----------------------------------------------------------------------------
// SPI instance: use 2 or 3 for black F4, because the on-board LED conflicts with SPI1_MISO(PA6)
#ifdef OPTION_FLASH
SPIFlash flash(FLASH_SS);
#endif
HardwareSerial Serial2(RX2, TX2);
//-----------------------------------------------------------------------
// Update Inverter
//-----------------------------------------------------------------------
void updatePVI()
{
#ifdef OPTION_NO_PVI_POLL
actBstTemp = 0.0;
actInvTemp = 0.0;
actDc1Voltage = 0.0;
actDc2Voltage = 0.0;
actDc1Current = 0.0;
actDc2Current = 0.0;
Power1 = 0;
Power2 = 0;
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d INFORMATION: Inverter polling is disabled by build option.", hour(), minute(), second()); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
// return;
#endif
if (!min_serial || (upTime == 0) ) {
if ((lastSecond == 5) || (todayCnt == 0) || (lastSecond == 30) || (upTime == 0) || SerInpRcdv) {
SerInpRcdv = false;
time_t poll_tijd = now();
sprintf(c_poll_tijd, "%02d:%02d:%02d", hour(poll_tijd), minute(poll_tijd), second(poll_tijd)); // c_poll_tijd is max 12 char
sprintf(temptxtbuff, "\r\n%s polling PVI: Begin ", c_poll_tijd); // temptxtbuff is max 1024 char
textlog(temptxtbuff, false);
} else {
textStringLog("Begin ", false);
}
}
#ifndef OPTION_NO_PVI_POLL
getGridVoltage(); // Sends the command get current power feeding to grid
// Serial.print(F("spanning: "));
// Serial.println(actTension);
if (actTension > 0.0) {
PVIPolled++;
delay(60);
getGridPowerPeakDay(); // Sends the command get the grid power peak current day (collects pwrPeak)
delay(60);
getGridPower(); // Sends the command get current power feeding to grid
if (GridPower > peakpower5min) {
peakpower5min = GridPower;
}
delay(60);
getTempBst();
delay(60);
getTempInv();
delay(60);
getDc1Voltage();
delay(60);
getDc1Current();
delay(60);
getDc2Voltage();
delay(60);
getDc2Current();
delay(60);
getRiso();
if ( Riso > 0.0 ) {
invRiso = Riso;
}
delay(60);
getEnergyDay(); // Sends the command get the cumulated energy current day
delay(60);
getEnergyTotal(); // Sends the command get the total cumulated
if (PVIPolled == 90) // check time 15 minutes after wake/start
{
setinvTime();
}
if (MaxcmltvPwr < cmltvPwr)
MaxcmltvPwr = cmltvPwr; // store for off line use
if (maxtotalToday < totalToday )
maxtotalToday = totalToday; // store for off line use
float Pwr1 = actDc1Voltage * actDc1Current;
float Pwr2 = actDc2Voltage * actDc2Current;
Power1 = (int)Pwr1;
Power2 = (int)Pwr2;
int inputPower = Power1 + Power2;
// mark only the max inputpower if the input is max 20% > inputpower and the output power is gt 50W, 1-12-2017 modified to 15. This prevents startup peakrecording.
// verwijderd uit de onderste regel van if ((unsigned int)(pwrPeak * 10) > (unsigned int)(inputPower * 10)) &&
if ( (((unsigned int)(pwrPeak * 12) > (unsigned int)(inputPower * 10)) && (inputPower > PeakPowerInObserved) && (GridPower > 15)) ||
(((inputPower > PeakPowerInObserved) && (GridPower > 0) && (inputPower < 50))) )
{
PeakPowerInObserved = Power1 + Power2;
sprintf(TimePeakPowerIn, "%02d:%02d", hour(), minute()); // max is 6 char
}
if ((Power1 != 0) && (Power2 != 0)) {
rendement = (float)GridPower / (float)((Pwr1 + Pwr2) / (float)100.0);
}
else {
rendement = 0;
}
// if (prevPwrPeak == pwrPeak && pwrPeak > PeakPowerObserved) { // if pwrPeak is two cycles the same, the reading is valid
// was ook geen verbetering, daarom nu direct gekoppeld aan rendement
if ( (prevPwrPeak == pwrPeak) && (pwrPeak > PeakPowerObserved) && (rendement < 100.0)) { // if pwrPeak is two cycles the same, the reading is valid
PeakPowerObserved = pwrPeak;
sprintf(TimePeakPower, "%02d:%02d", hour(), minute()); // max is 6 char
}
prevPwrPeak = pwrPeak;
if ((startInvTemp == 0.0) && (actInvTemp > 5.0)) {
startInvTemp = actInvTemp;
sprintf(TimestartInvTemp, "%02d:%02d", hour(), minute()); // TimestartInvTemp max is 6
}
if (actInvTemp > maxInvTemp)
{
maxInvTemp = actInvTemp;
sprintf(TimePeakTemp, "%02d:%02d", hour(), minute()); // max is 6 char
}
}
if ((prevTension > 0.0) && (actTension == 0.0)) {
actBstTemp = 0.0;
actInvTemp = 0.0;
actDc1Voltage = 0.0;
actDc2Voltage = 0.0;
actDc1Current = 0.0;
actDc2Current = 0.0;
Power1 = 0;
Power2 = 0;
}
#endif
if (!min_serial || (upTime == 0)) {
// overdag || 's avonds tbv pluvio en weer ||
//if ( ((lastSecond == 55) && (receiveMaxRS485Wait == longRS485Wait)) || ( (receiveMaxRS485Wait == shortRS485Wait) && ( ((lastMinute % 5) == 3) || (((lastMinute % 5) == 0)) && (lastSecond == 0) ) ) || ((receiveMaxRS485Wait == shortRS485Wait) && brlogging) || upTime == 0 || todayCnt == 0 ) {
// overdag || 's avonds tbv pluvio en weer || start
// if ( ((lastSecond == 55) && (receiveMaxRS485Wait == longRS485Wait)) || (receiveMaxRS485Wait == shortRS485Wait) || upTime == 0 || todayCnt == 0 ) {
// textStringLog("End\r\n", false);
// }
// else {
textStringLog("End ", false);
// }
}
}
void StoreFlashError(String sectorname, long sectoraddr) {
char cobuff[256];
/*
Serial.print("type schrijffout: ");
Serial.println(sectorname);
Serial.print("sectoraddr: ");
Serial.println(sectoraddr, 16);
*/
int slt = sectorname.length();
if (slt > 238 ) {
slt = 238; // leave space for ERROR and address
}
sectorname.toCharArray(cobuff, slt + 1); // cobuff is max 256 char
// cobuff[slt] = '\0'; //toCHarArray sets terminator
sprintf(flasherror, "\r\nERROR: %s:0x%08lx", cobuff, sectoraddr); // flasherror is max 256 char
// Serial.print("flasherror: ");
// Serial.println(flasherror);
// char flsh_err[80];
// sprintf(flsh_err, "\r\nERROR, %s", flasherror);
// textlog(flsh_err, true);
textlog(flasherror, true);
nr_flsh_error++;
}
void StoreDayYield( byte dy, byte mnth) {
// compute the index for the month
byte monthIndex = (mnth - 1) % 3;
unsigned int err;
int idxpos = 0;
if (!min_serial || (upTime == 0)) {
idxpos = dy - 1 + (monthIndex * 31);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Yield written to RAM array address: %d, value: %u -> ", hour(), minute(), second(), idxpos, maxtotalToday); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
}
// add the harvest of the day
dayYield[(dy - 1) + (monthIndex * 31)] = maxtotalToday;
textStringLog("maxtotalToday stored in RAM array", false);
#ifdef OPTION_FLASH
uint32_t workaddr;
uint32_t val2store;
err = 0;
workaddr = monthBase_addr + (((monthIndex * 31) + dy - 1) * EEPROM_PAGE_SIZE);
if (!flash.eraseSector( workaddr )) {
flash_error = "eraseSector monthBase_addr FAILED! ";
StoreFlashError(flash_error, monthBase_addr);
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d ERROR: sector erasure for daily yield", hour(), minute(), second());
textlog(temptxtbuff, false);
}
delay(25);
val2store = dayYield[dy - 1 + (monthIndex * 31)];
sprintf(temptxtbuff, "\r\n%02d:%02d:%02d Yield from today %uWh stored in dayYield array to flash address = 0x%08lx day: %d month: %d calculated index: %d", hour(), minute(), second(), val2store, workaddr, dy, mnth, monthIndex); // temptxtbuff is max 1024
textlog(temptxtbuff, false);
delay(25);
if (!flash.writeULong( workaddr, val2store)) {
flash_error = F("WriteULong monthBase_addr FAIL");
StoreFlashError(flash_error, workaddr);
}
delay(25);
#endif
}
void storeWeatherCondition1300() {
// store the weathertype from 13:00
wCLength = weatherCondition1300.length();
if (wCLength > 63)
{
char chararraystring[64];
wCLength = 63;
weatherCondition1300.toCharArray(chararraystring, wCLength + 1);
// chararraystring[wCLength]= '\0'; //toCHarArray sets terminator
weatherCondition1300 = String(chararraystring);
// wCLength = 60; //maximize to the end of flash
// weatherCondition1300[60] = '\0';
}
#ifdef OPTION_FLASH
flash.eraseSector(wCLength_addr);
if (!flash.writeLong(wCLength_addr, wCLength)) {
flash_error = F("WriteULong wCLength_addr FAIL");
StoreFlashError(flash_error, wCLength_addr);
}
delay(3);
flash.eraseSector(weatherCondition1300_addr);
delay(25);
if (!flash.writeStr(weatherCondition1300_addr, weatherCondition1300)) {
flash_error = F("WriteStr weatherCondition1300_addr FAIL");
StoreFlashError(flash_error, weatherCondition1300_addr);
}
delay(3);
#endif
}
void ReadWeatherCondition1300() {
#ifdef OPTION_FLASH
char RWCcobuff[16];
wCLength = flash.readLong(wCLength_addr);
textStringLog("Length of restored weather condition @13:00 = ", false);
itoa(wCLength, RWCcobuff, 10);
textlog(RWCcobuff, false);
if ((wCLength != 0) && (wCLength != 0xFFFFFFFF)) {
flash.readStr(weatherCondition1300_addr, weatherCondition1300);
textStringLog("\r\nweather condition @13:00 restored to: ", false);
char costr[128];
strcpy(costr, weatherCondition1300.c_str()); //costr is max 128 char
textlog(costr, false);
textStringLog("\r\nstringlength from function: ", false);
itoa(weatherCondition1300.length(), RWCcobuff, 10);
textlog(RWCcobuff, false);
}
else {
textStringLog("\r\nweather condition @13:00 is not restored", false);
}
#endif
}
void StoreDayTotal() // userdefs.h contains the memorymap
{
#ifdef OPTION_FLASH
flash.eraseSector(shadow_addr);
delay(25);
if (!flash.writeLong(shadow_addr, shadow)) {
flash_error = F("writeLong shadow_addr FAIL");
StoreFlashError(flash_error, shadow_addr);
}
delay(3);
flash.eraseSector(firstShadowTime_addr);
delay(25);
if (!flash.writeAnything(firstShadowTime_addr, firstShadowTime)) {
flash_error = F("writeAnything firstShadowTime_addr FAIL");
StoreFlashError(flash_error, firstShadowTime_addr);
}
delay(3);
flash.eraseSector(lastShadowTime_addr);
delay(25);
if (!flash.writeAnything(lastShadowTime_addr, lastShadowTime)) {
flash_error = F("writeAnything lastShadowTime_addr FAIL");
StoreFlashError(flash_error, lastShadowTime_addr);
}
delay(3);
flash.eraseSector(fanAOnCount_addr);
delay(25);
if (!flash.writeLong(fanAOnCount_addr, fanAOnCount)) {
flash_error = F("writeLong fanAOnCount_addr FAIL");
StoreFlashError(flash_error, fanAOnCount_addr);
}
delay(3);
flash.eraseSector(fanBOnCount_addr);
delay(25);
if (!flash.writeLong(fanBOnCount_addr, fanBOnCount)) {
flash_error = F("writeLong fanBOnCount_addr FAIL");
StoreFlashError(flash_error, fanBOnCount_addr);
}
delay(3);
flash.eraseSector(fanCOnCount_addr);
delay(25);
if (!flash.writeLong(fanCOnCount_addr, fanCOnCount)) {
flash_error = F("writeLong fanCOnCount_addr FAIL");
StoreFlashError(flash_error, fanCOnCount_addr);
}
delay(3);
flash.eraseSector(runTimeThisDayfanA_addr);
delay(25);
if (!flash.writeLong(runTimeThisDayfanA_addr, runTimeThisDayfanA)) {
flash_error = F("writeLong runTimeThisDayfanA_addr FAIL");
StoreFlashError(flash_error, runTimeThisDayfanA_addr);
}
delay(3);
flash.eraseSector(runTimeThisDayfanB_addr);
delay(25);
if (!flash.writeLong(runTimeThisDayfanB_addr, runTimeThisDayfanB)) {
flash_error = F("writeLong runTimeThisDayfanB_addr FAIL");
StoreFlashError(flash_error, runTimeThisDayfanB_addr);
}
delay(3);
flash.eraseSector(runTimeThisDayfanC_addr);
delay(25);
if (!flash.writeLong(runTimeThisDayfanC_addr, runTimeThisDayfanC)) {
flash_error = F("writeLong runTimeThisDayfanC_addr FAIL");
StoreFlashError(flash_error, runTimeThisDayfanC_addr);
}
delay(3);
flash.eraseSector(TimeMinTemp_addr);
delay(25);
if (!flash.writeAnything(TimeMinTemp_addr, TimeMinTemp)) {
flash_error = F("writeAnything TimeMinTemp_addr FAIL");
StoreFlashError(flash_error, TimeMinTemp_addr);
}
delay(3);
flash.eraseSector(TimeMaxTemp_addr);
delay(25);
if (!flash.writeAnything(TimeMaxTemp_addr, TimeMaxTemp)) {
flash_error = F("writeAnything TimeMaxTemp_addr FAIL");
StoreFlashError(flash_error, TimeMaxTemp_addr);
}
delay(3);
flash.eraseSector(TimePeakTemp_addr);
delay(25);
if (!flash.writeAnything(TimePeakTemp_addr, TimePeakTemp)) {
flash_error = F("writeAnything TimePeakTemp_addr FAIL");
StoreFlashError(flash_error, TimePeakTemp_addr);
}
delay(3);
flash.eraseSector(TimestartInvTemp_addr);
delay(25);
if (!flash.writeAnything(TimestartInvTemp_addr, TimestartInvTemp)) {
flash_error = F("writeAnything TimePeakTemp_addr FAIL");
StoreFlashError(flash_error, TimePeakTemp_addr);
}
delay(3);
flash.eraseSector(saveFlag_addr);
delay(25);
if (!flash.writeByte(saveFlag_addr, 0xC5)) { //flag for saving this day
flash_error = F("writeByte saveFlag_addr FAIL");
StoreFlashError(flash_error, saveFlag_addr);
}
delay(3);
flash.eraseSector(startInvTemp_addr);
delay(25);
if (!flash.writeFloat(startInvTemp_addr, startInvTemp)) {
flash_error = F("writeFloat startInvTemp_addr FAIL");
StoreFlashError(flash_error, startInvTemp_addr);
}
delay(3);
flash.eraseSector(maxInvTemp_addr);
delay(25);
if (!flash.writeFloat(maxInvTemp_addr, maxInvTemp)) {
flash_error = F("writeFloat maxInvTemp_addr FAIL");
StoreFlashError(flash_error, maxInvTemp_addr);
}
delay(3);
flash.eraseSector(runTimefanA_addr);
delay(25);
if (!flash.writeLong(runTimefanA_addr, runTimefanA)) { // store the total fan runtime
flash_error = F("writeLong runTimefanA_addr FAIL");
StoreFlashError(flash_error, runTimefanA_addr);
}
delay(3);
flash.eraseSector(runTimefanB_addr);
delay(25);
if (!flash.writeLong(runTimefanB_addr, runTimefanB)) {
flash_error = F("writeLong runTimefanB_addr FAIL");
StoreFlashError(flash_error, runTimefanB_addr);
}
delay(3);
flash.eraseSector(runTimefanC_addr);
delay(25);
if (!flash.writeLong(runTimefanC_addr, runTimefanC)) {
flash_error = F("writeLong runTimefanC_addr FAIL");
StoreFlashError(flash_error, runTimefanC_addr);
}
delay(3);
flash.eraseSector(maxtotalToday_addr);
delay(25);
if (!flash.writeLong(maxtotalToday_addr, maxtotalToday)) {
flash_error = F("writeLong maxtotalToday_addr FAIL");
StoreFlashError(flash_error, maxtotalToday_addr);
}
delay(3);
flash.eraseSector(PeakPowerInObserved_addr);
delay(25);
if (!flash.writeLong(PeakPowerInObserved_addr, PeakPowerInObserved)) {
flash_error = F("writeLong PeakPowerInObserved_addr FAIL");
StoreFlashError(flash_error, PeakPowerInObserved_addr);
}
delay(3);
flash.eraseSector(TimePeakPowerIn_addr);
delay(25);
if (!flash.writeAnything(TimePeakPowerIn_addr, TimePeakPowerIn)) {
flash_error = F("writeAnything TimePeakPowerIn_addr FAIL");
StoreFlashError(flash_error, TimePeakPowerIn_addr);
}
delay(3);
flash.eraseSector(PeakPowerObserved_addr);
delay(25);
if (!flash.writeLong(PeakPowerObserved_addr, PeakPowerObserved)) {
flash_error = F("writeLong PeakPowerObserved_addr FAIL");
StoreFlashError(flash_error, PeakPowerObserved_addr);
}
delay(3);
flash.eraseSector(TimePeakPower_addr);
delay(25);
if (!flash.writeAnything(TimePeakPower_addr, TimePeakPower)) {
flash_error = F("writeAnything TimePeakPower_addr FAIL");
StoreFlashError(flash_error, TimePeakPower_addr);
}
delay(3);
flash.eraseSector(MaxcmltvPwr_addr);
delay(25);
if (!flash.writeLong(MaxcmltvPwr_addr, MaxcmltvPwr)) {
flash_error = F("writeLong MaxcmltvPwr_addr FAIL");
StoreFlashError(flash_error, MaxcmltvPwr_addr);
}
delay(3);
flash.eraseSector(invRiso_addr);
delay(25);
if (!flash.writeFloat(invRiso_addr, invRiso)) {
flash_error = F("writeFloat invRiso_addr FAIL");
StoreFlashError(flash_error, invRiso_addr);
}
delay(3);
flash.eraseSector(MaxTemp_addr);
delay(25);
if (!flash.writeFloat(MaxTemp_addr, MaxTemp)) {
flash_error = F("writeFloat MaxTemp_addr FAIL");
StoreFlashError(flash_error, MaxTemp_addr);
}
delay(3);
flash.eraseSector(MinTemp_addr);
delay(25);
if (!flash.writeFloat(MinTemp_addr, MinTemp)) {
flash_error = F("writeFloat MinTemp_addr FAIL");
StoreFlashError(flash_error, MinTemp_addr);
}
delay(3);
flash.eraseSector(prevActual_addr);
delay(25);
if (!flash.writeFloat(prevActual_addr, prevActual)) {
flash_error = F("writeFloat prevActual_addr FAIL");
StoreFlashError(flash_error, prevActual_addr);
}
delay(3);
flash.eraseSector(luchtvochtigheid_addr);
delay(25);
if (!flash.writeLong(luchtvochtigheid_addr, luchtvochtigheid)) {
flash_error = F("writeLong luchtvochtigheid_addr FAIL");
StoreFlashError(flash_error, luchtvochtigheid_addr);
}
delay(3);
flash.eraseSector(AlreadyAdded_addr);
delay(25);
if (!flash.writeByte(AlreadyAdded_addr, AlreadyAdded)) {
flash_error = F("writeByte AlreadyAdded_addr FAIL");
StoreFlashError(flash_error, AlreadyAdded_addr);
}
delay(3);
flash.eraseSector(AlreadyDailySend_addr);
delay(25);
if (!flash.writeByte(AlreadyDailySend_addr, AlreadyDailySend)) {
flash_error = F("writeByte AlreadyDailySend_addr FAIL");
StoreFlashError(flash_error, AlreadyDailySend_addr);
}
delay(3);
flash.eraseSector(AlreadyMonthSend_addr);
delay(25);
if (!flash.writeByte(AlreadyMonthSend_addr, AlreadyMonthSend)) {
flash_error = F("writeByte AlreadyMonthSend_addr FAIL");
StoreFlashError(flash_error, AlreadyMonthSend_addr);
}
delay(3);
flash.eraseSector(totPluvio_br_addr);
delay(25);
if ( !(isnan(totPluvio_br) || isinf(totPluvio_br)) ) {
if (!flash.writeFloat(totPluvio_br_addr, totPluvio_br)) {
flash_error = F("writeFloat totPluvio_br_addr FAIL");
StoreFlashError(flash_error, totPluvio_br_addr);
}
}
else {
flash_error = F("writeFloat totPluvio_br_addr FAIL, ERROR totPluvio_br contains not a number!");
textStringLog("ERROR totPluvio_br contains not a number!\7", true);
}
delay(3);
flash.eraseSector(last_neerslagintensiteit_addr);
delay(25);
if (!flash.writeFloat(last_neerslagintensiteit_addr, last_neerslagintensiteit)) {
flash_error = F("writeFloat last_neerslagintensiteit_addr FAIL");
StoreFlashError(flash_error, last_neerslagintensiteit_addr);
}
delay(3);
flash.eraseSector(lastpluvioTime_br_addr);
delay(25);
if (!flash.writeLong(lastpluvioTime_br_addr, lastpluvioTime_br)) {
flash_error = F("writeLong lastpluvioTime_br_addr FAIL");
StoreFlashError(flash_error, lastpluvioTime_br_addr);
}
delay(3);
flash.eraseSector(max_neerslagintensiteit_addr);
delay(25);
if (!flash.writeFloat(max_neerslagintensiteit_addr, max_neerslagintensiteit)) {
flash_error = F("writeFloat max_neerslagintensiteit_addr FAIL");
StoreFlashError(flash_error, max_neerslagintensiteit_addr);
}
delay(3);
flash.eraseSector(maxpluvioTime_br_addr);
delay(25);
if (!flash.writeLong(maxpluvioTime_br_addr, maxpluvioTime_br)) {
flash_error = F("writeLong maxpluvioTime_br_addr FAIL");
StoreFlashError(flash_error, maxpluvioTime_br_addr);
}
delay(3);
storeWeatherCondition1300();
#endif
}
void RestoreDayTotal() {
#ifdef OPTION_FLASH
textStringLog(" = += += += RestoreDayTotal from FLASH = += += += ", true);
byte strd_saveFlag;
strd_saveFlag = flash.readByte(saveFlag_addr);
if (strd_saveFlag != 0xC5) {
textStringLog(" = -= -= -Complete restore skipped. -= -= -= Checking Already Added to PVoutput", true);
//restore the flag if already updated output
AlreadyAdded = flash.readByte(AlreadyAdded_addr);
//restore the flag if already daily mail send
AlreadyDailySend = flash.readByte(AlreadyDailySend_addr);
//restore the flag if already output for this month is send
AlreadyMonthSend = flash.readByte(AlreadyMonthSend_addr);
if (!AlreadyAdded) {
if (iHour >= 13) {
ReadWeatherCondition1300();
textStringLog("partial restore... Weather from 13:00 restored", true);
}
}
return;
}
textStringLog("Restore started.", true);
shadow = flash.readLong(shadow_addr); // 3725
fanAOnCount = flash.readULong(fanAOnCount_addr);
fanBOnCount = flash.readULong(fanBOnCount_addr);
fanCOnCount = flash.readULong(fanCOnCount_addr);
runTimeThisDayfanA = flash.readULong(runTimeThisDayfanA_addr);
runTimeThisDayfanB = flash.readULong(runTimeThisDayfanB_addr);
runTimeThisDayfanC = flash.readULong(runTimeThisDayfanC_addr);
flash.readAnything(TimeMinTemp_addr, TimeMinTemp); //3739,40, 41, 42, 43, 3744, this is the time for minweatherTemp
flash.readAnything(TimeMaxTemp_addr, TimeMaxTemp); //3745,6,7,8,9,3750, this is the time for max weather Temp
flash.readAnything(TimePeakTemp_addr, TimePeakTemp); //3751,2,3, 4,5,3756, this is the time for peak InvTemp
flash.readAnything(TimestartInvTemp_addr, TimestartInvTemp); //3628,9,0, 1,2,3633, this is the time for InvTemp @ wakeup
flash.readAnything(firstShadowTime_addr, firstShadowTime);
flash.readAnything(lastShadowTime_addr, lastShadowTime);
maxtotalToday = flash.readULong(maxtotalToday_addr); // 4000 & 4001
totalToday = maxtotalToday;
if ( !isnan(flash.readFloat(maxInvTemp_addr))) {
maxInvTemp = flash.readFloat(maxInvTemp_addr);
}
else {
maxInvTemp = 0.0;
textStringLog("ERROR: reading maxInvTemp from EEPROM", true);
}
if ( !isnan(flash.readFloat(startInvTemp_addr))) {
startInvTemp = flash.readFloat(startInvTemp_addr);
}
else {
startInvTemp = 0.0;
textStringLog("ERROR: reading startInvTemp from EEPROM", true);
}
PeakPowerObserved = flash.readULong(PeakPowerObserved_addr); // 4002 & 4003
PeakPowerInObserved = flash.readULong(PeakPowerInObserved_addr); // 3792 & 3793
// restore the time for the PeakPower
// we'll need to write the string contents
// plus the string terminator byte (0x00)
flash.readAnything(TimePeakPower_addr, TimePeakPower);