Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. //include necessary libraries
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4.  
  5. //mac address does not change
  6. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7.  
  8. //change this to your IP address
  9. //this can be found by running the program
  10. //File --> Examples --> Ethernet --> DhcpAddressPrinter
  11. IPAddress ip(10,0,0, 188);
  12.  
  13. // Initialize the Ethernet server library
  14. // with the IP address and port you want to use
  15. // (port 80 is default for HTTP):
  16. EthernetServer server(8087);
  17.  
  18. String webClickRequest;
  19.  
  20.  
  21. void setup(){
  22.  
  23. // initialize the digital pin as an output.
  24. pinMode(24, OUTPUT);
  25. pinMode(25, OUTPUT);
  26. pinMode(26, OUTPUT);
  27. pinMode(27, OUTPUT);
  28.  
  29.  
  30. // Open serial communications and wait for port to open:
  31. Serial.begin(9600);
  32. while (!Serial) {
  33. ; // wait for serial port to connect. Needed for Leonardo only
  34. }
  35.  
  36.  
  37. // start the Ethernet connection and the server:
  38. Ethernet.begin(mac, ip);
  39. server.begin();
  40. Serial.print("server is at ");
  41. Serial.println(Ethernet.localIP());
  42. }
  43.  
  44.  
  45. void loop(){
  46. // Create a client connection
  47. EthernetClient client = server.available();
  48. if (client) {
  49. while (client.connected()) {
  50. if (client.available()) {
  51. char c = client.read();
  52.  
  53. //read the incoming HTTP request
  54. if (webClickRequest.length() < 100) {
  55.  
  56. //store the request to a string
  57. webClickRequest += c;
  58.  
  59. }
  60.  
  61. //check to see if the request has come to an end
  62. if (c == '\n') {
  63.  
  64. //Begin drawing out the website using
  65. //using basic HTML formatting with some CSS
  66. client.println("HTTP/1.1 200 OK");
  67. client.println("Content-Type: text/html");
  68. client.println();
  69. client.println("<HTML>");
  70. client.println("<HEAD>");
  71. client.println("<TITLE>Internet Controlled RC Car</TITLE>");
  72. client.println("<STYLE>");
  73. client.println("body{margin:50px 0px; padding:0px; text-align:center;}");
  74. client.println("h1{text-align: center; font-family:\"Trebuchet MS\",Arial, Helvetica, sans-serif; font-size:24px;}");
  75. client.println("a{text-decoration:none; width:75px; height:50px; border-color:black; font-family:\"Trebuchet MS\",Arial, Helvetica, sans-serif; padding:6px; background-color:#aaaaaa; text-align:center; border-radius:10px 10px 10px; font-size:24px;}");
  76. client.println("a:link {color:white;}");
  77. client.println("a:visited {color:white;}");
  78. client.println("a:hover {color:red;}");
  79. client.println("a:active {color:white;}");
  80. client.println("</STYLE>");
  81. client.println("</HEAD>");
  82. client.println("<BODY>");
  83. client.println("<H1>Controle de Carro</H1>");
  84. client.println("<br />");
  85. client.println("<br />");
  86. client.println("<a href=\"/?left\"\">LEFT</a>");
  87. client.println(" ");
  88. client.println("<a href=\"/?forward\"\">FORWARD</a>");
  89. client.println(" ");
  90. client.println("<a href=\"/?right\"\">RIGHT</a>");
  91.  
  92. client.println("<br />");
  93. client.println("<br />");
  94. client.println("<br />");
  95. client.println(" ");
  96. client.println("<a href=\"/?back\"\">BACK</a>");
  97. client.println(" ");
  98.  
  99. client.println("</BODY>");
  100. client.println("</HTML>");
  101.  
  102. //Stop loading the website
  103. delay(1);
  104. client.stop();
  105.  
  106.  
  107.  
  108. //check to see if any of the drive commands have been sent
  109. //from the webpage to the Arduino
  110.  
  111. if(webClickRequest.indexOf("?left") > 0){
  112. Serial.println("hello");
  113. left();
  114. delay(1000);
  115. // brake();
  116. }
  117.  
  118. else if(webClickRequest.indexOf("?forward") >0){
  119. forward();
  120. delay(1000);
  121. // brake();
  122. }
  123.  
  124. else if(webClickRequest.indexOf("?right") >0){
  125. right();
  126. delay(1000);
  127. // brake();
  128. }
  129.  
  130. else if(webClickRequest.indexOf("?backleft") >0){
  131. left();
  132. reverse();
  133. delay(1000);
  134. //brake();
  135. }
  136.  
  137. else if(webClickRequest.indexOf("?back") >0){
  138. reverse();
  139. delay(1000);
  140. //brake();
  141. }
  142.  
  143. else if(webClickRequest.indexOf("?backright") >0){
  144. right();
  145. reverse();
  146. delay(1000);
  147. //brake();
  148. }
  149.  
  150.  
  151. //clear the string for the new incoming data
  152. webClickRequest="";
  153.  
  154. }
  155. }
  156. }
  157. }
  158. }
  159.  
  160. //drive functions
  161. //here on down
  162.  
  163. void reverse(){
  164. digitalWrite(24, LOW);
  165. digitalWrite(25, HIGH);
  166. }
  167.  
  168. void forward(){
  169. digitalWrite(24, HIGH);
  170. digitalWrite(25, LOW);
  171. }
  172.  
  173. void right(){
  174. digitalWrite(26, HIGH);
  175. digitalWrite(27, LOW);
  176. }
  177.  
  178. void left(){
  179. digitalWrite(26, LOW);
  180. digitalWrite(27, HIGH);
  181. }
  182.  
  183. void brake(){
  184. digitalWrite(24, LOW);
  185. digitalWrite(25, LOW);
  186. digitalWrite(26, LOW);
  187. digitalWrite(27, LOW);
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement