Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <SPI.h>
  3. //network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
  4. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  5. byte ip[] = { 192, 168, 0, 177 };
  6. byte gateway[] = { 192, 168, 1, 100 };
  7. byte subnet[] = { 255, 255, 255, 0 };
  8.  
  9. String inString = String(35);
  10.  
  11. String Led;
  12.  
  13. int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8,9 }; //Led pins num 0 in arry is not used
  14. int numofleds = 8; //numofleds
  15. String value[] = {"on","on","on","on","on","on","on","on","on"}; //startup all led are off
  16.  
  17. EthernetServer server(80);
  18. String data;
  19. void setup()
  20. {
  21. Serial.begin(9600);
  22. Ethernet.begin(mac, ip,gateway,subnet);
  23. server.begin();
  24. //set pin mode
  25. for (int j = 1; j < (numofleds + 1); j++){
  26. pinMode(led[j], OUTPUT);
  27. }
  28. Serial.println("Serial READY");
  29. Serial.println("Ethernet READY");
  30. Serial.println("Server READY");
  31. }
  32.  
  33. void loop()
  34. {
  35. EthernetClient client = server.available();
  36.  
  37. if(client){
  38. // an http request ends with a blank line
  39. boolean current_line_is_blank = true;
  40. while (client.connected()) {
  41.  
  42. if(client.available()) {
  43.  
  44. char c = client.read();
  45. // if we've gotten to the end of the line (received a newline
  46. // character) and the line is blank, the http request has ended,
  47. // so we can send a reply
  48. if (inString.length() < 35) {
  49. inString.concat(c);
  50. }
  51. if (c == '\n' && current_line_is_blank) {
  52.  
  53. // send a standard http response header
  54. client.println("HTTP/1.1 200 OK");
  55. client.println("Content-Type: text/html");
  56. client.println();
  57. client.println("<html><body><form method=get>");
  58. client.println("<p>Led controller</p>");
  59.  
  60. for(int i=1;i < (numofleds + 1) ;i++){
  61. Led = String("led") + i;
  62.  
  63. if(inString.indexOf(Led+"=on")>0 || inString.indexOf("all=on")>0){
  64. Serial.println(Led+"on");
  65. digitalWrite(led[i], HIGH);
  66. value[i] = "off";
  67. }else if(inString.indexOf(Led+"=off")>0 || inString.indexOf("all=off")>0 ){
  68. Serial.println(Led+"on");
  69. digitalWrite(led[i], LOW);
  70. value[i] = "on";
  71. }
  72. client.println("<br>"+Led+" <input type=submit name="+Led+" value="+value[i]+">");
  73. }
  74. client.println("<br>All <input type=submit name=all value=on><input type=submit name=all value=off>");
  75. client.println("</from></html></body>");
  76. break;
  77. }
  78. if (c == '\n') {
  79. // we're starting a new line
  80. current_line_is_blank = true;
  81. } else if (c != '\r') {
  82. // we've gotten a character on the current line
  83. current_line_is_blank = false;
  84. }
  85. }
  86. }
  87. // give the web browser time to receive the data
  88. delay(1);
  89. inString = "";
  90. client.stop();
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement