Hi everyone.
I'm doing a project with the Arduino Yún in which I have to control a pair of servomotors via WiFi.
Although I'm kind of new to Arduino and programming, I mannaged myself to make this program in order to do what I want using the Knob and Bridge examples, but when I run it, it doesn't do anything. I'm testing with LEDs and they just light in one intensity.
It would be very helpful if you could help me a little to see what i'm doing wrong.
#include <Servo.h> //Biblioteca para manejar los servomotores
#include <Bridge.h> //Biblioteca que permite la comunicacion entre los procesadores AR9331 y Atmel 32u4 del Arduino Yún
#include <Console.h> //Biblioteca que envía los datos a un monitor de consola
#include <Wire.h> //Biblioteca que permite la comunicación I2C
#include <YunServer.h>
#include <YunClient.h>
Servo myservox; //Crea un objeto para controlar el servomotor 1, eje X
Servo myservoy; //Crea un objeto para controlar el servomotor 2, eje Y
YunServer server; //Inicia el servidor para el Yún, usa el puerto 5555 por defecto
void setup() {
Bridge.begin(); //Inicia la biblioteca Bridge
myservox.attach(9); //Asocia el servo en pin 9 al objeto servo
myservoy.attach(10); //Asocia el servo en pin 10 al objeto servo
// Escucha toda la red (comunicación por sockets)
server.noListenOnLocalhost();
server.begin();
Serial.begin(9600); //Inicia la comunicación serial
// Console.begin();
// while (!Console) {
// ; // wait for Console port to connect.
// }
}
void loop() {
//Se inician bucles para controlar activamente la placa Arduino
// Get clients coming from server
YunClient client = server.accept();
// There is a new client
if (client) {
// Change the predifined timeout from 2000 to 5. Avoid impressive timeout.
client.setTimeout(5);
Serial.println("Client connected!");
// When we get a client, go in the loop and exit only when the client disconnect. This will automatically happens from the website for each http request.
while(client.connected()){
// Process request
process(client);
}
// Close connection and free resources.
client.stop();
}
else {
Serial.println("no client connected, retrying");
}
// Delay for the battery, for the debug too. Doesn't affect the response time of the Arduino. (Check if there is another client each second)
delay(1000);
}
/**
* Will get the command request from the socket/http/etc. entry point.
* Will parse the request and execute it.
*/
void process(YunClient client) {
String command = client.readStringUntil('/'); //reads command
// Avoid interferences when there is no request. (Because we are in an infinite loop!)
if(command.length() > 0){
if (command == "servox"){
servoX(client);
}
if (command == "servoy"){
servoY(client);
}
}
}
void servoX(YunClient client) { //Creates a function to control the servo in x
int valx = 0;
valx = client.parseInt();
valx = map(valx, 0, 255, 0, 180);
myservox.write(valx);
delay(15);
}
void servoY(YunClient client) { //Creates a function to control the servo in y
int valy = 0;
valy = client.parseInt();
valy = map(valy, 0, 255, 0, 180);
myservoy.write(valy);
delay(15);
}
Thanks in advance