I need help with an ESP8266 and Servo.H



  • Hello All,

    Can anyone offer guidance or show me how to add additional servos to this project? I can't seem to make any forward progress.

    The sketch is working perfectly to control a single servo connected to pin D4 via a webpage.

    I'm using a NodeMCU ESP-12 (ESP8266) and some legos.

    I adopted a 12 week old puppy that requires medication midday.

    My commute is 38 miles each way so giving him his meds remotely is ideal.

    Unfortunately he also suffers from separation anxiety and I would like to be able to give him a treat and a toy during the day to help him with this. (That's the reason for servo's 3 and 4)

    Any help would be greatly appreciated!

    Johnny

    Insert Code Here
    ```I started with this code: https://github.com/hobbzey/Simplest-SmartThings-ESP8266-Blinds/blob/master/ESP8266-Sketch.ino<img 
    
    
    
    My working sketch:
    
    
    /*
     *  Simple HTTP get webclient test
     */
     
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <Servo.h>
    
    #define ssid      "ChangeME"
    #define password  "Password"
     
    ESP8266WebServer server(83);
    
    Servo myservo;
    int pos = 0;
    int state = 0;
    int prevstate = 0;
    int dirc = 0;
    int servoPin = D4;
    
    
    String webPage = "";
    
    void setup() {
      Serial.begin(115200);
      delay(100);
     
      // We start by connecting to a WiFi network
     
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
     
      WiFi.begin(ssid, password);
     
       // Set a static IP (optional)
      IPAddress ip(192, 168, 1, 71);
      IPAddress gateway(192, 168, 1, 1);
      IPAddress subnet(255, 255, 255, 0);
      WiFi.config(ip, gateway, subnet);
      // End of set a static IP (optional)
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
     
      Serial.println("");
      Serial.println("WiFi connected"); 
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    
      webPage += "<h1>Dog Rx Machine</h1><p>AM Medicine  <a href=\"deliver\"><button>Deliver</button></a>&nbsp;<a href=\"reset\"><button>Reset</button></a></p>";
      webPage += "<h2> </h1><p>PM Medicine  <a href=\"deliver\"><button>Deliver</button></a>&nbsp;<a href=\"reset\"><button>Reset</button></a></p>";
      webPage += "<h3> </h1><p>Treat  <a href=\"deliver\"><button>Deliver</button></a>&nbsp;<a href=\"reset\"><button>Reset</button></a></p>";
      webPage += "<h4> </h1><p>Kong Toy  <a href=\"deliver\"><button>Deliver</button></a>&nbsp;<a href=\"reset\"><button>Reset</button></a></p>";
       server.on("/", []() {
        server.send(200, "text/html", webPage);
       });
     
       server.on("/deliver", []() {
        server.send(200, "text/html", webPage);
        Serial.println("HTTP OPEN COMMAND RECEIVED");
        dirc = 0;  // direction for servo to run
        state = 2; // sets current state
      });
     
      server.on("/reset", []() {
        server.send(200, "text/html", webPage);
        Serial.println("HTTP CLOSE COMMAND RECEIVED");
        dirc = 180; // direction for servo to run
        state = 1;  // sets current state
      });
    
      server.begin();
      Serial.println("Server started");
    }
    
    void servo_move() {
      Serial.println("State Change. Rotating Servo");
      if ( dirc == 180) {
        myservo.attach(servoPin);              // energize servo
        delay(50);
        for (pos = 0; pos <= 180; pos += 1) {   // goes from 0 degrees to 90 degrees in steps of 1 degree CHANGE 90 TO MATCH ANGLE OF TILT DESIRED
          myservo.write(pos);                  // tell servo to go to position in variable 'pos'
          delay(10);                           // waits 30ms between each degree to slow rotation speed
        }
        delay(50);
        myservo.detach();                      // movement finished so detach servo to conserve power
      }
      else if (dirc == 0) {
        myservo.attach(servoPin);              // energize servo
        delay(50);
        for (pos = 90; pos >= 0; pos -= 1) {   // goes from 90 degrees to 0 degrees in steps of 1 degree CHANGE 90 TO MIRROR ANGLE OF TILT DESIRED ABOVE
          myservo.write(pos);                  // tell servo to go to position in variable 'pos'
          delay(10);                           // waits 30ms between each degree to slow rotation speed
        }
        delay(50);
        myservo.detach();                      // movement finished so detach servo to conserve power
      }
    
      Serial.println("Returning to main loop");
      return;
    }
     
     
    void loop() {
        if (state != prevstate) {
        Serial.println("State change!");
        servo_move();
      }
      prevstate = state;
      server.handleClient();
    }

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.