Advertisement
Guest User

FlyingPoo's Home Automation Script

a guest
Jan 1st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  5. byte ip[] = { 192, 168, 1, 178 }; // ip in lan
  6. byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
  7. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  8. EthernetServer server(80); //server port
  9.  
  10. String readString;
  11.  
  12. int ledPin[] = {
  13. 2, 3, 4, 5, 6, 7, 8, 9, A5, A4, A3, A2, A1, A0 };
  14. int pinCount = 14;
  15.  
  16. void setup(){
  17.  
  18. for (int i = 0; i < pinCount; i++) {
  19. pinMode(ledPin[i], OUTPUT);
  20. digitalWrite(ledPin[i], HIGH);
  21. }
  22.  
  23. //start Ethernet
  24. Ethernet.begin(mac, ip, gateway, subnet);
  25. server.begin();
  26. //the pin for the servo co
  27. //enable serial data print
  28. Serial.begin(9600);
  29. Serial.println("server LED test 2.2"); // so I can keep track of what is loaded
  30. }
  31.  
  32. void loop(){
  33. // Create a client connection
  34. EthernetClient client = server.available();
  35. if (client) {
  36. while (client.connected()) {
  37. if (client.available()) {
  38. char c = client.read();
  39.  
  40. //read char by char HTTP request
  41. if (readString.length() < 100) {
  42.  
  43. //store characters to string
  44. readString += c;
  45. //Serial.print(c);
  46. }
  47.  
  48. //if HTTP request has ended
  49. if (c == '\n') {
  50.  
  51. ///////////////
  52. Serial.println(readString); //print to serial monitor for debuging
  53.  
  54.  
  55. client.println("HTTP/1.1 200 OK"); //send new page
  56. client.println("Content-Type: text/html");
  57. client.println();
  58. client.println("<HTML>");
  59. client.println("<HEAD><TITLE>Home Automation System</TITLE></HEAD>");
  60. client.println("<BODY>");
  61.  
  62. for (int j = 0; j < pinCount; j++) {
  63. client.println("<p><input type=button value='Channel " + String(j) + " ON' onmousedown=location.href='?ch" + String(j) + "on'>");
  64. client.println("<input type=button value='Channel " + String(j) + " OFF' onmousedown=location.href='?ch" + String(j) + "off'></p>");
  65. }
  66. client.println("</BODY>");
  67. client.println("</HTML>");
  68.  
  69. delay(1);
  70. //stopping client
  71. client.stop();
  72.  
  73. ///////////////////// control arduino pin
  74. for (int i = 0; i < pinCount; i++) {
  75. if (readString.indexOf("?ch" + String(i) + "on") >0)//checks for on
  76. {
  77. digitalWrite(ledPin[i], LOW); // set channel i high
  78. Serial.print("Channel " + String(i) + " On");
  79. }
  80. if (readString.indexOf("?ch" + String(i) + "off") >0)//checks for off
  81. {
  82. digitalWrite(ledPin[i], HIGH); // set channel i low
  83. Serial.print("Channel " + String(i) + " Off");
  84. }
  85. }
  86. //clearing string for next read
  87. readString="";
  88. }
  89. }
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement