D1 Alexa



  • Hello All,

    Goal: Using a DRV8871 from adafruit to turn on 2 pins to control a DC motor to open or close a curtain. there are limit switches that are default INPUT_PULLUP and LOW when it reaches their desired location. I tried to open a curtain and close a curtain using one boolean variable with amazon echo the Wemos D1 and MJRobots.org sketch to say alexa turn Light On, the DRV8871 run until openSwitch is disconnected by going from a HIGH to a LOW state. I modified MJRobot.org's code to get a basic sketch. When I say Alexa light on, I added a boolean state to track the change State to allow the motor pins to run until the reed switches disconnect the pins (They are input_pullup as LOW until reached desired distance then the magnet disconnected circuit and digitalRead = HIGH). below is the main sketch minus the other standard libraries. There maybe a simplier way, I'm open to suggestions.

    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <WiFiUdp.h>
    #include <functional>

    #include "switch.h"
    #include "UpnpBroadcastResponder.h"
    #include "CallbackFunction.h"

    // prototypes
    boolean connectWifi();

    // Single devices on/off callbacks
    void curtainOpen();
    void curtainClose();

    // Change this before you flash
    const char* ssid = ".......";
    const char* password = ".......";

    boolean wifiConnected = false;

    UpnpBroadcastResponder upnpBroadcastResponder;

    Switch *curtain1 = NULL;

    // Set Relay Pins
    int motorOpen = D1; // wemos pin D1
    int motorClose = D2; // wemos pin D2

    int openSwitch = D8; //Tilt Switch for open
    int closeSwitch = D7; //Tilt Switch for Close

    int state = 0;

    void setup()
    {
    Serial.begin(115200);
    wifiConnected = connectWifi();
    Serial.print("WiFi Connected");

    if (wifiConnected) {
    upnpBroadcastResponder.beginUdpMulticast();

    // Devices Definition (Max 14)
    // Format: Alexa invocation name, local port no, on callback, off callback
    
    // Single devices definition
    curtain1 = new Switch("Curtain", 80, curtainOpen, curtainClose);
    
    
    Serial.println("Adding switches upnp broadcast responder");
    upnpBroadcastResponder.addDevice(*curtain1);
    
    
    //Set relay pins to outputs
    pinMode(motorOpen, OUTPUT);
    pinMode(motorClose, OUTPUT);
    
    
    //Set each relay pin to HIGH
    digitalWrite(motorOpen, LOW);   // sets motorOpen OFF
    delay(500);
    digitalWrite(motorClose, LOW);   // sets motorClose OFF
    delay(500);
    
    
    pinMode(openSwitch, INPUT_PULLUP);
    pinMode(closeSwitch, INPUT_PULLUP);
    

    }
    }

    void loop()
    {
    changeCurtain();
    state = 0;
    if (wifiConnected)
    {
    upnpBroadcastResponder.serverLoop();
    curtain1->serverLoop();
    }

    }

    /* Functions for Curtain */
    void curtainOpen() {
    state = 1;
    Serial.print("open curtain State: ");
    Serial.println(state);
    }

    void curtainClose() {
    state = 2;
    Serial.print("close curtain State: ");
    Serial.println(state);
    }

    void changeCurtain() {
    if (state = 1) { //curtain Open
    Serial.print("curtain State: ");
    Serial.println(state);
    Serial.println("Curtain Open on ...");
    while (digitalRead(openSwitch) == LOW) {
    Serial.println("motor Opening....");
    digitalWrite(motorOpen, HIGH);
    }
    Serial.println("Done Opening");
    digitalWrite(motorOpen, LOW); // Stops motor
    digitalWrite(motorClose, LOW); // Stops motor
    }

    if (state = 2) { //curtain Close
    Serial.print("curtain State: ");
    Serial.println(state);
    Serial.println("Curtain Close on ...");
    while (digitalRead(closeSwitch) == LOW) {
    Serial.println("motor Closing....");
    digitalWrite(motorClose, HIGH);
    }
    Serial.println("Done Closing");
    digitalWrite(motorOpen, LOW); // Stops motor
    digitalWrite(motorClose, LOW); // Stops motor
    }

    state = 0;
    Serial.println("changeCurtain Done curtain State: ");
    Serial.print(state);
    }

    // connect to wifi – returns true if successful or false if not
    boolean connectWifi() {
    boolean state = true;
    int i = 0;

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");
    Serial.println("Connecting to WiFi");

    // Wait for connection
    Serial.print("Connecting ...");
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 10) {
    state = false;
    break;
    }
    i++;
    }

    if (state)
    {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("Local IP address: ");
    Serial.println(WiFi.localIP());
    }
    else
    {
    Serial.println("");
    Serial.println("Connection failed.");
    }

    return state;
    }


Log in to reply
 

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