sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
Other
230 stars 124 forks source link

Garage Door example #96

Closed sirmitchdog closed 3 years ago

sirmitchdog commented 3 years ago

So, I'm relatively new to coding, and i've got a project i was hoping I could get some help with. So, i'm using a nodemcu esp8266 to control a relay board, that controls some linear actuators that open/close a gate outside. I got the garagedoor example to load onto the board, and can communicate with it via alexa/sinricpro website, but I cannot figure out how to use the code to control a pin on my esp8266. Right now, just to figure out how the functions work, i'm just trying to turn the onboard led on my esp8266 on when I say "alexa, open the gate". I can see through the serial monitor that it gets the command, but my sketch just makes the led switch states each time I say open or close, even if i just say open 2 times in a row.

An example of what code I would put in the:

bool onDoorState(const String& deviceId, bool &doorState) { Serial.printf("Garagedoor is %s now.\r\n", doorState?"closed":"open"); return true; }

would be really helpful.

I know I'm way out of my league here, but if anyone could help, I'd be very appreciative! thanks, mitch

MacSass commented 3 years ago

Hi, I guess it would be depending on what you need to do ... not sure I fully understand you issue, eventually you might past a little more of your code.

I guess you would have to check the doorState and depending on if it true or false trigger one of your relays?

Something down that line:

bool onDoorState(const String& deviceId, bool &doorState) {
  if (doorState == false) {
    Serial.println("Triggering door open");
    // Add code here to trigger door open relay
  } else {
    Serial.println("Triggering door close");
    // Add code here to trigger door close relay
  }
return true;
}

See also here: https://sinricpro.github.io/esp8266-esp32-sdk/class_sinric_pro_garage_door.html I just started converting to Sinric.pro myself ... so don´t take my words for granted :-)

MacSass

kakopappa commented 3 years ago

Inside onDoorState you have to put the code to control the linear actuators. (turn on/off relay?)

I like to recommend you how to learn to control the linear actuators first in a simple sketch and copy the code to this sketch later when you have it working

This is how to control the relay which is connected to the GPIO pin 7

int relay = 7;

void setup() {
  pinMode(relay, OUTPUT);
}

void loop() {
  digitalWrite(relay, HIGH);
  delay(10000);
  digitalWrite(relay, LOW);
  delay(10000);
}
sivar2311 commented 3 years ago

I guess you would have to check the doorState and depending on if it true or false trigger one of your relays?

This is correct.

Do not forget to check the current door state and return it in the variable doorState. Any state parameter can return the current status.

For example, if the door could not be closed you should set the doorState parameter to false. See also the documentation.

sirmitchdog commented 3 years ago

Ah ok. Yeah, I know how I'll do the code to control the actuators, I just didn't know how to actually use the onDoorState function. So the onDoorState function in the garage door xample sketch is called whenever I tell Alexa or sinric to open/ close the gate? So, in the code you posted, all I have to do is check if the doorState is false(closed) or true(open)?

If I have a separate button connected to my esp8266 that will open/ close the gate, how do I update sinric/ Alexa to know that the state has been changed?

sivar2311 commented 3 years ago

So the onDoorState function in the garage door xample sketch is called whenever I tell Alexa or sinric to open/ close the gate? So, in the code you posted, all I have to do is check if the doorState is false(closed) or true(open)?

Correct, that's the way it works :)

If I have a separate button connected to my esp8266 that will open/ close the gate, how do I update sinric/ Alexa to know that the state has been changed?

Every device can send it's state to SinricPro server using it's sendEvent function. For Garage Door device it is sendDoorStateEvent()

Here i made a little example for you, how this can be done.

void checkButton() {
  // debounce button
  static unsigned long lastButtonPress;               
  unsigned long currentMillis = millis();             
  if (currentMillis - lastButtonPress < 50) return;   
  lastButtonPress = currentMillis;                    

  // check if the button state changed
  static bool lastButtonState;                        
  bool buttonState = digitalRead(BUTTON_PIN);         
  if (buttonState == lastButtonState) return;         
  lastButtonState = buttonState;

  // if the button state changed
  SinricProGarageDoor &myGarageDoor = SinricPro[GARAGEDOOR_ID];  // get the device instance
  myGarageDoor.sendDoorStateEvent(buttonState);                  // send event depending to the buttonState
}

First 4 lines are for debouncing the button. Next 4 lines check if the button state changed Last 2 lines: This is how you can send events

You have to call checkButton() inside your loop() function.

sivar2311 commented 3 years ago

Due to inactivity I assume that the problem has been solved. For this reason, the issue will be closed now. If there are still errors or questions related to this topic, please reopen this issue.