Servo control with Alexa and wifimanager need code help

echo

(Nick) #1

I am having trouble getting my servo to respond to alexa commands. it works fine if i go to the ip address, and serial monitor.

serial monitor also shows the commands from alexa are processing but no servo movement. i think I have something wrong on the digitalwrite command or something

PLEASE HELP

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <WiFiUdp.h>
#include <functional>
#include <Servo.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>

ESP8266WebServer server(80);

Servo myservo;
int pos = 0;
int state = 0;
int prevstate = 0;
int dirc = 0;
int servoPin = D3; //CHANGE TO WHATEVER PIN YOUR USING

// Declare function prototypes
bool connectUDP();
void prepareIds();
void respondToSearch();
void startHttpServer();


// Change these to whatever you'd prefer:
String device_name = "servo controler";  // Name of device
bool debug = false;                       // If you want debug messages
bool squawk = true;                       // For on/off messages

// Some UDP / WeMo specific variables:
WiFiUDP UDP;
IPAddress ipMulti(239, 255, 255, 250);
unsigned int portMulti = 1900; // local port to listen on
ESP8266WebServer HTTP(80);
String serial; // Where we save the string of the UUID
String persistent_uuid; // Where we save some socket info with the UUID

// Buffer to save incoming packets:
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

void setup() {
 pinMode(servoPin, OUTPUT);
  delay(1000);
  Serial.begin(9600);
  delay(500);
  Serial.println("Startup Sequence");
  delay(500);
  Serial.println();
  Serial.print("Connecting to ");
  // Set the UUIDs and socket information:
  prepareIds();

  // Get settings from WiFi Manager:
  WiFiManager wifiManager;
  //wifiManager.resetSettings(); // Uncomment this to test WiFi Manager function
  wifiManager.autoConnect("Autocontrol");

  // Wait til WiFi is connected properly:
  int counter = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    counter++;
  }
  Serial.println("Connected to WiFi");
Serial.println("IP address: "); Serial.println(WiFi.localIP());

  // Connect to UDP:
  bool udpConnected = connectUDP();
  if (udpConnected){
    startHttpServer(); // Start the HTTP Server
  }

}

void loop() {
  HTTP.handleClient();
  delay(1);

  // If there are packets, we parse them:
  int packetSize = UDP.parsePacket();

  if(packetSize) {
    if (debug) {
      Serial.println("");
      Serial.print("Received packet of size ");
      Serial.println(packetSize);
      Serial.print("From ");
      IPAddress remote = UDP.remoteIP();

      for (int i =0; i < 4; i++) {
        Serial.print(remote[i], DEC);
        if (i < 3) {
          Serial.print(".");
        }
      }

      Serial.print(", port ");
      Serial.println(UDP.remotePort());
    }

    int len = UDP.read(packetBuffer, 255);

    if (len > 0) {
      packetBuffer[len] = 0;
    }

    String request = packetBuffer;

    if(request.indexOf('M-SEARCH') > 0) {
      if(request.indexOf("urn:Belkin:device:**") > 0) {
        if (debug) {
          Serial.println("Responding to search request ...");
        }
        respondToSearch();
      }
    }
  }

  delay(10);
}

void prepareIds() {
  uint32_t chipId = ESP.getChipId();
  char uuid[64];
  sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
  (uint16_t) ((chipId >> 16) & 0xff),
  (uint16_t) ((chipId >>  8) & 0xff),
  (uint16_t)   chipId        & 0xff);

  serial = String(uuid);
  persistent_uuid = "Socket-1_0-" + serial;
}

bool connectUDP(){
  boolean state = false;
  Serial.println("Connecting to UDP");

  if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
    Serial.println("Connection successful");
    state = true;
  }
  else{
    Serial.println("Connection failed");
  }

  return state;
}
void servo_move() {
  Serial.println("State Change. Rotating Servo");
  if ( dirc == 180) {
    myservo.attach(servoPin);              // energize servo
    delay(50);
    for (pos = 0; pos <= 90; 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(30);                           // 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(30);                           // 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 startHttpServer() {
  HTTP.on("/index.html", HTTP_GET, [](){
    if (debug) {
      Serial.println("Got Request index.html ...\n");
    }
    HTTP.send(200, "text/plain", "Hello World!");
  });

  HTTP.on("/upnp/control/basicevent1", HTTP_POST, []() {
    if (debug) {
    Serial.println("########## Responding to  /upnp/control/basicevent1 ... ##########");
    }


    //for (int x=0; x <= HTTP.args(); x++) {
    //  Serial.println(HTTP.arg(x));
    //}

    String request = HTTP.arg(0);
    if (debug) {
      Serial.print("request:");
      Serial.println(request);
    }


    if(request.indexOf("<BinaryState>1</BinaryState>") > 0) {
      if (squawk) {
          Serial.println("Got open request");
      }

      digitalWrite(servoPin, HIGH); 
    }

    if(request.indexOf("<BinaryState>0</BinaryState>") > 0) {
      if (squawk) {
          Serial.println("Got close request");
      }

      digitalWrite(servoPin, LOW);  
    }

    HTTP.send(200, "text/plain", "");
  });

  HTTP.on("/eventservice.xml", HTTP_GET, [](){
    if (debug) {
        Serial.println(" ########## Responding to eventservice.xml ... ########\n");
    }

    String eventservice_xml = "<?scpd xmlns=\"urn:Belkin:service-1-0\"?>"
    "<actionList>"
    "<action>"
    "<name>SetBinaryState</name>"
    "<argumentList>"
    "<argument>"
    "<retval/>"
    "<name>BinaryState</name>"
    "<relatedStateVariable>BinaryState</relatedStateVariable>"
    "<direction>in</direction>"
    "</argument>"
    "</argumentList>"
    "<serviceStateTable>"
    "<stateVariable sendEvents=\"yes\">"
    "<name>BinaryState</name>"
    "<dataType>Boolean</dataType>"
    "<defaultValue>0</defaultValue>"
    "</stateVariable>"
    "<stateVariable sendEvents=\"yes\">"
    "<name>level</name>"
    "<dataType>string</dataType>"
    "<defaultValue>0</defaultValue>"
    "</stateVariable>"
    "</serviceStateTable>"
    "</action>"
    "</scpd>\r\n"
    "\r\n";

    HTTP.send(200, "text/plain", eventservice_xml.c_str());
  });

  HTTP.on("/setup.xml", HTTP_GET, [](){
    if (debug) {
        Serial.println(" ########## Responding to setup.xml ... ########\n");
    }


    IPAddress localIP = WiFi.localIP();
    char s[16];
    sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);

    String setup_xml = "<?xml version=\"1.0\"?>"
    "<root>"
    "<device>"
    "<deviceType>urn:Belkin:device:controllee:1</deviceType>"
    "<friendlyName>"+ device_name +"</friendlyName>"
    "<manufacturer>Belkin International Inc.</manufacturer>"
    "<modelName>Emulated Socket</modelName>"
    "<modelNumber>3.1415</modelNumber>"
    "<UDN>uuid:"+ persistent_uuid +"</UDN>"
    "<serialNumber>221517K0101769</serialNumber>"
    "<binaryState>0</binaryState>"
    "<serviceList>"
    "<service>"
    "<serviceType>urn:Belkin:service:basicevent:1</serviceType>"
    "<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
    "<controlURL>/upnp/control/basicevent1</controlURL>"
    "<eventSubURL>/upnp/event/basicevent1</eventSubURL>"
    "<SCPDURL>/eventservice.xml</SCPDURL>"
    "</service>"
    "</serviceList>"
    "</device>"
    "</root>\r\n"
    "\r\n";

    HTTP.send(200, "text/xml", setup_xml.c_str());
    if (debug) {
      Serial.print("Sending :");
      Serial.println(setup_xml);
    }
  });

  HTTP.begin();
  if (debug) {
    Serial.println("HTTP Server started ..");
  }
}

void respondToSearch() {
  if (debug) {
    Serial.println("");
    Serial.print("Sending response to ");
    Serial.println(UDP.remoteIP());
    Serial.print("Port : ");
    Serial.println(UDP.remotePort());
  }

  IPAddress localIP = WiFi.localIP();
  char s[16];
  sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);

  String response =
  "HTTP/1.1 200 OK\r\n"
  "CACHE-CONTROL: max-age=86400\r\n"
  "DATE: Tue, 14 Dec 2016 02:30:00 GMT\r\n"
  "EXT:\r\n"
  "LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
  "OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
  "01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
  "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
  "ST: urn:Belkin:device:**\r\n"
  "USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
  "X-User-Agent: redsonic\r\n\r\n";

  UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
  UDP.write(response.c_str());
  UDP.endPacket();
  if (debug) {
    Serial.println("Response sent !");
  }
}

void configModeCallback(WiFiManager *myWiFiManager) {
  Serial.println("Entered config mode");
  Serial.println("Soft AP's IP Address:");
  Serial.println(WiFi.softAPIP());
  Serial.println("WiFi Manager: Please connect to AP:");
  Serial.println(myWiFiManager->getConfigPortalSSID());
  Serial.println("To setup WiFi Configuration");
}