kakopappa / arduino-esp8266-alexa-multiple-wemo-switch

multiple belkin wemos switch emulator using ESP8266
https://sinric.pro
MIT License
308 stars 196 forks source link

Add a push button to manually turn on/off the lights #74

Closed ChompsR closed 4 years ago

ChompsR commented 4 years ago

Good evening

I've been working on this project for the last week, so it came this idea to add a push button to manually turn on and off the lights. After studying where this instructions had to be placed, I finally found that is on Switch.cpp (continue reading).

There's a void called Switch::handleUpnpControl() which I presume this is the one that controls when you ask Alexa to turn on/off the device, this is where I add some code lines to include a push button.

RESULTS:

Any ideas of how we could add this button to had a fully functional smart device?

I also added some new lines to the original Wemos page to make the device smarter, I will upload the pull request once the push button is coded.

Things I added:

For this changes stay tuned.

This is what I modified on Switch.cpp:

void Switch::handleEventservice(){
  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>"
          "</action>"
          "<action>"
            "<name>GetBinaryState</name>"
            "<argumentList>"
              "<argument>"
                "<retval/>"
                "<name>BinaryState</name>"
                "<relatedStateVariable>BinaryState</relatedStateVariable>"
                "<direction>out</direction>"
                "</argument>"
            "</argumentList>"
          "</action>"
      "</actionList>"
        "<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>"
        "</scpd>\r\n"
        "\r\n";
          
    server->send(200, "text/plain", eventservice_xml.c_str());
}

void Switch::handleUpnpControl(){
  Serial.println("########## Responding to  /upnp/control/basicevent1 ... ##########");      
  
  //for (int x=0; x <= HTTP.args(); x++) {
  //  Serial.println(HTTP.arg(x));
  //}

pinMode(Boton,INPUT_PULLUP); // PUSH BUTTON
Estado = digitalRead(Boton); // STATE

  String request = server->arg(0);      
  Serial.print("request:");
  Serial.println(request);
  

  if(request.indexOf("SetBinaryState") >= 0) {
    if(request.indexOf("<BinaryState>1</BinaryState>") >= 0) {
        Serial.println("Got Turn on request");
        switchStatus = onCallback();

        sendRelayState();
// Push Button for turning off the lights
        Estado = digitalRead(Boton);
      
      if(Estado != HIGH){
        request.indexOf("<BinaryState>0</BinaryState>") >= 0;
        Serial.println("Got Turn off request");
        switchStatus = offCallback();
        
        sendRelayState();
      }
// MOD END
    }

    if(request.indexOf("<BinaryState>0</BinaryState>") >= 0) {
        Serial.println("Got Turn off request");
        switchStatus = offCallback();
        
        sendRelayState();

// Push Button for turning on the lights
        Estado = digitalRead(Boton);

      if(Estado != HIGH){
        request.indexOf("<BinaryState>1</BinaryState>") >= 0;
        Serial.println("Got Turn on request");
        switchStatus = onCallback();

        sendRelayState();
      }
// MOD END 
    }

  }

  if(request.indexOf("GetBinaryState") >= 0) {
    Serial.println("Got binary state request");
    sendRelayState();
  }
  
  server->send(200, "text/plain", "");
  
}

Thank you very much and keep safe.

kakopappa commented 4 years ago

Thanks for sharing. FYI Emulator technique time to time cause problems. I have switched to amazon skill based solution now using https://sinric.pro

On Thu, 23 Apr 2020 at 6:56 AM ChompsR notifications@github.com wrote:

Good evening

I've been working on this project for the last week, so it came this idea to add a push button to manually turn on and off the lights. After studying where this instructions had to be placed, I finally found that is on Switch.cpp (continue reading).

There's a void called Switch::handleUpnpControl() which I presume this is the one that controls when you ask Alexa to turn on/off the device, this is where I add some code lines to include a push button.

RESULTS:

  • I can get Alexa to turn on/off the device.
  • The code detects the push button BUT it only makes changes when the button is pressed and I had to ask Alexa to turn on/off the lights. I thought this can be because there's some statement that I can not found yet that takes the instruction direct to this void.

Any ideas of how we could add this button to had a fully functional smart device?

I also added some new lines to the original Wemos page to make the device smarter, I will upload the pull request once the push button is coded.

Things I added:

  • Automatic reset when connection lost or not found.
  • Detection for and OLED display to show a little logo, SSID and IP address.

For this changes stay tuned.

This is what I modified on Switch.cpp:

void Switch::handleEventservice(){ Serial.println(" ########## Responding to eventservice.xml ... ########\n");

String eventservice_xml = "<scpd xmlns=\"urn:Belkin:service-1-0\">" "" "" "SetBinaryState" "" "" "" "BinaryState" "BinaryState" "in" "" "" "" "" "GetBinaryState" "" "" "" "BinaryState" "BinaryState" "out" "" "" "" "" "" "<stateVariable sendEvents=\"yes\">" "BinaryState" "Boolean" "0" "" "<stateVariable sendEvents=\"yes\">" "level" "string" "0" "" "" "\r\n" "\r\n";

server->send(200, "text/plain", eventservice_xml.c_str());

}

void Switch::handleUpnpControl(){ Serial.println("########## Responding to /upnp/control/basicevent1 ... ##########");

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

pinMode(Boton,INPUT_PULLUP); // PUSH BUTTON Estado = digitalRead(Boton); // STATE

String request = server->arg(0); Serial.print("request:"); Serial.println(request);

if(request.indexOf("SetBinaryState") >= 0) { if(request.indexOf("1") >= 0) { Serial.println("Got Turn on request"); switchStatus = onCallback();

    sendRelayState();

// Push Button for turning off the lights Estado = digitalRead(Boton);

  if(Estado != HIGH){
    request.indexOf("<BinaryState>0</BinaryState>") >= 0;
    *Serial*.println("Got Turn off request");
    switchStatus = offCallback();

    sendRelayState();
  }

// MOD END }

if(request.indexOf("<BinaryState>0</BinaryState>") >= 0) {
    *Serial*.println("Got Turn off request");
    switchStatus = offCallback();

    sendRelayState();

// Push Button for turning on the lights Estado = digitalRead(Boton);

  if(Estado != HIGH){
    request.indexOf("<BinaryState>1</BinaryState>") >= 0;
    *Serial*.println("Got Turn on request");
    switchStatus = onCallback();

    sendRelayState();
  }

// MOD END }

}

if(request.indexOf("GetBinaryState") >= 0) { Serial.println("Got binary state request"); sendRelayState(); }

server->send(200, "text/plain", "");

}

Thank you very much and keep safe.

  • ChompsR

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/arduino-esp8266-alexa-multiple-wemo-switch/issues/74, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZWQJGUA5WSTXHXE3MDRN574VANCNFSM4MOTF2LQ .

ChompsR commented 4 years ago

Kakopappa, thank you very much! Sinric Pro skill is amazing, and the codes/libraries/examples, just wow, I'm really impressed, if you don't mind I will make some changes to the Switch example just to add the lines that I've already coded on wemo's example.

I'll be in touch!