1

I'm using my ESP8266 as a web server to tilt a camera with the servo Tower Pro Micro Servo 9G.

#include <Servo.h>
#include <ESP8266WiFi.h>

const char* ssid     = "SSID";
const char* password = "password";

Servo tilt;

WiFiServer server(8082);

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.println("Server started");
  tilt.attach(2);
  tilt.write(100);
}

void loop() {
  WiFiClient client = server.available();
  if (!client)
  {
    return;
  }
  Serial.println("Waiting for new client");
  while(!client.available())
  {
    delay(1);
  }
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  
  int value = LOW;
  
  if(request.indexOf("/SERVO=UP") != -1){
        int t = tilt.read();
        if(t <= 125) {
          delay(10);
          tilt.write(t+8);
        }
  }
  if(request.indexOf("/SERVO=DOWN") != -1){
        int t1 = tilt.read();
        if(t1 >= 75) {
          delay(10);
          tilt.write(t1-8);
        }
    }   

  client.println("HTTP/1.1 200 OK"); //
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.print(" CONTROL Servo: ");

  if(value == HIGH)
  {
    client.print("ON");
  }
  else
  {
    client.print("OFF");
  }
  client.println("<br><br>");
  client.println("<a href=\"/SERVO=UP\"\"><button>ON</button></a>");
  client.println("<a href=\"/SERVO=DOWN\"\"><button>OFF</button></a><br />");
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

The first minutes it works fine but after a few minutes it starts turning 360° nonstop. Although, you can't turn this servo over 180°?! Does anyone of you know what is going on here? Thanks!

7
  • Edited your link into the question. You can get rid of the message. So, this has been modified for continuous rotation or not?
    – timemage
    Mar 22, 2021 at 17:34
  • 1
    No I haven't modified it. Mar 22, 2021 at 17:50
  • Without it having been modified, I'm unable to make sense of "it starts turning 360° nonstop".
    – timemage
    Mar 22, 2021 at 17:52
  • Thats why I'm confused ^^ Maybe it is some cheap china stuff ^^ But do you know why it starts rotating randomly? Mar 22, 2021 at 17:57
  • You could remove the two calls to tilt.write in loop to eliminate the basic logic of the program from consideration. If it is still behaving oddly, consider removing all of the WiFi related code, even the header, and see if that changes the behaviour. If you servo still acts weird with nothing but a virtually empty sketch with little more than a call to tilt.write (and prerequisites) in setup(), if nothing else it makes for a cleaner question.
    – timemage
    Mar 22, 2021 at 18:03

1 Answer 1

0

If it works and then doesn't, it makes this seem like a mechanical issue.

To check, write a simple program that just moves the servo around. Leave out all the extra web code. Wait to see if starts behaving erratically and you can save yourself the headache of hunting issues in your code that don't exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.