Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include necessary libraries
- #include <SPI.h>
- #include <Ethernet.h>
- //mac address does not change
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- //change this to your IP address
- //this can be found by running the program
- //File --> Examples --> Ethernet --> DhcpAddressPrinter
- IPAddress ip(10,0,0, 188);
- // Initialize the Ethernet server library
- // with the IP address and port you want to use
- // (port 80 is default for HTTP):
- EthernetServer server(8087);
- String webClickRequest;
- void setup(){
- // initialize the digital pin as an output.
- pinMode(24, OUTPUT);
- pinMode(25, OUTPUT);
- pinMode(26, OUTPUT);
- pinMode(27, OUTPUT);
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- // start the Ethernet connection and the server:
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
- void loop(){
- // Create a client connection
- EthernetClient client = server.available();
- if (client) {
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //read the incoming HTTP request
- if (webClickRequest.length() < 100) {
- //store the request to a string
- webClickRequest += c;
- }
- //check to see if the request has come to an end
- if (c == '\n') {
- //Begin drawing out the website using
- //using basic HTML formatting with some CSS
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<HTML>");
- client.println("<HEAD>");
- client.println("<TITLE>Internet Controlled RC Car</TITLE>");
- client.println("<STYLE>");
- client.println("body{margin:50px 0px; padding:0px; text-align:center;}");
- client.println("h1{text-align: center; font-family:\"Trebuchet MS\",Arial, Helvetica, sans-serif; font-size:24px;}");
- 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;}");
- client.println("a:link {color:white;}");
- client.println("a:visited {color:white;}");
- client.println("a:hover {color:red;}");
- client.println("a:active {color:white;}");
- client.println("</STYLE>");
- client.println("</HEAD>");
- client.println("<BODY>");
- client.println("<H1>Controle de Carro</H1>");
- client.println("<br />");
- client.println("<br />");
- client.println("<a href=\"/?left\"\">LEFT</a>");
- client.println(" ");
- client.println("<a href=\"/?forward\"\">FORWARD</a>");
- client.println(" ");
- client.println("<a href=\"/?right\"\">RIGHT</a>");
- client.println("<br />");
- client.println("<br />");
- client.println("<br />");
- client.println(" ");
- client.println("<a href=\"/?back\"\">BACK</a>");
- client.println(" ");
- client.println("</BODY>");
- client.println("</HTML>");
- //Stop loading the website
- delay(1);
- client.stop();
- //check to see if any of the drive commands have been sent
- //from the webpage to the Arduino
- if(webClickRequest.indexOf("?left") > 0){
- Serial.println("hello");
- left();
- delay(1000);
- // brake();
- }
- else if(webClickRequest.indexOf("?forward") >0){
- forward();
- delay(1000);
- // brake();
- }
- else if(webClickRequest.indexOf("?right") >0){
- right();
- delay(1000);
- // brake();
- }
- else if(webClickRequest.indexOf("?backleft") >0){
- left();
- reverse();
- delay(1000);
- //brake();
- }
- else if(webClickRequest.indexOf("?back") >0){
- reverse();
- delay(1000);
- //brake();
- }
- else if(webClickRequest.indexOf("?backright") >0){
- right();
- reverse();
- delay(1000);
- //brake();
- }
- //clear the string for the new incoming data
- webClickRequest="";
- }
- }
- }
- }
- }
- //drive functions
- //here on down
- void reverse(){
- digitalWrite(24, LOW);
- digitalWrite(25, HIGH);
- }
- void forward(){
- digitalWrite(24, HIGH);
- digitalWrite(25, LOW);
- }
- void right(){
- digitalWrite(26, HIGH);
- digitalWrite(27, LOW);
- }
- void left(){
- digitalWrite(26, LOW);
- digitalWrite(27, HIGH);
- }
- void brake(){
- digitalWrite(24, LOW);
- digitalWrite(25, LOW);
- digitalWrite(26, LOW);
- digitalWrite(27, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement