hello all , for a project Im working on at the moment i'm looking to control a few servos via a web page that can be accessed from anywhere.with the example sketch I found the Arduino board serves up a page with 5 buttons , 2 control an LED , one off and one on , and the other 3 have the ability to turn the Arduino to positions 0,90 and 180. I tried to code for an a additional servo and found that the controls on the web page duplicated with no problems but that for the additional servo only 1 button would effect the servo. after pressing one button it would go to the designated position and then hum and become unresponsive.
I'm using and arduino uno and a compatible ethernet shield.
heres the sketch
#include <SPI.h>
#include <Ethernet.h> add libraries for servo and ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; my mac address and arduinos ip
byte ip[] = { 192,168,0,8 };
const int MAX_PAGENAME_LEN = 8; // max characters in page name
char buffer[MAX_PAGENAME_LEN+1]; // additional character for terminating null
EthernetServer server(8081);
// Servo Library and setup
#include <Servo.h>
Servo myservo ; // create servo object to control a servo
int servo = 6; //Define what pin the servo is on
Servo myservo2; , servo number 2 here
int servo2=3;
void setup()
{
Serial.begin(9600); //Start serial port for debugging
myservo.attach(servo);
myservo2.attach(servo2); // attaches the servo on pin 6 to the servo object
Ethernet.begin(mac, ip);
server.begin();
delay(2000);
}
void loop()
{
EthernetClient client = server.available();
if (client) {
int type = 0;
while (client.connected()) {
if (client.available()) {
// GET, POST, or HEAD
memset(buffer,0, sizeof(buffer)); // clear the buffer
if(client.readBytesUntil('/', buffer,sizeof(buffer))){
if(strcmp(buffer,"POST ") == 0){
client.find("\n\r"); // skip to the body
// find string starting with "pin", stop on first blank line
// the POST parameters expected in the form pinDx=Y
// where x is the pin number and Y is 0 for LOW and 1 for HIGH
while(client.findUntil("pinD", "\n\r")){
int pin = client.parseInt(); // the pin number
int val = client.parseInt();
int val2 = client.parseInt(); // 0 or 1
pinMode(pin, OUTPUT);
if(pin == 9){
digitalWrite(pin , val );
}
Serial.println(val);
if(pin == 6){
myservo.write(val);
}
Serial.println(val2);
if(pin == 3){
myservo2.write(val2);
not entirely sure about the above code , probably my problem. just tried to replicated what I saw as being the logic driving the first servo
}
}
}
sendHeader(client,"Post example");
//Control of Light
//create HTML button to control pin 9
client.println("<h2><font color=#f6a343>IoT - Servo and LED</h2>");
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD9'");
client.println(" value='0'><input type='submit' value='Light Off'/></form>");
//create HTML button to turn on pin 9
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD9'");
client.print(" value='1'><input type='submit' value='Light On'/></form>");
//Control of Servo
//Turn Right
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD6'"); form action sends data from the server , the board to the client , the web page. here the pinD is specified. little hazy here as the term value seems to spring from nowhere , this is not the same as the int val specified earlier in the sketch? client.print sends html code to the browser.
client.println(" value='0'><input type='submit' value='Servo Right'/></form>");
//Turn Left
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD6'");
client.print(" value='180'><input type='submit' value='Servo Left'/></form>");
//Stop
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD6'");
client.print(" value='90'><input type='submit' value='Servo Stop'/></form>");
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD3'"); I replicated the above and specified the pin the other servo was attached to , pin 3
client.println(" value='0'><input type='submit' value='Servo Right'/></form>");
//Turn Left
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD3'");
client.print(" value='180'><input type='submit' value='Servo Left'/></form>");
//Stop
client.print(
"<form action='/' method='POST'><p><input type='hidden' name='pinD3'");
client.print(" value='90'><input type='submit' value='Servonob Stop'/></form>");
//Create webcam feed
//client.print("<img name=""FoscamCamera"" src=""http://67.168.78.50:1026/videostream.cgi?user=peon&pwd=noob"" width=""480"" height=""360"" alt=""Live Feed"" style=""background-color: #009999"" />"); not using this section
client.println("</body></html>"); I think html requires a body for every page , hence this empty tag.
client.stop();
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
void sendHeader(EthernetClient client, char *title)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>");
client.print(title);
client.println("</title><body>");
}
I'm not quite sure what my problem is anyone who could tell me would be much appreciated