Advertisement
computermuseo

comandare relè via ethernet arduino

Mar 11th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. boolean statop1 = false;
  5. boolean statop2 = false;
  6. boolean statop3 = false;
  7. boolean statop4 = false;
  8.  
  9. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  10. byte ip[] = { 192, 168, 0, 50 }; // ip in lan
  11. byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
  12. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  13. EthernetServer server(80); //server port
  14.  
  15. String readString;
  16.  
  17. //////////////////////
  18.  
  19. void setup(){
  20.  
  21. pinMode(7, OUTPUT); //pin selected to control
  22. pinMode(6, OUTPUT); //pin selected to control
  23. pinMode(5, OUTPUT); //pin selected to control
  24. pinMode(4, OUTPUT); //pin selected to control
  25. //start Ethernet
  26. Ethernet.begin(mac, ip, gateway, subnet);
  27. server.begin();
  28. Serial.begin(9600);
  29. Serial.println("Domotic Server test 1.0"); // so I can keep track of what is loaded
  30.  
  31. }
  32.  
  33. void loop(){
  34. // Create a client connection
  35. EthernetClient client = server.available();
  36. if (client) {
  37. while (client.connected()) {
  38. if (client.available()) {
  39. char c = client.read();
  40.  
  41. //read char by char HTTP request
  42. if (readString.length() < 100) {
  43.  
  44. //store characters to string
  45. readString += c;
  46. //Serial.print(c);
  47. }
  48.  
  49. //if HTTP request has ended
  50. if (c == '\n') {
  51.  
  52. ///////////////
  53. Serial.println(readString); //print to serial monitor for debuging
  54.  
  55. client.println("HTTP/1.1 200 OK"); //send new page
  56. client.println("Content-Type: text/html");
  57. client.println();
  58.  
  59. client.println("<!DOCTYPE html><html><head><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black-translucent'><link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css'><TITLE>rele' tester computermuseo</TITLE></HEAD><BODY><H1>relè tester computermuseo</H1><hr><br><ul><li><p align=\"left\">rele 1:<a href=\"/?light1on\">accendi</a><a href=\"/?light1off\">spegni</a></li></p><br><br><li><p align=\"left\">rele 2:<a href=\"/?light2on\">accendi</a><a href=\"/?light2off\">spegni</a></li></p><br><br><li><p align=\"left\">rele 3:<a href=\"/?light3on\">accendi</a><a href=\"/?light3off\">spegni</a></li></p><br><br><li><p align=\"left\">rele 4:<a href=\"/?light4on\">accendi</a><a href=\"/?light4off\">spegni</a></li></p><br><br></ul></BODY></HTML>");
  60.  
  61. delay(1);
  62. //stopping client
  63. client.stop();
  64.  
  65. ///////////////////// control arduino pin
  66. if(readString.indexOf("?light1on") >0)//checks for on
  67. {
  68. statop1 = true;
  69. }
  70. if(readString.indexOf("?light1off") >0)//checks for off
  71. {
  72. statop1 = false;
  73. }
  74. if(readString.indexOf("?light2on") >0)//checks for on
  75. {
  76. statop2 = true;
  77. }
  78. if(readString.indexOf("?light2off") >0)//checks for off
  79. {
  80. statop2 = false;
  81. }
  82. if(readString.indexOf("?light3on") >0)//checks for on
  83. {
  84. statop3 = true;
  85. }
  86. if(readString.indexOf("?light3off") >0)//checks for off
  87. {
  88. statop3 = false;
  89. }
  90. if(readString.indexOf("?light4on") >0)//checks for on
  91. {
  92. statop4 = true;
  93. }
  94. if(readString.indexOf("?light4off") >0)//checks for off
  95. {
  96. statop4 = false;
  97. }
  98.  
  99.  
  100. if(statop1 == true)
  101. {
  102. digitalWrite(7, LOW);
  103. }
  104. if(statop1 == false)
  105. {
  106. digitalWrite(7, HIGH);
  107. }
  108. if(statop2 == true)
  109. {
  110. digitalWrite(6, LOW);
  111. }
  112. if(statop2 == false)
  113. {
  114. digitalWrite(6, HIGH);
  115. }
  116. if(statop3 == true)
  117. {
  118. digitalWrite(5, LOW);
  119. }
  120. if(statop3 == false)
  121. {
  122. digitalWrite(5, HIGH);
  123. }
  124. if(statop4 == true)
  125. {
  126. digitalWrite(4, LOW);
  127. }
  128. if(statop4 == false)
  129. {
  130. digitalWrite(4, HIGH);
  131. }
  132. //clearing string for next read
  133. readString="";
  134.  
  135.  
  136.  
  137. }
  138. }
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement