-
Notifications
You must be signed in to change notification settings - Fork 5
/
Valden_Display.ino
2682 lines (2457 loc) · 85.6 KB
/
Valden_Display.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
/*
Valden Heat Pump.
Remote Display firmware.
https://github.com/OpenHP/
Copyright (C) 2019-2021 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//-----------------------USER OPTIONS-----------------------
const int i2c_DisplayAddr = 0x27; //if 1602: i2c address, if no text on screen: use i2c scanner sketch, typical 0x3f, 0x27, if address ok: tune 1602 contrast
#define T_SETPOINT_MAX 48.0; //maximum "setpoint" temperature that an ordinary user can set
#define T_SETPOINT_MIN 10.0; //min. "setpoint" temperature that an ordinary user can set, lower values are not recommended until antifreeze fluids at hot side are used.
#define T_SETPOINT_DAY 22.00;
#define T_SETPOINT_NIGHT 24.00;
#define T_EEV_SETPOINT 04.00; //ordinary user CANNOT set this value via display! you can change it only from host, value will be transmitted to controller.
#define WATCHDOG //disable for older bootloaders
//-----------------------USER OPTIONS END-----------------------
//#define A_MEGA 2 //service display
#define A_MINI 1 //user display
//-----------------------ADVANCED OPTIONS -----------------------
#define RS485_HP_MODBUS 3 //use this option
//#define RS485_HP_JSON 13 //not implemented
//#define RS485_HOST_MODBUS 11 //implemented but untested, please send bug reports if you'll test this option
#define RS485_HOST_JSON 1 //use this option
const char devID = 0x40; //this device, external net, json communication, see example at https://github.com/openhp/HP-integration-example
const char hostID = 0x30; //host/master, external net, not used if modbus
const char hpID = 0x00; //internal net: do not change if heat pump controller https://github.com/OpenHP/ HP responses to broadcast via Modbus to simplify installation process
#define MAX_SEQUENTIAL_ERRORS 7 //max cycles to wait, before "no connection" message
const int i2c_dsAddr = 0x68; //RTC address, used directly only to check if RTC alive
#define MAGIC 0x45; //change this value if you want to rewrite target T values
//-----------------------ADVANCED OPTIONS END-----------------------
//#define wifi
//#define wifi_ap "ap_q0"
//#define wifi_password "eiyafiafjioucudl"
//-----------------------changelog-----------------------
/*
v1.0, 15 jun 2019:
+ initial version
v1.1, 04 jan 2020:
+ wifi prototyped
v1.2, 06 jan 2019:
+ modbus
v1.3, 06 jun 2020:
+ buttons
+ conditional day/night
+ display,modbus full functionality done
+ poss. DoS: infinite read to nowhere, fix it, set finite counter (ex: 200)
v1.4, 01 dec 2020:
+ "mega" scheme support
v1.5: 04 Feb 2021
+ new "mini" hardware revision (1.6), older ones treated as development branch and not supported anymore
+ auto Ts1 view on main display, if not -127.0
+ local copy of SoftSerial with increased buffer
+ mini: version at startup, credits at startup
v1.6: 27 Feb 2021
+ partial old code (Tsl and so on) cleanup
v1.7: 20 Mar 2021
+ RTC check
v1.8: 21 Mar 2021
+ artefacts from long values prevention at tft
v1.9: 26 Mar 2021:
+ rounding error via Modbus found and fixed
v1.10: 27 Mar 2021:
+ LSM faster redraw
TODO:
- display: rewrite to interrupts
- proto: implement in translated stats get EEV MANUAL mode
*/
//-----------------------changelog END-----------------------
//Connections:
//DS18B20 Pinout (Left to Right, pins down, flat side toward you)
//- Left = Ground
//- Center = Signal (Pin N of arduino): (with 3.3K to 4.7K resistor to +5 or 3.3 )
//- Right = +5 or +3.3 V
//
//
// high volume scheme: +---- +5V (12V not tested)
// |
// +----+
// 1MOhm piezo
// +----+
// |(C)
// pin -> 1.6 kOhms -> (B) 2n2222 < front here
// |(E)
// +--- GND
//
//MAX 485 voltage - 5V
//
// use resistor at RS-485 GND
// 1st test: 10k result lot of issues
// 2nd test: 1k, issues
// 3rd test: 100, see discussions
String fw_version = "1.10";
#ifdef A_MINI
String hw_version = "1.6+";
#endif
#define DISP_ADJ_MENUS 0
#define DISP_ADJ_HOURS 1
#define DISP_ADJ_MINUTES 2
#define DISP_ADJ_DAYTEMP 3
#define DISP_ADJ_NIGHTTEMP 4
#ifdef A_MINI
#define but_left 3 //buton pin for decrease/prev
#define but_mid A2 //buton pin for ok/set
#define but_right 2 //buton pin for increase/next
//external - to server
#define Serial1_TxControl 5 //RS485 Direction control DE and RE to this pin
#define Serial1_RX 6 //RX connected to RO - Receiver Output
#define Serial1_TX 4 //TX connected to DI - Driver Output Pin
//internal - to HP controller
#define Serial2_TxControl A1 //RS485 Direction control DE and RE to this pin
#define Serial2_RX A3 //RX connected to RO - Receiver Output
#define Serial2_TX A0 //TX connected to DI - Driver Output Pin
#endif
#ifdef A_MEGA
#include <Adafruit_GFX.h> // Core graphics library
#include "SWTFT.h" // Hardware-specific library, !!! slightly modified, upload to GitLab with this source !!!
#include <TouchScreen.h>
//external - to server
#define Serial1_TxControl A12 //RS485 Direction control DE and RE to this pin
#define Serial1_RX A13 //RX connected to RO - Receiver Output
#define Serial1_TX A11 //TX connected to DI - Driver Output Pin
//internal - to HP controller
#define Serial2_TxControl A9 //RS485 Direction control DE and RE to this pin
#define Serial2_RX A10 //RX connected to RO - Receiver Output
#define Serial2_TX A8 //TX connected to DI - Driver Output Pin
#endif
#define RS485Transmit HIGH
#define RS485Receive LOW
#define speakerOut 9
#ifdef A_MINI
#include <DS3231.h>
#include "LiquidCrystal_I2C.h"
#include <Wire.h>
#endif
#ifdef WATCHDOG
#include <avr/wdt.h>
#endif
#include <EEPROM.h>
#include <stdio.h>
#include <math.h>
#define SEED 0xFFFF
#define POLY 0xA001
unsigned int crc16;
int cf;
#define MODBUS_MR 50 //50 ok now
const unsigned char regsToReq = 0x22; //max reg N + 1 last was 0x1c now last 0x21
#include "SoftwareSerial.h"
#ifdef A_MINI
LiquidCrystal_I2C lcd(i2c_DisplayAddr,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//RTClib rtc;
//DateTime datetime;
DS3231 Clock;
//char _char_temp[30];
char minute = 0;
char hour = 0;
bool h12;
bool PM;
//bool adjust = false; // adjust clock mode yes/no
//bool ahour, aminute, aday, amonth, ayear, rfin = false; // adjust hour, minute, day ... mode
#endif
#ifdef A_MEGA
double Tci_prev = 0.0;
double Tco_prev = 0.0;
double Tbe_prev = 0.0;
double Tae_prev = 0.0;
double Tho_prev = 0.0;
double Thi_prev = 0.0;
double Tac_prev = 0.0;
double Tbc_prev = 0.0;
double Tcrc_prev = 0.0;
double Ts1_prev = 0.0;
double async_wattage_prev = -100.0;
int EEV_cur_pos_prev = -10;
char heatpump_state_prev = -1;
char hotside_circle_state_prev = -1;
char coldside_circle_state_prev = -1;
char crc_heater_state_prev = -1;
char HP_conn_state_prev = -1;
char lastStopCauseTxt_prev[20];
char lastStartMsgTxt_prev[20];
int errorcode_prev = -1;
double disp_T_setpoint_night_prev = 0.0;
/*bool heatpump_redraw = 0;
bool hotside_circle_redraw = 0;
bool coldside_circle_redraw = 0;
bool crc_heater_state_prev = 0; //!!!*/
char minute = 0;
char hour = 0;
int x1, y1, x2, y2;
#define BACKGROUND 0x0020
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define VERYDARKGREY 0x10A2
#define BLUE 0x001F /* 0, 0, 255 */
#define LIGHTBLUE 0xAEDC
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define DARKRED 0x7800
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF8C3
#define VERYLIGHTRED 0xFC51
#define VERYLIGHTBLUE 0xBDFF
SWTFT tft;
int curr_color = 0;
int curr_color2 = 0;
int curr_color3 = 0;
int prec = 1; //precision
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// Recently Point was renamed TSPoint in the TouchScreen library
// If you are using an older version of the library, use the
// commented definition instead.
// Point p = ts.getPoint();
TSPoint p;
void drawPieSlice(int x, int y, int radius, int color, int startAngle, int EndAngle){
for (int i=startAngle; i<EndAngle; i++){
double radians = i * PI / 180;
double px = x + radius * cos(radians);
double py = y + radius * sin(radians);
tft.drawPixel(px, py, color);
}
}
#endif
//------------------------ Modbus values, readed from HP --------------------------------
double Tci = -127.0;
double Tco = -127.0;
double Tbe = -127.0;
double Tae = -127.0;
double Tsg = -127.0;
double Tsl = -127.0;
double Tbv = -127.0;
double Tsuc = -127.0;
double Ts1 = -127.0;
double Ts2 = -127.0;
double Tcrc = -127.0;
double Treg = -127.0;
double Tac = -127.0;
double Tbc = -127.0;
double Tho = -127.0;
double Thi = -127.0;
double T_setpoint = 0.0;
double T_EEV_setpoint = 0.0;
bool heatpump_state = 0;
bool hotside_circle_state = 0;
bool coldside_circle_state = 0;
bool crc_heater_state = 0;
bool reg_heater_state = 0;
int EEV_cur_pos = 0;
bool EEV_manual = 0; //todo: implement in stats get!!!!
int errorcode = 0;
double async_wattage = 0;
//------------------------ Local values, used to drive HP --------------------------------
double disp_T_setpoint_day = T_SETPOINT_DAY;
double disp_T_setpoint_night = T_SETPOINT_NIGHT;
double disp_T_EEV_setpoint = T_EEV_SETPOINT;
bool disp_T_setpoint_sent = 0;
bool disp_T_EEV_setpoint_sent = 0;
double disp_T_setpoint_day_lastsaved = disp_T_setpoint_day;
double disp_T_setpoint_night_lastsaved = disp_T_setpoint_night;
double disp_T_EEV_setpoint_lastsaved = disp_T_EEV_setpoint;
//------------------------ --------------------------------
const char c_day_start_H = 06;
const char c_day_start_M = 55;
const char c_night_start_H = 23;
const char c_night_start_M = 05;
const double cT_setpoint_max = T_SETPOINT_MAX;
const double cT_setpoint_min = T_SETPOINT_MIN;
#define RS485_1 0
#define RS485_2 1
SoftwareSerial RS485Serial_1(Serial1_RX, Serial1_TX); // RX, TX
SoftwareSerial RS485Serial_2(Serial2_RX, Serial2_TX); // RX, TX
SoftwareSerial * multiSerial[2] = { &RS485Serial_1, &RS485Serial_2 };
char iface = RS485_1;
#ifdef wifi
#define SerialWIFI_RX 11 //RX connected to RO - Receiver Output
#define SerialWIFI_TX 8 //TX connected to DI - Driver Output Pin
SoftwareSerial SerialWIFI(SerialWIFI_RX, SerialWIFI_TX); // RX, TX
String _espspeed = "AT+UART_CUR=9600,8,1,0,0\r\n";
String _espdefspeed = "AT+UART_DEF=115200,8,1,0,0\r\n";
//String _espspeed = "AT+UART=9600\r\n";
bool wifierror = 0;
#endif
//main cycle vars
unsigned long millis_prev = 0;
unsigned long millis_now = 0;
unsigned long millis_cycle = 50;
unsigned long millis_lasteesave = 0;
#ifdef A_MINI
unsigned long millis_modbus03_cycle = 3500;
#endif
#ifdef A_MEGA
unsigned long millis_modbus03_cycle = 3500;
#endif
unsigned long millis_modbus03_prev = 0;
unsigned char sequential_errors = 0;
bool deb_mod = 0;
#define MOD_READY 0
#define MOD_03_SENT 1
unsigned char mod_485_2_state = 0;
unsigned long tmic1 = 0;
unsigned long tmic2 = 0;
const unsigned long c_resp_timeout = 2500; //milliseconds
unsigned long time_sent = 0;
#define BUFSIZE 90
unsigned char dataBuf[BUFSIZE+1]; // Allocate some space for the string
char inChar= -1; // space to store the character read
byte index = 0; // Index into array; where to store the character
unsigned char HP_conn_state = 0; //0 == connection lost 1 == connection OK
unsigned char disp_adj_state = DISP_ADJ_MENUS;
//-------------EEPROM
int eeprom_magic_read = 0x00;
int eeprom_addr = 0x00;
const int eeprom_magic = MAGIC;
//-------------temporary variables
char temp[10];
int i = 0;
int u = 0;
int z = 0;
int x = 0;
int y = 0;
double tempdouble = 0.0;
double tempdouble_intpart = 0.0;
int tempint = 0;
char tempchar = 0;
char fp_integer = 0;
char fp_fraction = 0;
char convBuf[13];
char lastStopCauseTxt[20];
char lastStartMsgTxt[20];
String outString;
String outString_S2;
//----------------------------------------------------------
byte custom_DayChar[] = {
B00000,
B00000,
B00101,
B00101,
B11100,
B10101,
B11101,
B00000
};
byte custom_NightChar[] = {
B00000,
B00000,
B11001,
B10101,
B10100,
B10101,
B10101,
B00000
};
//---------------------------memory debug
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory() {
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
void Calc_CRC(unsigned char b) { //uses/changes y
crc16 ^= b & 0xFF;
for (y=0; y<8; y++) {
cf = crc16 & 0x0001;
crc16>>=1;
if (cf) { crc16 ^= POLY; }
}
}
char Check_CRC() { //uses/changes x
if (index < 3) {
Serial.println(F("Mod.ShortMsg"));
return 1;
}
crc16 = SEED;
for (x = 0; x < (index-2); x++) {
Calc_CRC(dataBuf[x]);
}
if (( dataBuf[index - 2] != (crc16 & 0xFF )) || ( dataBuf[index - 1] != (crc16 >> 8))) {
Serial.println(F("Mod.MsgCRCerr"));
return 1;
}
return 0;
}
void CalcAdd_BufCRC(){
crc16 = SEED;
for (x = 0; x < i; x++) {
Calc_CRC(dataBuf[x]);
}
dataBuf[i] = crc16 & 0xFF;
i++;
dataBuf[i] = crc16 >> 8;
i++;
}
void Add_Double_To_Buf_IntFract (double float_to_convert) { //uses tempdouble tempdouble_intpart fp_integer fp_fraction
if (float_to_convert > 255.0 || float_to_convert < -127.0) {
fp_integer = -127;
fp_fraction = 0;
} else {
tempdouble = modf (float_to_convert , &tempdouble_intpart);
fp_integer = trunc(tempdouble_intpart);
tempdouble = tempdouble * 100;
fp_fraction = round(tempdouble);
}
dataBuf[i] = fp_integer;
i++;
dataBuf[i] = fp_fraction;
i++;
/*Serial.println(float_to_convert);
Serial.println(fp_integer, DEC);
Serial.println(fp_fraction, DEC);*/
}
void IntFract_to_tempdouble (char _int_to_convert, char _fract_to_convert) { //fract is also signed now!
tempdouble = (double) _fract_to_convert / 100;
tempdouble += _int_to_convert;
/*
Serial.println(_int_to_convert);
Serial.println(_fract_to_convert);
Serial.println(tempdouble);*/
}
char ReadModbusMsg(){ //uses index, z
//Serial.println("ReadModbusMsg");
index = 0;
z = 0; //error flag
while ( 1 == 1 ) {//9600
//read
//Serial.println("-");
if (multiSerial[iface]->available()) {
if(index < BUFSIZE) {
inChar = multiSerial[iface]->read();
//Serial.print(inChar, HEX);
//Serial.print(" ");
dataBuf[index] = inChar;
index++;
dataBuf[index] = '\0';
delayMicroseconds(80); //yep, 80, HERE
} else {
z = 1;
while (multiSerial[iface]->available()) {
inChar = multiSerial[iface]->read();
delayMicroseconds(1800);
}
break;
}
} else {
tmic1 = micros();
for (i = 0; i < 10; i++) {
delayMicroseconds(180);
if (multiSerial[iface]->available()){
//Serial.print("babaika");
//Serial.println(i);
tmic2 = micros();
break;
}
tmic2 = micros();
if ( (unsigned long)(tmic2 - tmic1) > 1800 ) {
i = 10;
break;
}
}
if (i == 10 && multiSerial[iface]->available()) {
z = 2;
i = 0;
while (multiSerial[iface]->available()) {
if (i > 200){
break;
}
inChar = multiSerial[iface]->read();
delayMicroseconds(1800);
i++;
}
break;
} else if (!multiSerial[iface]->available()) {
break;
} else if (multiSerial[iface]->available()) {
continue;
} else {
Serial.println(F("e2245"));
}
}
}
return z;
}
#ifdef A_MINI
void Print_D1 () {
lcd.setCursor(0, 0);
//lcd.backlight();
//lcd.clear();
for (i=outString.length(); i<16; i++){
outString += " ";
}
lcd.print(outString);
}
void Print_D2 () {
for (i=outString.length(); i<16; i++){
outString += " ";
}
lcd.setCursor(0, 1);
lcd.print(outString);
}
#endif
#ifdef A_MEGA
void set_precision ( double tocheck ) {
if ( (tocheck > 99.9) || (tocheck < -9.9) ) {
prec = 0;
} else {
prec = 1;
}
}
#endif
void WriteFloatEEPROM(int addr, float val) {
byte *x = (byte *)&val;
for(byte u = 0; u < 4; u++) EEPROM.write(u+addr, x[u]);
}
float ReadFloatEEPROM(int addr) {
byte x[4];
for(byte u = 0; u < 4; u++) x[u] = EEPROM.read(u+addr);
float *y = (float *)&x;
return y[0];
}
void SaveDayNightEE(void) {
if( (disp_T_setpoint_day_lastsaved != disp_T_setpoint_day) &&
( ((unsigned long)(millis_now - millis_lasteesave) > 15*60*1000 ) || (millis_lasteesave == 0) ) ) {
Serial.println(F("EE_Day"));
eeprom_addr = 1;
WriteFloatEEPROM(eeprom_addr, disp_T_setpoint_day);
millis_lasteesave = millis_now;
disp_T_setpoint_day_lastsaved = disp_T_setpoint_day;
}
if( (disp_T_setpoint_night_lastsaved != disp_T_setpoint_night) &&
( ((unsigned long)(millis_now - millis_lasteesave) > 15*60*1000 ) || (millis_lasteesave == 0) ) ) {
Serial.println(F("EE_Night"));
eeprom_addr = 5;
WriteFloatEEPROM(eeprom_addr, disp_T_setpoint_night);
millis_lasteesave = millis_now;
disp_T_setpoint_night_lastsaved = disp_T_setpoint_night;
}
if( (disp_T_EEV_setpoint_lastsaved != disp_T_EEV_setpoint) &&
( ((unsigned long)(millis_now - millis_lasteesave) > 15*60*1000 ) || (millis_lasteesave == 0) ) ) {
Serial.println(F("EE_EEV"));
eeprom_addr = 9;
WriteFloatEEPROM(eeprom_addr, disp_T_EEV_setpoint);
millis_lasteesave = millis_now;
disp_T_EEV_setpoint_lastsaved = disp_T_EEV_setpoint;
}
}
#ifdef wifi
void Read_WIFI (bool _readln = 0, bool _longrun = 0) { //saves data to dataBuf, counter to index, cuts all > 49 symbols
index = 0;
if (SerialWIFI.available() > 0) {
//Serial.println("something on WiFi.."); //!!!debug
i = 0;
while (SerialWIFI.available() > 0) { // Don't read unless you know there is data
if(index < (BUFSIZE)) { // size of the array minus 1
inChar = SerialWIFI.read(); // Read a character
//Serial.print("-");
//Serial.print(inChar);
//Serial.print(inChar, HEX);
dataBuf[index] = inChar; // Store it
index++; // Increment where to write next
dataBuf[index] = '\0'; // clear next symbol, null terminate the string
if (_readln == 1 && dataBuf[index-2] == 0x0D && dataBuf[index-1] == 0x0A){
Serial.print("GotLn:");
Serial.flush();
Serial.write(dataBuf,index);
/*Serial.print(dataBuf[index-1-2], HEX);
Serial.print(dataBuf[index-1-1], HEX);*/
return;
}
delayMicroseconds(80); //80 microseconds - the best choice at 9600, "no answer"disappeared
//40(20??) microseconds seems to be good, 9600, 49 symbols
//
} else { //too long message! read it to nowhere
if ( ((_longrun == 1) && (i > 10000)) || ((_longrun == 0) && (i > 100)) ){
break;
}
inChar = SerialWIFI.read();
//!!!
//Serial.print("+");
//Serial.print(inChar);
delayMicroseconds(80);
i++;
}
}
}
if (index > 0){
Serial.print("Got:");
Serial.print(index);
Serial.flush();
Serial.write(dataBuf,index);
}
}
void Read_WIFI_ln(){
Read_WIFI(1);
}
bool check_AT_OK() {
//Serial.println(index);
if ( index > BUFSIZE || index < 4 || dataBuf[index-1-3] != 0x4F || dataBuf[index-1-2] != 0x4B ) {
return 0;
} else {
return 1;
}
}
void wait_AT_OK() {
index = 0;
z = 0;
while (!check_AT_OK()){
if (z > 20000){
wifierror = 1;
break;
}
delay(1);
index = 0;
Read_WIFI_ln();
//check_AT_OK();
z++;
}
}
void set_WiFi_9600() { //set 9600 or wifierror flag
SerialWIFI.print(F("AT\r\n"));
delay(200);
Read_WIFI();
z = 0;
while ( !check_AT_OK() ) {
Serial.println(F("WiFi set 9600"));
if (z > 200){
wifierror = 1;
break;
}
SerialWIFI.begin(57600);
cli();
SerialWIFI.print(_espspeed);
SerialWIFI.flush();
sei();
delay(250);
Read_WIFI();
SerialWIFI.begin(115200);
cli();
SerialWIFI.print(_espspeed);
SerialWIFI.flush();
sei();
delay(250);
Read_WIFI();
SerialWIFI.begin(9600);
SerialWIFI.print(_espspeed);
delay(250);
Read_WIFI();
SerialWIFI.print(F("AT\r\n"));
delay(200);
Read_WIFI();
z++;
}
}
#endif
void setup() {
#ifdef WATCHDOG
wdt_disable();
#endif
delay(2000);
Serial.begin(9600);
Serial.print(F("Starting, dev_id:"));
Serial.println(devID);
#ifdef A_MINI
Serial.println(F("Valden Remote Display, https://github.com/OpenHP/Display/"));
#endif
#ifdef A_MEGA
Serial.println(F("Valden Service Display, https://github.com/OpenHP/ServiceDisplay/"));
#endif
iface = RS485_1;
multiSerial[iface]->begin(9600);
iface = RS485_2;
multiSerial[iface]->begin(9600);
#ifdef wifi
//tests only
SerialWIFI.begin(9600);
SerialWIFI.listen();
Read_WIFI(0, 1);
set_WiFi_9600();
SerialWIFI.print(F("AT+CWMODE_CUR=2\r\n"));
wait_AT_OK();
delay(10000);
Read_WIFI_ln();
//cnange SSID and MAC
SerialWIFI.print("AT+CIPSTART=0,\"UDP\",\"192.168.0.140\",4445,4445,2\r\n");
while (1 == 1){
for (x = 10; x < 99; x++){
SerialWIFI.print("AT+CWSAP_CUR=\"" + String(x) + "\",\"1\","+ String(random(6,6)) + ",0\r\n");
SerialWIFI.flush();
wait_AT_OK();
SerialWIFI.print("AT+CIPAPMAC_CUR=\"00:00:00:00:00:" + String(x) +"\"\r\n");
SerialWIFI.flush();
wait_AT_OK();
delay(5);
}
}
//client
SerialWIFI.print(F("AT+CWMODE_CUR=1\r\n"));
wait_AT_OK();
//scan APs example
SerialWIFI.print(F("AT+CWLAP\r\n"));
index = 0;
z = 0;
while ( !check_AT_OK() ){
index = 0;
delay(1);
Read_WIFI_ln();
z++;
if (z > 10000){
wifierror = 1;
break;
}
}
//connect to ap example
SerialWIFI.print("AT+CWJAP_CUR=\""+String(wifi_ap)+"\",\""+String(wifi_password)+"\"\r\n");
delay(200);
wait_AT_OK();
SerialWIFI.print("AT+CIPSTA_CUR?\r\n"); //get current IP
//?!?! incompeted answer for unknown reason
delay(200);
wait_AT_OK();
//server start example
SerialWIFI.print("AT+CIPMUX=1\r\n");
delay(200);
wait_AT_OK();
SerialWIFI.print("AT+CIPSERVER=0\r\n");
delay(200);
wait_AT_OK();
SerialWIFI.print("AT+CIPSERVER=1,666\r\n");
delay(200);
wait_AT_OK();
//client example
/*SerialWIFI.print("AT+CIPSEND=0,7,\"192.168.0.140\",4445\r\n");
SerialWIFI.flush();
delay(50);
SerialWIFI.print("hello\r\n");
SerialWIFI.flush();
wait_AT_OK();
*/
#endif
//
eeprom_magic_read = EEPROM.read(eeprom_addr);
eeprom_addr += 1;
if (eeprom_magic_read == eeprom_magic){
Serial.println(F("EEPROM->mem"));
} else {
Serial.println(F("mem->EEPROM"));
WriteFloatEEPROM(eeprom_addr, disp_T_setpoint_day);
eeprom_addr += 4;
WriteFloatEEPROM(eeprom_addr, disp_T_setpoint_night);
eeprom_addr += 4;
WriteFloatEEPROM(eeprom_addr, disp_T_EEV_setpoint);
EEPROM.write(0x00, eeprom_magic);
eeprom_addr = 1;
}
disp_T_setpoint_day = ReadFloatEEPROM(eeprom_addr);
eeprom_addr += 4;
disp_T_setpoint_night = ReadFloatEEPROM(eeprom_addr);
eeprom_addr += 4;
disp_T_EEV_setpoint = ReadFloatEEPROM(eeprom_addr);
//eeprom_addr += 4;
disp_T_setpoint_day_lastsaved = disp_T_setpoint_day;
disp_T_setpoint_night_lastsaved = disp_T_setpoint_night;
disp_T_EEV_setpoint_lastsaved = disp_T_EEV_setpoint;
#ifdef A_MINI
lcd.init();
lcd.noBacklight();
delay(300);
lcd.backlight();
lcd.createChar(0x01, custom_DayChar);
lcd.createChar(0x02, custom_NightChar);
pinMode (but_left, INPUT);
pinMode (but_mid, INPUT);
pinMode (but_right, INPUT);
outString = F("ValdenDisplay");
Print_D1();
outString = F("starting...");
Print_D2();
delay(2500);
outString = "ID: 0x" + String(devID, HEX);
Print_D1();
outString = "Fw:" + fw_version + " HW:" + hw_version;
Print_D2();
delay(2500);
Wire.begin(); //clock
//test
//BAD way, not working
/*while ( 1 == 1 ){
Wire.beginTransmission (i2c_dsAddr);
Wire.write(0x0F); //Control/Status Register to check OSF flag
Wire.endTransmission();
Wire.requestFrom(i2c_dsAddr, 1);
tempchar = Wire.read();
if((tempchar & 0x80) != 0x80) {
break;
} else {
outString = F("ERR: no RTC");
Print_D1();
outString = F("");
Print_D2();
Serial.println("No RTC found! check display hardware!");
tone(speakerOut, 2250);
delay (500);
noTone(speakerOut);
delay (5000);
}
}*/
Clock.setClockMode(false); //24h
#endif
#ifdef A_MEGA
tft.reset();
uint16_t identifier = tft.readID();
Serial.print(F("LCD driver chip: "));
Serial.println(identifier, HEX);
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(BACKGROUND);
delay(100);
tft.fillScreen(BACKGROUND);
//defaults
tft.setTextColor(YELLOW);
tft.setTextSize(2);
//creds
tft.setCursor(1, 50);
tft.println(F("Valden Service Display"));