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 121 forks source link

Alexa Integration for Custom Sinric Devices #243

Closed MarineCorpse closed 2 years ago

MarineCorpse commented 2 years ago

Alexa is able to discover all default Sinric devices (i.e. Fan, Switch), but when I try to create a custom device with different control levels Alexa will not discover the device. I have tried disabling and re-enabling Sinric skill. Is this a known issue? I could not find any information searching online. I am using an ESP8266 as my Sinric device.

sivar2311 commented 2 years ago

Last time i checked this, everything worked fine. Did you create a device based on your template?

sivar2311 commented 2 years ago

I ran a quick test. After creating a device based on my template, the device was automatically recognized by Alexa. If you still have problems, please export and share your device template for further analysis.

MarineCorpse commented 2 years ago

Thanks for the quick reply. I did create a device, but it was not found. Here is the device template code:

{
  "name": "OverheadFan",
  "description": "Overhead Fan Control",
  "deviceTypeId": "600d0a4994acbe29a9ce1bbf",
  "capabilities": [
    {
      "id": "5ff0b30e994fd31b7d5e865b"
    },
    {
      "id": "5ff0b41b994fd31b7d5e8961",
      "mode": {
        "instanceId": "modeInstance1",
        "locale": "en-US",
        "modeName": "Speed",
        "modeValues": [
          "Off",
          "Low",
          "Medium",
          "High",
          "Reverse"
        ],
        "nonControllable": false
      }
    }
  ]
}

Here is the code for the device. I have tried both to include the .h file directly in the sketch and copied it to the folder where the Sinric library is and the other devices are stored. Here's the code, main.ino:

``/*
 * Example
 *
 * If you encounter any issues:
 * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
 * - ensure all dependent libraries are installed
 * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
 * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
 * - open serial monitor and check whats happening
 * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
 * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
 */

 // Custom devices requires SinricPro ESP8266/ESP32 SDK 2.9.6 or later

// 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 <Arduino.h>
#ifdef ESP8266
  #include <ESP8266WiFi.h>
#endif
#ifdef ESP32
  #include <WiFi.h>
#endif

#include <SinricPro.h>
#include "OverheadFan.h"

#define APP_KEY    "<removed>"
#define APP_SECRET "<removed>"
#define DEVICE_ID  "<removed>"

#define SSID       "YOUR_WIFI_SSID"
#define PASS       "YOUR_WIFI_PASS"

#define BAUD_RATE  9600

OverheadFan &overheadFan = SinricPro[DEVICE_ID];

/*************
 * Variables *
 ***********************************************
 * Global variables to store the device states *
 ***********************************************/

// PowerStateController
bool globalPowerState;

// ModeController
std::map<String, String> globalModes;

/*************
 * Callbacks *
 *************/

// PowerStateController
bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("[Device: %s]: Powerstate changed to %s\r\n", deviceId.c_str(), state ? "on" : "off");
  globalPowerState = state;
  return true; // request handled properly
}

// ModeController
bool onSetMode(const String& deviceId, const String& instance, String &mode) {
  Serial.printf("[Device: %s]: Modesetting for \"%s\" set to mode %s\r\n", deviceId.c_str(), instance.c_str(), mode.c_str());
  globalModes[instance] = mode;
  return true;
}

/**********
 * Events *
 *************************************************
 * Examples how to update the server status when *
 * you physically interact with your device or a *
 * sensor reading changes.                       *
 *************************************************/

// PowerStateController
void updatePowerState(bool state) {
  overheadFan.sendPowerStateEvent(state);
}

// ModeController
void updateMode(String instance, String mode) {
  overheadFan.sendModeEvent(instance, mode, "PHYSICAL_INTERACTION");
}

/********* 
 * Setup *
 *********/

void setupSinricPro() {

  // PowerStateController
  overheadFan.onPowerState(onPowerState);

  // ModeController
  overheadFan.onSetMode("modeInstance1", onSetMode);

  SinricPro.onConnected([]{ Serial.printf("[SinricPro]: Connected\r\n"); });
  SinricPro.onDisconnected([]{ Serial.printf("[SinricPro]: Disconnected\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
};

void setupWiFi() {
  WiFi.begin(SSID, PASS);
  Serial.printf("[WiFi]: Connecting to %s", SSID);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  Serial.printf("connected\r\n");
}

void setup() {
  Serial.begin(BAUD_RATE);
  setupWiFi();
  setupSinricPro();
}

/********
 * Loop *
 ********/

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

OverheadFan.h code:

#ifndef _OVERHEADFAN_H_
#define _OVERHEADFAN_H_

#include <SinricProDevice.h>
#include <Capabilities/PowerStateController.h>
#include <Capabilities/ModeController.h>

class OverheadFan 
: public SinricProDevice
, public PowerStateController<OverheadFan>
, public ModeController<OverheadFan> {
  friend class PowerStateController<OverheadFan>;
  friend class ModeController<OverheadFan>;
public:
  OverheadFan(const String &deviceId) : SinricProDevice(deviceId, "OverheadFan") {};
};

#endif
sivar2311 commented 2 years ago

A device based on your template is discovered immediately: image

Discovery should occur automatically immediately after the device is created. It is not necessary that an ESP is connected.

I have tried both to include the .h file directly in the sketch and copied it to the folder where the Sinric library is and the other devices are stored

You don't need to copy any files into the library folder. Leave them in your project folder. But this is a different topic and has nothing to do with device discovery.

sivar2311 commented 2 years ago

To avoid misunderstandings: Is the device not discovered by Alexa or does the ESP not connect?

Your sketch seems to be exactly the automatically generated sample code (including the credentials). The only thing missing is your WiFi credentials so that your ESP can connect.

Please note: You should keep the credentials secret. For security reasons, I recommend creating new credentials and deleting the old ones.

MarineCorpse commented 2 years ago

Hmmm...tried all morning and it wasn't appearing. Now it showing up in my Alexa devices.

I had posted a generic ESP8266 code that was autogenerated. The code was working on the ESP, I could control through Sinric website interface. What I was having trouble with was getting Alexa to recognize the device, but that seems to have resolved. Thanks for helping to look into this.

kakopappa commented 2 years ago

When you add a device, the server sends it to Alexa. You don’t need to run the device discover again. If you do, it says no new devices found..

Alexa used to show a push notification before, now it doesn’t show it and leads to this confusion

On Mon, 17 Jan 2022 at 4:40 AM MarineCorpse @.***> wrote:

Hmmm...tried all morning and it wasn't appearing. Now it showing up in my Alexa devices.

I had posted a generic ESP8266 code that was autogenerated. The code was working on the ESP, I could control through Sinric website interface. What I was having trouble with was getting Alexa to recognize the device, but that seems to have resolved. Thanks for helping to look into this.

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/243#issuecomment-1013957695, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZQTPUOBI7XV5DMMDD3UWM3N5ANCNFSM5MDEUGGA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

sivar2311 commented 2 years ago

I cannot confirm this. During my test yesterday, I received the push notifications as usual.

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!