├── Arduino_with_ethernet_shield.ino ├── HAS_Control_220V_Lamps.ino ├── LICENSE └── README.md /Arduino_with_ethernet_shield.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This code was created by Rui Santos 3 | In order to use this code you must create your free account at 4 | http://homeautomationserver.com 5 | 6 | Thanks to: 7 | Nelson Adriano 8 | David A. Mellis 9 | Adam Meyer 10 | Barragam 11 | 12 | If you have any questions please feel free to visit our forum 13 | http://randomnerdtutorials.com/forum 14 | 15 | Created in January 2014 16 | Future code modfications can be found at: 17 | https://github.com/RuiSantosdotme/HomeAutomationServer 18 | 19 | */ 20 | 21 | //This project Only works with ARDUINO IDE 1.0+ 22 | #include 23 | #include 24 | #include 25 | 26 | /******************************************************************************************* 27 | ******** CONFIGURURATION *** 28 | *******************************************************************************************/ 29 | 30 | //server[] = { 192,168,1,73 }; //ip Address of the server you will connect to or the URL you want to connect to 31 | //Please don't change the URL 32 | char server[] = "www.homeautomationserver.com"; 33 | 34 | /******************************************************************************************* 35 | **** You must replace the XXXXXXXXXXXXXX with your unique user_key *** 36 | **** we provide that key in the members area at http://homeautomationserver.com/ *** 37 | **** this is an user_key example: do13snpx37hici1 **** 38 | *******************************************************************************************/ 39 | 40 | //keep HTTP/1.1 at the end. this is telling it what type of file it is 41 | //Replace the XXXXXXXXXXXXXX with your unique user_key 42 | String location = "/f/XXXXXXXXXXXXXX.txt HTTP/1.1"; 43 | 44 | // if need to change the MAC address (Very Rare) 45 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 46 | IPAddress ip(192,168,0,177); 47 | 48 | EthernetClient client; // defines our client 49 | char lastState[32]; // string for incoming serial data 50 | int lastStatePos = 0; // index counter for lastState variable 51 | boolean startRead = false; // if it's true is reading the page 52 | int stop_connection = 0; // if the client is not available stops the connection 53 | int light_1 = 3; // "Light #1" connected to the Ditial Pin3 of the Arduino 54 | int light_2 = 4; // "Light #2" connected to the Ditial Pin4 of the Arduino 55 | int light_3 = 5; // "Light #3" connected to the Ditial Pin5 of the Arduino 56 | int garage_door = 6; // "Garage Door" connected to the Ditial Pin6 of the Arduino 57 | int home_door = 7; // "Home Door" connected to the Ditial Pin7 of the Arduino 58 | Servo baseServo; // create servo object to control a servo 59 | Servo topServo; // create servo object to control a servo 60 | int bPos = 0; // variable to store the baseServo position 61 | int tPos = 0; // variable to store the topServo position 62 | 63 | /******************************************************************************************* 64 | **** feel free to add more outputs, inputs or servos in the void setup() *** 65 | *******************************************************************************************/ 66 | void setup(){ 67 | //starts the serial communication for debugging purpose 68 | Serial.begin(9600); 69 | //initialize the lights as outputs 70 | pinMode(light_1, OUTPUT); 71 | pinMode(light_2, OUTPUT); 72 | pinMode(light_3, OUTPUT); 73 | //initialize the doors as outputs 74 | pinMode(garage_door, OUTPUT); 75 | pinMode(home_door, OUTPUT); 76 | //webcam servos 77 | baseServo.attach(8); // attaches the servo on pin 8 to the servo object 78 | topServo.attach(9); // attaches the servo on pin 9 to the servo object 79 | 80 | /******You need to edit all the servos positions to fit your webcam setup!******/ 81 | //turns the Camera to the "Center" position 82 | bPos=78; 83 | baseServo.write(bPos); 84 | delay(50); 85 | tPos = 80; 86 | topServo.write(tPos); 87 | delay(50); 88 | //starts the Ethernet Connection 89 | if (Ethernet.begin(mac) == 0) { 90 | Serial.println("Failed to configure Ethernet using DHCP"); 91 | // no point in carrying on, so do nothing forevermore 92 | // try to congifure using IP address instead of DHCP 93 | Ethernet.begin(mac, ip); 94 | } 95 | // give the Ethernet shield a second to initialize 96 | delay(5000); 97 | } 98 | 99 | /******************************************************************************************* 100 | ********* you don't need to change anything in the void loop() *********** 101 | *******************************************************************************************/ 102 | void loop(){ 103 | //reset the stop_connection counter 104 | stop_connection = 0; 105 | //connects to your homeautomationserver file and read the current state 106 | String pageState = connect_and_read(); 107 | //updates the Arduino state 108 | updateState(pageState); 109 | //prints the state in the serial monitor 110 | Serial.print("Current state: "); 111 | Serial.println(pageState); 112 | //wait 5 seconds before reconnecting again 113 | delay(5000); 114 | } 115 | /******************************************************************************************* 116 | **** Change the updateState(String pageState) function ******** 117 | ******************************************************************************************* 118 | This function tells your arduino exactly what to do when you press one button in your 119 | dashboard at homeautomationserver.com. So simply change those if conditions to do whatever 120 | you want. You can control lights with relays, servos, transistors, LED's, buzzers, etc... 121 | pretty much anything you desire. 122 | the pageState value is indicated at your dashboard: homeautomationserver.com/dashboard 123 | there's a table at the end of the page which tells you the values of each button when 124 | it's pressed 125 | *******************************************************************************************/ 126 | 127 | void updateState(String pageState){ 128 | //when the button "Turn On" from the "Button #1" is Pressed 129 | if(pageState=="1"){ 130 | digitalWrite(light_1, HIGH); 131 | } 132 | //when the button "Turn Off" from the "Button #1" is Pressed 133 | else if(pageState=="2"){ 134 | digitalWrite(light_1, LOW); 135 | } 136 | //when the button "Turn On" from the "Button #2" is Pressed 137 | else if(pageState=="3"){ 138 | digitalWrite(light_2, HIGH); 139 | } 140 | //when the button "Turn Off" from the "Button #2" is Pressed 141 | else if(pageState=="4"){ 142 | digitalWrite(light_2, LOW); 143 | } 144 | //when the button "Turn On" from the "Button #3" is Pressed 145 | else if(pageState=="5"){ 146 | digitalWrite(light_3, HIGH); 147 | } 148 | //when the button "Turn Off" from the "Button #3" is Pressed 149 | else if(pageState=="6"){ 150 | digitalWrite(light_3, LOW); 151 | } 152 | //when the button "Turn On" from the "Button #4" is Pressed 153 | else if(pageState=="7"){ 154 | digitalWrite(light_1, HIGH); 155 | digitalWrite(light_2, HIGH); 156 | digitalWrite(light_3, HIGH); 157 | } 158 | //when the button "Turn Off" from the "Button #4"" is Pressed 159 | else if(pageState=="8"){ 160 | digitalWrite(light_1, LOW); 161 | digitalWrite(light_2, LOW); 162 | digitalWrite(light_3, LOW); 163 | } 164 | //when the button "Turn On" from the "Button #5" is Pressed 165 | else if(pageState=="9"){ 166 | digitalWrite(garage_door, HIGH); 167 | } 168 | //when the button "Turn Off" from the "Button #5" is Pressed 169 | else if(pageState=="10"){ 170 | digitalWrite(garage_door, LOW); 171 | } 172 | //when the button "Turn On" from the "Button #6" is Pressed 173 | else if(pageState=="11"){ 174 | digitalWrite(home_door, HIGH); 175 | } 176 | //when the button "Turn Off" from the "Button #6" is Pressed 177 | else if(pageState=="12"){ 178 | digitalWrite(home_door, LOW); 179 | } 180 | 181 | /******You need to edit all the servos positions to fit your webcam setup!******/ 182 | //when the button "Camera Up" from the "Camera" is Pressed 183 | else if(pageState=="25" && bPos!=35){ 184 | for(bPos; bPos>35; bPos-=1){ // goes from 0 degrees to 180 degrees in steps of 1 degree 185 | baseServo.write(bPos); // tell servo to go to position in variable 'bPos' 186 | delay(20); // waits 20ms for the servo to reach the position 187 | } 188 | } 189 | //when the button "Camera Down" from the "Camera" is Pressed 190 | else if(pageState=="26" && bPos!=120){ 191 | for(bPos; bPos < 120; bPos += 1){ // goes from 0 degrees to 180 degrees in steps of 1 degree 192 | baseServo.write(bPos); // tell servo to go to position in variable 'bPos' 193 | delay(20); // waits 20ms for the servo to reach the position 194 | } 195 | } 196 | //when the button "Camera left" from the "Camera" is Pressed 197 | else if(pageState=="27" && tPos!=20){ 198 | for(tPos; tPos>20; tPos-=1){ // goes from 0 degrees to 180 degrees on steps of 1 degree 199 | topServo.write(tPos); // tell servo to go to position in variable 'tPos' 200 | delay(20); // waits 20ms for the servo to reach the position 201 | } 202 | 203 | } 204 | //when the button "Center" from the "Camera" is Pressed 205 | else if(pageState=="28"){ 206 | bPos=78; 207 | baseServo.write(bPos); 208 | delay(50); 209 | tPos = 80; 210 | topServo.write(tPos); 211 | delay(50); 212 | } 213 | //when the button "Camera Right" from the "Camera" is Pressed 214 | else if(pageState=="29" && tPos!=140){ 215 | for(tPos; tPos < 140; tPos += 1){ // goes from 0 degrees to 180 degrees in steps of 1 degree 216 | topServo.write(tPos); // tell servo to go to position in variable 'tPos' 217 | delay(20); // waits 20ms for the servo to reach the position 218 | } 219 | } 220 | 221 | } 222 | /******************************************************************************************* 223 | ******************************************************************************************* 224 | ************************ Don't change the code below ******************************** 225 | ********* If you want to improve the code feel free to do so ************* 226 | * tell me what you improved at: https://github.com/RuiSantosdotme/HomeAutomationServer * 227 | ******* or at our forum: http://randomnerdtutorials.com/forum ***** 228 | ******************************************************************************************* 229 | *******************************************************************************************/ 230 | 231 | //connect to the server and read the state 232 | String connect_and_read(){ 233 | //connecting to the server 234 | Serial.println("connecting..."); 235 | 236 | //port 80 is typical of a www or http:// page 237 | if (client.connect(server, 80)) { 238 | Serial.println("connected"); 239 | //the exact location you want to connect to 240 | client.print("GET "); 241 | client.println(location); 242 | //your host 243 | client.println("Host: www.homeautomationserver.com"); 244 | client.println("Connection: close"); 245 | client.println(); 246 | //it's Connected - So let's read the state on our page 247 | return readState(); 248 | } 249 | //if the client can't connect to the server 250 | else{ 251 | //restarts the ethernet 252 | restart(); 253 | //tells the user that failed the connection 254 | return "connection failed"; 255 | } 256 | } 257 | //reads the state of the button your pressed on the website 258 | String readState(){ 259 | //resets the position counter 260 | lastStatePos = 0; 261 | //clear lastState memory 262 | memset( &lastState, 0, 32 ); 263 | 264 | while(true){ 265 | //checks if the client is available 266 | if (client.available()) { 267 | //stores what is reading in the char c 268 | char c = client.read(); 269 | //'<' is our begining character, our state is between '<>' for example: <1> 270 | if (c == '<' ) { 271 | //Now is Ready to start reading the page 272 | startRead = true; 273 | } 274 | //after reading the '<' 275 | else if(startRead){ 276 | //'>' is our ending character 277 | if(c != '>'){ 278 | lastState[lastStatePos] = c; 279 | lastStatePos ++; 280 | } 281 | else{ 282 | //got what we need here! We can disconnect now 283 | startRead = false; 284 | client.stop(); 285 | client.flush(); 286 | Serial.println("disconnecting."); 287 | //returns the last button pressed 288 | return lastState; 289 | } 290 | } 291 | } 292 | 293 | stop_connection ++; 294 | //if the client is not avaibable when the stop_connection reaches 10000 295 | //it will restart the ethernet connection 296 | if(stop_connection > 10000){ 297 | //stops the client 298 | client.stop(); 299 | delay(50); 300 | Serial.println("The Client is not available."); 301 | delay(50); 302 | //restarts the ethernet connection 303 | restart(); 304 | delay(100); 305 | return "Error during connection, stop_connection was activated"; 306 | } 307 | }//where the while(true) ends 308 | 309 | } 310 | 311 | //restarts the ethernet connection 312 | void restart(){ 313 | Serial.println("restarting..."); 314 | if (Ethernet.begin(mac) == 0) { 315 | Serial.println("Failed to configure Ethernet using DHCP"); 316 | // no point in carrying on, so do nothing forevermore: 317 | // try to congifure using IP address instead of DHCP: 318 | Ethernet.begin(mac, ip); 319 | } 320 | // give the Ethernet shield a second to initialize: 321 | delay(1000); 322 | } 323 | -------------------------------------------------------------------------------- /HAS_Control_220V_Lamps.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This code was created by Rui Santos 3 | In order to use this code you must create your free account at 4 | http://homeautomationserver.com 5 | 6 | Thanks to: 7 | Nelson Adriano 8 | David A. Mellis 9 | Adam Meyer 10 | Barragam 11 | 12 | If you have any questions please feel free to visit our forum 13 | http://randomnerdtutorials.com/forum 14 | 15 | Created in January 2014 16 | Future code modfications can be found at: 17 | https://github.com/RuiSantosdotme/HomeAutomationServer 18 | 19 | */ 20 | 21 | //This project Only works with ARDUINO IDE 1.0+ 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | /******************************************************************************************* 29 | ******** CONFIGURURATION *** 30 | *******************************************************************************************/ 31 | 32 | //server[] = { 192,168,1,73 }; //ip Address of the server you will connect to or the URL you want to connect to 33 | //Please don't change the URL 34 | char server[] = "www.homeautomationserver.com"; 35 | 36 | /******************************************************************************************* 37 | **** You must replace the XXXXXXXXXXXXXX with your unique user_key *** 38 | **** we provide that key in the members area at http://homeautomationserver.com/ *** 39 | **** this is an user_key example: do13snpx37hici1 **** 40 | *******************************************************************************************/ 41 | 42 | //keep HTTP/1.1 at the end. this is telling it what type of file it is 43 | //Replace the XXXXXXXXXXXXXX with your unique user_key 44 | String location = "/f/XXXXXXXXXXXXXX.txt HTTP/1.1"; 45 | 46 | // if need to change the MAC address (Very Rare) 47 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 48 | IPAddress ip(192,168,0,177); 49 | 50 | EthernetClient client; // defines our client 51 | char lastState[32]; // string for incoming serial data 52 | int lastStatePos = 0; // index counter for lastState variable 53 | boolean startRead = false; // if it's true is reading the page 54 | int stop_connection = 0; // if the client is not available stops the connection 55 | RCSwitch mySwitch = RCSwitch(); //Socket switch 56 | int flag_1=0; // flag for the socket #1 57 | int flag_2=0; // flag for the socket #2 58 | 59 | /******************************************************************************************* 60 | **** feel free to add more outputs, inputs or servos in the void setup() *** 61 | *******************************************************************************************/ 62 | void setup(){ 63 | //starts the serial communication for debugging purpose 64 | Serial.begin(9600); 65 | 66 | //initialize the 433mhz transmitter 67 | // Transmitter is connected to Arduino Pin #10 68 | mySwitch.enableTransmit(10); 69 | // Optional set pulse length. 70 | mySwitch.setPulseLength(504); 71 | // Optional set protocol (default is 1, will work for most outlets) 72 | // mySwitch.setProtocol(2); 73 | // Optional set number of transmission repetitions. 74 | mySwitch.setRepeatTransmit(30); 75 | //the socket starts off by default 76 | mySwitch.send(1332564, 24); 77 | delay(300); 78 | 79 | //starts the Ethernet Connection 80 | if (Ethernet.begin(mac) == 0) { 81 | Serial.println("Failed to configure Ethernet using DHCP"); 82 | // no point in carrying on, so do nothing forevermore 83 | // try to congifure using IP address instead of DHCP 84 | Ethernet.begin(mac, ip); 85 | } 86 | // give the Ethernet shield a second to initialize 87 | delay(5000); 88 | } 89 | 90 | /******************************************************************************************* 91 | ********* you don't need to change anything in the void loop() *********** 92 | *******************************************************************************************/ 93 | void loop(){ 94 | //reset the stop_connection counter 95 | stop_connection = 0; 96 | //connects to your homeautomationserver file and read the current state 97 | String pageState = connect_and_read(); 98 | //updates the Arduino state 99 | updateState(pageState); 100 | //prints the state in the serial monitor 101 | Serial.print("Current state: "); 102 | Serial.println(pageState); 103 | //wait 5 seconds before reconnecting again 104 | delay(5000); 105 | } 106 | /******************************************************************************************* 107 | **** Change the updateState(String pageState) function ******** 108 | ******************************************************************************************* 109 | This function tells your arduino exactly what to do when you press one button in your 110 | dashboard at homeautomationserver.com. So simply change those if conditions to do whatever 111 | you want. You can control lights with relays, servos, transistors, LED's, buzzers, etc... 112 | pretty much anything you desire. 113 | the pageState value is indicated at your dashboard: homeautomationserver.com/dashboard 114 | there's a table at the end of the page which tells you the values of each button when 115 | it's pressed 116 | *******************************************************************************************/ 117 | 118 | void updateState(String pageState){ 119 | //when the button "Turn On" from the "Button #1" is Pressed 120 | if(pageState=="1" && flag_1==0){ 121 | mySwitch.send(XXXXXXX, 24); 122 | flag_1=1; 123 | } 124 | //when the button "Turn Off" from the "Button #1" is Pressed 125 | else if(pageState=="2" && flag_1==1){ 126 | mySwitch.send(XXXXXXX, 24); 127 | flag_1=0; 128 | } 129 | //when the button "Turn On" from the "Button #2" is Pressed 130 | if(pageState=="3" && flag_2==0){ 131 | mySwitch.send(XXXXXXX, 24); 132 | flag_2=1; 133 | } 134 | //when the button "Turn Off" from the "Button #2" is Pressed 135 | else if(pageState=="4" && flag_2==1){ 136 | mySwitch.send(XXXXXXX, 24); 137 | flag_2=0; 138 | } 139 | } 140 | /******************************************************************************************* 141 | ******************************************************************************************* 142 | ************************ Don't change the code below ******************************** 143 | ********* If you want to improve the code feel free to do so ************* 144 | * tell me what you improved at: https://github.com/RuiSantosdotme/HomeAutomationServer * 145 | ******* or at our forum: http://randomnerdtutorials.com/forum ***** 146 | ******************************************************************************************* 147 | *******************************************************************************************/ 148 | 149 | //connect to the server and read the state 150 | String connect_and_read(){ 151 | //connecting to the server 152 | Serial.println("connecting..."); 153 | 154 | //port 80 is typical of a www or http:// page 155 | if (client.connect(server, 80)) { 156 | Serial.println("connected"); 157 | //the exact location you want to connect to 158 | client.print("GET "); 159 | client.println(location); 160 | //your host 161 | client.println("Host: www.homeautomationserver.com"); 162 | client.println("Connection: close"); 163 | client.println(); 164 | //it's Connected - So let's read the state on our page 165 | return readState(); 166 | } 167 | //if the client can't connect to the server 168 | else{ 169 | //restarts the ethernet 170 | restart(); 171 | //tells the user that failed the connection 172 | return "connection failed"; 173 | } 174 | } 175 | //reads the state of the button your pressed on the website 176 | String readState(){ 177 | //resets the position counter 178 | lastStatePos = 0; 179 | //clear lastState memory 180 | memset( &lastState, 0, 32 ); 181 | 182 | while(true){ 183 | //checks if the client is available 184 | if (client.available()) { 185 | //stores what is reading in the char c 186 | char c = client.read(); 187 | //'<' is our begining character, our state is between '<>' for example: <1> 188 | if (c == '<' ) { 189 | //Now is Ready to start reading the page 190 | startRead = true; 191 | } 192 | //after reading the '<' 193 | else if(startRead){ 194 | //'>' is our ending character 195 | if(c != '>'){ 196 | lastState[lastStatePos] = c; 197 | lastStatePos ++; 198 | } 199 | else{ 200 | //got what we need here! We can disconnect now 201 | startRead = false; 202 | client.stop(); 203 | client.flush(); 204 | Serial.println("disconnecting."); 205 | //returns the last button pressed 206 | return lastState; 207 | } 208 | } 209 | } 210 | 211 | stop_connection ++; 212 | //if the client is not avaibable when the stop_connection reaches 10000 213 | //it will restart the ethernet connection 214 | if(stop_connection > 10000){ 215 | //stops the client 216 | client.stop(); 217 | delay(50); 218 | Serial.println("The Client is not available."); 219 | delay(50); 220 | //restarts the ethernet connection 221 | restart(); 222 | delay(100); 223 | return "Error during connection, stop_connection was activated"; 224 | } 225 | }//where the while(true) ends 226 | 227 | } 228 | 229 | //restarts the ethernet connection 230 | void restart(){ 231 | Serial.println("restarting..."); 232 | if (Ethernet.begin(mac) == 0) { 233 | Serial.println("Failed to configure Ethernet using DHCP"); 234 | // no point in carrying on, so do nothing forevermore: 235 | // try to congifure using IP address instead of DHCP: 236 | Ethernet.begin(mac, ip); 237 | } 238 | // give the Ethernet shield a second to initialize: 239 | delay(1000); 240 | } 241 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | http://creativecommons.org/licenses/by-sa/3.0/ 2 | 3 | You are free to: 4 | Share — copy and redistribute the material in any medium or format 5 | Adapt — remix, transform, and build upon the material for any purpose, even commercially. 6 | 7 | The licensor cannot revoke these freedoms as long as you follow the license terms. 8 | Under the following terms: 9 | 10 | Attribution — You must give appropriate credit, provide a link to the license, 11 | and indicate if changes were made. You may do so in any reasonable manner, but 12 | not in any way that suggests the licensor endorses you or your use. 13 | ShareAlike — If you remix, transform, or build upon the material, you must distribute 14 | your contributions under the same license as the original. 15 | No additional restrictions — You may not apply legal terms or technological measures 16 | that legally restrict others from doing anything the license permits. 17 | Notices: 18 | 19 | You do not have to comply with the license for elements of the material in the public domain 20 | or where your use is permitted by an applicable exception or limitation. 21 | No warranties are given. The license may not give you all of the permissions necessary for your intended use. 22 | For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HomeAutomationServer 2 | ==================== 3 | 4 | Visit HomeAutomationServer.com. 5 | --------------------------------------------------------------------------------