Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active November 17, 2024 04:43
Show Gist options
  • Select an option

  • Save kakopappa/5de59e34522c7ea21e3eef72c2e2bcd7 to your computer and use it in GitHub Desktop.

Select an option

Save kakopappa/5de59e34522c7ea21e3eef72c2e2bcd7 to your computer and use it in GitHub Desktop.

Revisions

  1. kakopappa revised this gist Nov 17, 2024. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions SmartButton.ino
    Original file line number Diff line number Diff line change
    @@ -36,14 +36,20 @@ SmartButton &smartButton = SinricPro[DEVICE_ID];

    // SmartButtonStateController
    bool onSinglePress(const String &deviceId) {
    // Triggers when you press the "SmartButton" on the app

    return true; // request handled properly
    }

    bool onDoublePress(const String &deviceId) {
    // Triggers when you double press the "SmartButton" on the app

    return true; // request handled properly
    }

    bool onLongPress(const String &deviceId) {
    // Triggers when you long press the "SmartButton" on the app

    return true; // request handled properly
    }

  2. kakopappa revised this gist Nov 17, 2024. 1 changed file with 98 additions and 0 deletions.
    98 changes: 98 additions & 0 deletions SmartButton.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    // 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 "SmartButton.h"

    #define APP_KEY "APP_KEY"
    #define APP_SECRET "APP_SECRET"
    #define DEVICE_ID "DEVICE_ID"

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

    #define BAUD_RATE 115200

    SmartButton &smartButton = SinricPro[DEVICE_ID];


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

    // SmartButtonStateController
    bool onSinglePress(const String &deviceId) {
    return true; // request handled properly
    }

    bool onDoublePress(const String &deviceId) {
    return true; // request handled properly
    }

    bool onLongPress(const String &deviceId) {
    return true; // request handled properly
    }



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

    void setupSinricPro() {

    // SmartButtonStateController
    smartButton.onSinglePress(onSinglePress);
    smartButton.onDoublePress(onDoublePress);
    smartButton.onLongPress(onLongPress);

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

    void setupWiFi() {
    #if defined(ESP8266)
    WiFi.setSleepMode(WIFI_NONE_SLEEP);
    WiFi.setAutoReconnect(true);
    #elif defined(ESP32)
    WiFi.setSleep(false);
    WiFi.setAutoReconnect(true);
    #endif

    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();
    }
  3. kakopappa created this gist Nov 17, 2024.
    15 changes: 15 additions & 0 deletions SmartButton.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    #ifndef _SMARTBUTTON_H_
    #define _SMARTBUTTON_H_

    #include <SinricProDevice.h>
    #include <Capabilities/SmartButtonStateController.h>

    class SmartButton
    : public SinricProDevice
    , public SmartButtonStateController<SmartButton> {
    friend class SmartButtonStateController<SmartButton>;
    public:
    SmartButton(const String &deviceId) : SinricProDevice(deviceId, "SmartButton") {};
    };

    #endif