I try to hook up a servo to an ethernet shield because I want to control the servo with an textfield over the internet. Since I am new to the ethernet shield I still have some problems regarding the code.
I manage to put up a string i.e. 192.168.0.3/?servo=123 by using the GET command but I have some problems reading it with the arduino and put it in an int. In the end I want the arduino to read sthg like: int servo = 123 which i can send to the servo. Is there any easy way to do this?
I was also thinking of some read command which searches the readString for “?servo=” and puts whatever comes afterwards in a special variable. do you know any special command for this? After this I also want to light an LED while doing this so dont mind the LED parts.
Thanks in advance!
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
byte mac[] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 };
byte ip[] = { 192, 168, 0, 3 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
Servo myservo;
int Pin3 = 3;
char Data[4];
int i;
int x;
int servo;
String readString=String(100); // string for fetching data from address
void setup(){
Ethernet.begin(mac, ip, subnet);
server.begin();
myservo.attach(9);
pinMode(Pin3, OUTPUT);
Serial.begin(9600); }
void loop(){
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 100) {readString = readString + c;}
if (c == '\n')
{
Serial.print(readString); //I get something like "100GET ?servo=xxx" Where does the 100 come from?
do {
if(readString>0)
{ Data[x] = readString[x+13]; i++; } //Problem is probably here! :)
}
while(x<4);
x=0;
servo = atof(Data);
Serial.print("Received: ");
Serial.print(servo);
Serial.println();
myservo.writeMicroseconds(servo);
delay(1000);
myservo.writeMicroseconds(1400);
}
/*if(readString.indexOf("") > -1){
digitalWrite(Pin3, LOW);
Serial.println("LED off");
Pin3ON = false;
}
*/
//--------------------------HTML------------------------
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head>");
client.print("<title>Servotest</title>");
client.println("</head>");
client.print("<body bgcolor='#444444'>");
client.println("
<hr />");
client.println("<h1><div align='center'><font color='#2076CD'>Servotest</font color></div></h1>");
client.println("<hr />
");
client.println("<tr bgColor='#222222'>");
client.println("<td bgcolor='#222222'><font face='Verdana' color='#CFCFCF' size='2'>Servo Test
</font></td>");
client.println("<td align='center' bgcolor='#222222'><form method=get><input type='text' name='servo'><input type='submit' value='Drehen'></form></td>");
client.println("<td align='center' bgcolor='#222222'><form method=get><input type=submit name='servo' value='90'></form></td>");
client.println("<td align='center'><font color='green' size='5'>Servovalue: ");
client.print(servo);
client.println("</tr>");
client.println("</tr>");
client.println("</table>");
client.println("</body></html>");
readString="";
client.stop();
}}}}
String readString=String(100); // string for fetching data from address
This is creating a String object bu converting the value 100 to a string, and then wrapping that string in the String object. It then copies that String to readString, wasting more memory. What did you think it was doing?
Serial.print(readString); //I get something like "100GET ?servo=xxx" Where does the 100 come from?
Well, now you know.
do {
if(readString>0)
{ Data[x] = readString[x+13]; i++; } //Problem is probably here! :)
}
while(x<4);
A for loop is so much easier to understand…
If you printed out Data after filling it, you’d see that it contained ‘=’, ‘x’, ‘x’, and ‘x’. Which, by the way, is not the same as “=xxx”.
servo = atof(Data);
The atof() function (Why are you converting what you expect to be an int to a float?) expects a NULL terminated array of characters. You are not passing it a NULL terminated array of characters.
I was also thinking of some read command which searches the readString for “?servo=”
Like maybe String::indexOf()?
and puts whatever comes afterwards in a special variable.
Because readString doesn't contain an = sign? I don't know. I can't see that you are printing readString, or what you are doing to cause readString to be valued. You've got to help out a little...
And to preempt the next problem - As you are running a shield and a servo, you should consider separate power as this will likely cause you problems soon if not already.
See the links in my signature for a demonstration why
Ah, ok sry I mean this part:
it looks like I dont get any chars in readString but I dont know where i did the mistake…
void loop(){
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
if (readString.length() < 100) readString = readString + c; //read chars one by one and store them in readString
if (c == '\n') //...till the end of line
{
Serial.println(readString); //I forgot this in my previous post
ind1 = readString.indexOf("=");
ServoString = readString.substring(ind1, ind1+4); //I wonder if thats going to work :-P
Serial.println(ServoString);
}
}
if (readString.length() < 100) readString = readString + c; //read chars one by one and store them in readString
This implies that you have a maximum length command in mind. In which case, you should ditch the String class, and use a static char array. If you persist in this folly (and you might be able to), at least minimize the amount of thrashing that goes on by using the += operator.
readString += c;
Snippets don’t tell us anything.
But, before you paste any more code, use the Tools + Auto Format option. That random indenting is quite unprofessional.