Calificación:
  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Aporte:  Arduino, controlar servo vía Wifi con módulo HLK-RM04
#1
Saludos....

En esta ocasión vamos a establecer el giro de un servo vía wifi desde una página web, por lo que podremos controlarlo desde un PC, tableta, smartphone, etc.


Materiales utilizados
  • Una placa Arduino (yo he utilizado la Uno).
  • 7 Cables dupont.
  • Un servo, yo he utilizado el de la imagen de mas abajo.
  • Un módulo wifi HLK-RM04.



[Imagen: servo.jpg]


[Imagen: WIFI-HLK-RM04.jpg]



Conexiones
  • Pin Rx del módulo HLK-RM04 al pin Tx de arduino.
  • Pin Tx del módulo HLK-RM04 al pin Rx de arduino.
  • Pin 5V del módulo HLK-RM04 al pin 5V de arduino.
  • Pin GND del módulo HLK-RM04 al pin GND de arduino.
  • Cable naranja del servo (datos) al pin 9 de arduino
  • Cable marrón del servo a pin GND de arduino.
  • Cable rojo del servo a pin 5v de arduino


Lo que vamos a necesitar son dos clases de código. Por una parte una página web que estará en nuestro servidor web en la que tendremos el formulario con los grados (de 0 a 180) que queremos que gire el servo y el código para la placa arduino que recibirá la los grados de giro del servo.


Código página web, index.html:

Código:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//ES" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
   <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
   <title>Control servo</title>
<style type="text/css">
#juan {
   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);
   -webkit-transform: translate(-50%, -50%);
}
</style>
 </head>
 <body>
<div id="juan">
<form action="http://192.168.1.254:8080/servo" method="get">
 
 <input type="submit" name="l" value="000">
 <input type="submit" name="l" value="005">
 <input type="submit" name="l" value="010">
 <input type="submit" name="l" value="015">
 <input type="submit" name="l" value="020">
 <input type="submit" name="l" value="025"></br>
 <input type="submit" name="l" value="030">
 <input type="submit" name="l" value="035">
 <input type="submit" name="l" value="040">
 <input type="submit" name="l" value="045">
 <input type="submit" name="l" value="050">
 <input type="submit" name="l" value="055"></br>
 <input type="submit" name="l" value="060">
 <input type="submit" name="l" value="065">
 <input type="submit" name="l" value="070">
 <input type="submit" name="l" value="075">
 <input type="submit" name="l" value="080">
 <input type="submit" name="l" value="085"></br>
 <input type="submit" name="l" value="090">
 <input type="submit" name="l" value="095">
 <input type="submit" name="l" value="100">
 <input type="submit" name="l" value="105">
 <input type="submit" name="l" value="110">
 <input type="submit" name="l" value="115"></br>
 <input type="submit" name="l" value="120">
 <input type="submit" name="l" value="125">
 <input type="submit" name="l" value="130">
 <input type="submit" name="l" value="135">
 <input type="submit" name="l" value="140">
 <input type="submit" name="l" value="145"></br>
 <input type="submit" name="l" value="150">
 <input type="submit" name="l" value="155">
 <input type="submit" name="l" value="160">
 <input type="submit" name="l" value="165">
 <input type="submit" name="l" value="170">
 <input type="submit" name="l" value="175"></br>
 <input type="submit" name="l" value="180">
</form>
</div>
</body>
</html>


Este código lo que hace básicamente es crear un formulario con los grados que queremos que gire el servo y lo envía, por el método get, a la dirección ip de nuestro arduino.

[Imagen: grados.png]


Código para la placa arduino:

Código:
#include <Servo.h>
const int servo_pin = 9;
Servo myservo;  
unsigned int old_value;
void setup()
{
 Serial.begin(57600);
 myservo.attach(servo_pin);
   old_value = 90;
}

int f = 0;
void loop()
{
boolean has_request = false;
 String in = "";
   if (Serial.available()) {
in = "";
   while (true) {  
      while (Serial.available() == false) {}
     in += (char)(Serial.read());
     if (in.endsWith("\r\n")) {
       has_request = true;  break;
     }
   }  
 }
 if (has_request) {
   char i1 = in.indexOf("GET /servo?l="), i2;
   if (i1 != -1) {
     i2 = in.indexOf(" ", i1+13);
     f = in.substring(i1+13, i2).toInt();
   }  
Serial.println("HTTP/1.1 200 OK");
   Serial.println("Content-Type: text/html");
   Serial.println("Connection: close");

   String sr = "<!DOCTYPE HTML>\n";
   sr += "<html>\n";
   sr += "<body onload='history.back();'>\n";    
   sr += "</body>";  
   sr += "</html>";
   Serial.print("Content-Length: ");
   Serial.print(sr.length());
   Serial.print("\r\n\r\n");
   Serial.print(sr);
  has_request = false;  
   
   if ((0 <= f) && (f <= 180)) {
         
      if (f < old_value)
       {  
      for(int i = old_value ; i > f ; i -= 1)
         {
         myservo.write(i);
         delay(15);
         }
         old_value = f;
       }
       
     if (f > old_value)
       {
       for(int i = old_value ; i < f ; i += 1)
         {
         myservo.write(i);
         delay(15);
         }
         old_value = f;
      }

   }  

 }
 delay(100);

}


El código lo que que hace es cargar la librería necesaria, recoger en una variable el valor que le enviamos desde la página web, que será el valor de giro del servo. También crea una página web que lo que hace es volver al index.html desde el que le enviamos los datos.
Mencionar que el script está preparado para que el giro del servo sea un poco mas lento de lo normal, que no gire muy rápido.


.
Responder


Salto de foro:


Usuarios navegando en este tema: 1 invitado(s)