-
Notifications
You must be signed in to change notification settings - Fork 22
/
Daikin-AC-remote.pde
1609 lines (1250 loc) · 43.2 KB
/
Daikin-AC-remote.pde
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
/*
An Arduino sketch to emulate IR Daikin ARC433** remote control unit
Read more on http://harizanov.com/2012/02/control-daikin-air-conditioner-over-the-internet/
Daikin Remote Control Over the Internet by https://github.com/mharizanov/Daikin-AC-remote-control-over-the-Internet is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Martin Harizanov 2012
mharizanov gmail com
*/
#include <EtherCard.h>
#include <avr/eeprom.h>
#include <Time.h>
uint32_t lastUpdate = 0;
uint32_t time =0L;
// Default NTP client
int ntpclientportL = 0;
#define FW_VER 1
#define FW_SUBVER 0
#define TIME_REDIRECT_DELAY 2
#define DEBUG 1 // set to 1 to display free RAM on web page
#define SERIAL 1 // set to 1 to show incoming requests on serial port
#include <IRremote.h>
// # of bytes per command
const int COMMAND_LENGTH = 27;
unsigned char daikin[COMMAND_LENGTH] = {
0x11,0xDA,0x27,0xF0,0x00,0x00,0x00,0x20,
//0 1 2 3 4 5 6 7
0x11,0xDA,0x27,0x00,0x00,0x41,0x1E,0x00,
//8 9 10 11 12 13 14 15
0xB0,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0xE3 };
//16 17 18 19 20 21 22 23 24 25 26
/*
Daikin AC map
byte 7= checksum of the first part (and last byte before a 29ms pause)
byte 13=mode
b7 = 0
b6+b5+b4 = Mode
Modes: b6+b5+b4
011 = Cool
100 = Heat (temp 23)
110 = FAN (temp not shown, but 25)
000 = Fully Automatic (temp 25)
010 = DRY (temp 0xc0 = 96 degrees c)
b3 = 0
b2 = OFF timer set
b1 = ON timer set
b0 = Air Conditioner ON
byte 14=temp*2 (Temp should be between 18 - 32)
byte 16=Fan
FAN control
b7+b6+b5+b4 = Fan speed
Fan: b7+b6+b5+b4
0×30 = 1 bar
0×40 = 2 bar
0×50 = 3 bar
0×60 = 4 bar
0×70 = 5 bar
0xa0 = Auto
0xb0 = Not auto, moon + tree
b3+b2+b1+b0 = Swing control up/down
Swing control up/down:
0000 = Swing up/down off
1111 = Swing up/down on
byte 17
Swing control left/right:
0000 = Swing left/right off
1111 = Swing left/right on
byte 21=Aux -> Powerful (bit 1), Silent (bit 5)
byte 24=Aux2 -> Intelligent eye on (bit 7)
byte 26= checksum of the second part
*/
IRsend irsend;
#define CONFIG_EEPROM_ADDR ((byte*) 0x10)
#define CONFIG_USEDHCP 0
#define CONFIG_IP 1
#define CONFIG_GW 5
#define CONFIG_DNS 9
#define CONFIG_NTP 13 //32 bytes
#define CONFIG_TIMEZONEOFFSET 45
#define CONFIG_HTTPPORT 46
#define CONFIG_HTTPAUTH 48
#define CONFIG_HTTPJQENABLE 49
#define CONFIG_DDNSENABLE 50
#define CONFIG_DDNSUSER 51 //15 char
#define CONFIG_DDNSPASS 66 //15 char
#define CONFIG_HTTPUSER 81 //10 char
#define CONFIG_HTTPPASS 91 //10 char
#define CONFIG_DDNSHOST 101 //32 char
#define CONFIG_WANIP 133
#define CONFIG_VALID 137
const char map64[] PROGMEM =
{
0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
};
const char b64[] PROGMEM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
byte ntp[4]; //= { 204,9,54,119}; //={'n','t','p','.','y','o','u','r','.','o','r','g',0x00};
#define IRSTATE_EEPROM_ADDR ((byte*) 0x100)
struct IRState {
byte mode;
byte temp;
byte fan;
byte aux;
byte state;
byte enabled;
byte sched;
byte hour;
byte minutes;
long lastused;
} irstate;
#define ledPin 6 // the number of the LED pin
#define switchPin 5 // the number of the button pin
// Variables will change:
byte ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
#define interval 500 // interval at which to blink (milliseconds)
//timer vars
#define numtimers 10
long previousMillisTimer = 0;
long previousMillisExtIP = 0;
#define intervalTimer 1000 * 30 //30 sec
const long intervalextip = 5*60*1000; //5 min
const long authorizationInterval = 60*60*1000; // time to keep the user authorized from that IP
long authorizedT = 0;
byte authorizedIP[] = { 0,0,0,0 };
byte extip[] = { 0,0,0,0 };
char DDNSchecksite[] PROGMEM = "checkip.dyndns.org";
char DDNSupdatesite[] PROGMEM = "members.dyndns.org";
char DDNSupdatesite2[] PROGMEM = "members.dyndns.org\r\nAuthorization: Basic ";
static BufferFiller bfill; // used as cursor while filling the buffer
byte Ethernet::buffer[1100]; // tcp/ip send and receive buffer
byte loadConfig(int configopt) {
return eeprom_read_byte(CONFIG_EEPROM_ADDR + configopt);
}
static void loadConfig2(byte *var, int configopt,int len) {
for (byte i = 0; i < len; i++)
var[i] = eeprom_read_byte(CONFIG_EEPROM_ADDR + configopt +i);
}
static void saveConfig(int configopt,byte configval) {
eeprom_write_byte(CONFIG_EEPROM_ADDR + configopt, configval);
}
static void saveConfig2(byte *var,int configopt,int len) {
for (byte i = 0; i < len; i++)
eeprom_write_byte(CONFIG_EEPROM_ADDR + configopt +i, var[i]); //((byte*) &var)[i]
}
static void loadIRstate(int offset) {
for (byte i = 0; i < sizeof irstate; i++)
((byte*) &irstate)[i] = eeprom_read_byte(IRSTATE_EEPROM_ADDR + (offset*sizeof irstate) + i);
}
static void saveIRstate(int offset) {
for (byte i = 0; i < sizeof irstate; i++)
eeprom_write_byte(IRSTATE_EEPROM_ADDR + (offset*sizeof irstate) + i, ((byte*) &irstate)[i]);
}
#if DEBUG
static int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
#endif
static void gotPinged(byte* ptr) {
//ether.printIp(">>> ping from: ",ptr);
}
char okHeader[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
;
char okHeaderjs[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: application/x-javascript\r\n"
"Pragma: no-cache\r\n"
"\r\n"
;
char rdHeader[] PROGMEM =
"HTTP/1.0 302 found\r\n"
"Location: /\r\n"
"\r\n"
;
char rdHTML[] PROGMEM = "\r\n<meta http-equiv=\"refresh\" content=\"$D; url=/$S$S\">Saved.. returning in $D seconds...";
char jqHTML[] PROGMEM =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Remote Control</title>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<link rel='stylesheet' href='http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css' />"
"<script src='http://code.jquery.com/jquery-1.6.4.min.js'></script>"
"<script src='http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js'></script>"
"</head>"
"<body>"
;
char njqHTML[] PROGMEM =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Remote Control</title>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"</head>"
"<body>"
;
// Javascript for printing values in the homepage
prog_uchar js0[] PROGMEM =
"function w(s){document.writeln(s)}\n"
"w(\"Mode:<select data-native-menu='false' id=d name=d><option value='4'>Heat</option><option value='3'>Cool</option><option value='0'>Auto</option><option value='2'>Dry</option></select><br>Temp:<select name=t id=t>\")\n"
"for(i=14;i<32;i++){w(\"<option value='\"+i+\"'>\"+i+\"</option>\");}\n"
"w(\"</select><br>\")\n"
"w(\"Fan:<select data-native-menu='false' id=f name=f><option value='176'>Night</option><option value='160'>Auto</option><option value='48'>*</option><option value='80'>***</option><option value='112'>*****</option></select><br>\")\n"
"w(\"Aux:<select data-native-menu='false' name=a id=a><option value='0'>Normal</option><option value='1'>Powerful</option><option value='16'>Silent</option></select><br>\")\n"
"w(\"State:<select name=s id=s data-role='slider' ><option value='1'>On</option><option value='0'>Off</option></select><br>\");\n"
;
prog_uchar js1[] PROGMEM =
"function w(s){document.writeln(s)}\n"
"w(\"<button type='button' onClick=\\\"parent.location='/'\\\">Back</button>\");\n"
"w(\"<button type='submit'>Set</button>\");";
prog_uchar js2[] PROGMEM =
"function w(s){document.writeln(s)}\n"
"w(\"Go to timer # <br>\");"
"for(i=0;i<10;i++){w(\"<a href='s\"+i+\"' data-ajax='false'>\"+i+\"</a> \");}"
"w(\"<br>Enabled?<select id=e name=e data-role='slider'><option value='0'>No</option><option value='1'>Yes</option></select>\")\n"
"w(\"<br>Day:<select data-native-menu='false' id=c name=c><option value='127'>MTWTFSS</option><option value='31'>MTWTF</option><option value='96'>SS</option><option value='1'>Mon</option><option value='2'>Tue</option><option value='4'>Wed</option><option value='8'>Thu</option><option value='16'>Fri</option><option value='32'>Sat</option><option value='64'>Sun</option></select>\")\n"
"w(\"<br>Hour:<select id=h name=h>\")\n"
"for(i=0;i<24;i++){w(\"<option value='\"+i+\"'>\"+i+\"</option>\");}"
"w(\"</select><br>Min:<select data-native-menu='false' id=m name=m><option value='0'>00</option><option value='15'>15</option><option value='30'>30</option><option value='45'>45</option></select></fieldset><br>\")\n"
;
prog_uchar js3[] PROGMEM =
"function sv(e,v) {\n"
"document.getElementById(e).value=v;}\n"
"function svi(v1,v2,v3,v4,v5) {\n"
"sv('d',v1);"
"sv('t',v2);"
"sv('f',v3);"
"sv('a',v4);"
"sv('s',v5);"
"}\n"
"function svt(v1,v2,v3,v4) {\n"
"sv('e',v1);"
"sv('c',v2);"
"sv('h',v3);"
"sv('m',v4);}\n";
prog_uchar js4[] PROGMEM =
"function w(s){document.writeln(s)}\n"
"function sd(l,v) {\n"
"if(document.getElementById(l)) document.getElementById(l).disabled=v;}\n"
"function di(){\n"
"var v=true;\n"
"if (document.getElementById('a').checked) v=false;\n"
"sd('h',v);\n"
"sd('u',v);\n"
"sd('p',v);}\n"
"function wi(l) {\n"
"w(\"<input type=text name=\"+l+\" id=\"+l+\" value='' size=15 /><br />\");}\n"
"function sv(l,v) {\n"
"document.getElementById(l).value=v;}\n"
"function sc(l,v) {\n"
"document.getElementById(l).checked=v;}\n"
;
byte *htmlWebpage_js[] = {js0,
js1,
js2,
js3,
js4};
static void homePage(BufferFiller& buf) {
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>Main page</h1>"
"</div>"
"<br><ul data-role='listview' data-inset='true'>"
"<li><a href='r' data-ajax='false'>Remote</a></li>"
"<li><a href='s0' data-ajax='false'>Schedule</a></li>"
"<li><a href='c' data-ajax='false'>Network</a></li>"
"<li><a href='n' data-ajax='false'>NTP</a></li>"
"<li><a href='d' data-ajax='false'>DynDNS</a></li>"
"<li><a href='h' data-ajax='false'>HTTP options</a></li>"
"</ul>"
), okHeader, loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML, airConroller_getMode());
// buf.emit_p(PSTR("<br>FW $D.$D<br>"), FW_VER,FW_SUBVER);
#if DEBUG
// buf.emit_p(PSTR("$D bytes free RAM <br>"), freeRam());
#endif
byte d = ((millis() / 1000) / 86400L ); //60*60*24
byte h = ((millis() / 1000) / 3600) % 24;
byte m = ((millis() / 1000) / 60) % 60;
buf.emit_p(PSTR("Up: $D$D days $D$Dh $D$Dm<br>"), d/10, d%10, h/10, h%10, m/10, m%10);
// buf.emit_p(PSTR("NTP time "));
// if(timeStatus()==timeNotSet) buf.emit_p(PSTR("<b>not</b> "));
// buf.emit_p(PSTR("set: $D/$D/$D $D:$D<br>"), year(), month(), day(), hour(),minute() );
buf.emit_p(PSTR("WAN IP: $D.$D.$D.$D<br>"), extip[0], extip[1], extip[2], extip[3]);
// buf.emit_p(PSTR("Client IP: $D.$D.$D.$D"), ether.buffer[0x1A],ether.buffer[0x1B],ether.buffer[0x1C],ether.buffer[0x1D]);
buf.emit_p(PSTR("</body></html>"));
}
static int getIntArg(const char* data, const char* key, int value =-1) {
char temp[10];
memset(temp,NULL,sizeof(temp));
if (ether.findKeyVal(data + 7, temp, sizeof temp, key) > 0)
value = atoi(temp);
return value;
}
static char* getArg(const char* data, const char* key) {
char temp[64];
memset(temp,NULL,sizeof(temp));
ether.findKeyVal(data + 7, temp, sizeof temp, key);
return temp;
}
static void configPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[6] == '?') {
// ether.parseIp(extip, (char*) Ethernet::buffer + off);
// Serial.print((const char*) Ethernet::buffer + off);
byte ip[4];
if(ether.parseIp(ip,getArg(data, "i"))==0)
saveConfig2(ip,CONFIG_IP,4);
if(ether.parseIp(ip,getArg(data, "g"))==0)
saveConfig2(ip,CONFIG_GW,4);
if(ether.parseIp(ip,getArg(data, "d"))==0)
saveConfig2(ip,CONFIG_DNS,4);
bfill.emit_p(rdHeader);
// bfill.emit_p(rdHTML, 2,"c"," ",2 );
return;
}
// else show a configuration form
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>Network Settings</h1>"
"</div>"
"<form data-ajax='false'>"
"<script src='p4.js'></script>"
"<script type='text/javascript'>"
"w('IP:');\n"
"wi('i');\n"
"w('GW:');\n"
"wi('g');\n"
"w('DNS:');\n"
"wi('d');\n"
"sv('i','$D.$D.$D.$D');\n"
"sv('g','$D.$D.$D.$D');\n"
"sv('d','$D.$D.$D.$D');\n"
"</script>"
"<script src='p1.js'></script>"
"</form>"
"</div>"
"</body>"
"</html>"
),okHeader,loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML,
loadConfig(CONFIG_IP),loadConfig(CONFIG_IP+1),loadConfig(CONFIG_IP+2),loadConfig(CONFIG_IP+3),
loadConfig(CONFIG_GW),loadConfig(CONFIG_GW+1),loadConfig(CONFIG_GW+2),loadConfig(CONFIG_GW+3),
loadConfig(CONFIG_DNS),loadConfig(CONFIG_DNS+1),loadConfig(CONFIG_DNS+2),loadConfig(CONFIG_DNS+3));
}
static void RCPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[6] == '?') {
// char* temp;
int temp = getIntArg(data, "s"); //state
switch (temp) {
case 0:
airController_off();
break;
case 1:
airController_on();
break;
default:
break;
}
temp=getIntArg(data, "t"); //temp
if(temp) {
if(temp>=14 && temp <=32) {
airController_setTemp(temp);
// Serial.println(temp);
}
}
temp=getIntArg(data, "f"); //fan
if (temp) {
if(temp >=1 && temp <=250) {
airController_setFan(temp);
// Serial.println(temp);
}
}
temp=getIntArg(data, "d",4); ///mode
if(temp >=0 && temp <=7) {
airController_setMode(temp);
}
temp=getIntArg(data, "a"); //aux
if(temp==0 || temp==1 || temp==16) airController_setAux(temp);
airController_checksum();
irsend.sendDaikin(daikin, 8,0);
delay(29);
irsend.sendDaikin(daikin, 19,8);
// redirect to the home page
bfill.emit_p(rdHeader);
return;
}
// else show a configuration form
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>Remote control</h1>"
"</div>"
"<form data-ajax='false'>"
"<script src=p0.js></script>"
"<script src=p1.js></script>"
"</form>"
"<script src=p3.js></script>"
"<script type='text/javascript'>"
"svi($D,$D,$D,$D,$D);"
"</script>"
"</div>"
"</body>"
"</html>"
),okHeader, loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML, airConroller_getMode(), airConroller_getTemp(),airConroller_getFan(),airController_getAux(), airConroller_getState() );
}
static void SchedPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[7] == '?') {
irstate.mode=getIntArg(data, "d",0); //mode
irstate.temp=getIntArg(data, "t",15); //temp
irstate.fan=getIntArg(data, "f"); //fan
irstate.aux=getIntArg(data, "a"); //aux
irstate.state=getIntArg(data, "s"); //state
irstate.enabled=getIntArg(data, "e"); //enabled
irstate.sched=getIntArg(data, "c"); //sched
irstate.hour=getIntArg(data, "h"); //hour
irstate.minutes=getIntArg(data, "m"); //min
// irstate.lastused=now();
int page=data[6]-'0';
if(page<0 || page>9) return;
saveIRstate(page);
bfill.emit_p(rdHeader);
// bfill.emit_p(rdHTML, TIME_REDIRECT_DELAY, "s",page+"0",TIME_REDIRECT_DELAY );
return;
}
//load page's configuration from EEPROM
loadIRstate(data[6]-'0');
// else show a configuration form
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>Timer $D</h1>"
"</div>"),okHeader,loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML, data[6]-'0');
if(timeStatus()==timeNotSet)
buf.emit_p(PSTR("NTP time <b>not set!</b> "));
bfill.emit_p(PSTR("<br><form data-ajax='false'>"
"<script src=p2.js></script>"
"<script src=p0.js></script>"
"<script src=p1.js></script>"
"</form>"
"<script src=p3.js></script>"
"<script type='text/javascript'>"
"svt($D,$D,$D,$D);"
"svi($D,$D,$D,$D,$D);"
"</script>"
"</div>"
"</body>"
"</html>"),irstate.enabled, irstate.sched,irstate.hour,irstate.minutes,irstate.mode, irstate.temp,irstate.fan,irstate.aux,irstate.state );
}
static void JSONPage(const char* data, BufferFiller& buf) {
bfill.emit_p(PSTR("$F\r\n"
"{\"state\":\"$D\",\"mode\":\"$D\",\"temp\":\"$D\",\"fan\":\"$D\",\"aux\":\"$D\"}"),okHeader, airConroller_getState(), airConroller_getMode(), airConroller_getTemp(),airConroller_getFan(),airController_getAux());
}
// print javascript webpage
static void JSPage(const char* data, BufferFiller& buf, byte idx) {
bfill.emit_p(PSTR("$F$F"), okHeaderjs, htmlWebpage_js[idx]);
}
static void DDNScallbackupd (byte status, word off, word len) {
#if SERIAL
writestrln(PSTR("Got DDNS feedback:"));
Serial.println((char*)&Ethernet::buffer[off]);
#endif
}
// called when the client request is complete
static void DDNScallback (byte status, word off, word len) {
// Ethernet::buffer[off+300] = 0;
// ether.parseIp(extip, (char*) Ethernet::buffer + off);
// Serial.print((const char*) Ethernet::buffer + off);
// Serial.println("...");
const char headerEnd[2] = {'\r','\n' };
int contentLen = 0;
if (off != 0)
{
uint16_t pos = off;
while ( Ethernet::buffer[pos] ) // loop until end of buffer (or we break out having found what we wanted)
{
// Look for line with \r\n on its own
if( strncmp ((char*)&Ethernet::buffer[pos],headerEnd, 2) == 0 ) {
pos += 2;
break;
}
// Scan to end of line
while( Ethernet::buffer[pos++] != '\r' ) { }
while( Ethernet::buffer[pos++] != '\n' ) { }
}
// search for our IP in the http
uint16_t startpos = pos;
uint16_t endpos = pos;
boolean eop = false;
while((Ethernet::buffer[pos]<'.' && Ethernet::buffer[pos] >'9') || Ethernet::buffer[pos]!='\\')
{
pos++;
}
while(!eop){
switch(Ethernet::buffer[pos]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
eop = false;
break;
default:
eop=true;
break;
}
pos++;
}
endpos = pos-1;
Ethernet::buffer[endpos] = '\0';
//Serial.println((char*)&Ethernet::buffer[startpos]);
if(ether.parseIp(extip, (char*)&Ethernet::buffer[startpos])==0) {
byte currwan[4];
loadConfig2(currwan,CONFIG_WANIP,4);
// check if WAN is not zeroes
if(memcmp(currwan,extip,4)!=0) {
//update DynDNS
if(loadConfig(CONFIG_DDNSENABLE)==1) {
saveConfig2(extip,CONFIG_WANIP,4);
updateddns();
} //DDNS updates enabled?
} // WAN changed?
} // WAN IP parses?
} //site responded?
}
static void NTPPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[6] == '?') {
saveConfig2((byte*)getArg(data, "n"),CONFIG_NTP,31);
int tof=getIntArg(data, "t");
saveConfig(CONFIG_TIMEZONEOFFSET,(byte)tof);
bfill.emit_p(rdHeader);
// bfill.emit_p(rdHTML, TIME_REDIRECT_DELAY,"n", " ",TIME_REDIRECT_DELAY );
return;
}
// else show a configuration form
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>NTP Settings</h1>"
"</div>"
),okHeader,loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML);
buf.emit_p(PSTR("NTP time "));
if(timeStatus()==timeNotSet)
buf.emit_p(PSTR("<b>not set!</b> "));
else
buf.emit_p(PSTR("set: $D/$D/$D $D:$D"), year(), month(), day(), hour(),minute() );
bfill.emit_p(PSTR("<form data-ajax='false'>"
"<br>NTP Server: <input type='text' size=32 name=n id=n value='$E'>"
"<br>Time offset: <input type='text' size=2 name=t id=t value='$D'>"
"<br><script src='p1.js'></script>"
"</form>"
"</div>"
"</body>"
"</html>"
), CONFIG_EEPROM_ADDR+CONFIG_NTP ,(int8_t)loadConfig(CONFIG_TIMEZONEOFFSET) ); //CONFIG_EEPROM_ADDR+CONFIG_NTP
}
static void HTTPPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[6] == '?') {
byte temp=*getArg(data, "q");
if(temp!=0) saveConfig(CONFIG_HTTPJQENABLE,1); else saveConfig(CONFIG_HTTPJQENABLE,0);
temp=*getArg(data, "a");
if(temp!=0) {
saveConfig(CONFIG_HTTPAUTH,1);
saveConfig2((byte*)getArg(data, "u"),CONFIG_HTTPUSER,10);
saveConfig2((byte*)getArg(data, "p"),CONFIG_HTTPPASS,10);
}
else saveConfig(CONFIG_HTTPAUTH,0);
uint16_t port=getIntArg(data, "t",80);
saveConfig(CONFIG_HTTPPORT,(byte)port);
saveConfig(CONFIG_HTTPPORT+1,(byte)(port >>8));
bfill.emit_p(rdHeader);
// bfill.emit_p(rdHTML, TIME_REDIRECT_DELAY,"h", " ",TIME_REDIRECT_DELAY );
return;
}
// else show a configuration form
uint16_t port = loadConfig(CONFIG_HTTPPORT+1)<<8;
port+=loadConfig(CONFIG_HTTPPORT);
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>HTTP Options</h1>"
"</div>"
"<form data-ajax='false'>"
"<br>Server port: <input type='text' name=t id=t value='$D'>"
"<br><input type='checkbox' name=q id=q /><label for=q>JQ UI?</label>"
"<br><input type='checkbox' name=a id=a onclick='di()'/><label for=a>HTTP Auth?</label>"
"<br>User: <input type='text' name=u id=u value='$E'>"
"<br>Pass: <input type='text' name=p id=p value='$E'><br>"
"<script src='p4.js'></script>"
"<script src='p1.js'></script>"
),okHeader,loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML, port,CONFIG_EEPROM_ADDR+CONFIG_HTTPUSER,CONFIG_EEPROM_ADDR+CONFIG_HTTPPASS ); //CONFIG_EEPROM_ADDR+CONFIG_NTP
bfill.emit_p(PSTR("<script type='text/javascript'>"
"sc('q',$D);\n"
"sc('a',$D);\n"
"di();\n"
"</script>"
"</form></div></body></html>"
),loadConfig(CONFIG_HTTPJQENABLE),loadConfig(CONFIG_HTTPAUTH));
}
static void DDNSPage(const char* data, BufferFiller& buf) {
// pick up submitted data, if present
if (data[6] == '?') {
byte temp=*getArg(data, "a");
if(temp!=0) {
saveConfig(CONFIG_DDNSENABLE,1);
saveConfig2((byte*)getArg(data, "h"),CONFIG_DDNSHOST,32);
saveConfig2((byte*)getArg(data, "u"),CONFIG_DDNSUSER,10);
saveConfig2((byte*)getArg(data, "p"),CONFIG_DDNSPASS,10);
}
else saveConfig(CONFIG_DDNSENABLE,0);
bfill.emit_p(rdHeader);
// bfill.emit_p(rdHTML, TIME_REDIRECT_DELAY,"d", " ",TIME_REDIRECT_DELAY );
return;
}
// else show a configuration form
bfill.emit_p(PSTR("$F\r\n$F"
"<div data-role='page'>"
"<div data-role='header'>"
"<h1>DDNS Options</h1>"
"</div>"
"<form data-ajax='false'>"
"<br><input type='checkbox' name=a id=a onclick='di()'/><label for=a>DynDNS enabled?</label>"
"<br>Host: <input type='text' name=h id=h value='$E'>"
"<br>User: <input type='text' name=u id=u value='$E'>"
"<br>Pass: <input type='text' name=p id=p value='$E'><br><input type='hidden' name=z value='0'></input>"
"<script src='p4.js'></script>"
"<script src='p1.js'></script>"
),okHeader,loadConfig(CONFIG_HTTPJQENABLE) ? jqHTML : njqHTML,CONFIG_EEPROM_ADDR+CONFIG_DDNSHOST,CONFIG_EEPROM_ADDR+CONFIG_DDNSUSER,CONFIG_EEPROM_ADDR+CONFIG_DDNSPASS ); //CONFIG_EEPROM_ADDR+CONFIG_NTP
bfill.emit_p(PSTR("<script type='text/javascript'>"
"sc('a',$D);\n"
"di();\n"
"</script>"
"</form></div></body></html>"
),loadConfig(CONFIG_DDNSENABLE));
}
void setup(){
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT); // Set the switch pin as input
#if SERIAL
Serial.begin(9600);
writestrln(PSTR("\n[Configuration]"));
#endif
if ((eeprom_read_byte(CONFIG_EEPROM_ADDR + CONFIG_VALID)!=0xAA) || (digitalRead(switchPin) == HIGH)) {
//Load defaults and save to EEPROM
reseteeprom();
}
const byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {
#if SERIAL
writestrln(PSTR("Failed to start ethernet.."));
#endif
}
byte ip[4];
byte gw[4];
byte dns[4];
loadConfig2(ip,CONFIG_IP,4);
loadConfig2(gw,CONFIG_GW,4);
loadConfig2(dns,CONFIG_DNS,4);
if (!ether.staticSetup(ip,gw,dns)) {
#if SERIAL
writestrln(PSTR("Failed static IP address.."));
#endif
}
#if SERIAL
writestr(PSTR("IP:"));
printIp(ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
writestr(PSTR("GW:"));
printIp(ether.gwip);
writestr(PSTR("DNS:"));
printIp(ether.dnsip);
writestr(PSTR("MAC:"));
printMAC(ether.mymac);
Serial.println();
writestr(PSTR("Free RAM: "));
Serial.println(freeRam());
#endif
ether.registerPingCallback(gotPinged);
memset(ntp,0,4);
getntpip();
setSyncInterval(3600*3);
setSyncProvider(getNtpTime);
previousMillisExtIP=millis();
if (ether.dnsLookup(DDNSchecksite)) {
ether.browseUrl(PSTR("/"), "","", DDNSchecksite, DDNScallback);
}
ether.hisport = loadConfig(CONFIG_HTTPPORT);
#if SERIAL
printHelp();
#endif
}
void loop(){
// if (ether.dhcpExpired())
// ether.dhcpSetup();
if(digitalRead(switchPin) == HIGH){
digitalWrite(ledPin, LOW);
airController_checksum();
irsend.sendDaikin(daikin, 8,0);
delay(29);
irsend.sendDaikin(daikin, 19,8);
delay(500);
digitalWrite(ledPin, HIGH);
}
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
// check if valid tcp data is received
if (pos) {
bfill = ether.tcpOffset();
char* data = (char *) Ethernet::buffer + pos;
#if SERIAL
// Serial.println(data);
// Serial.println(freeRam());
#endif
// receive buf hasn't been clobbered by reply yet
byte authorized=false;
if(loadConfig(CONFIG_HTTPAUTH)==0) {
authorized=true;
authorizedT=millis();
}
//If the same IP as the last authorized and time since last auth from that IP is < authorizationInterval then consider the user authorized
if(authorizedIP[0]==ether.buffer[0x1A] && authorizedIP[1]==ether.buffer[0x1B] && authorizedIP[2]==ether.buffer[0x1C] && authorizedIP[3]==ether.buffer[0x1D] && millis()-authorizedT<authorizationInterval ){
authorized=true;
authorizedT=millis();
}
if(getIntArg(data, "z",-1)==7) authorized=true;
int pos2=pos;
while ( Ethernet::buffer[pos2++] ) // loop until end of buffer (or we break out having found what we wanted)
{
// Look for Authorization header along with the hardcoded password
if( strncmp_P ((char*)&Ethernet::buffer[pos2],PSTR("Authorization: Basic "), 21) == 0 ) {
if(verifycred(pos2+21)) {
authorized=true;
authorizedIP[0]= ether.buffer[0x1A]; //If authorized, save the last IP
authorizedIP[1]= ether.buffer[0x1B];
authorizedIP[2]= ether.buffer[0x1C];
authorizedIP[3]= ether.buffer[0x1D];
authorizedT=millis(); //Save time when last authorized, so we don't bother the user with authorization the next "authorizationInterval" millis
break;
}
}
}
if(!authorized) {
bfill.emit_p(PSTR(
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n"
"WWW-Authenticate: Basic realm=\"Remote Controller\""
"\r\n"
"<h1>401 Unauthorized</h1>"));
ether.httpServerReply(bfill.position()); // send web page data
}
else {
if (strncmp_P(data,PSTR("GET / "), 6) == 0)
homePage(bfill);
else if (strncmp_P(data,PSTR("GET /c"), 6) == 0)
configPage(data, bfill);
else if (strncmp_P(data,PSTR("GET /r"), 6) == 0)
RCPage(data, bfill);
else if (strncmp_P(data,PSTR("GET /s"), 6) == 0)
SchedPage(data, bfill);
else if (strncmp_P(data,PSTR("GET /n"), 6) == 0)
NTPPage(data, bfill);
else if (strncmp_P(data,PSTR("GET /h"), 6) == 0)