Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //simple button GET server code to control 4 relays with arduino pins
- //for use with Arduino IDE 1.0
- //open serial monitor to see what the arduino receives
- //use the \ slash to escape the " in the html
- //for use with Wiznet W5100 based ethernet shields
- //added uptime shown in serial monitor and bottom of web page
- //added multiple buttons to alternately control lights/fans
- //added button control of relays at Arduino location
- //added LED status display for relay-controlled circuits at Arduino location
- //buttons are debounced with millis()
- //added serial control for all circuits by input (a-k)
- //added AJAX using code from ...
- //http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-read-switch-using-AJAX/
- // ******
- //see my comprehensive tutorial on how I put most of this together at
- //http://www.instructables.com/id/Arduino-Powered-Home/
- // ******
- #include <SPI.h>
- #include <Ethernet.h>
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
- byte ip[] = {
- 192, 168, 1, 97 }; // ip
- byte gateway[] = {
- 192, 168, 1, 1 }; // gateway
- byte subnet[] = {
- 255, 255, 255, 0 }; //subnet mask
- EthernetServer server(80); //server port
- String readString;
- //////////////////////
- int relay1 = 5; // relay IN1 connected to digital pin 5
- int relay2 = 6; // relay IN2 connected to digital pin 6
- int relay3 = 7; // relay IN3 connected to digital pin 7
- int relay4 = 8; // relay IN4 connected to digital pin 8
- int rmtsw1 = 28; // Power switch tail remote control switch 1 (channel 1 on)
- int rmtsw2 = 30; // Power switch tail remote control switch 2 (channel 1 off)
- int rmtsw3 = 32; // Power switch tail remote control switch 3 (channel 2 on)
- int rmtsw4 = 34; // Power switch tail remote control switch 4 (channel 2 off)
- int rmtsw5 = 36; // Power switch tail remote control switch 5 (channel 3 on)
- int rmtsw6 = 38; // Power switch tail remote control switch 6 (channel 3 off)
- int statesDisplay1 = 29; // digital pin 29 connected to LED7 to indicate circuit status when Button5 pressed
- int statesDisplay2 = 33; // digital pin 33 connected to LED6 to indicate circuit status when Button5 pressed
- int statesDisplay3 = 37; // digital pin 37 connected to LED5 to indicate circuit status when Button5 pressed
- int statesDisplay4 = 41; // digital pin 41 connected to LED4 to indicate circuit status when Button5 pressed
- int statesDisplay5 = 45; // digital pin 45 connected to LED3 to indicate circuit status when Button5 pressed
- int statesDisplay6 = 49; // digital pin 49 connected to LED2 to indicate circuit status when Button5 pressed
- //Button Press Detection - debounce with millis()
- int buttonPin1 = 22; // pin connected to button1 for control of livingroom lights
- int buttonPin2 = 23; // pin connected to button2 for control of livingroom ceiling fan
- int buttonPin3 = 24; // pin connected to button3 for control of master bedroom light
- int buttonPin4 = 25; // pin connected to button4 for control of master bedroom ceiling fan
- int buttonPin5 = 26; // pin connected to button5 for control of function allStatesDisplay()
- boolean currentState1 = LOW;//storage for current measured button1 state
- boolean currentState2 = LOW;//storage for current measured button2 state
- boolean currentState3 = LOW;//storage for current measured button3 state
- boolean currentState4 = LOW;//storage for current measured button4 state
- boolean currentState5 = LOW;//storage for current measured button5 state
- boolean lastState1 = LOW;//storage for last measured button1 state
- boolean lastState2 = LOW;//storage for last measured button2 state
- boolean lastState3 = LOW;//storage for last measured button3 state
- boolean lastState4 = LOW;//storage for last measured button4 state
- boolean lastState5 = LOW;//storage for last measured button5 state
- boolean debouncedState1 = LOW;//debounced button1 state
- boolean debouncedState2 = LOW;//debounced button2 state
- boolean debouncedState3 = LOW;//debounced button3 state
- boolean debouncedState4 = LOW;//debounced button4 state
- boolean debouncedState5 = LOW;//debounced button5 state
- int debounceInterval = 20;//wait 20 ms for button1 pin to settle
- unsigned long timeOfLastButtonEvent1 = 0;//store the last time button1 state changed
- unsigned long timeOfLastButtonEvent2 = 0;//store the last time button2 state changed
- unsigned long timeOfLastButtonEvent3 = 0;//store the last time button3 state changed
- unsigned long timeOfLastButtonEvent4 = 0;//store the last time button4 state changed
- unsigned long timeOfLastButtonEvent5 = 0;//store the last time button5 state changed
- unsigned long currentMillis = 0; // store current time
- int pinState1 = digitalRead(relay1);
- int pinState2 = digitalRead(relay2);
- int pinState3 = digitalRead(relay3);
- int pinState4 = digitalRead(relay4);
- int pinState5 = digitalRead(buttonPin1);
- int pinState6 = digitalRead(buttonPin2);
- int pinState7 = digitalRead(buttonPin3);
- int pinState8 = digitalRead(buttonPin4);
- //enable read on serial port
- int incomingByte = 0; // for incoming serial data
- void setup(){
- pinMode(relay1, OUTPUT); //pin to control livingroom lights
- pinMode(relay2, OUTPUT); //pin to control livingroom ceiling fan
- pinMode(relay3, OUTPUT); //pin to control master bedroom light
- pinMode(relay4, OUTPUT); //pin to control master bedroom ceiling fan
- pinMode(buttonPin1, INPUT); //pin to monitor button1 state, not necessary to declare an input
- pinMode(buttonPin2, INPUT); //pin to monitor button2 state, not necessary to declare an input
- pinMode(buttonPin3, INPUT); //pin to monitor button3 state, not necessary to declare an input
- pinMode(buttonPin4, INPUT); //pin to monitor button4 state, not necessary to declare an input
- pinMode(buttonPin5, INPUT); //pin to monitor button5 state, not necessary to declare an input
- digitalWrite(relay1, HIGH); //Prevents relays from starting up engaged
- digitalWrite(relay2, HIGH); //Prevents relays from starting up engaged
- digitalWrite(relay3, HIGH); //Prevents relays from starting up engaged
- digitalWrite(relay4, HIGH); //Prevents relays from starting up engaged
- pinMode(statesDisplay1, OUTPUT); //pin to display circuit 6 status via LED6
- pinMode(statesDisplay2, OUTPUT); //pin to display circuit 5 status via LED5
- pinMode(statesDisplay3, OUTPUT); //pin to display circuit 4 status via LED4
- pinMode(statesDisplay4, OUTPUT); //pin to display circuit 3 status via LED3
- pinMode(statesDisplay5, OUTPUT); //pin to display circuit 2 status via LED2
- pinMode(statesDisplay6, OUTPUT); //pin to display circuit 1 status via LED1
- digitalWrite(statesDisplay1, LOW);
- digitalWrite(statesDisplay2, LOW);
- digitalWrite(statesDisplay3, LOW);
- digitalWrite(statesDisplay4, LOW);
- digitalWrite(statesDisplay5, LOW);
- digitalWrite(statesDisplay6, LOW);
- pinMode(rmtsw1, OUTPUT); //pin to trigger remote control channel 1 on
- pinMode(rmtsw2, OUTPUT); //pin to trigger remote control channel 1 off
- pinMode(rmtsw3, OUTPUT); //pin to trigger remote control channel 2 on
- pinMode(rmtsw4, OUTPUT); //pin to trigger remote control channel 2 off
- pinMode(rmtsw5, OUTPUT); //pin to trigger remote control channel 3 on
- pinMode(rmtsw6, OUTPUT); //pin to trigger remote control channel 3 off
- digitalWrite(rmtsw1, LOW);
- digitalWrite(rmtsw2, LOW);
- digitalWrite(rmtsw3, LOW);
- digitalWrite(rmtsw4, LOW);
- digitalWrite(rmtsw5, LOW);
- digitalWrite(rmtsw6, LOW);
- //start Ethernet connection and server
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- //enable serial data print
- Serial.begin(9600);
- Serial.println("home automation server v24a with AJAX for livingroom fan only"); // so I can keep track of what is loaded
- Serial.print("Free memory: ");
- Serial.println(freeRam());
- Serial.println("----------");
- Serial.println();
- }
- void loop(){
- //enable serial data read
- if (Serial.available() > 0) {
- // read the incoming byte:
- int incomingByte = Serial.read();
- // say what you got:
- Serial.print("I received: ");
- Serial.println(incomingByte, DEC);
- // do something different depending on the character received via serial.
- // The switch statement expects single number values for each case;
- // in this example, though, you're using single quotes to tell
- // the controller to get the ASCII value for the character. For
- // example 'a' = 97, 'b' = 98, and so forth:
- switch (incomingByte) {
- case 'a':
- //do something here like livingroom light on;
- if (digitalRead(relay1) >0){ // >0 is HIGH and this is active low relay circuit, so it's off
- digitalWrite(relay1,LOW); // LOW is on for active low relay circuit
- Serial.println("Relay1/livingroom lights are ON.");
- }
- break;
- case 'b':
- //do something here like livingroom light off;
- if (digitalRead(relay1) <1){ // <1 is LOW and this is active low relay circuit, so it's on
- digitalWrite(relay1,HIGH); // HIGH is off for active low relay circuit
- Serial.println("Relay1/livingroom lights are OFF.");
- }
- break;
- case 'c':
- //do something here like livingroom fan on;
- if (digitalRead(relay2) >0){ // >0 is HIGH and this is active low relay circuit, so it's off
- digitalWrite(relay2,LOW); // LOW is on for active low relay circuit
- Serial.println("Relay2/livingroom fan is ON.");
- }
- break;
- case 'd':
- //do something here like livingroom fan off;
- if (digitalRead(relay2) <1){ // <1 is LOW and this is active low relay circuit, so it's on
- digitalWrite(relay2,HIGH); // HIGH is off for active low relay circuit
- Serial.println("Relay2/livingroom fan is OFF.");
- }
- break;
- case 'e':
- //do something here like bedroom light on;
- if (digitalRead(relay3) >0){ // >0 is HIGH and this is active low relay circuit, so it's off
- digitalWrite(relay3,LOW); // LOW is on for active low relay circuit
- Serial.println("Relay3/bedroom light is ON.");
- }
- break;
- case 'f':
- //do something here like bedroom light off;
- if (digitalRead(relay3) <1){ // <1 is LOW and this is active low relay circuit, so it's on
- digitalWrite(relay3,HIGH); // HIGH is off for active low relay circuit
- Serial.println("Relay3/bedroom light is OFF.");
- }
- break;
- case 'g':
- //do something here like bedroom fan on;
- if (digitalRead(relay4) >0){ // >0 is HIGH and this is active low relay circuit, so it's off
- digitalWrite(relay4,LOW); // LOW is on for active low relay circuit
- Serial.println("Relay4/bedroom fan is ON.");
- }
- break;
- case 'h':
- //do something here like bedroom fan off;
- if (digitalRead(relay4) <1){ // <1 is LOW and this is active low relay circuit, so it's on
- digitalWrite(relay4,HIGH); // HIGH is off for active low relay circuit
- Serial.println("Relay4/bedroom fan is OFF.");
- }
- break;
- case 'i':
- //do something here like auto-reset disable; to disable auto-reset I close
- //transistor circuit via A0 connecting capacitor between reset and ground.
- // if (digitalRead(myResetPin) <1){ // <1 is LOW and this is active high circuit, so auto-reset is enabled
- // digitalWrite(myResetPin,HIGH); // HIGH is on, or auto-reset disabled
- Serial.println("case 'i' has been disabled!");
- // }
- // Serial.println("Auto-reset is disabled for normal operation.");
- break;
- case 'j':
- //do something here like auto-reset enable; to enable auto-reset I open
- //transistor circuit via A0 disconnecting capacitor between reset and ground.
- // if (digitalRead(myResetPin) >0){ // >0 is HIGH and this is active high circuit, so auto-reset is disabled
- // digitalWrite(myResetPin,LOW); // LOW is off, or auto-reset enabled
- Serial.println("case 'j' has been disabled!");
- // }
- // Serial.println("Auto-reset is enabled for programming.");
- break;
- case 'k':
- //do something here like print state of all output pins used, D5-D8, A0.
- Serial.print("D5 state is ");
- Serial.print(digitalRead(relay1));
- Serial.print(".");
- Serial.print(" Livingroom lights are ");
- if (digitalRead(relay1) >0){
- Serial.println("OFF.");
- }
- else
- {
- Serial.println("ON.");
- }
- Serial.print("D6 state is ");
- Serial.print(digitalRead(relay2));
- Serial.print(".");
- Serial.print(" Livingroom fan is ");
- if (digitalRead(relay2) >0){
- Serial.println("OFF.");
- }
- else
- {
- Serial.println("ON.");
- }
- Serial.print("D7 state is ");
- Serial.print(digitalRead(relay3));
- Serial.print(".");
- Serial.print(" Bedroom light is ");
- if (digitalRead(relay3) >0){
- Serial.println("OFF.");
- }
- else
- {
- Serial.println("ON.");
- }
- Serial.print("D8 state is ");
- Serial.print(digitalRead(relay4));
- Serial.print(".");
- Serial.print(" Bedroom fan is ");
- if (digitalRead(relay4) >0){
- Serial.println("OFF.");
- }
- else
- {
- Serial.println("ON.");
- }
- // Serial.print("A0 state is ");
- // Serial.print(digitalRead(myResetPin));
- // Serial.print(". Auto-reset is ");
- // if (digitalRead(myResetPin) >0){ // >0 is HIGH and this is active high circuit, so auto-reset is disabled
- // Serial.println("disabled for normal operation.");
- // }
- // else
- // {
- // Serial.println("enabled for programming.");
- // }
- break;
- }
- }
- //livingroom lights
- currentState1 = digitalRead(buttonPin4);
- unsigned long currentTime1 = millis();
- if (currentState1 != lastState1){
- timeOfLastButtonEvent1 = currentTime4;
- }
- if (currentTime1 - timeOfLastButtonEvent1 > debounceInterval){ //if enough time has passed
- if (currentState1 != debouncedState1){ //if the current state is still different than our last stored debounced state
- debouncedState1 = currentState1; //update the debounced state
- //trigger an event for livingroom lights
- if (debouncedState1 == HIGH){
- Serial.println("Button4 pressed");
- digitalWrite(relay1, !digitalRead(relay1)); // invert state of pin 5
- Serial.println("----------");
- Serial.println();
- }
- else {
- allStatesDisplay();
- Serial.println("Button4 released");
- Serial.println("----------");
- Serial.println();
- }
- }
- }
- lastState1 = currentState1;
- //livingroom fan
- currentState2 = digitalRead(buttonPin1);
- unsigned long currentTime2 = millis();
- if (currentState2 != lastState2){
- timeOfLastButtonEvent2 = currentTime2;
- }
- if (currentTime2 - timeOfLastButtonEvent2 > debounceInterval){ //if enough time has passed
- if (currentState2 != debouncedState2){ //if the current state is still different than our last stored debounced state
- debouncedState2 = currentState2; //update the debounced state
- //trigger an event for livingroom fan
- if (debouncedState2 == HIGH){
- Serial.println("Button1 pressed");
- digitalWrite(relay2, !digitalRead(relay2)); // invert state of pin 6
- Serial.println("----------");
- Serial.println();
- }
- else {
- allStatesDisplay();
- Serial.println("Button1 released");
- Serial.println("----------");
- Serial.println();
- }
- }
- }
- lastState2 = currentState2;
- //master bedroom light
- currentState3 = digitalRead(buttonPin2);
- unsigned long currentTime3 = millis();
- if (currentState3 != lastState3){
- timeOfLastButtonEvent3 = currentTime3;
- }
- if (currentTime3 - timeOfLastButtonEvent3 > debounceInterval){ //if enough time has passed
- if (currentState3 != debouncedState3){ //if the current state is still different than our last stored debounced state
- debouncedState3 = currentState3; //update the debounced state
- //trigger an event for master bedroom light
- if (debouncedState3 == HIGH){
- Serial.println("Button2 pressed");
- digitalWrite(relay3, !digitalRead(relay3)); // invert state of pin 7
- Serial.println("----------");
- Serial.println();
- }
- else {
- allStatesDisplay();
- Serial.println("Button2 released");
- Serial.println("----------");
- Serial.println();
- }
- }
- }
- lastState3 = currentState3;
- //master bedroom fan
- currentState4 = digitalRead(buttonPin3);
- unsigned long currentTime4 = millis();
- if (currentState4 != lastState4){
- timeOfLastButtonEvent4 = currentTime3;
- }
- if (currentTime4 - timeOfLastButtonEvent4 > debounceInterval){ //if enough time has passed
- if (currentState34 != debouncedState4){ //if the current state is still different than our last stored debounced state
- debouncedState4 = currentState4; //update the debounced state
- //trigger an event for master bedroom fan
- if (debouncedState4 == HIGH){
- Serial.println("Button3 pressed");
- digitalWrite(relay4, !digitalRead(relay4)); // invert state of pin 8
- Serial.println("----------");
- Serial.println();
- }
- else {
- allStatesDisplay();
- Serial.println("Button3 released");
- Serial.println("----------");
- Serial.println();
- }
- }
- }
- lastState4 = currentState4;
- //button read for allStatesDisplay() function to light LEDs representing on/off state of each connected circuit for 250 milliseconds
- //LED1=LR lights; LED2=LR fan; LED3=BR light; LED4=BR fan; LED5=MV; LED6=PC; LED7=<future-possibly to indicate saved states enabled
- currentState5 = digitalRead(buttonPin5);
- unsigned long currentTime5 = millis();
- if (currentState5 != lastState5){
- timeOfLastButtonEvent5 = currentTime5;
- }
- if (currentTime5 - timeOfLastButtonEvent5 > debounceInterval){ //if enough time has passed
- if (currentState5 != debouncedState5){ //if the current state is still different than our last stored debounced state
- debouncedState5 = currentState5; //update the debounced state
- //trigger an event for statesDisplay
- if (debouncedState5 == HIGH){
- allStatesDisplay();
- Serial.println("Button5 pressed");
- Serial.println("----------");
- Serial.println();
- }
- else {
- Serial.println();
- Serial.print("Free memory: ");
- Serial.println(freeRam());
- Serial.println();
- Serial.println("Button5 released");
- Serial.println("----------");
- Serial.println();
- }
- }
- }
- lastState5 = currentState5;
- // Create a client connection
- EthernetClient client = server.available();
- if (client) {
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //read char by char HTTP request
- // if (readString.length() < 100) {
- //store characters to string
- readString += c;
- //Serial.print(c);
- // }
- //if HTTP request has ended
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: keep-alive");
- client.println();
- // display received HTTP request on serial port
- Serial.println(readString); //print to serial monitor for debugging
- // based on string received from web client, where to go from here
- ///////////////////// control arduino pin 5
- if(readString.indexOf("?device1on") >=0) { //checks header string for on request
- digitalWrite(relay1, LOW); // set pin 5 low
- allStatesDisplay();
- Serial.println("Living Room Lights On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?device1off") >=0) { //checks header string for off request
- digitalWrite(relay1, HIGH); // set pin 5 high
- allStatesDisplay();
- Serial.println("Living Room Lights Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pin 6
- else if(readString.indexOf("?device2on") >=0) { //checks header string for on request
- digitalWrite(relay2, LOW); // set pin 6 low
- allStatesDisplay();
- Serial.println("Living Room Fan On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?device2off") >=0) { //checks header string for off request
- digitalWrite(relay2, HIGH); // set pin 6 high
- allStatesDisplay();
- Serial.println("Living Room Fan Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pin 7
- else if(readString.indexOf("?device3on") >=0) { //checks header string for on request
- digitalWrite(relay3, LOW); // set pin 7 low
- allStatesDisplay();
- Serial.println("Master Bedroom Light On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?device3off") >=0) { //checks header string for off request
- digitalWrite(relay3, HIGH); // set pin 7 high
- allStatesDisplay();
- Serial.println("Master Bedroom Light Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pin 8
- else if(readString.indexOf("?device4on") >=0) { //checks header string for on request
- digitalWrite(relay4, LOW); // set pin 8 low
- allStatesDisplay();
- Serial.println("Master Bedroom Fan On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?device4off") >=0) { //checks header string for off request
- digitalWrite(relay4, HIGH); // set pin 8 high
- allStatesDisplay();
- Serial.println("Master Bedroom Fan Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pins 28 & 30
- else if(readString.indexOf("?ch1on") >=0) { //checks header string for on request
- digitalWrite(rmtsw1, HIGH); // set pin 28 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw1, LOW); // set pin 28 low
- allStatesDisplay();
- Serial.println("Channel 1 power switch tail On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?ch1off") >=0) { //checks header string for off request
- digitalWrite(rmtsw2, HIGH); // set pin 30 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw2, LOW); // set pin 30 low
- allStatesDisplay();
- Serial.println("Channel 1 power switch tail Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pins 32 & 34
- else if(readString.indexOf("?ch2on") >=) { //checks header string for on request
- digitalWrite(rmtsw3, HIGH); // set pin 32 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw3, LOW); // set pin 32 low
- allStatesDisplay();
- Serial.println("Channel 2 power switch tail On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?ch2off") >=0) { //checks header string for off request
- digitalWrite(rmtsw4, HIGH); // set pin 34 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw4, LOW); // set pin 34 low
- allStatesDisplay();
- Serial.println("Channel 2 power switch tail Off");
- Serial.println("----------");
- Serial.println();
- }
- ///////////////////// control arduino pins 36 & 38
- else if(readString.indexOf("?ch3on") >=0) { //checks header string for on request
- digitalWrite(rmtsw5, HIGH); // set pin 36 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw5, LOW); // set pin 36 low
- allStatesDisplay();
- Serial.println("Channel 3 power switch tail On");
- Serial.println("----------");
- Serial.println();
- }
- else if(readString.indexOf("?ch3off") >=0) { //checks header string for off request
- digitalWrite(rmtsw6, HIGH); // set pin 38 high
- delay(100); // wait 100ms for radio to power and send
- digitalWrite(rmtsw6, LOW); // set pin 38 low
- allStatesDisplay();
- Serial.println("Channel 3 power switch tail Off");
- Serial.println("----------");
- Serial.println();
- }
- else if (readString.indexOf("ajax_switch") > -1) {
- // read switch state and send appropriate paragraph text
- // start by using this for livingroom fan only, then once working
- // add to it to get the full affect.
- GetPinState(client);
- }
- else
- {
- // HTTP request for web page
- // send web page - contains JavaScript with AJAX calls
- client.println("<!DOCTYPE html>");
- client.println("<HTML>");
- client.println("<HEAD>");
- client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
- client.println("<TITLE>Arduino Home Automation</TITLE>");
- client.println("<script>");
- client.println("function GetPinState() {");
- client.println("var request = new XMLHttpRequest();");
- client.println("request.onreadystatechange = function() {");
- client.println("if (this.readyState == 4) {");
- client.println("if (this.status == 200) {");
- client.println("if (this.responseText != null) {");
- client.println("document.getElementById(\"switch_txt\").innerHTML = this.responseText;");
- client.println("}}}}");
- client.println("request.open(\"GET\", \"ajax_switch\", true);");
- client.println("request.send(null);");
- client.println("setTimeout('GetPinState()', 3000);");
- client.println("}");
- client.println("</script>");
- client.println("</HEAD>");
- client.println("<body onload=\"GetPinState()\">");
- client.println("<H1>Arduino Home Automation</H1>");
- client.println("<hr />");
- client.println("<H2>Living Room</H2>");
- client.println("<br />");
- if (digitalRead(relay1) >0){
- client.print("Light is <font color='red'><b>OFF</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device1on\"\">Light On</a><br /><br />");
- }
- else{
- client.print("Light is <font color='green'><b>ON</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device1off\"\">Light Off</a><br /><br />");
- }
- client.println("<p id=\"switch_txt\">Fan state is not requested...<br />");
- client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
- client.println("<br />");
- client.println("<hr />");
- client.println("<H2>Master Bedroom</H2>");
- client.println("<br />");
- if (digitalRead(relay3) >0){
- client.print("Light is <font color='red'><b>OFF</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device3on\"\">Light On</a><br /><br /><br />");
- }
- else{
- client.print("Light is <font color='green'><b>ON</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device3off\"\">Light Off</a><br /><br /><br />");
- }
- if (digitalRead(relay4) >0){
- client.print("Fan is <font color='red'><b>OFF</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device4on\"\">Fan On</a><br /><br /><br />");
- }
- else{
- client.print("Fan is <font color='green'><b>ON</b></font><br />");
- client.println("<br />");
- client.println("<a href=\"http://192.168.1.97/?device4off\"\">Fan Off</a><br /><br /><br />");
- }
- client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
- client.println("<br />");
- client.println("<hr />");
- client.println("<H2>Power Switch Tails</H2>");
- client.println("<br />");
- client.println("Channel 1<br /><br />");
- client.print("<a href=\"http://192.168.1.97/?ch1on\"\">On</a> ");
- client.println("<a href=\"http://192.168.1.97/?ch1off\"\">Off</a><br /><br />");
- client.println("<br />");
- client.println("Channel 2<br /><br />");
- client.print("<a href=\"http://192.168.1.97/?ch2on\"\">On</a> ");
- client.println("<a href=\"http://192.168.1.97/?ch2off\"\">Off</a><br /><br />");
- client.println("<br />");
- client.println("Channel 3<br /><br />");
- client.print("<a href=\"http://192.168.1.97/?ch3on\"\">On</a> ");
- client.println("<a href=\"http://192.168.1.97/?ch3off\"\">Off</a><br /><br /><br />");
- client.println("<a href=\"http://192.168.1.97/\"\">Refresh</a><br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println("<hr />");
- currentMillis=millis(); // get the current milliseconds from arduino
- long days=0;
- long hours=0;
- long mins=0;
- long secs=0;
- secs = currentMillis/1000; // convert milliseconds to seconds
- mins = secs/60; // convert seconds to minutes
- hours = mins/60; // convert minutes to hours
- days = hours/24; // convert hours to days
- secs=secs-(mins*60); //subtract the converted seconds to minutes in order to display 59 secs max
- mins=mins-(hours*60); //subtract the converted minutes to hours in order to display 59 minutes max
- hours=hours-(days*24); //subtract the converted hours to days in order to display 23 hours max
- //Display results in web page
- client.println("<b>Arduino Uptime</b><br />");
- client.println("<b>---------------------------<br />");
- if (days>0) // variable 'days' will be displayed only if value is greater than zero
- {
- if (days<2) // text 'day' will display instead of 'days' for single day
- {
- client.print(days);
- client.print(" day, ");
- }
- else
- {
- client.print(days);
- client.print(" days, ");
- }
- }
- if (hours>0 && hours<2) // text 'hour' will display instead of 'hours' for single hour
- {
- client.print(hours);
- client.print(" hour, ");
- }
- else
- {
- client.print(hours);
- client.print(" hours, ");
- }
- if (mins>0 && mins<2) // text 'min' will display instead of 'mins' for single minute
- {
- client.print(mins);
- client.print(" min, ");
- }
- else
- {
- client.print(mins);
- client.print(" mins, ");
- }
- if (secs>0 && secs<2) // text 'sec' will display instead of 'secs' for single second
- {
- client.print(secs);
- client.print(" sec");
- }
- else
- {
- client.print(secs);
- client.print(" secs");
- }
- client.println("<br />");
- client.println("control_lights_and_fans_v24a.ino<br />");
- client.println("Uploaded November 4, 2014<br />");
- client.print("Free memory: ");
- client.print(freeRam());
- client.print("</b>");
- client.println("</BODY>");
- client.println("</HTML>");
- }
- break;
- }
- // every line of text received from the client ends with \r\n
- if (c == '\n') {
- // last character on line of received text
- // starting new line with next character read
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- // a text character was received from client
- currentLineIsBlank = false;
- }
- } // end if (client.available())
- } // end while (client.connected())
- delay(1);
- client.stop(); //close connection
- //clearing string for next read
- readString="";
- } // end if (client)
- } // end loop()
- //function for reading pin states and displaying on LEDs when button5 is pressed
- //Circuit 1/LED1 is LR lights
- //Circuit 2/LED2 is LR fan
- //Circuit 3/LED3 is BR light
- //Circuit 4/LED4 is BR fan
- void allStatesDisplay(){
- pinState1 = digitalRead(relay1);
- pinState2 = digitalRead(relay2);
- pinState3 = digitalRead(relay3);
- pinState4 = digitalRead(relay4);
- digitalWrite(statesDisplay6, !pinState1); //invert read state of relay1 (pin5) (active LOW) to indicate Circuit 1's state
- digitalWrite(statesDisplay5, !pinState2); //invert read state of relay2 (pin6) (active LOW) to indicate Circuit 2's state
- digitalWrite(statesDisplay4, !pinState3); //invert read state of relay3 (pin7) (active LOW) to indicate Circuit 3's state
- digitalWrite(statesDisplay3, !pinState4); //invert read state of relay4 (pin8) (active LOW) to indicate Circuit 4's state
- for (int x=0; x < 500; x++) { // wait for half a secoond
- delay(1);
- }
- digitalWrite(statesDisplay3, LOW); //turn LED3 off
- digitalWrite(statesDisplay4, LOW); //turn LED4 off
- digitalWrite(statesDisplay5, LOW); //turn LED5 off
- digitalWrite(statesDisplay6, LOW); //turn LED6 off
- }
- //free memory function prints in serial monitor and bottom of web page
- int freeRam () {
- extern int __heap_start, *__brkval;
- int v;
- return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
- }
- void GetPinState(EthernetClient cl)
- {
- if (digitalRead(relay2) >0){
- cl.println("Fan is <font color='red'><b>OFF</b></font><br /><br />");
- cl.println("<a href=\"http://192.168.1.97/?device2on\"\">Fan On</a><br /><br />");
- }
- else{
- cl.println("Fan is <font color='green'><b>ON</b></font><br /><br />");
- cl.println("<a href=\"http://192.168.1.97/?device2off\"\">Fan Off</a><br /><br />");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement