Wiznet W5100 Web Controllable Servo/s

Hi guys,

I am trying to get at least one serveo to be controllable from the web. There is alot of internet talk, but very few code examples pertaining to this subject. The few examples I have seen are for the old IDE. I can control a switch and show analog values from the web using the standard Ethernet.h library, but not control analog from the web. Once I get something working, I am going to make a really nice tutorial for those who need help like me. This is the first and most important step of my project.

Highest regards,
Dave

simple web servo control test 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, 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 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="";

        }
      }
    }
  }
}

zoomkat:
simple web servo control test code:

Thanks. I have seen this one, its just a digital switch with some standard servo.h commands added in. There are no statements in there to handle analog over web.

There are no statements in there to handle analog over web.

Servos are not controlled by analog data. They are controlled by specifying, to the Servo library, either an angle value, that the write() method maps to a microsecond value, or a microsecond value, using the writeMicroseconds() method.

Why do you think that you need to send some kind of analog data to the Arduino?

PaulS:
Why do you think that you need to send some kind of analog data to the Arduino?

You are correct, most of the hobby servos take a pwm input. I guess I have always thought of them conceptually as analog, but I have down a basic pwm with a 555. Thank you for correcting my mistake, although it doesn't really help the thread.

I got something working but it needs alot of work. Going to attempt to make real buttons for the text buttons.

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

#include <Servo.h> 
Servo myservo1;  
Servo myservo2;  

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

String readString; 

void setup(){

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

  myservo1.write(90); 
  myservo2.write(90); 
  
  myservo1.attach(9);  
  myservo2.attach(8);  
  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server servo/pin 9,8 test 1.0");
}

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

   
        if (readString.length() < 100) {

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

   
        if (c == '\n') {

          ///////////////
          Serial.println(readString); 

          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>Pan / Tilt Servo Test</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();


          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="";

        }
      }
    }
  }
}