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
228 stars 122 forks source link

sinricpro and esp8266 #221

Closed carloslassous closed 2 years ago

carloslassous commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Thanks a lot.

From: Boris Jäger @.> Date: Wednesday, October 6, 2021 at 12:24 AM To: sinricpro/esp8266-esp32-sdk @.> Cc: carloslassous @.>, Author @.> Subject: Re: [sinricpro/esp8266-esp32-sdk] sinricpro and esp8266 (#221)

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 documentationhttps://sinricpro.github.io/esp8266-esp32-sdk/class_power_state_controller.html#aad370bc6b280bbdeac98181a31f22df4. In the documentation you will also find an overviewhttps://sinricpro.github.io/esp8266-esp32-sdk/class_power_state_controller.html 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();

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/sinricpro/esp8266-esp32-sdk/issues/221#issuecomment-935486137, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEVYOZLZXIS45LC2K6HQ2XLUFPMQXANCNFSM5FNL6RPQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

stale[bot] commented 2 years ago

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!

carloslassous commented 2 years ago

Thanks, the issued was resolve by the person that answered me. 

Sent from Yahoo Mail for iPhone

On Friday, October 15, 2021, 6:22 AM, stale[bot] @.***> wrote:

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!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

stale[bot] commented 2 years ago

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[bot] commented 2 years ago

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!