/
ArduPowerStrip.ino
1431 lines (1071 loc) · 33.4 KB
/
ArduPowerStrip.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
/*
ARDUPOWERSTRIP - JJFALLING ©2012
https://github.com/jjfalling/ArduPowerStrip
Published under GNU GPL.
Much of this is based off of this: https://github.com/ajfisher/arduino-command-server
Also where I found the split library
Flash library found here: http://arduiniana.org/libraries/flash/
Got EmonLib from here: http://openenergymonitor.org
Also modified EmonLib using http://roysoala.wordpress.com/2012/04/20/energy-monitoring-using-pachube-and-arduino-1-0/
TODO:
-enable telent session timeout
-allow only one session
-on/off/reboot/status all
-remove debug option for serial
-add snmp support
-up/down arrows/history?
-snmp support -v5
-various items commented as fix -v4
-change network/hostname over telnet? move said settings to flash. -v4/v5
-free up memory by making as many globals local as possible (voltage, amps, temp/humid, etc). also turn into functions - v4
*/
//##################
//User settings
//##################
//Keep in mind, standard ethernet shield reserves pins 4,10,11,12,13
//Also, you can use analog pins instead of dig pins by using A[pin number]. I have only tested this with relays.
//Network info
byte mac[] = {
0xDE, 0xAD, 0xFE, 0xAB, 0xFE, 0xED };
byte ip[] = {
192, 168, 15, 17 };
byte gateway[] = {
192, 168, 15, 1 };
byte subnet[] = {
255, 255, 255, 0 };
//Hostname
const char hostname[] = "APS-rpc1";
//You need to define the type of relay you are using or how you have it wired. Some relays
// are off when set to low while others are on while set to low. You many need to play with this.
// To try an make this more simple, here is a guide for if your relay is set to NO or NC:
//NC: 1 is off=pin low, 0 is off=pin high | NO: 1 is off=pin high, 0 is off=pin low
#define relayType 0
//What digital pins are your outlets attached to (outlet1 is the first pin listed, outlet2 is the second pin, etc)?
const int outlets[] = {
A0,A1,A2 };
//How long should the delay between off and on during a reboot be (in milliseconds)?
#define rebootDelay 3000
//How long before the telnet session times out(in milliseconds)?
#define telnetTimeout 300000
//What pin is the statusLED connected to?
#define statusLED 2
// What pin is the lcd backlight button connected to?
#define lcdButtonPin 3
//What pin is the lcd serial pin connected to?
#define lcdTxPin 5
//What pin is the "factory default" button connected to?
#define defaultPin 6
//How long should the lcd be on before it turns its self off (in ms) [Set to 0 to disable]?
#define lcdTimeout 600000
//What pin is the internal humid/temp (dht11) sensor on?
#define intDHT11 7
//What pin is the external humid/temp 1 (dht11) sensor on?
#define ext1DHT11 8
//What pin is the external humid/temp 2 (dht11) sensor on?
#define ext2DHT11 9
//Do you want fahrenheit or celsius? True is f, false if c
#define tempF true
//What pin are you using to sense voltage?
#define voltSensorPin 4
//What voltage in the ac line?
#define acVoltage 120
//What is your phase shift?
#define voltphaseShift 1.7
//What pin are you using to sense amperage?
#define ampSensorPin 5
//What amperage calibration?
#define ampCalibration 29
//FIX rm this: Enable serial debug?
#define debug true
//##################
//End user settings
//##################
//start global section
#include <SPI.h>
#include <Ethernet.h>
#include <split.h>
#include <Flash.h>
#include <avr/wdt.h>
#include <SoftwareSerial.h>
#include <dht11.h>
#include <EmonLib.h>
//program name and version
#define _NAME "ArduPowerStrip"
#define _VERSION "0.4"
// Create an instance
EnergyMonitor emon1;
//Define the dht instance (I think?)
dht11 DHT11;
//define software serial for the lcd
SoftwareSerial lcdSerial = SoftwareSerial(255, lcdTxPin);
//disable watchdog, this is needed on newer chips to prevent a reboot loop. However I can't test this...
void wdt_init(void)
{
MCUSR = 0;
wdt_disable();
return;
}
//status led
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 700; // interval at which to blink (milliseconds)
int buttonState = 0; // current state of the button
long previousMillisLCD = 0;
boolean backlight = true; // variable for reading the pin status
int chk;
Print *client = &Serial;
Client *ethcl = NULL;
typedef void (* CommandFuncPtr)(String args); // typedef to the command
struct Command {
char code[7]; // the code used to call the command
String help; // the snippet help version
CommandFuncPtr cmd; // point to the command to be called
};
String command;
//used to store time since last command in telnet
unsigned long telnetTime=0;
//array for sensors (even is humid, odd is temp)
float sensorData[6];
//used for figuring out where we shove data into the array.
int startNum = 0;
//the sensors need a delay between reading, so we need a timer for them.
long previousMillisSensor = 0;
int sensorCounter = 0;
long previousMillisLCDT = 0;
int lcdCounter = 0;
//set this to the number of commands that are defined
#define MAX_COMMANDS 8
Command com[MAX_COMMANDS]={
};
//Don't know why, but for some reason I have to divide by 2 to get the real number
const int numOfOutlets = (sizeof(outlets))/2;
// start server on port 23 (telnet)
EthernetServer server(23);
boolean newClient = false;
boolean allRequested = false;
boolean validateError = false;
byte firstClientIP[4];
//init the ethernet library here so we can use it outside of the main loop.
EthernetClient eclient;
//shove a bunch of data into flash to save on sram...
FLASH_STRING(help_string,"----------------------\n| Available Commands |\n----------------------\nTry help <cmd> for more info\n\n");
FLASH_STRING(info_string,"----------------------\n| System Information |\n----------------------\n");
FLASH_STRING(boot1,"\nBooting...\n\n");
FLASH_STRING(boot2,"Outlet ");
FLASH_STRING(boot3," is pin ");
FLASH_STRING(boot4,"Device ready, telnet to ");
FLASH_STRING(boot5," to use this device.\n");
FLASH_STRING(main1,"Welcome to ");
FLASH_STRING(main2," running ");
FLASH_STRING(main3,"Enter command or type HELP...\n");
FLASH_STRING(process1,"ERR: Unknown command, try HELP?\n");
FLASH_STRING(error_1,"ERR: Outlet number not supplied\n");
FLASH_STRING(error_2,"ERR: Outlet number too high or too many arguments\n");
FLASH_STRING(error_3,"ERR: That outlet does not exist\n");
FLASH_STRING(error_4,"ERR: invalid power_req, this is an internal error that should not happen...\n");
FLASH_STRING(error_5,"ERR: Syntax error please use a valid command\n");
FLASH_STRING(set1,"Setting outlet ");
FLASH_STRING(set2," to ");
FLASH_STRING(off1,"OFF\n");
FLASH_STRING(on1,"ON\n");
FLASH_STRING(reboot1,"Rebooting outlet ");
FLASH_STRING(reboot2,", please wait about ");
FLASH_STRING(reboot3," ms.\n");
FLASH_STRING(done1,"Done.\n");
FLASH_STRING(status4,"Status of outlet ");
FLASH_STRING(status5," is: ");
FLASH_STRING(status6,"OFF\n");
FLASH_STRING(status7,"ON\n");
FLASH_STRING(status8,"UNKNOWN, found value is: ");
FLASH_STRING(info1,"Software: ");
FLASH_STRING(info2,"Version: ");
FLASH_STRING(info3,"Free memory (bytes): ");
FLASH_STRING(info4,"IP: ");
FLASH_STRING(info5,".");
FLASH_STRING(info6,"Mask: ");
FLASH_STRING(info7,"Gateway: ");
FLASH_STRING(info8,"MAC: ");
FLASH_STRING(info9,":");
FLASH_STRING(info10,"Hostname: ");
FLASH_STRING(info11,"Number of outlets: ");
FLASH_STRING(info12,"Reboot delay (ms): ");
FLASH_STRING(info13,"Volts: ");
FLASH_STRING(info14,"Amps: ");
FLASH_STRING(info15,"Int humid/temp: ");
FLASH_STRING(info16,"Ext1 humid/temp: ");
FLASH_STRING(info17,"Ext2 humid/temp: ");
FLASH_STRING(reset1,"Resetting controller.\n");
FLASH_STRING(exit1,"Closing connection. Goodbye...\n");
FLASH_STRING(connect1,"\n\nAnother user is already connected.");
FLASH_STRING(connect2,"\nOnly one user can connect at a time.\nClosing connection. Goodbye...\n\n");
FLASH_STRING(connect3,"Second user tried to connect");
FLASH_STRING(lcd1,"Volts: ");
FLASH_STRING(lcd2,"Amps: ");
FLASH_STRING(lcd3,"Humid I: ");
FLASH_STRING(lcd4,"Humid E1: ");
FLASH_STRING(lcd5,"Humid E2: ");
FLASH_STRING(lcd6,"Temp I: ");
FLASH_STRING(lcd7,"Temp E1: ");
FLASH_STRING(lcd8,"Temp E2: ");
FLASH_STRING(lcd9," Uptime:");
FLASH_STRING(rest1,"Default? press");
FLASH_STRING(rest2,"LCD button to");
FLASH_STRING(rest3,"default settings");
FLASH_STRING(rest4,"or reset device");
FLASH_STRING(rest5,"to cancel and");
FLASH_STRING(rest6,"boot normally");
FLASH_STRING(rest7,"Defaulting all");
FLASH_STRING(rest8,"settings...");
double Irms;
double Vrms;
//end of global section
//##########################
void setup() {
Serial.begin(9600);
client = &Serial;
if (debug) Serial << boot1;
//set the pin modes
int x;
for (x=0; x < numOfOutlets; x++) {
int curr_pin=outlets[x];
pinMode(curr_pin, OUTPUT);
//set init pin state for the relay according to user settings
if (relayType==0){
digitalWrite(curr_pin, HIGH);
}
else {
digitalWrite(curr_pin, LOW);
}
if (debug) Serial << boot2;
if (debug) Serial.print(x +1);
if (debug) Serial << boot3;
if (debug) Serial.println(curr_pin);
}
client = &Serial;
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
Serial.println();
Serial << boot4;
Serial.print(ip[0]);
Serial.print(".");
Serial.print(ip[1]);
Serial.print(".");
Serial.print(ip[2]);
Serial.print(".");
Serial.print(ip[3]);
Serial << boot5;
com[0]=(Command){
"HELP", "Prints this. Try HELP <CMD> for more", command_help };
com[1]=(Command){
"INFO", "Shows system information", command_info };
com[2]=(Command){
"STATUS", "Shows the status of a outlet", command_status };
com[3]=(Command){
"ON", "Sets an outlet to on", command_on };
com[4]=(Command){
"OFF", "Sets an outlet to off", command_off };
com[5]=(Command){
"REBOOT", "Reboots an outlet", command_reboot };
com[6]=(Command){
"QUIT", "Quits this session gracefully", command_quit };
com[7]=(Command){
"RESET", "Preform a software reset on this device (and resets ALL relays!)", command_reset };
//com[8]=(Command){"SET", "Set system params (maybe?)", command_set};
pinMode(statusLED, OUTPUT);
// initialize the button pin as a input:
pinMode(lcdButtonPin, INPUT);
pinMode(lcdTxPin, OUTPUT);
digitalWrite(lcdTxPin, HIGH); //parallax uses this in their example, so I assume its required...
lcdSerial.begin(19200);
delay(100);
lcdSerial.write(22); // Turn backlight on
lcdSerial.write(12); // Clear
delay(5); // Required delay
lcdSerial.write(17); // Turn backlight on
//start up scale chirp thing
lcdSerial.write(216); //scale
lcdSerial.write(208); //note
lcdSerial.write(220); // play a
lcdSerial.write(222); // play b
lcdSerial.write(223); // play c
lcdSerial.write(225); // play d
lcdSerial.write(227); // play e
lcdSerial.write(228); // play f
lcdSerial.write(230); // play g
//print name and version to lcd
lcdSerial.write(128); // line 0 pos 0
lcdSerial.write(" ");
lcdSerial.write(_NAME);
lcdSerial.write(148); // line 1 pos 0
lcdSerial.write(" "); //as the version changes, the padding may need adjusting
lcdSerial.write(_VERSION);
pinMode(defaultPin, INPUT_PULLUP);
// if the resetSwitch is LOW restore password and tcp parameters todefaul values
if ( digitalRead( defaultPin ) == LOW ) {
delay(1000);
Serial.println("Default button pressed");
unsigned long previousMillisSensor = 0;
lcdSerial.write(12); // Clear
int foovar = 0;
int resetMessage = 0;
// lcdSerial.write(128); // line 0 pos 0
// lcdSerial.write("Default? press"); // Turn backlight on
// lcdSerial.write(148); // line 1 pos 0
// lcdSerial.write("LCD button to"); // Turn backlight on
while(foovar < 200){
unsigned long currentMillis = millis();
// read the current state of the button
buttonState = digitalRead(lcdButtonPin);
if (buttonState == HIGH) {
}
if(currentMillis - previousMillisSensor > 2400) {
previousMillisSensor = millis();
if (resetMessage == 0) {
lcdSerial.write(12); // Clear
lcdSerial.write(128); // line 0 pos 0
lcdSerial << rest1;
lcdSerial.write(148); // line 1 pos 0
lcdSerial << rest2;
resetMessage = 1;
}
else if (resetMessage == 1 ) {
lcdSerial.write(12); // Clear
lcdSerial.write(128); // line 0 pos 0
lcdSerial << rest3;
lcdSerial.write(148); // line 1 pos 0
lcdSerial << rest4;
resetMessage = 2;
}
else if (resetMessage == 2 ) {
lcdSerial.write(12); // Clear
lcdSerial.write(128); // line 0 pos 0
lcdSerial << rest5;
lcdSerial.write(148); // line 1 pos 0
lcdSerial << rest6;
resetMessage = 0;
}
}
}
}
//update voltage and amperage data
emon1.voltage(voltSensorPin, acVoltage, voltphaseShift); // Voltage: input pin, calibration, phase_shift
emon1.current(ampSensorPin, ampCalibration); // Current: input pin, calibration. calibration const= 1800/62. CT SCT-013-030 ratio=1800, RL 62ohm
}
void loop() {
eclient = server.available();
//turn status led on since the device is now operational
digitalWrite(statusLED, HIGH);
if (eclient && !newClient) {
if (debug) Serial.println("User connected");
newClient = true;
//FIX: get client ip so the multiple user error message will say who is logged in
//get client's ip
//firstClientIP = eclient.remoteIP()
client = &eclient;
ethcl = &eclient; // set the global to use later
client->println();
client->println();
eclient << main1;
client->print(hostname);
eclient << main2;
client->print(_NAME);
client->print(" ");
client->println(_VERSION);
eclient << main3;
print_prompt();
eclient.flush();
while(eclient.connected()) {
if (eclient.available()){
char ch = eclient.read();
//Serial.println(ch, DEC);
if (ch == 10) {
// new line so now we can attempt to process the line
//eclient.println(command);
process_command(&command);
command = "";
}
else if ((ch < 10 && ch > 0) || (ch > 10 && ch < 32)) {
// ignore control chars up to space and allow nulls to pass through
;
}
else {
command += String(ch);
}
}
//Blink status led while there is an active telnet session
//This is from http://arduino.cc/en/Tutorial/BlinkWithoutDelay
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(statusLED, ledState);
}
//control lcd backlight
controlLCDBacklight();
//update sensors
updateSensors();
//write data to lcd
writeLCD();
//FIX: calculating the power causes the prompt to freeze for about 2 seconds...
//update power usage
Irms = emon1.calcIrms(1480); // Calculate Irms only
Vrms = emon1.calcVrms(1480); // Calculate Vrms only
}
if (!eclient.connected() && newClient) {
eclient.stop();
newClient = false;
if (debug) Serial.println("User disconnected");
}
}
else if (eclient) {
Serial << connect3;
eclient << connect1;
// eclient << firstClientIP;
eclient << connect2;
client->println();
eclient.stop();
}
//control lcd backlight
controlLCDBacklight();
//update sensors
updateSensors();
//write data to lcd
writeLCD();
//update power usage
Irms = emon1.calcIrms(1480); // Calculate Irms only
Vrms = emon1.calcVrms(1480); // Calculate Vrms only
}
//############################
//start defining commands/functions
void process_command(String* command) {
// this method takes the command string and then breaks it down
// looking for the relevant command and doing something with it or giving an error.
String argv[2]; // we have 2 args, the command and the param
split(' ', *command, argv, 1); // so split only once
int cmd_index = command_item(argv[0]);
//check if the all param was used. if so, change the allRequested to true
if (argv[1] == "all"){
allRequested = true;
}
if (cmd_index >= 0) {
com[cmd_index].cmd(argv[1]);
}
else {
eclient << process1;
print_prompt();
}
return;
}
int command_item(String cmd_code) {
// this method does all of the comparison stuff to determine the id of a command
// which it then passes back
int i=0;
boolean arg_found = false;
// look through the array of commands until you find it or else you exhaust the list.
while (!arg_found && i<MAX_COMMANDS) {
if (cmd_code.equalsIgnoreCase((String)com[i].code)) {
arg_found = true;
}
else {
i++;
}
}
if (arg_found) {
return (i);
}
else {
return (-1);
}
}
void command_status(String args) {
// argument passed in should simply be a number and it's that one we read.
// we do need to get both chars though because it can be 2 digits
int pin = atoi(&args[0]);
validatePin(pin, args);
if (validateError == true){
validateError = false;
return;
}
else {
int realPin = pin -1;
realPin = outlets[realPin];
client->println();
eclient << status4;
client->print(pin);
eclient << status5;
int stat_pin = (digitalRead(realPin));
if (relayType == 0) {
switch (stat_pin){
//is off
case false:
eclient << status6;
print_prompt();
break;
//is on
case true:
eclient << status7;
print_prompt();
break;
//unknown
default:
eclient << status8;
client->println(stat_pin);
print_prompt();
break;
}
}
else {
switch (stat_pin){
//is off
case true:
eclient << status6;
print_prompt();
break;
//is on
case false:
eclient << status7;
print_prompt();
break;
//unknown
default:
eclient << status8;
client->println(stat_pin);
print_prompt();
break;
}
}
}
}
void command_on(String args) {
// argument passed in should simply be a number and it's that one we read.
// we do need to get both chars though because it can be 2 digits
int pin = atoi(&args[0]);
validatePin(pin, args);
if (validateError == true){
validateError = false;
return;
}
else {
int realPin = pin -1;
realPin = outlets[realPin];
client->println();
eclient << set1;
client->print(pin);
eclient << set2;
eclient << on1;
set_outlet(realPin, 1);
eclient << done1;
print_prompt();
}
}
void command_off(String args) {
// argument passed in should simply be a number and it's that one we read.
// we do need to get both chars though because it can be 2 digits
int pin = atoi(&args[0]);
validatePin(pin, args);
if (validateError == true){
validateError = false;
return;
}
else {
int realPin = pin -1;
realPin = outlets[realPin];
client->println();
eclient << set1;
client->print(pin);
eclient << set2;
eclient << off1;
set_outlet(realPin, 2);
eclient << done1;
print_prompt();
}
}
void command_reboot(String args) {
// argument passed in should simply be a number and it's that one we read.
// we do need to get both chars though because it can be 2 digits
int pin = atoi(&args[0]);
validatePin(pin, args);
if (validateError == true){
validateError = false;
return;
}
else {
int realPin = pin -1;
realPin = outlets[realPin];
client->println();
eclient << reboot1;
client->print(pin);
eclient << reboot2;
client->print(rebootDelay);
eclient << reboot3;
set_outlet(realPin, 3);
eclient << done1;
print_prompt();
}
}
void set_outlet(int pin, int power_req) {
switch (power_req){
//on
case 1:
{
if (relayType == 0) {
digitalWrite(pin, HIGH);
}
else {
digitalWrite(pin, LOW);
}
}
break;
//off
case 2:
{
if (relayType == 0) {
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin, HIGH);
}
}
break;
//reboot
//FIX: change from delay to something else so other tasks dont pause (such as status light, sensor updates, etc)
case 3:
{
if (relayType == 0) {
digitalWrite(pin, LOW);
delay (rebootDelay);
digitalWrite(pin, HIGH);
}
else {
digitalWrite(pin, HIGH);
delay (rebootDelay);
digitalWrite(pin, LOW);
}
}
break;
//something went wrong
default:
{
if (debug) Serial << error_4;
client->println();
eclient << error_4;
}
break;
}
}
//to save a bit of memory, making this into a function
void print_prompt() {
client->println();
client->print("> ");
}
//user requests help
void command_help(String args) {
// this command spits out the help messages
int cmd_index;
if (args.length() >=2) {
// we attempt to see if there is a command we should spit out instead.
cmd_index = command_item(args);
if (cmd_index < 0) eclient << error_5;
}
else {
cmd_index = -1;
}
if (cmd_index < 0) {
client->println();
eclient << help_string;
for (int i=0; i<MAX_COMMANDS; i++){
if (com[i].help != ""){
client->print(com[i].code);
if (i % 3 == 0) {
client->println();
}
else {
client->print("\t");
}
}
}
client->println();
print_prompt();
}
else {
// client->println("HELP");
client->println();
client->print(com[cmd_index].code);
client->print(": ");
client->println(com[cmd_index].help);
client->println();
print_prompt();
}
}
//user requests info
void command_info(String args) {
// this command spits out the info messages
client->println();
eclient << info_string;
client->println();
eclient << info1;
client->println(_NAME);
eclient << info2;
client->print(_VERSION);
client->println();
//ip address
eclient << info4;
client->print(ip[0]);
eclient << info5;
client->print(ip[1]);
eclient << info5;
client->print(ip[2]);
eclient << info5;
client->println(ip[3]);
//subnet mask
eclient << info6;
client->print(subnet[0]);
eclient << info5;
client->print(subnet[1]);
eclient << info5;
client->print(subnet[2]);
eclient << info5;
client->println(subnet[3]);
//gateway
eclient << info7;
client->print(gateway[0]);
eclient << info5;
client->print(gateway[1]);
eclient << info5;
client->print(gateway[2]);
eclient << info5;
client->println(gateway[3]);
//mac address
eclient << info8;
client->print(mac[0], HEX);
eclient << info9;