Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys!
I Was Practicing with my Arduino Ethernet Shield for a Project i wanted to do and it consists of controlling a servo motor from the Internet, I had a html code on my SD Card
C++
 #include <spi.h>
#include <ethernet.h>
#include <servo.h>
#include<sd.h>
Servo myservo;

byte mac[] ={0xDE, 0xAD, 0xBB, 0xEF, 0xFE, 0xED};
IPAddress ip(10,150,40,84);
IPAddress gateway(10,150,40,1);
IPAddress subnet(255,255,255,0);
EthernetServer server(80);
File webFile;
int pos =10;
String redString;
void setup(){
  myservo.attach(9);
  myservo.write(pos);
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  Ethernet.begin(mac,ip,gateway,subnet);
  Serial.print("My Ip is : ");
  Serial.println(ip);
  server.begin();
  //Init SD Card
  if(!SD.begin(4)){
    Serial.println("ERROR - Initialization Failed!");
    return;
  }
  Serial.println("SUCCESS -SD card Initialized.");
  if(!SD.exists("securino.htm")){
    Serial.println("ERROR - Can't find securino.html");
  }else{
  Serial.println("SUCCESS - Found securino.html.");
  }
}
void loop(){
  EthernetClient client = server.available(); // try to get a client
  if(client){ // got a client
   boolean currentLineIsBlank = true;
   while(client.connected()){
     if(client.available()){// client data avail to read
       char c = client.read();// read 1 byte char from client
         // last line of client req. is blank and ends with \n
         // respond to client only after last line recieved
         if(redString.length()<100){
           redString+=c;
         }
         if(c="\n" && currentLineIsBlank){
           Serial.println(redString);
           // send a standard http response header
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type:text/html");
           client.println("Connenction:close");
           client.println();
           // send Web page
           webFile = SD.open("securino.htm");//open webpage
           if(webFile){
             while(webFile.available()){
               client.write(webFile.read());// send webpage to client
             }
             webFile.close();
           }
           break;
           if(redString.indexOf("?turnlft")>0){ // PROBLEM HERE ****

             pos = pos -10;
             myservo.write(pos);
           }
           if(redString.indexOf("?turnrght")>0){ // AND HERE ****
             pos = pos +10;
             myservo.write(pos);
           }
           redString ="";
         }
         //every line of text recieved from client ends with \r\n
         if(c=='\n'){
           //last char on line of recieved text
           //starting new line with next character read
           currentLineIsBlank = true;
         }
         else if(c!='\r'){
           //a text character was reciever from client
           currentLineIsBlank = false;
         }
     }
   }
   delay(1); //  give the browser time to recieve the data
   client.stop();
  }
}
           <pre lang="c++">

and Here is the HTML Code, I Think the Problem lies Here Could you Help me

This are the Two Link i used to Control the Motor
..........
<a href="\"/?turnlft\"\"">Turn Left</a>
<a href="\"/?turnrght\"\"">Turn Right</a>
Posted

1 solution

1. you have mixed client.write and client.println calls and you are closing the connection before sending the html code (body)
2. find a http client and url parsing library for the arduino to ensure "redString" contains the request url. I doubt your client.read method works!
3. debug your http traffic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900