Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sinricpro and esp8266 #221

Closed
carloslassous opened this issue Oct 6, 2021 · 6 comments
Closed

sinricpro and esp8266 #221

carloslassous opened this issue Oct 6, 2021 · 6 comments
Labels
stale Issue that may be closed soon due to the original author not responding any more.

Comments

@carloslassous
Copy link

Hello:

I am trying to create a personal switch with Sindric.

This should happen when I am turning on the device. If I turn Off the device something else should happen.
How do I create another method for this, I mean, one for On and one for Off
bool onPowerState1(const String &deviceId, bool &state)
{
digitalWrite(D0, LOW);
delay(1000);
digitalWrite(D0, HIGH);
Serial.printf("Device 1 turned %s\r\n", state?"on":"off");
return true; // request handled properly

}

@sivar2311
Copy link
Collaborator

The onPowerStateCallback has the parameter state of type bool.

The meaning of the state parameter is
true = the device is requested to turn on
false = The device is requested to switch off

A detailed description can be found in the documentation.
In the documentation you will also find an overview of which device types implement the onPowerState callback.

If you need dedicated functions for on and off, you can call them from the onPowerState callback. Like so:

void turnOn() {
// do something
}

void turnOff() {
// do something else
}

bool onPowerState(const String& deviceId, bool& state) {
  if (state) {
    turnOn();
  } else {
    turnOff();
  }
  return true;
}

Since HIGH and LOW are only defined values for true and false, the shortest way to switch a digital PIN on or off is this:

bool onPowerState(const String& deviceId, bool& state) {
  digitalWrite(pin, state);
  return true;
}

You are completely free to choose what should happen in the callbacks.

The callbacks always have a return value of the type bool which is returned to the SinricServer (and also to Alexa etc.).
A return true; at the end of the callback function signals that the action was executed successfully (Alexa will reply with "OK").
A return false; signals that something failed (e.g. a motor could not close the door). Alexa will then reply something like "Sorry, but something went wrong".

For this reason it is a good idea that your dedicated functions also have a return value of type bool which is then returned by the onPowerState callback like so:

bool turnOn() {
// do something
  return true; // operation was successful
}

bool turnOff() {
// do something else
  return true; // operation was successful
}

bool onPowerState(const String& deviceId, bool& state) {
  if (state) {
    return turnOn();
  } else {
    return turnOff();
  }
}

A much shorter way of writing to achieve the same thing is:

bool onPowerState(const String& deviceId, bool& state) {
  return state ? turnOn() : turnOff();
}

@carloslassous
Copy link
Author

carloslassous commented Oct 8, 2021 via email

@stale
Copy link

stale bot commented Oct 15, 2021

This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!

@stale stale bot added the stale Issue that may be closed soon due to the original author not responding any more. label Oct 15, 2021
@carloslassous
Copy link
Author

carloslassous commented Oct 15, 2021 via email

@stale stale bot removed the stale Issue that may be closed soon due to the original author not responding any more. label Oct 15, 2021
@stale
Copy link

stale bot commented Oct 23, 2021

This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!

@stale stale bot added the stale Issue that may be closed soon due to the original author not responding any more. label Oct 23, 2021
@stale
Copy link

stale bot commented Oct 30, 2021

Hey again! It’s been 14 days since anything happened on this issue, so our friendly robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks again for being a part of the SinricPro community!

@stale stale bot closed this as completed Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issue that may be closed soon due to the original author not responding any more.
Projects
None yet
Development

No branches or pull requests

2 participants