controlling servo motor over internet using erduino ethernet shield

Hi,

I want to start a project on Arduino. I m actually a newbie to the arduino.
Now getting into the project , i want to give a pan and tilt movement to a webcam through internet or intranet. i googled it and found that i need to the arduino board and arduino ethernet shield to get into action. I bought all the parts , started doing the project. I got strucked with the code.
I actually want control the servos by clicking a pushbutton in the webpage. i want to use two servos for the pan & tilt movement. Please help me on code .. please do the needful .

Thankz in advance,
Prawin.

Hi,

Are you hosting the web server on the Arduino itself? In such case you need to parse the data from the client (web browser). The web page buttons shall post some variables to the server, and the server shall parse the incoming data from the client and move the servos.

Check this http://www.instructables.com/id/ServDuino-Arduino-Webserver/

Also, the following code will probably work for you:

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; 
byte gateway[] = { 192, 168, 1, 1 }; 
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString, servo1, servo2;

Servo myservo1;  


void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);
myservo1.attach(7);

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

        //store characters to string
        readString += c;
        }

        //if HTTP request has ended
        if (c == '\n') {
          Serial.println(readString);

          //readString looks like "GET /?-1500 HTTP/1.1"

          if (readString.length() >0) {
              Serial.println(readString);
              servo1 = readString.substring(7, 11);
              Serial.println(servo1);
              int n1;
              char carray1[6];
              servo1.toCharArray(carray1, sizeof(carray1));
              n1 = atoi(carray1);
              myservo1.writeMicroseconds(n1);
              readString="";
            }
           client.stop();

           readString="";

          }
        }
      }
    }
}s

Please help me on code

You need to post the code you're having problems with, otherwise it is difficult to help you.

Some web based control code.

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on\"\">ON</a>"); 
          client.println("<a href=\"/?off\"\">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Hey thankz for the code ...it really worked for my for my project with very little manipulations....it is very much helpful to me.. :slight_smile:

But i m not satisfied with the web interface that i look at .... if anyone can help me to use some push-buttons to give the control to servo to move, it could be helpful to run my project as i wish ....

Thankz once again to charalamposdoukas & zoomkat .. :smiley:

i attached my code ,which was working 100% for my project...

#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h> 
Servo myservo1;  // create servo object to control a servo1
Servo myservo2;  // create servo object to control a servo2


byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEC }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){


  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  myservo1.write(90); //set initial servo1 position if desired
  myservo2.write(90); //set initial servo2 position if desired
  
  myservo1.attach(9);  //the pin for the servo1 control
  myservo2.attach(8);  //the pin for the servo2 control
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 9,8 test 1.0"); // so I can keep track of what is loaded
}

void loop()
{
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Arduino </H1>");
         
         //Servo1 movement
          client.println("<a href=\"/?right\"\">RIGHT</a>");
          client.println("<a href=\"/?center\"\">CENTER</a>"); 
          client.println("<a href=\"/?left\"\">LEFT</a>"); 
          
          //servo2 movement
          client.println("<a href=\"/?up\"\">UP</a>");
          client.println("<a href=\"/?mid\"\">MIDDLE</a>"); 
          client.println("<a href=\"/?down\"\">DOWN</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(100);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin

          if(readString.indexOf("left") >0)
          {
            myservo1.write(135);
            delay (50);          
            Serial.println(" Turn to Left");
          }
                    
          if(readString.indexOf("center") >0)
          {
            myservo1.write(90);
            delay (50);            
            Serial.println(" Turn to Center");
          }
          if(readString.indexOf("right") >0)
          {
            myservo1.write(45);
            delay (50);            
            Serial.println(" Turn to Right");
          }
          
          
          
          
          if(readString.indexOf("up") >0)
          {
            myservo2.write(135);
            delay (50);            
            Serial.println(" Move Upwards ");
          }
                    
          if(readString.indexOf("mid") >0)
          { 
            myservo2.write(90);
            delay (50);
            Serial.println(" Move to Center");
          }
          
          if(readString.indexOf("down") >0)
          {
            myservo2.write(45);
            delay (50);            
            Serial.println(" Move downwards ");
          }
          
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

panntiltcode.ino (3.79 KB)

You can find lots of html tutorials with various buttons. below is code with mousedown activated buttons.

//zoomkat 3-17-12
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 

#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  myservo.write(90); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo co
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          // DIY buttons
          client.println("<a href=\"/?on\"\">ON</a>"); 
          client.println("<a href=\"/?off\"\">OFF</a>
"); 

          // mousedown buttons
          client.println("
<input type=\"button\" value=\"ON\" onmousedown=\"location.href ('/?on');\"/>"); 
          client.println("<input type=\"button\" value=\"OFF\" onmousedown=\"location.href ('/?off');\"/>");        
          
          // mousedown radio buttons
          client.println("

<input type=\"radio\" value=\"ON\" onmousedown=\"location.href ('/?on');\"\">ON</>"); 
          client.println("<input type=\"radio\" value=\"OFF\" onmousedown=\"location.href ('/?off');\"\">OFF</>");        
 
          client.println("</BODY>");
          client.println("</HTML>");
 
          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") >0)//checks for on
          {
            myservo.write(40);
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            myservo.write(140);
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Hello,

I have started a similar project:

http://lulzepp.tumblr.com/ Is it what you are trying to realize ?

I have posted the code:

http://arduino.cc/forum/index.php/topic,124966.0.html

Do no hesitate to ask for help !!

@jackhobbiest, as a part of my project i need to control servo motor via ethernet , so can you please provide the circuit of your project that is working ....
thank you

someone that knows how to program a adrino to control a motor over the internet please contact me, i am a 25 year old entrapenuer than has a great business idea willing to split 50/50 no questions asked for a change to retire , it is a likely business op that i have that i have the idea and don't have the programming background to get it off the ground. if your interested in hearing my idea please please contact me at colt.dunagan@gmail.com lets retire together. :slight_smile: