Advertisement
kentnoel

Kent's Arduino Home Automation

Nov 4th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 37.03 KB | None | 0 0
  1. //simple button GET server code to control 4 relays with arduino pins
  2. //for use with Arduino IDE 1.0
  3. //open serial monitor to see what the arduino receives
  4. //use the \ slash to escape the " in the html
  5. //for use with Wiznet W5100 based ethernet shields
  6. //added uptime shown in serial monitor and bottom of web page
  7. //added multiple buttons to alternately control lights/fans
  8. //added button control of relays at Arduino location
  9. //added LED status display for relay-controlled circuits at Arduino location
  10. //buttons are debounced with millis()
  11. //added serial control for all circuits by input (a-k)
  12. //added AJAX using code from ...
  13. //http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-read-switch-using-AJAX/
  14.  
  15. // ******
  16. //see my comprehensive tutorial on how I put most of this together at
  17. //http://www.instructables.com/id/Arduino-Powered-Home/
  18. // ******
  19.  
  20. #include <SPI.h>
  21. #include <Ethernet.h>
  22.  
  23.  
  24. byte mac[] = {
  25.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  26. byte ip[] = {
  27.   192, 168, 1, 97 }; // ip
  28. byte gateway[] = {
  29.   192, 168, 1, 1 }; // gateway
  30. byte subnet[] = {
  31.   255, 255, 255, 0 }; //subnet mask
  32. EthernetServer server(80); //server port
  33.  
  34. String readString;
  35. //////////////////////
  36.  
  37. int relay1 = 5;                 // relay IN1 connected to digital pin 5
  38. int relay2 = 6;                 // relay IN2 connected to digital pin 6
  39. int relay3 = 7;                 // relay IN3 connected to digital pin 7
  40. int relay4 = 8;                 // relay IN4 connected to digital pin 8
  41. int rmtsw1 = 28;                // Power switch tail remote control switch 1 (channel 1 on)
  42. int rmtsw2 = 30;                // Power switch tail remote control switch 2 (channel 1 off)
  43. int rmtsw3 = 32;                // Power switch tail remote control switch 3 (channel 2 on)
  44. int rmtsw4 = 34;                // Power switch tail remote control switch 4 (channel 2 off)
  45. int rmtsw5 = 36;                // Power switch tail remote control switch 5 (channel 3 on)
  46. int rmtsw6 = 38;                // Power switch tail remote control switch 6 (channel 3 off)
  47.  
  48.  
  49. int statesDisplay1 = 29;        // digital pin 29 connected to LED7 to indicate circuit status when Button5 pressed
  50. int statesDisplay2 = 33;        // digital pin 33 connected to LED6 to indicate circuit status when Button5 pressed
  51. int statesDisplay3 = 37;        // digital pin 37 connected to LED5 to indicate circuit status when Button5 pressed
  52. int statesDisplay4 = 41;        // digital pin 41 connected to LED4 to indicate circuit status when Button5 pressed
  53. int statesDisplay5 = 45;        // digital pin 45 connected to LED3 to indicate circuit status when Button5 pressed
  54. int statesDisplay6 = 49;        // digital pin 49 connected to LED2 to indicate circuit status when Button5 pressed
  55.  
  56. //Button Press Detection - debounce with millis()
  57.  
  58. int buttonPin1 = 22;           // pin connected to button1 for control of livingroom lights
  59. int buttonPin2 = 23;           // pin connected to button2 for control of livingroom ceiling fan
  60. int buttonPin3 = 24;           // pin connected to button3 for control of master bedroom light
  61. int buttonPin4 = 25;           // pin connected to button4 for control of master bedroom ceiling fan
  62. int buttonPin5 = 26;           // pin connected to button5 for control of function allStatesDisplay()
  63.  
  64.  
  65. boolean currentState1 = LOW;//storage for current measured button1 state
  66. boolean currentState2 = LOW;//storage for current measured button2 state
  67. boolean currentState3 = LOW;//storage for current measured button3 state
  68. boolean currentState4 = LOW;//storage for current measured button4 state
  69. boolean currentState5 = LOW;//storage for current measured button5 state
  70. boolean lastState1 = LOW;//storage for last measured button1 state
  71. boolean lastState2 = LOW;//storage for last measured button2 state
  72. boolean lastState3 = LOW;//storage for last measured button3 state
  73. boolean lastState4 = LOW;//storage for last measured button4 state
  74. boolean lastState5 = LOW;//storage for last measured button5 state
  75. boolean debouncedState1 = LOW;//debounced button1 state
  76. boolean debouncedState2 = LOW;//debounced button2 state
  77. boolean debouncedState3 = LOW;//debounced button3 state
  78. boolean debouncedState4 = LOW;//debounced button4 state
  79. boolean debouncedState5 = LOW;//debounced button5 state
  80.  
  81. int debounceInterval = 20;//wait 20 ms for button1 pin to settle
  82. unsigned long timeOfLastButtonEvent1 = 0;//store the last time button1 state changed
  83. unsigned long timeOfLastButtonEvent2 = 0;//store the last time button2 state changed
  84. unsigned long timeOfLastButtonEvent3 = 0;//store the last time button3 state changed
  85. unsigned long timeOfLastButtonEvent4 = 0;//store the last time button4 state changed
  86. unsigned long timeOfLastButtonEvent5 = 0;//store the last time button5 state changed
  87. unsigned long currentMillis = 0;  // store current time
  88.  
  89. int pinState1 = digitalRead(relay1);
  90. int pinState2 = digitalRead(relay2);
  91. int pinState3 = digitalRead(relay3);
  92. int pinState4 = digitalRead(relay4);
  93. int pinState5 = digitalRead(buttonPin1);
  94. int pinState6 = digitalRead(buttonPin2);
  95. int pinState7 = digitalRead(buttonPin3);
  96. int pinState8 = digitalRead(buttonPin4);
  97.  
  98. //enable read on serial port
  99. int incomingByte = 0;   // for incoming serial data
  100.  
  101. void setup(){
  102.  
  103.   pinMode(relay1, OUTPUT);             //pin to control livingroom lights
  104.   pinMode(relay2, OUTPUT);             //pin to control livingroom ceiling fan
  105.   pinMode(relay3, OUTPUT);             //pin to control master bedroom light
  106.   pinMode(relay4, OUTPUT);             //pin to control master bedroom ceiling fan
  107.   pinMode(buttonPin1, INPUT);          //pin to monitor button1 state, not necessary to declare an input
  108.   pinMode(buttonPin2, INPUT);          //pin to monitor button2 state, not necessary to declare an input
  109.   pinMode(buttonPin3, INPUT);          //pin to monitor button3 state, not necessary to declare an input
  110.   pinMode(buttonPin4, INPUT);          //pin to monitor button4 state, not necessary to declare an input
  111.   pinMode(buttonPin5, INPUT);          //pin to monitor button5 state, not necessary to declare an input
  112.   digitalWrite(relay1, HIGH);          //Prevents relays from starting up engaged
  113.   digitalWrite(relay2, HIGH);          //Prevents relays from starting up engaged
  114.   digitalWrite(relay3, HIGH);          //Prevents relays from starting up engaged
  115.   digitalWrite(relay4, HIGH);          //Prevents relays from starting up engaged
  116.   pinMode(statesDisplay1, OUTPUT);      //pin to display circuit 6 status via LED6
  117.   pinMode(statesDisplay2, OUTPUT);      //pin to display circuit 5 status via LED5
  118.   pinMode(statesDisplay3, OUTPUT);      //pin to display circuit 4 status via LED4
  119.   pinMode(statesDisplay4, OUTPUT);      //pin to display circuit 3 status via LED3
  120.   pinMode(statesDisplay5, OUTPUT);      //pin to display circuit 2 status via LED2
  121.   pinMode(statesDisplay6, OUTPUT);      //pin to display circuit 1 status via LED1
  122.   digitalWrite(statesDisplay1, LOW);
  123.   digitalWrite(statesDisplay2, LOW);
  124.   digitalWrite(statesDisplay3, LOW);
  125.   digitalWrite(statesDisplay4, LOW);
  126.   digitalWrite(statesDisplay5, LOW);
  127.   digitalWrite(statesDisplay6, LOW);
  128.   pinMode(rmtsw1, OUTPUT);             //pin to trigger remote control channel 1 on
  129.   pinMode(rmtsw2, OUTPUT);             //pin to trigger remote control channel 1 off
  130.   pinMode(rmtsw3, OUTPUT);             //pin to trigger remote control channel 2 on
  131.   pinMode(rmtsw4, OUTPUT);             //pin to trigger remote control channel 2 off
  132.   pinMode(rmtsw5, OUTPUT);             //pin to trigger remote control channel 3 on
  133.   pinMode(rmtsw6, OUTPUT);             //pin to trigger remote control channel 3 off
  134.   digitalWrite(rmtsw1, LOW);
  135.   digitalWrite(rmtsw2, LOW);
  136.   digitalWrite(rmtsw3, LOW);
  137.   digitalWrite(rmtsw4, LOW);
  138.   digitalWrite(rmtsw5, LOW);
  139.   digitalWrite(rmtsw6, LOW);
  140.  
  141.  
  142.   //start Ethernet connection and server
  143.   Ethernet.begin(mac, ip, gateway, subnet);
  144.   server.begin();
  145.   //enable serial data print
  146.   Serial.begin(9600);
  147.   Serial.println("home automation server v24a with AJAX for livingroom fan only"); // so I can keep track of what is loaded
  148.   Serial.print("Free memory: ");
  149.   Serial.println(freeRam());
  150.   Serial.println("----------");
  151.   Serial.println();
  152. }
  153.  
  154. void loop(){
  155.  
  156.   //enable serial data read
  157.   if (Serial.available() > 0) {
  158.     // read the incoming byte:
  159.     int incomingByte = Serial.read();
  160.     // say what you got:
  161.     Serial.print("I received: ");
  162.     Serial.println(incomingByte, DEC);
  163.     // do something different depending on the character received via serial.  
  164.     // The switch statement expects single number values for each case;
  165.     // in this example, though, you're using single quotes to tell
  166.     // the controller to get the ASCII value for the character.  For
  167.     // example 'a' = 97, 'b' = 98, and so forth:
  168.  
  169.     switch (incomingByte) {
  170.     case 'a':    
  171.       //do something here like livingroom light on;
  172.       if (digitalRead(relay1) >0){  // >0 is HIGH and this is active low relay circuit, so it's off
  173.         digitalWrite(relay1,LOW);      // LOW is on for active low relay circuit
  174.         Serial.println("Relay1/livingroom lights are ON.");
  175.       }
  176.       break;
  177.     case 'b':    
  178.       //do something here like livingroom light off;
  179.       if (digitalRead(relay1) <1){  // <1 is LOW and this is active low relay circuit, so it's on
  180.         digitalWrite(relay1,HIGH);      // HIGH is off for active low relay circuit
  181.         Serial.println("Relay1/livingroom lights are OFF.");
  182.       }
  183.       break;
  184.     case 'c':    
  185.       //do something here like livingroom fan on;
  186.       if (digitalRead(relay2) >0){  // >0 is HIGH and this is active low relay circuit, so it's off
  187.         digitalWrite(relay2,LOW);      // LOW is on for active low relay circuit
  188.         Serial.println("Relay2/livingroom fan is ON.");
  189.       }
  190.       break;
  191.     case 'd':    
  192.       //do something here like livingroom fan off;
  193.       if (digitalRead(relay2) <1){  // <1 is LOW and this is active low relay circuit, so it's on
  194.         digitalWrite(relay2,HIGH);      // HIGH is off for active low relay circuit
  195.         Serial.println("Relay2/livingroom fan is OFF.");
  196.       }
  197.       break;
  198.     case 'e':    
  199.       //do something here like bedroom light on;
  200.       if (digitalRead(relay3) >0){  // >0 is HIGH and this is active low relay circuit, so it's off
  201.         digitalWrite(relay3,LOW);      // LOW is on for active low relay circuit
  202.         Serial.println("Relay3/bedroom light is ON.");
  203.       }
  204.       break;
  205.     case 'f':
  206.       //do something here like bedroom light off;
  207.       if (digitalRead(relay3) <1){  // <1 is LOW and this is active low relay circuit, so it's on
  208.         digitalWrite(relay3,HIGH);      // HIGH is off for active low relay circuit
  209.         Serial.println("Relay3/bedroom light is OFF.");
  210.       }
  211.       break;
  212.     case 'g':
  213.       //do something here like bedroom fan on;
  214.       if (digitalRead(relay4) >0){  // >0 is HIGH and this is active low relay circuit, so it's off
  215.         digitalWrite(relay4,LOW);      // LOW is on for active low relay circuit
  216.         Serial.println("Relay4/bedroom fan is ON.");
  217.       }
  218.       break;
  219.     case 'h':
  220.       //do something here like bedroom fan off;
  221.       if (digitalRead(relay4) <1){  // <1 is LOW and this is active low relay circuit, so it's on
  222.         digitalWrite(relay4,HIGH);      // HIGH is off for active low relay circuit
  223.         Serial.println("Relay4/bedroom fan is OFF.");
  224.       }
  225.       break;
  226.     case 'i':
  227.       //do something here like auto-reset disable; to disable auto-reset I close
  228.       //transistor circuit via A0 connecting capacitor between reset and ground.
  229.       //      if (digitalRead(myResetPin) <1){  // <1 is LOW and this is active high circuit, so auto-reset is enabled
  230.       //        digitalWrite(myResetPin,HIGH);    // HIGH is on, or auto-reset disabled
  231.       Serial.println("case 'i' has been disabled!");
  232.       //      }
  233.       //        Serial.println("Auto-reset is disabled for normal operation.");
  234.       break;
  235.     case 'j':
  236.       //do something here like auto-reset enable; to enable auto-reset I open
  237.       //transistor circuit via A0 disconnecting capacitor between reset and ground.
  238.       //      if (digitalRead(myResetPin) >0){  // >0 is HIGH and this is active high circuit, so auto-reset is disabled
  239.       //        digitalWrite(myResetPin,LOW);     // LOW is off, or auto-reset enabled
  240.       Serial.println("case 'j' has been disabled!");
  241.       //      }
  242.       //        Serial.println("Auto-reset is enabled for programming.");
  243.       break;
  244.     case 'k':
  245.       //do something here like print state of all output pins used, D5-D8, A0.
  246.       Serial.print("D5 state is ");
  247.       Serial.print(digitalRead(relay1));
  248.       Serial.print(".");
  249.       Serial.print(" Livingroom lights are ");
  250.       if (digitalRead(relay1) >0){
  251.         Serial.println("OFF.");
  252.       }
  253.       else
  254.       {
  255.         Serial.println("ON.");
  256.       }
  257.       Serial.print("D6 state is ");
  258.       Serial.print(digitalRead(relay2));
  259.       Serial.print(".");
  260.       Serial.print(" Livingroom fan is ");
  261.       if (digitalRead(relay2) >0){
  262.         Serial.println("OFF.");
  263.       }
  264.       else
  265.       {
  266.         Serial.println("ON.");
  267.       }
  268.       Serial.print("D7 state is ");
  269.       Serial.print(digitalRead(relay3));
  270.       Serial.print(".");
  271.       Serial.print(" Bedroom light is ");
  272.       if (digitalRead(relay3) >0){
  273.         Serial.println("OFF.");
  274.       }
  275.       else
  276.       {
  277.         Serial.println("ON.");
  278.       }
  279.       Serial.print("D8 state is ");
  280.       Serial.print(digitalRead(relay4));
  281.       Serial.print(".");
  282.       Serial.print(" Bedroom fan is ");
  283.       if (digitalRead(relay4) >0){
  284.         Serial.println("OFF.");
  285.       }
  286.       else
  287.       {
  288.         Serial.println("ON.");
  289.       }
  290.       //        Serial.print("A0 state is ");
  291.       //        Serial.print(digitalRead(myResetPin));
  292.       //        Serial.print(". Auto-reset is ");
  293.       //          if (digitalRead(myResetPin) >0){  // >0 is HIGH and this is active high circuit, so auto-reset is disabled
  294.       //            Serial.println("disabled for normal operation.");
  295.       //      }
  296.       //      else
  297.       //      {
  298.       //        Serial.println("enabled for programming.");
  299.       //        }
  300.       break;
  301.     }
  302.   }
  303.  
  304.   //livingroom lights
  305.   currentState1 = digitalRead(buttonPin4);
  306.   unsigned long currentTime1 = millis();
  307.  
  308.   if (currentState1 != lastState1){
  309.     timeOfLastButtonEvent1 = currentTime4;
  310.   }
  311.  
  312.   if (currentTime1 - timeOfLastButtonEvent1 > debounceInterval){      //if enough time has passed
  313.     if (currentState1 != debouncedState1){                            //if the current state is still different than our last stored debounced state
  314.       debouncedState1 = currentState1;                                //update the debounced state
  315.  
  316.       //trigger an event for livingroom lights
  317.       if (debouncedState1 == HIGH){
  318.         Serial.println("Button4 pressed");
  319.         digitalWrite(relay1, !digitalRead(relay1));                             // invert state of pin 5
  320.         Serial.println("----------");
  321.         Serial.println();
  322.       }
  323.       else {
  324.         allStatesDisplay();
  325.         Serial.println("Button4 released");
  326.         Serial.println("----------");
  327.         Serial.println();
  328.       }
  329.     }
  330.   }
  331.  
  332.   lastState1 = currentState1;  
  333.  
  334.  
  335.   //livingroom fan
  336.   currentState2 = digitalRead(buttonPin1);
  337.   unsigned long currentTime2 = millis();
  338.  
  339.   if (currentState2 != lastState2){
  340.     timeOfLastButtonEvent2 = currentTime2;
  341.   }
  342.  
  343.   if (currentTime2 - timeOfLastButtonEvent2 > debounceInterval){      //if enough time has passed
  344.     if (currentState2 != debouncedState2){                            //if the current state is still different than our last stored debounced state
  345.       debouncedState2 = currentState2;                                //update the debounced state
  346.  
  347.       //trigger an event for livingroom fan
  348.       if (debouncedState2 == HIGH){
  349.         Serial.println("Button1 pressed");
  350.         digitalWrite(relay2, !digitalRead(relay2));                             // invert state of pin 6
  351.         Serial.println("----------");
  352.         Serial.println();
  353.       }
  354.       else {
  355.         allStatesDisplay();
  356.         Serial.println("Button1 released");
  357.         Serial.println("----------");
  358.         Serial.println();
  359.       }
  360.     }
  361.   }
  362.  
  363.   lastState2 = currentState2;  
  364.  
  365.   //master bedroom light
  366.   currentState3 = digitalRead(buttonPin2);
  367.   unsigned long currentTime3 = millis();
  368.  
  369.   if (currentState3 != lastState3){
  370.     timeOfLastButtonEvent3 = currentTime3;
  371.   }
  372.  
  373.   if (currentTime3 - timeOfLastButtonEvent3 > debounceInterval){      //if enough time has passed
  374.     if (currentState3 != debouncedState3){                            //if the current state is still different than our last stored debounced state
  375.       debouncedState3 = currentState3;                                //update the debounced state
  376.  
  377.       //trigger an event for master bedroom light
  378.       if (debouncedState3 == HIGH){
  379.         Serial.println("Button2 pressed");
  380.         digitalWrite(relay3, !digitalRead(relay3));                             // invert state of pin 7
  381.         Serial.println("----------");
  382.         Serial.println();
  383.       }
  384.       else {
  385.         allStatesDisplay();
  386.         Serial.println("Button2 released");
  387.         Serial.println("----------");
  388.         Serial.println();
  389.       }
  390.     }
  391.   }
  392.  
  393.   lastState3 = currentState3;  
  394.  
  395.   //master bedroom fan
  396.   currentState4 = digitalRead(buttonPin3);
  397.   unsigned long currentTime4 = millis();
  398.  
  399.   if (currentState4 != lastState4){
  400.     timeOfLastButtonEvent4 = currentTime3;
  401.   }
  402.  
  403.   if (currentTime4 - timeOfLastButtonEvent4 > debounceInterval){      //if enough time has passed
  404.     if (currentState34 != debouncedState4){                            //if the current state is still different than our last stored debounced state
  405.       debouncedState4 = currentState4;                                //update the debounced state
  406.  
  407.       //trigger an event for master bedroom fan
  408.       if (debouncedState4 == HIGH){
  409.         Serial.println("Button3 pressed");
  410.         digitalWrite(relay4, !digitalRead(relay4));                             // invert state of pin 8
  411.         Serial.println("----------");
  412.         Serial.println();
  413.       }
  414.       else {
  415.         allStatesDisplay();
  416.         Serial.println("Button3 released");
  417.         Serial.println("----------");
  418.         Serial.println();
  419.       }
  420.     }
  421.   }
  422.  
  423.   lastState4 = currentState4;  
  424.  
  425.   //button read for allStatesDisplay() function to light LEDs representing on/off state of each connected circuit for 250 milliseconds
  426.   //LED1=LR lights; LED2=LR fan; LED3=BR light; LED4=BR fan; LED5=MV; LED6=PC; LED7=<future-possibly to indicate saved states enabled
  427.   currentState5 = digitalRead(buttonPin5);
  428.   unsigned long currentTime5 = millis();
  429.  
  430.   if (currentState5 != lastState5){
  431.     timeOfLastButtonEvent5 = currentTime5;
  432.   }
  433.  
  434.   if (currentTime5 - timeOfLastButtonEvent5 > debounceInterval){      //if enough time has passed
  435.     if (currentState5 != debouncedState5){                            //if the current state is still different than our last stored debounced state
  436.       debouncedState5 = currentState5;                                //update the debounced state
  437.  
  438.       //trigger an event for statesDisplay
  439.       if (debouncedState5 == HIGH){
  440.         allStatesDisplay();
  441.         Serial.println("Button5 pressed");
  442.         Serial.println("----------");
  443.         Serial.println();
  444.       }
  445.       else {
  446.         Serial.println();
  447.         Serial.print("Free memory: ");
  448.         Serial.println(freeRam());
  449.         Serial.println();
  450.         Serial.println("Button5 released");
  451.         Serial.println("----------");
  452.         Serial.println();
  453.       }
  454.     }
  455.   }
  456.  
  457.   lastState5 = currentState5;  
  458.  
  459.   // Create a client connection
  460.   EthernetClient client = server.available();
  461.   if (client) {
  462.     boolean currentLineIsBlank = true;
  463.     while (client.connected()) {
  464.       if (client.available()) {
  465.         char c = client.read();
  466.  
  467.         //read char by char HTTP request
  468. //        if (readString.length() < 100) {
  469.  
  470.           //store characters to string
  471.           readString += c;
  472.           //Serial.print(c);
  473. //        }
  474.  
  475.         //if HTTP request has ended
  476.         if (c == '\n' && currentLineIsBlank) {
  477.           // send a standard http response header
  478.           client.println("HTTP/1.1 200 OK");
  479.           client.println("Content-Type: text/html");
  480.           client.println("Connection: keep-alive");
  481.           client.println();
  482.           // display received HTTP request on serial port
  483.           Serial.println(readString); //print to serial monitor for debugging
  484.  
  485.           // based on string received from web client, where to go from here
  486.  
  487.           ///////////////////// control arduino pin 5
  488.           if(readString.indexOf("?device1on") >=0) {  //checks header string for on request
  489.             digitalWrite(relay1, LOW);    // set pin 5 low
  490.             allStatesDisplay();
  491.             Serial.println("Living Room Lights On");
  492.             Serial.println("----------");
  493.             Serial.println();
  494.           }
  495.           else if(readString.indexOf("?device1off") >=0)  { //checks header string for off request
  496.               digitalWrite(relay1, HIGH);    // set pin 5 high
  497.               allStatesDisplay();
  498.               Serial.println("Living Room Lights Off");
  499.               Serial.println("----------");
  500.               Serial.println();
  501.             }
  502.  
  503.           ///////////////////// control arduino pin 6
  504.           else if(readString.indexOf("?device2on") >=0)  { //checks header string for on request
  505.             digitalWrite(relay2, LOW);    // set pin 6 low
  506.             allStatesDisplay();
  507.             Serial.println("Living Room Fan On");
  508.             Serial.println("----------");
  509.             Serial.println();
  510.           }
  511.           else if(readString.indexOf("?device2off") >=0)  { //checks header string for off request
  512.               digitalWrite(relay2, HIGH);    // set pin 6 high
  513.               allStatesDisplay();
  514.               Serial.println("Living Room Fan Off");
  515.               Serial.println("----------");
  516.               Serial.println();
  517.             }
  518.  
  519.           ///////////////////// control arduino pin 7
  520.           else if(readString.indexOf("?device3on") >=0)  { //checks header string for on request
  521.             digitalWrite(relay3, LOW);    // set pin 7 low
  522.             allStatesDisplay();
  523.             Serial.println("Master Bedroom Light On");
  524.             Serial.println("----------");
  525.             Serial.println();
  526.           }
  527.           else if(readString.indexOf("?device3off") >=0)  { //checks header string for off request
  528.               digitalWrite(relay3, HIGH);    // set pin 7 high
  529.               allStatesDisplay();
  530.               Serial.println("Master Bedroom Light Off");
  531.               Serial.println("----------");
  532.               Serial.println();
  533.             }
  534.  
  535.           ///////////////////// control arduino pin 8
  536.           else if(readString.indexOf("?device4on") >=0)  { //checks header string for on request
  537.             digitalWrite(relay4, LOW);    // set pin 8 low
  538.             allStatesDisplay();
  539.             Serial.println("Master Bedroom Fan On");
  540.             Serial.println("----------");
  541.             Serial.println();
  542.           }
  543.           else if(readString.indexOf("?device4off") >=0)  { //checks header string for off request
  544.               digitalWrite(relay4, HIGH);    // set pin 8 high
  545.               allStatesDisplay();
  546.               Serial.println("Master Bedroom Fan Off");
  547.               Serial.println("----------");
  548.               Serial.println();
  549.             }
  550.  
  551.           ///////////////////// control arduino pins 28 & 30
  552.           else if(readString.indexOf("?ch1on") >=0)  { //checks header string for on request
  553.             digitalWrite(rmtsw1, HIGH);    // set pin 28 high
  554.             delay(100);                   // wait 100ms for radio to power and send
  555.             digitalWrite(rmtsw1, LOW);    // set pin 28 low
  556.             allStatesDisplay();
  557.             Serial.println("Channel 1 power switch tail On");
  558.             Serial.println("----------");
  559.             Serial.println();
  560.           }
  561.           else if(readString.indexOf("?ch1off") >=0)  { //checks header string for off request
  562.               digitalWrite(rmtsw2, HIGH);    // set pin 30 high
  563.               delay(100);                   // wait 100ms for radio to power and send
  564.               digitalWrite(rmtsw2, LOW);    // set pin 30 low
  565.               allStatesDisplay();
  566.               Serial.println("Channel 1 power switch tail Off");
  567.               Serial.println("----------");
  568.               Serial.println();
  569.             }
  570.  
  571.           ///////////////////// control arduino pins 32 & 34
  572.           else if(readString.indexOf("?ch2on") >=)  { //checks header string for on request
  573.             digitalWrite(rmtsw3, HIGH);    // set pin 32 high
  574.             delay(100);                   // wait 100ms for radio to power and send
  575.             digitalWrite(rmtsw3, LOW);    // set pin 32 low
  576.             allStatesDisplay();
  577.             Serial.println("Channel 2 power switch tail On");
  578.             Serial.println("----------");
  579.             Serial.println();
  580.           }
  581.           else if(readString.indexOf("?ch2off") >=0)  { //checks header string for off request
  582.               digitalWrite(rmtsw4, HIGH);    // set pin 34 high
  583.               delay(100);                   // wait 100ms for radio to power and send
  584.               digitalWrite(rmtsw4, LOW);    // set pin 34 low
  585.               allStatesDisplay();
  586.               Serial.println("Channel 2 power switch tail Off");
  587.               Serial.println("----------");
  588.               Serial.println();
  589.             }
  590.  
  591.           ///////////////////// control arduino pins 36 & 38
  592.           else if(readString.indexOf("?ch3on") >=0)  { //checks header string for on request
  593.             digitalWrite(rmtsw5, HIGH);    // set pin 36 high
  594.             delay(100);                   // wait 100ms for radio to power and send
  595.             digitalWrite(rmtsw5, LOW);     // set pin 36 low
  596.             allStatesDisplay();
  597.             Serial.println("Channel 3 power switch tail On");
  598.             Serial.println("----------");
  599.             Serial.println();
  600.           }
  601.           else if(readString.indexOf("?ch3off") >=0)  { //checks header string for off request
  602.               digitalWrite(rmtsw6, HIGH);    // set pin 38 high
  603.               delay(100);                   // wait 100ms for radio to power and send
  604.               digitalWrite(rmtsw6, LOW);     // set pin 38 low
  605.               allStatesDisplay();
  606.               Serial.println("Channel 3 power switch tail Off");
  607.               Serial.println("----------");
  608.               Serial.println();
  609.             }
  610.           else if (readString.indexOf("ajax_switch") > -1) {
  611.               // read switch state and send appropriate paragraph text
  612.               // start by using this for livingroom fan only, then once working
  613.               // add to it to get the full affect.
  614.               GetPinState(client);
  615.             }
  616.           else
  617.             {
  618.           // HTTP request for web page
  619.             // send web page - contains JavaScript with AJAX calls
  620.  
  621.             client.println("<!DOCTYPE html>");
  622.             client.println("<HTML>");
  623.             client.println("<HEAD>");
  624.             client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
  625.             client.println("<TITLE>Arduino Home Automation</TITLE>");
  626.             client.println("<script>");
  627.             client.println("function GetPinState() {");
  628.             client.println("var request = new XMLHttpRequest();");
  629.             client.println("request.onreadystatechange = function() {");
  630.             client.println("if (this.readyState == 4) {");
  631.             client.println("if (this.status == 200) {");
  632.             client.println("if (this.responseText != null) {");
  633.             client.println("document.getElementById(\"switch_txt\").innerHTML = this.responseText;");
  634.             client.println("}}}}");
  635.             client.println("request.open(\"GET\", \"ajax_switch\", true);");
  636.             client.println("request.send(null);");
  637.             client.println("setTimeout('GetPinState()', 3000);");
  638.             client.println("}");
  639.             client.println("</script>");
  640.             client.println("</HEAD>");
  641.             client.println("<body onload=\"GetPinState()\">");
  642.             client.println("<H1>Arduino Home Automation</H1>");
  643.             client.println("<hr />");
  644.  
  645.             client.println("<H2>Living Room</H2>");
  646.             client.println("<br />");
  647.             if (digitalRead(relay1) >0){
  648.               client.print("Light is <font color='red'><b>OFF</b></font><br />");
  649.               client.println("<br />");
  650.               client.println("<a href=\"http://192.168.1.97/?device1on\"\">Light On</a><br /><br />");
  651.             }
  652.             else{
  653.               client.print("Light is <font color='green'><b>ON</b></font><br />");
  654.               client.println("<br />");
  655.               client.println("<a href=\"http://192.168.1.97/?device1off\"\">Light Off</a><br /><br />");
  656.             }
  657.            
  658.             client.println("<p id=\"switch_txt\">Fan state is not requested...<br />");
  659.            
  660.             client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
  661.             client.println("<br />");
  662.             client.println("<hr />");
  663.             client.println("<H2>Master Bedroom</H2>");
  664.             client.println("<br />");
  665.             if (digitalRead(relay3) >0){
  666.               client.print("Light is <font color='red'><b>OFF</b></font><br />");
  667.               client.println("<br />");
  668.               client.println("<a href=\"http://192.168.1.97/?device3on\"\">Light On</a><br /><br /><br />");
  669.             }
  670.             else{
  671.               client.print("Light is <font color='green'><b>ON</b></font><br />");
  672.               client.println("<br />");
  673.               client.println("<a href=\"http://192.168.1.97/?device3off\"\">Light Off</a><br /><br /><br />");
  674.             }
  675.             if (digitalRead(relay4) >0){
  676.               client.print("Fan is <font color='red'><b>OFF</b></font><br />");
  677.               client.println("<br />");
  678.               client.println("<a href=\"http://192.168.1.97/?device4on\"\">Fan On</a><br /><br /><br />");
  679.             }
  680.             else{
  681.               client.print("Fan is <font color='green'><b>ON</b></font><br />");
  682.               client.println("<br />");
  683.               client.println("<a href=\"http://192.168.1.97/?device4off\"\">Fan Off</a><br /><br /><br />");
  684.             }
  685.             client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
  686.             client.println("<br />");
  687.             client.println("<hr />");
  688.             client.println("<H2>Power Switch Tails</H2>");
  689.             client.println("<br />");
  690.             client.println("Channel 1<br /><br />");
  691.             client.print("<a href=\"http://192.168.1.97/?ch1on\"\">On</a>&nbsp");
  692.             client.println("<a href=\"http://192.168.1.97/?ch1off\"\">Off</a><br /><br />");
  693.             client.println("<br />");
  694.             client.println("Channel 2<br /><br />");
  695.             client.print("<a href=\"http://192.168.1.97/?ch2on\"\">On</a>&nbsp");
  696.             client.println("<a href=\"http://192.168.1.97/?ch2off\"\">Off</a><br /><br />");
  697.             client.println("<br />");
  698.             client.println("Channel 3<br /><br />");
  699.             client.print("<a href=\"http://192.168.1.97/?ch3on\"\">On</a>&nbsp");
  700.             client.println("<a href=\"http://192.168.1.97/?ch3off\"\">Off</a><br /><br /><br />");
  701.             client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
  702.             client.println("<br />");
  703.             client.println("<br />");
  704.             client.println("<br />");
  705.             client.println("<hr />");          
  706.             currentMillis=millis(); // get the  current milliseconds from arduino
  707.             long days=0;
  708.             long hours=0;
  709.             long mins=0;
  710.             long secs=0;
  711.             secs = currentMillis/1000;  // convert milliseconds to seconds
  712.             mins = secs/60;  // convert seconds to minutes
  713.             hours = mins/60;  // convert minutes to hours
  714.             days = hours/24;  // convert hours to days
  715.             secs=secs-(mins*60); //subtract the converted seconds to minutes in order to display 59 secs max
  716.             mins=mins-(hours*60); //subtract the converted minutes to hours in order to display 59 minutes max
  717.             hours=hours-(days*24); //subtract the converted hours to days in order to display 23 hours max
  718.             //Display results in web page
  719.  
  720.             client.println("<b>Arduino Uptime</b><br />");
  721.             client.println("<b>---------------------------<br />");
  722.             if (days>0)  // variable 'days' will be displayed only if value is greater than zero
  723.             {
  724.               if (days<2)  // text 'day' will display instead of 'days' for single day
  725.               {
  726.                 client.print(days);
  727.                 client.print(" day, ");
  728.               }
  729.               else
  730.               {
  731.                 client.print(days);
  732.                 client.print(" days, ");
  733.               }
  734.             }
  735.             if (hours>0 && hours<2)  // text 'hour' will display instead of 'hours' for single hour
  736.             {
  737.               client.print(hours);
  738.               client.print(" hour, ");
  739.             }
  740.             else
  741.             {
  742.               client.print(hours);
  743.               client.print(" hours, ");
  744.             }
  745.             if (mins>0 && mins<2)  // text 'min' will display instead of 'mins' for single minute
  746.             {
  747.               client.print(mins);
  748.               client.print(" min, ");
  749.             }
  750.             else
  751.             {
  752.               client.print(mins);
  753.               client.print(" mins, ");
  754.             }
  755.             if (secs>0 && secs<2)  // text 'sec' will display instead of 'secs' for single second
  756.             {
  757.               client.print(secs);
  758.               client.print(" sec");
  759.             }
  760.             else
  761.             {
  762.               client.print(secs);
  763.               client.print(" secs");
  764.             }
  765.             client.println("<br />");
  766.             client.println("control_lights_and_fans_v24a.ino<br />");
  767.             client.println("Uploaded November 4, 2014<br />");
  768.             client.print("Free memory: ");
  769.             client.print(freeRam());
  770.             client.print("</b>");
  771.             client.println("</BODY>");
  772.             client.println("</HTML>");
  773.           }
  774.             break;
  775.           }
  776.                 // every line of text received from the client ends with \r\n
  777.                 if (c == '\n') {
  778.                     // last character on line of received text
  779.                     // starting new line with next character read
  780.                     currentLineIsBlank = true;
  781.                 }
  782.                 else if (c != '\r') {
  783.                     // a text character was received from client
  784.                     currentLineIsBlank = false;
  785.                 }
  786.       } // end if (client.available())
  787.     } // end while (client.connected())
  788.  
  789.             delay(1);
  790.             client.stop(); //close connection
  791.             //clearing string for next read
  792.             readString="";
  793.     } // end if (client)            
  794.  
  795. }  // end loop()
  796.  
  797. //function for reading pin states and displaying on LEDs when button5 is pressed
  798. //Circuit 1/LED1 is LR lights
  799. //Circuit 2/LED2 is LR fan
  800. //Circuit 3/LED3 is BR light
  801. //Circuit 4/LED4 is BR fan
  802. void allStatesDisplay(){
  803.   pinState1 = digitalRead(relay1);
  804.   pinState2 = digitalRead(relay2);
  805.   pinState3 = digitalRead(relay3);
  806.   pinState4 = digitalRead(relay4);
  807.   digitalWrite(statesDisplay6, !pinState1);  //invert read state of relay1 (pin5) (active LOW) to indicate Circuit 1's state
  808.   digitalWrite(statesDisplay5, !pinState2);  //invert read state of relay2 (pin6) (active LOW) to indicate Circuit 2's state
  809.   digitalWrite(statesDisplay4, !pinState3);  //invert read state of relay3 (pin7) (active LOW) to indicate Circuit 3's state
  810.   digitalWrite(statesDisplay3, !pinState4);  //invert read state of relay4 (pin8) (active LOW) to indicate Circuit 4's state
  811.   for (int x=0; x < 500; x++) {             // wait for half a secoond
  812.     delay(1);
  813.   }
  814.   digitalWrite(statesDisplay3, LOW);  //turn LED3 off
  815.   digitalWrite(statesDisplay4, LOW);  //turn LED4 off
  816.   digitalWrite(statesDisplay5, LOW);  //turn LED5 off
  817.   digitalWrite(statesDisplay6, LOW);  //turn LED6 off
  818. }
  819. //free memory function prints in serial monitor and bottom of web page
  820. int freeRam () {
  821.   extern int __heap_start, *__brkval;
  822.   int v;
  823.   return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  824. }
  825. void GetPinState(EthernetClient cl)
  826. {
  827.   if (digitalRead(relay2) >0){
  828.     cl.println("Fan is <font color='red'><b>OFF</b></font><br /><br />");
  829.     cl.println("<a href=\"http://192.168.1.97/?device2on\"\">Fan On</a><br /><br />");
  830.   }
  831.   else{
  832.     cl.println("Fan is <font color='green'><b>ON</b></font><br /><br />");
  833.     cl.println("<a href=\"http://192.168.1.97/?device2off\"\">Fan Off</a><br /><br />");
  834.   }
  835. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement