kakopappa / arduino-esp8266-alexa-wemo-switch

Amazon Alexa + WeMos switch made with Arduino D1 Mini
https://sinric.pro
MIT License
281 stars 249 forks source link

arduino esp8266 script changed in Alexa #44

Open TexAcoon opened 4 years ago

TexAcoon commented 4 years ago

kakopappa - I have used this script for over a year and worked so well I was planning on adding more devices to my home automation. Not sure what has gone wrong when asking Alexa to turn on the appliance it "states there is a malfunction" OR "cant find the appliance" (depending on what day it is and how good she feels.

I have not made any chances to the software and still can not get Alexa do discover any of my WeMos ESP8266s ... tried looking in "Serial Monitor" and do not see anything working. Has something changed in the Amazon Alexa for the system not to recognize the WeMos ESP8266 any longer?

sivar2311 commented 4 years ago

You need to activate SinricPro skill and link it to your SinricPro account

TexAcoon commented 4 years ago

I did that when I did the fireplace setup On Sunday, November 10, 2019, 12:31:55 PM CST, Boris Jäger notifications@github.com wrote:

You need to activate SinricPro skill and link it to your SinricPro account

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

Please try to add another device, like a switch or doorbell. Right after adding you should see a pop up on your smartphone from alexa app, indicating a new device has been found.

clox-coder commented 4 years ago

@TexAcoon was curious would you mind sharing your whole code? are you using a simple relay setup?

TexAcoon commented 4 years ago

Please try to add another device, like a switch or doorbell. Right after adding you should see a pop up on your smartphone from alexa app, indicating a new device has been found.

Code is Great! and working as expected! All I need to do is hook up the mechanics on my garage door and I'll be set...

Great Job sivar2311! I'm really impressed with this ... This could be another one for kakopappa libraries on Sinric Pro....

TexAcoon commented 4 years ago

clox-coder You should see the code above it in our comments section by sivar2311 - He really did a great job tying the Arduino code in with Sinric Pro.

clox-coder commented 4 years ago

@TexAcoon I was just asking to make sure there was not a minor mistake. But also just in case this might help. I would take one 8266 at a time plug into pc with power. pull up serial monitor and identify ip address assigned to device. unless you know how to identify through router. then ping the 8266 device just to verify it's on the network. if so you know your getting this far. also reset your alexa etc. it could be this simple of a problem as well.

TexAcoon commented 4 years ago

Good Advice! .. Thanks! .. I use checking IP addresses Angry Scanner when I have to check IP addresses ... . You do bring up a good point in order to pinpoint the or localize the ESP8266. On Sunday, November 10, 2019, 01:06:52 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon I was just asking to make sure there was not a minor mistake. But also just in case this might help. I would take one 8266 at a time plug into pc with power. pull up serial monitor and identify ip address assigned to device. unless you know how to identify through router. then ping the 8266 device just to verify it's on the network. if so you know your getting this far. also reset your alexa etc. it could be this simple of a problem as well.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

clox-coder commented 4 years ago

@TexAcoon sorry your post was delayed! lol

sivar2311 commented 4 years ago

clox-coder You should see the code above it in our comments section by sivar2311 - He really did a great job tying the Arduino code in with Sinric Pro.

I am the guy who is writing the library. It is very flexible... you can use any device which SinricPro offers. For detailed infos about the library, how to use it and which devices are available please see readme in git repo. If you encounter any issues, please use the correct git repo and check for existing issues or open a new one.

TexAcoon commented 4 years ago

I sure wish I had the expertise you have ... I am really pleased with the code you wrote ... It is working great ... Leaving it running as a demo for the next couple of days. Still have the mechanics to do on my end.

sivar2311 commented 4 years ago

I would be pleased about some pictures or a video of the final result.

TexAcoon commented 4 years ago

Will pass those on to you ... I know others would enjoy doing the same type setup to their garage doors..

TexAcoon commented 4 years ago

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

On Tuesday, November 12, 2019, 12:22:56 PM CST, Doug Crawford <rocknbarc@yahoo.com> wrote:  

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

TexAcoon commented 4 years ago

Boris,I have some starter pics for you ...  What I would like to do is to do a write up and send to you.  Hopefully this could be useful for others.Doug On Tuesday, November 12, 2019, 05:17:42 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

On Tuesday, November 12, 2019, 12:22:56 PM CST, Doug Crawford <rocknbarc@yahoo.com> wrote:  

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

TexAcoon commented 4 years ago

Boris, Take a look at this code I found on instructables: https://www.instructables.com/id/Garage-Door-Opener-With-Feedback-Using-Esp8266/ It has the use of a magnetic relay installed for identifying your garage door position which I would have to believe most end users would like to have in the code.   If you have time, please take a look and see if there is a way to modify it for Sinric Pro.  I tired adding the api but would not work like it should.  By itself it worked fine but have to log into another site iot site like Sinric Pro.


// Close switch must be connected to D2// D1 is the output for the garage door motor. Hi-low-Hi //On line 18 you must enter your ssid//On line 19 you must enter your router password//For now the password is Passw0rd. This password must be in you internet shortcut. If you want to change this, hit ctrl-f enter Passw0rd, your new password and hit replace all.

include

const int statusPin = 4; //pin for the magnetic contact switch/ Open/Close D2const int activatePin = 5; //pin for the relay module D1

WiFiServer server(54195); //This is the port to listen. Need to be in your router to open the door remotely. If you need only local. Put 80 instead and forget the router partWiFiClient client; const char SSID = "enter your ssid here";const char PASS = "router passord here";

void setup() {  Serial.begin(115200);  pinMode(statusPin, INPUT_PULLUP);  pinMode(activatePin, OUTPUT);  digitalWrite(activatePin, HIGH);  delay(50);   Serial.println();  Serial.println();  Serial.print("Connecting to ");  Serial.println(SSID);   WiFi.begin(SSID, PASS);   while (WiFi.status() != WL_CONNECTED) {    delay(500);    Serial.print(".");  }  Serial.println();  Serial.println("WiFi connected");    //Start the server  server.begin();  Serial.println("Server started");   //Print the IP Address  Serial.print("IP Address: ");  Serial.println(WiFi.localIP());  delay(500);} void loop() {  client = server.available();  if (client)  // if you get a client  {    char getLine[128];    int i = 0;    bool getLineFound = false;    bool currentLineIsBlank = true;     Serial.println("");    Serial.println("new client");     while (client.connected()) // loop while the client's connected    {      if (client.available()) // if there's bytes to read from the client      {        char c = client.read(); // read 1 byte from client        Serial.print(c);         if (!getLineFound && i < sizeof(getLine))        {          //save the char to getLine          getLine[i] = c;          i++;        }                 // Request end: Now responds to it        if (c == '\n' && currentLineIsBlank) // respond to client only after last line is received, last line is blank and ends with \n        {          ProcessRequest(getLine);          break;        }         if (c == '\n') // end of the line, next char read will be a new line        {          if (!getLineFound) //the GET line is the first line from the client, save it for later          {            getLineFound = true;             //strip off the HTTP/1.1 from the end of the getLine            const char *ptr = strstr(getLine, "HTTP/1.1");            if (ptr)              getLine[ptr - getLine - 1] = '\0';          }           currentLineIsBlank = true;        }        else if (c != '\r') //text char received        {          currentLineIsBlank = false;        }      } //end if (client.available())    } //end while (client.connected())

    // close the connection    delay(100); //allow client to receive the data    client.stop();    Serial.println("Client disconnected");  } }

void mainPage(WiFiClient& client){  client.println("HTTP/1.1 200 OK");  client.println("Content-Type: text/html");  client.println("Connection: keep-alive\r\n");  client.println("<!DOCTYPE HTML>");  client.println("");  client.println("");  client.println("Garage Door Monitor");  client.println("");  client.println("");  client.println("<body onload=\"GetSwitchState()\">");  client.println("

");  client.println("

Garage Door Monitor

");  client.println("
\r\n
");  client.println("<button style=\"width:100%;font-size: 50px;\"><a href=\"/Passw0rd/activate\">Activate");  client.println("<p id=\"switch_txt\">Garage Door is now:

");  client.println("
\r\n\r\n"); }

void redirect(WiFiClient& client){  client.println("HTTP/1.1 303 See other");  client.println("Location: /Passw0rd");} void ActivateDoor(){  digitalWrite(activatePin, LOW);  delay(500);  digitalWrite(activatePin, HIGH);} void doorState(WiFiClient& client){  client.println("HTTP/1.1 200 OK");  client.println("Content-Type: text/html");  client.println("Connection: keep-alive\r\n");     if (digitalRead(statusPin) == 1) {    client.println("Garage Door is now: <font color=\"red\">Open");  }  else {    client.println("Garage Door is now: <font color=\"green\">Close");  }}

void ProcessRequest(char* getLine){    if (strstr(getLine, "GET /Passw0rd") != NULL)  {    if (strstr(getLine, "GET /Passw0rd/activate") != NULL)    {      Serial.println("Activating garage door");      ActivateDoor();      redirect(client);    }    else    {      mainPage(client);    }  }  else if(strstr(getLine, "GET /ajaxswitch") != NULL)    {      doorState(client);   }}

On Tuesday, November 12, 2019, 05:19:56 PM CST, Doug Crawford <rocknbarc@yahoo.com> wrote:  

Boris,I have some starter pics for you ...  What I would like to do is to do a write up and send to you.  Hopefully this could be useful for others.Doug On Tuesday, November 12, 2019, 05:17:42 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

On Tuesday, November 12, 2019, 12:22:56 PM CST, Doug Crawford <rocknbarc@yahoo.com> wrote:  

Hey Boris, Here is a pic of the setup I have at the moment - when I'm finished it I'll have a video and a section with parts. However, I ended having to setup the LEDs to #define LED_GREEN   D1#define LED_RED     D1 But I believe I need the D1 needs to be momentary switch pushing a button and releasing it. Just like the remote on the garage door that normally ties into switch box. Not sure at this time since I have not hooked it up yet but getting there.   Will hook it up as soon as possible - waiting for my 3D printer to finish printing the cover plate for the box. Future idea would be hooking up a magnetic switch to the code.  (not sure how to do this since I'm not a code writer) This would then give you the indication (door position) and acknowledgement that the garage door is unlocked or locked.

clox-coder commented 4 years ago

@TexAcoon Are you using any relays? When i used Fauxmo in past i would use digitalWrite(D1,HIGH); etc to trigger my relay 5 Volt. Cant seem to get it to engage

TexAcoon commented 4 years ago

Yes .. I am using a single 10 amp relay (3 volt coil) it's switching fine with the code from Boris - I hope he will be able to take the code I sent him and be able to make a Sinric Pro code out of it.   What I like is it has a magnetic relay (switch) to let you know the current condition of the garage door if it's open or not. You are still having issues? What code are you using? On Tuesday, November 12, 2019, 06:07:16 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon Are you using any relays? When i used Fauxmo in past i would use digitalWrite(D1,HIGH); etc to trigger my relay 5 Volt. Cant seem to get it to engage

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

clox-coder commented 4 years ago

@TexAcoon I have run from 5 volt side of 8266 to 5 volt relay. Shows it has power but its not switching. I tried a few variations of the code. Last one i tried to run was this below.

include

include

include

include

include

include

include

// Uncomment the following line to enable serial debug output //#define ENABLE_DEBUG

ifdef ENABLE_DEBUG

   #define DEBUG_ESP_PORT Serial
   #define NODEBUG_WEBSOCKETS
   #define NDEBUG

endif

include

ifdef ESP8266

   #include <ESP8266WiFi.h>

endif

ifdef ESP32

   #include <WiFi.h>

endif

include "SinricPro.h"

include "SinricProSwitch.h"

define WIFI_SSID ""

define WIFI_PASS ""

define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"

define APP_SECRET "3ea29c9d-1622-429c-9e51-4732eaba5b71-7f1a9ba9-3cf0-40a7-a48f-4f6c7bbe048c" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

define SWITCH_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"

define BAUD_RATE 115200 // Change baudrate to your need

define BTN_FLASH 0

bool myPowerState = false; unsigned long lastBtnPress = 0;

/ bool onPowerState(String deviceId, bool &state)

void handleButtonPress() { unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced? if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa myPowerState = false; } else { myPowerState = true; } digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)

// get Switch device back
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
// send powerstate event
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
Serial.printf("Device %s turned %s (manually via flashbutton)\r\n", mySwitch.getDeviceId(), myPowerState?"on":"off");

lastBtnPress = actualMillis;  // update last button press variable

} }

// setup function for WiFi connection void setupWiFi() { Serial.printf("\r\n[Wifi]: Connecting"); WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } IPAddress localIP = WiFi.localIP(); Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); }

// setup function for SinricPro void setupSinricPro() { // add device to SinricPro SinricProSwitch& mySwitch = SinricPro.add(SWITCH_ID);

// set callback function to device mySwitch.onPowerState(onPowerState);

// setup SinricPro SinricPro.begin(APP_KEY, APP_SECRET); }

// main setup function void setup() { pinMode(BTN_FLASH, INPUT_PULLUP); // GPIO 0 as input, pulled high pinMode(LED_BUILTIN, OUTPUT); // define LED GPIO as output digitalWrite(LED_BUILTIN, HIGH); // turn off LED on bootup

Serial.begin(BAUD_RATE); setupWiFi(); setupSinricPro(); }

void loop() { handleButtonPress(); SinricPro.handle(); }

TexAcoon commented 4 years ago

Difference I see is your DEVICE TYPE is using a SWITCH while mine is using SMART LOCK. We found the unit seemed to work better using SMART LOCK over the SWITCH.   Also you would need to go into your Alexa Cell Phone APP and adjust the Smart Lock device to allow your personal UNLOCK code for the door. On Tuesday, November 12, 2019, 06:34:37 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

Yes .. I am using a single 10 amp relay (3 volt coil) it's switching fine with the code from Boris - I hope he will be able to take the code I sent him and be able to make a Sinric Pro code out of it.   What I like is it has a magnetic relay (switch) to let you know the current condition of the garage door if it's open or not. You are still having issues? What code are you using? On Tuesday, November 12, 2019, 06:07:16 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon Are you using any relays? When i used Fauxmo in past i would use digitalWrite(D1,HIGH); etc to trigger my relay 5 Volt. Cant seem to get it to engage

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

clox-coder commented 4 years ago

@TexAcoon hmm. yea I know its gotta be something simple I am missing. what pin are you activating on relay. are you setting up defining LEDs both as pin 1? I am trying D2 etc to relay and nothing.

TexAcoon commented 4 years ago

3V to VCCGrn to GRND1 to IN I changed the pins D4 and D5 to both D1's..  On Tuesday, November 12, 2019, 07:13:51 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon hmm. yea I know its gotta be something simple I am missing. what pin are you activating on relay. are you setting up defining LEDs both as pin 1? I am trying D2 etc to relay and nothing.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

clox-coder commented 4 years ago

@TexAcoon I am using the usb for power on 8266. i have 5v pin on 8266 connected to dc+ on relay . grnd from 8266 to dc - on relay and pin D2 connected to IN on relay . 5v power showing lit on relay.

TexAcoon commented 4 years ago

Try D1 to IN You say you have 5V pin on DC+ ... ESP8266 are normally 3 volts unless you jump at the inverter. On Tuesday, November 12, 2019, 07:26:25 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon I am using the usb for power on 8266. i have 5v pin on 8266 connected to dc+ on relay . grnd from 8266 to dc - on relay and pin D2 connected to IN on relay . 5v power showing lit on relay.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

TexAcoon commented 4 years ago

If you have a power supply ...  test your relay ..  the IN wire will touch positive when triggered. (never know about these relays from China) My current relay is a 5 volt but measured voltage at the 3 volt pin was 3.2 volts .. just enough to trigger the 5 volt coil on the relay.However, I did order some 3 volt relays for the future ..  this way I'll have both 3 and 5 volts for my projects. On Tuesday, November 12, 2019, 08:06:48 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

Try D1 to IN You say you have 5V pin on DC+ ... ESP8266 are normally 3 volts unless you jump at the inverter. On Tuesday, November 12, 2019, 07:26:25 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon I am using the usb for power on 8266. i have 5v pin on 8266 connected to dc+ on relay . grnd from 8266 to dc - on relay and pin D2 connected to IN on relay . 5v power showing lit on relay.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

TexAcoon commented 4 years ago

Check out Garage Door Opener With Feedback Using Esp8266 As Web Server.

|

Garage Door Opener With Feedback Using Esp8266 As Web Server.

Garage Door Opener With Feedback Using Esp8266 As Web Server.: Hi,I'll show you how to make a simple way to do a... |

|

|

You'll see how they got the 5 volts from the ESP8266 On Tuesday, November 12, 2019, 08:10:06 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

If you have a power supply ...  test your relay ..  the IN wire will touch positive when triggered. (never know about these relays from China) My current relay is a 5 volt but measured voltage at the 3 volt pin was 3.2 volts .. just enough to trigger the 5 volt coil on the relay.However, I did order some 3 volt relays for the future ..  this way I'll have both 3 and 5 volts for my projects. On Tuesday, November 12, 2019, 08:06:48 PM CST, Doug Crawford rocknbarc@yahoo.com wrote:

Try D1 to IN You say you have 5V pin on DC+ ... ESP8266 are normally 3 volts unless you jump at the inverter. On Tuesday, November 12, 2019, 07:26:25 PM CST, clox-coder notifications@github.com wrote:

@TexAcoon I am using the usb for power on 8266. i have 5v pin on 8266 connected to dc+ on relay . grnd from 8266 to dc - on relay and pin D2 connected to IN on relay . 5v power showing lit on relay.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

clox-coder commented 4 years ago

@TexAcoon I checked the voltage its putting out 4.87 volts from esp8266 . I didnt want to use the sinric lock feature. I wonder if it is because of the code i am using is stating for Button Press. So it is not correct. I am probably gonna have too look over the code. Define Pins etc without the sinric lock feature @sivar2311 is this what i am missing?

void handleButtonPress() { unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced? if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa myPowerState = false; } else { myPowerState = true;

sivar2311 commented 4 years ago

Take a look at this code I found on instructables: https://www.instructables.com/id/Garage-Door-Opener-With-Feedback-Using-Esp8266/ ... By itself it worked fine but have to log into another site iot site like Sinric Pro.

The project from instructables doesn't require another external service. There is a webserver running on ESP8266 wich lets you open and close the door.

Afaik if you're using a relay and a magnetic switch to read the door state, you have 2 pins to define: 1x to turn on the coil and 1x feedback from magnetic switch. I think you're using two of them: one relay to close the door and another one to open the door? If it is so, you have to define 4 pins! 2 as output to control the relays and 2 as inputs to read the states of the magnetic switches.

Please take out the defines i made (LED_GREEN / LED_RED) and make your own defines matching your wiring! Assuming you're using RELAY_ONE to open the door, and RELAY_TWO to close the door

#define RELAY_ONE D1 // used to open the door
#define MAGNET_ONE D2 // high=door is open
#define RELAY_TWO D3 // used to close the door
#define MAGNET_TWO D4 // high = door is closed

in setup() you have to setup the pins correctly

  pinMode(RELAY_ONE, OUTPUT);
  pinMode(RELAY_TWO, OUTPUT);
  pinMode(MAGNET_ONE, INPUT);
  pinMode(MAGNET_TWO, INPUT);

To activate the relays

  digitalWrite(RELAY_ONE, HIGH); // will turn on relay 1 and open the door
  digitalWrite(RELAY_TWO, HIGH); // will turn on relay 2 and close the door

To read the states of the magnetic switches:

  bool state_one = digitalRead(MAGNET_ONE);
  bool state_two = digitalRead(MAGNET_TWO);
state_one state_two state
HIGH LOW Door is in open position
LOW HIGH Door is in closed position
LOW LOW Door is opening or closing
HIGH HIGH You have a problem, because door can't be open and closed at the same time ;)

So you have to write your own functions to open and close the door. Call these functions from onLockState callback like this:

#define RELAY_ONE  D1      // used to open the door
#define MAGNET_ONE D2      // high=door is open | low = door is NOT open
#define RELAY_TWO  D3      // used to close the door
#define MAGNET_TWO D4      // high = door is closed | low = door is NOT closed
#define MOVE_TIMEOUT 10000 // timout 60 seconds for door movement -> please use a value which fits to your open/close time

/* function: turn on relay connected to pin RELAY and waits until MAGNET gets closed or timeout happened
 * input:    RELAY  = pin which is RELAY connected to
 *           MAGNET = pin which is MAGNET connected to
 * return:   true if MAGNET is closed 
 *           false if MAGNET is still open -> timout happened!
*/
bool turnOnRelayAndWait(int RELAY, int MAGNET, unsigned long timeout) {
  unsigned long startMillis = millis();
  digitalWrite(RELAY, HIGH); // turn on relay

  bool success = false;
  while ((!success) && (millis()-startMillis<timeout)) { // wait until MAGNET is HIGH or timeout is reached
    success = digitalRead(MAGNET);
    yield(); 
  }
  digitalWrite(RELAY, LOW); // turn off relay
  return success; 
}

bool onLockState(String deviceId, bool &lockState) {
  if (lockState==true) { // command: close GarageDoor
    Serial.printf("Closing garage door...");
    bool success = turnOnRelayAndWait(RELAY_TWO, MAGNET_TWO, MOVE_TIMEOUT);
    switch(success) {
      case true:   Serial.printf("closed\r\n"); 
                   lockState = true; // tell sinric door is closed           
                   break; 
      case false:  Serial.printf("timeout, door is not closed!!\r\n");
                   lockState = false; // tell sinric door is not closed
                   break;
    }
  } else { // command: open GarageDoor
    Serial.printf("Opening garage door...");
    bool success = turnOnRelayAndWait(RELAY_ONE, MAGNET_ONE, MOVE_TIMEOUT);
    switch(success) {
      case true:   Serial.printf("open\r\n");
                   lockState = false;  // tell sinric door is open
                   break;
      case false:  Serial.printf("timeout, door is not open!!\r\n");
                   lockState = true; // tell sinric door is not open
                   break;
    }
  }
  return true; // callback handled successfully
}

I hope this will help you.

sivar2311 commented 4 years ago

@clox-coder

@sivar2311 is this what i am missing?

Sorry i can't follow you. What's your project like? The code for handleButtonPress is for manually switching the device (on/off) and send this as an event back to SinricPro Server. So alexa get's informed about the new device state.

sivar2311 commented 4 years ago

Because it was early in the morning when i wrote the code above...it turned out that it was not working correctly. That's why i edited the post. The code shown above is now correct.

sivar2311 commented 4 years ago

You'll find my complete demo and wiring here: GarageDoorExample

Because i don't have any relays or reed-switches i used two LED's and two pushbuttons.

TexAcoon commented 4 years ago

WOW! THanks Boris ...    I'll give it a try ..  I wanted to send you some pictures of the project. After sending I received a email rejection. Doug On Wednesday, November 13, 2019, 05:33:59 AM CST, Boris Jäger notifications@github.com wrote:

You'll find my complete demo and wiring here: GarageDoorExample

Because i don't have any relays or reed-switches i used two LED's and two pushbuttons.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

Hi Doug,

you can't send photos to a @reply.github.com address. There are two possibilities: a) upload your photos to a sharing site and post the link or b) send me your photos via e-mail to my address (sivar2311@gmail.com). Alternatively, if you're using google photos you can share your photos from there.

clox-coder commented 4 years ago

@sivar2311 that's what I was wondering. I wanted to have my relay switch on automatically at D2 when I ask Alexa to turn on device Instead of using push button method.

sivar2311 commented 4 years ago

@clox-coder But the difference between onPowerState and sendPowerStateEvent is clear to you now?

clox-coder commented 4 years ago

@sivar2311 I am looking over both of these now. My old code was completely different. I was using the below code to turn my relay on . Would i define both pins at the beginning and then use the onPowerState and sendPowerStateEvent under main setup function?

void callback(uint8_t device_id, const char * device_name, bool state) { Serial.print("Device "); Serial.print(device_name); Serial.print(" state: "); if (state) { Serial.println("ON"); digitalWrite(D2,HIGH); } else { Serial.println("OFF"); digitalWrite(D2,LOW); } }

sivar2311 commented 4 years ago

Yes because we are talking about SinricPro now. Topic changed during discussion. Have a look at https://sinric.pro and corresponding git repository: https://github.com/sinricpro/esp8266-esp32-sdk

TexAcoon commented 4 years ago

Hi Boris, I tried the Garage Door Example and it didn't work correctly for me.   I was using magnet reed switches in place of your push buttons.   Not sure if you live in an apartment or house with garage and door opener.  Besides having a regular remote control there is a remote push button normally mounted on the inside of the garage wall and is a NO momentary switch.  It does not stay on only off until push powering the garage door opener to move whether it's going up or going down.  There is no brain i.e., computer to identify its position ..  Pushing the wall button once (pending on door position) will either raise it or lower the door - if you push it while in motion or door travel it will stop the garage door because it has not hit it's setpoint that you established for full open and full closed prior to it's normal everyday operation. The push buttons that you have would normally be magnetic reed switch - and would work normally open or closed pending on door position. As far as a 5v relay goes (only one needed) - it should be on a timer i.e, 2 to 5 second trigger. 

Example: When asking Alexa to Unlock the garage door - she will ask for your code.   Current LOCKED Arduino Conditions ARE:

     On Wednesday, November 13, 2019, 01:06:52 PM CST, Boris Jäger notifications@github.com wrote:

Yes because we are talking about SinricPro now. Topic changed during discussion. Have a look at sinric.pro and corresponding git repository: https://github.com/sinricpro/esp8266-esp32-sdk

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

Oh okay... i thought you want to use the reed switches as endstop, to identify the position of the door. And no, i don't have automatic garage door opener....i don't have a garage :( So, scenario is following: You need to activate your existing garage door opener by a high signal for about 2 to 5 seconds. Problem is: you (your ESP) doesn't know door's position! It's just sending the same signal (2-5 seconds high) to open or close the door.

TexAcoon commented 4 years ago

I ended up fat fingering my last email to you ..  let me send this new one to you for clarification.

On Wednesday, November 13, 2019, 02:26:03 PM CST, Boris Jäger <notifications@github.com> wrote:  

Oh okay... i thought you want to use the reed switches as endstop, to identify the position of the door. And no, i don't have automatic garage door opener....i don't have a garage :( So, scenario is following: You need to activate your existing garage door opener by a high signal for about 2 to 5 seconds. Problem is: you (your ESP) doesn't know door's position! It's just sending the same signal (2-5 seconds high) to open or close the door.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

TexAcoon commented 4 years ago

Hi Boris, I tried the Garage Door Example and it didn't work correctly for me.   I was using magnet reed switches in place of your push buttons.   Not sure if you live in an apartment or house with garage door opener.  Besides having a regular remote control there is a remote push button normally usually mounted on the inside of the garage wall and is a NO momentary switch.  It does not stay on only off until push powering the garage door opener to move whether it's going up or going down.  There is no real brain i.e., computer to identify its position ..  Pushing the wall button once (pending on door position) will either raise it or lower the door - if you push it while in motion or door travel it will stop the garage door because it has not hit it's setpoint that you established for full open and full closed prior to it's normal everyday operation. The push buttons that you have would normally be magnetic reed switch - and would work normally open or closed pending door position. As far as a 5v relay goes (only one needed) - it should be on a timer i.e, 2 to 5 second trigger (this would depend on internet connection).  

Example:  Current LOCKED Arduino Conditions ARE:

Oh okay... i thought you want to use the reed switches as endstop, to identify the position of the door. And no, i don't have automatic garage door opener....i don't have a garage :( So, scenario is following: You need to activate your existing garage door opener by a high signal for about 2 to 5 seconds. Problem is: you (your ESP) doesn't know door's position! It's just sending the same signal (2-5 seconds high) to open or close the door.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

Okay, i think i've got it. so scenario is 1x relay to send 2-5 seconds high signal to turn the door (in whatever direction) 2x reed switches to identify door position. 1 for closed position and 1 for open position, right?

sivar2311 commented 4 years ago

@TexAcoon I changed the example in my git repo to your needs. please have a look: https://github.com/sivar2311/GarageDoorExample

Note: I changed the used pins: D5 = relay D6 = reedswitch for open door state D7 = reedswitch for closed door state

TexAcoon commented 4 years ago

Thats correct ...  I will try and hook up on that to be sure of the connection point..Will take photos and video ... At this time I'm testing on table top to make sure everything works .. On Wednesday, November 13, 2019, 02:43:37 PM CST, Boris Jäger notifications@github.com wrote:

Okay, i think i've got it. so scenario is 1x relay to send 2-5 seconds high signal to turn the door (in whatever direction) 2x reed switches to identify door position. 1 for closed position and 1 for open position, right?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

Sounds good. I am going to improve the code a bit...so that manually actions (open / close) will be sent to server. But let us close this issue here, because we're discussing in wrong place. This issue is related to alexa-wemo-switch. But we're working on a concrete implementation about a garage door using SinricPro service. Let us switch over to https://github.com/sivar2311/GarageDoorExample.

TexAcoon commented 4 years ago

Sounds good - This will be a great feature for Sinric Pro Service when we get this ironed out ..   I certainly appreciate your patience and expertise developing a script for the garage.We can switch over to https://github.com/sivar2311/GarageDoorExample. Doug  On Thursday, November 14, 2019, 07:59:32 AM CST, Boris Jäger notifications@github.com wrote:

Sounds good. I am going to improve the code a bit...so that manually actions (open / close) will be sent to server. But let us close this issue here, because we're discussing in wrong place. This issue is related to alexa-wemo-switch. But we're working on a concrete implementation about a garage door using SinricPro service. Let us switch over to https://github.com/sivar2311/GarageDoorExample.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

sivar2311 commented 4 years ago

I created a discussion "issue" -> https://github.com/sivar2311/GarageDoorExample/issues/1. So we can switch over now. @kakopappa I think we can close this issue here now