Pan tilt with Ethernet Shield

I have been working on a pan tilt web controller with the ethernet shield. Ive been searching the web and came across something that Dennis Schissler had wrote for the Xport not the WIZnet 1500. I've made some change to work with the wiznet but haven't had much luck getting it to work. I was wondering if some can take a look at the code?

Thanks
Jed

#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <WString.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <Servo.h>

char linebuffer[128]; // a large buffer to store our data

// keep track of how many connections we've got
int requestNumber = 0;

Servo servo1;
Servo servo2;

int value = 5; // user input value
int xposcenter = 50; // x center position of webcam
int yposcenter = 100; // y center position of webcam
int xpos; // current x position of webcam (x is left/right)
int ypos; // current y position of webcam (y is up/down)
int xpos_upperlimit = 95; // upper limit for x direction (left)
int xpos_lowerlimit = 5; // upper limit for x direction (right)
int ypos_upperlimit = 135; // upper limit for y direction (down) - note: inversed
int ypos_lowerlimit = 55; // upper limit for y direction (up) - note: inversed
//////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 142 };
Server server(80);

void setup() {
Ethernet.begin(mac, ip);
server.begin();

servo2.attach(10);
servo1.attach(9);

xpos = xposcenter;
ypos = yposcenter;

servo1.write(xposcenter);
servo2.write(yposcenter);

Serial.begin(38400);
Serial.println("serial port ready");

}

String readString = String(30); //string for fetching data from address
void loop()
{
// Create a client connection

Client client = server.available();

if (client) {

while (client.connected()) {

if (client.available()) {

char c = client.read();

//read char by char HTTP request

if (readString.length() < 30) {

//store characters to string

readString.append(c); }

//output chars to serial port

Serial.print(c);

//if HTTP request has ended
client.println ("Jed

<font size="5">Jed's<font size="5"> Webcam Positioning System

<form method="get"

 

   <button name="servo" value="8">Move Up

");
client.println ("<button name="servo" value="4">Move Left   <button name="servo" value="5">Center   <button name="servo" value="6">Move Right

<button name="servo" value="2">Move Down

");
if (c == '\n')

Serial.println(linebuffer)

char *found=0;

// Look for a ? style GET command
found = strstr(linebuffer, "?servo="); // "?inputfield=" GET request
if (found) {
// announce that we received a proper command
Serial.println("moving servo");
found += 7; // skip forward in the string to the data part

// print the data
value = int(found[0]);
Serial.println(value);
}
}

switch (value) {
case 52:
// User pressed '4', rotate left 10 degrees
if (xpos+10 < xpos_upperlimit) {
Serial.println("Moving LEFT");
xpos=xpos+10;
}
else Serial.println("Left limit reached");
break;

case 53:
// User pressed '5', center webcam
Serial.println("Centering Webcam");
xpos=xposcenter;
ypos=yposcenter;
break;

case 54:
// User pressed '6', rotate right 10 degrees
if (xpos-10 > xpos_lowerlimit) {
Serial.println("Moving RIGHT");
xpos=xpos-10;
}
else Serial.println("Right limit reached");
break;

case 56:
// User pressed '8', rotate up 10 degrees
if (ypos-10 > ypos_lowerlimit) {
Serial.println("Moving UP");
ypos=ypos-10;
}
else Serial.println("Upper limit reached");
break;

case 50:
// User pressed '2', rotate down 10 degrees
if (ypos+10 < ypos_upperlimit) {
Serial.println("Moving DOWN");
ypos=ypos+10;
}
else Serial.println("Lower limit reached");
break;

default:
Serial.print(value, BYTE);
Serial.println(" is not a correct key");

}

servo1.write(xpos);
servo2.write(ypos);

delay(1);
client.stop();
}}}

I've made some change to work with the wiznet but haven't had much luck getting it to work. I was wondering if some can take a look at the code?

I looked at it. Did that help?

Didn't think so.

Specifically, what is, and what is not, happening? How does what is happening differ from what you want to have happen?

LOL, The web page does work, but when you press a button none of the servo move. Here the link to the web page
http://jed42.homedns.org:88/

Is that the web page served up by the Arduino? If so, that works.

When I click a button, the URL changes. You have Serial.print statements throughout the code. Are you seeing stuff on the Serial Monitor (or whatever is monitoring the serial port that the Arduino is talking to)?

If so, what does linebuffer contain? What does value look like? Do you see any of the " Moving " statements?

Know what you see would be really helpful. :wink:

Yes that is the web page from the arduino. The only time i see anything from the serial monitor is when the web page get refreashed. Nothing elsa happens. I'm not that good in programing which probly why it does not work.

Man, this is like pulling teeth. Copy and paste the text in the serial monitor, so we can see what you are seeing.

Some tips, though. You are calling lots of functions (server.available, client.connected, readString.length), but all of your code is in loop.

Create some functions, like ServeWebPage(), where you actually serve up the web page.

Put each { and } on separate lines, and line them up. This:

if(client)
{
   while(client.connected())
   {
      if(client.available())
      {
         // Do something
      }
   }
}

is easier to see that braces match up correctly than:

if(client) {
   while(client.connected()) {
      if(client.available()) {
         // Do something
}}}

Just using a brouser, will a url like below (using the appropriate IP address and port) make the servo move?

http://192.168.1.1?8

Yes, that is what i like to do with it , but it is not working and right now my labtop just crashed.

Pauls

I get nothing when the serial monitor is up, So my buttons (get) is not working correctly any thoughts on this.

<form method=\"get\" isnt closed... + you close 2 times ? the rest is fine 'i think...' maybe try another shield if posible?

i'm trying the same thing only on a robot-car :stuck_out_tongue:
i might try your code :smiley:
i still have to built the webcam holder tho.

btw i agree that your code is annoying to read :smiley:
but it's your way! can't change that

btw i guess you have all the libraries?
if you miss one or have a rong one it won't work

  • Serial.println(linebuffer)
    doesn't have a ;

:stuck_out_tongue: cheers

I have been trying to get this to work for months. with no luck. I have almost given up but still working on it. My code is very hard to read, I am trying to work on that. Most of the libraries I have gotten online. The web part is the easy, its just reading the button from the web to the arduino that is the hardest. I will keep trying it until i get it. Thanks for taking time to look at it

Assuming your page is served up ok to the browser, then you probably need to add an "action" line similar to below to have the get request sent to the ip address of the arduino.

<FORM METHOD="get" action="http://127.0.0.1/cgi-bin/slider.bat"">
<INPUT TYPE="submit" VALUE="Clickable Button">
</FORM>

this code got me in the right direction
maybe it can help you to.
Got this PDE file from www.scienceprog.com

I already corrected the " and ' errors.

Got this PDE file from www.scienceprog.com

I got the code and changed the ledpin to 13 (below) just to see if I can turn it on/off, but it seems to just blink and stay on. Any Ideas?

#include <WString.h>
#include <Ethernet.h>

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
Server server(84); //server port
byte sampledata=50; //some sample data â?“ outputs 2 (ascii = 50 DEC) 
int ledPin =  13; // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){

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

//Set pin 4 to output
pinMode(ledPin, OUTPUT); 

//enable serial datada print 
Serial.begin(9600); }
void loop(){

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

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

//store characters to string 
readString.append(c); } 

//output chars to serial port
Serial.print(c);

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

//lets check if LED should be lighted
if(readString.contains("L=1")) {

//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{

//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }

// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

//set background to yellow
client.print("<body style=background-color:yellow>");

//send first heading
client.println("<font color='red'><h1>HTTP test routines</font></h1>");
client.println("<hr />");
client.println("<hr />");

//output some sample data to browser
client.println("<font color='blue' size='5'>Sample data: ");
client.print(sampledata);//lets output some data
client.println("
");//some space between lines
client.println("<hr />");

//drawing simple table
client.println("<font color='green'>Simple table: ");
client.println("
");
client.println("<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>");
client.println("<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>"); 
client.println("
");
client.println("<hr />");

//controlling led via checkbox
client.println("<h1>LED control</h1>");

//address will look like http://192.168.1.110/?L=1 when submited
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>"); 
client.println("
");

//printing LED status
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON"); 
else
client.println("<font color='grey' size='5'>OFF"); 
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");

//clearing string for next read
readString="";

//stopping client
client.stop();
}}}}}