Skip to content

SmartButton error #426

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

Closed
nbctcp opened this issue Apr 27, 2025 · 16 comments
Closed

SmartButton error #426

nbctcp opened this issue Apr 27, 2025 · 16 comments
Labels
stale Issue that may be closed soon due to the original author not responding any more.

Comments

@nbctcp
Copy link

nbctcp commented Apr 27, 2025

HW INFO:
esp32c3
SW INFO
Scetch folder path
C:\Users\user1\Documents\Arduino\Sinric_SmartButton
library path
C:\Users\user1\Documents\Arduino\libraries\SmartButton\SmartButton.h

https://help.sinric.pro/pages/tutorials/custom-device-types/smart-button/push-button
I am trying to follow that doc then download
But when I upload using IDE 1.8.19
I got this error

Arduino: 1.8.19 (Windows 10), Board: "XIAO_ESP32C3, Enabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 160MHz (WiFi), QIO, 80MHz, 4MB (32Mb), 921600, None, Disabled"
C:\Users\user1\Documents\Arduino\Sinric_SmartButton\Sinric_SmartButton.ino: In function 'void setupSinricPro()':
Sinric_SmartButton:74:14: error: 'class SmartButton' has no member named 'onSinglePress'
   74 |  smartButton.onSinglePress(onSinglePress);
      |              ^~~~~~~~~~~~~
Sinric_SmartButton:75:14: error: 'class SmartButton' has no member named 'onDoublePress'
   75 |  smartButton.onDoublePress(onDoublePress);
      |              ^~~~~~~~~~~~~
Sinric_SmartButton:76:14: error: 'class SmartButton' has no member named 'onLongPress'; did you mean 'onButtonPress'?
   76 |  smartButton.onLongPress(onLongPress);
      |              ^~~~~~~~~~~
      |              onButtonPress
exit status 1
'class SmartButton' has no member named 'onSinglePress'

?

  1. what I am missing there

tq

Sinric_SmartButton.ino

 // 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 "SmartButton.h"

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

#define SSID       ""
#define 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();
}

SmartButton.h

#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
@kakopappa
Copy link
Contributor

Thanks. There is an issue with the code generation. Will fix this ASAP.

This is what the correct code should look like

// 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    "xxx"
#define APP_SECRET "xxx"
#define DEVICE_ID  "xxx"

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

#define BAUD_RATE  115200

SmartButton &smartButton = SinricPro[DEVICE_ID];


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

// SmartButtonStateController
// SmartButtonStateController
bool onButtonPress(const String& deviceId, SmartButtonPressType pressType) {
  switch (pressType) {
    case SmartButtonPressType::SINGLE_PRESS:
      // Handle single press
      break;
    case SmartButtonPressType::DOUBLE_PRESS:
      // Handle double press
      break;
    case SmartButtonPressType::LONG_PRESS:
      // Handle long press
      break;
  }

  return true;  // request handled properly
}


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

void setupSinricPro() {

// SmartButtonStateController
  smartButton.onButtonPress(onButtonPress);
 
  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();
}

@kakopappa
Copy link
Contributor

I have fixed the issue. Please use the code generator again to generate the code.

@nbctcp
Copy link
Author

nbctcp commented Apr 27, 2025

ERROR
note: 'void loop()' previously defined here
99 | void loop() {
| ^~~~
exit status 1
redefinition of 'SmartButton& smartButton'

CODE

SmartButton &smartButton = SinricPro[DEVICE_ID];
/*************
 * Callbacks *
 *************/
// SmartButtonStateController
bool onButtonPress(const String& deviceId, SmartButtonPressType pressType) {
  switch (pressType) {
    case SmartButtonPressType::SINGLE_PRESS:
      // Handle single press
      break;
    case SmartButtonPressType::DOUBLE_PRESS:
      // Handle double press
      break;
    case SmartButtonPressType::LONG_PRESS:
      // Handle long press
      break;
  }
  return true;  // request handled properly
}

/********* 
 * Setup *
 *********/
void setupSinricPro() {
// SmartButtonStateController
 smartButton.onButtonPress(onButtonPress);
  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();
}

@sivar2311
Copy link
Collaborator

ERROR
note: 'void loop()' previously defined here
99 | void loop() {
| ^~~~
exit status 1
redefinition of 'SmartButton& smartButton'

The Code you have posted does not seem to complete.
It looks like you have multiple smartButton and multiple loop functions in your code.
Please check your code again, carefully!

@nbctcp
Copy link
Author

nbctcp commented Apr 29, 2025

I have fixed the issue. Please use the code generator again to generate the code.

Any update on this?
is SmartButton function same as physical push button?
I mean when I release it will off relay
If yes, I wanna buy few more devices
If possible, could you adding 1 more payment gateway which could pay in local currency
Exactly like Udemy did
$ rate and fee are high, if paid using Asian currency

@kakopappa
Copy link
Contributor

kakopappa commented Apr 29, 2025 via email

@nbctcp
Copy link
Author

nbctcp commented Apr 29, 2025

almost perfect
but I can't set auto off when on below 5s
because I want to trigger relay to start generator
is by design I can't hold SmartButton to ON only when I press?
tq

@kakopappa
Copy link
Contributor

What are you trying to achieve?

@nbctcp
Copy link
Author

nbctcp commented Apr 29, 2025

What are you trying to achieve?

my brother in an island have diesel generator to generate electricity for the village
the problem was someone has to press button manually to turn it on
there are 2 buttons
button1 Run, button2 Start
leave button1 Run ON, then press Start to turn it on for few second to trigger selonoid
press too long will fry selonoid I think
I want to do that automatic without human intervention
6PM ON, 11PM OFF, 3AM ON 6AM OFF
to turn diesel OFF, press Run OFF
That what I want to achieve

@sivar2311
Copy link
Collaborator

So the sequence's are like this?

RUN START
turn on OFF -> ON OFF -> ON (for few seconds) -> OFF
turn off ON -> OFF OFF

@nbctcp
Copy link
Author

nbctcp commented Apr 29, 2025

So the sequence's are like this?

RUN START
turn on OFF -> ON OFF -> ON (for few seconds) -> OFF
turn off ON -> OFF OFF

  1. yes that what I want
  2. if possible relay still ON while button being pushed, then OFF when released, so that I don't have to set auto OFF after few seconds

@sivar2311
Copy link
Collaborator

sivar2311 commented Apr 30, 2025

/* ... */
const int BTN_RUN = 16;
const int BTN_START = 17;

const unsigned long timerDuration = 1000; // 1 second
unsigned long timer = 0;

void startTimer() {
    timer = millis();
}

void stopTimer() {
    timer = 0;
}

void pressStartButton() {
    Serial.println("Pressing Start Button");
    digitalWrite(BTN_START, HIGH);
}

void releaseStartButton() {
    Serial.println("Releasing Start Button");
    digitalWrite(BTN_START, LOW);
}

void pressRunButton() {
    Serial.println("Pressing Run Button");
    digitalWrite(BTN_RUN, HIGH);
}

void releaseRunButton() {
    Serial.println("Releasing Run Button");
    digitalWrite(BTN_RUN, LOW);
}

bool timerIsRunning() {
    return timer > 0;
}

void startGenerator() {
    if (timerIsRunning()) return;
    Serial.println("Starting Generator");
    pressRunButton();
    pressStartButton();
    startTimer();
}

void stopGenerator() {
    Serial.println("Stopping Generator");
    releaseRunButton();
}

bool timerIsExpired() {
    return (millis() - timer) >= timerDuration;
}

void handleTimer() {
  if (timerIsRunning() && timerIsExpired()) {
    Serial.println("Timer is expired");
    releaseStartButton();
    stopTimer();
  }
}

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

/* ... */

void loop() {
    SinricPro.handle();
    handleTimer();
}
startGenerator.mp4

Wokwi-Example:
https://wokwi.com/projects/429630146964714497

@nbctcp
Copy link
Author

nbctcp commented Apr 30, 2025

/* ... */
const int BTN_RUN = 16;
const int BTN_START = 17;

const unsigned long timerDuration = 1000; // 1 second
unsigned long timer = 0;

void startTimer() {
timer = millis();
}

void stopTimer() {
timer = 0;
}

void pressStartButton() {
Serial.println("Pressing Start Button");
digitalWrite(BTN_START, HIGH);
}

void releaseStartButton() {
Serial.println("Releasing Start Button");
digitalWrite(BTN_START, LOW);
}

void pressRunButton() {
Serial.println("Pressing Run Button");
digitalWrite(BTN_RUN, HIGH);
}

void releaseRunButton() {
Serial.println("Releasing Run Button");
digitalWrite(BTN_RUN, LOW);
}

bool timerIsRunning() {
return timer > 0;
}

void startGenerator() {
if (timerIsRunning()) return;
Serial.println("Starting Generator");
pressRunButton();
pressStartButton();
startTimer();
}

void stopGenerator() {
Serial.println("Stopping Generator");
releaseRunButton();
}

bool timerIsExpired() {
return (millis() - timer) >= timerDuration;
}

void handleTimer() {
if (timerIsRunning() && timerIsExpired()) {
Serial.println("Timer is expired");
releaseStartButton();
stopTimer();
}
}

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

/* ... */

void loop() {
SinricPro.handle();
handleTimer();
}
startGenerator.mp4
Wokwi-Example: https://wokwi.com/projects/429630146964714497

tq its working
you combining Run+Start into 1 device

@sivar2311
Copy link
Collaborator

you combining Run+Start into 1 device

Yes, it's one physical device - a power generator - which you can turn on and off.
It just a "button press sequence" to turn on the generator.
Isn't it ? ;)

Copy link

github-actions bot commented May 8, 2025

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!

@github-actions github-actions bot added the stale Issue that may be closed soon due to the original author not responding any more. label May 8, 2025
Copy link

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!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2025
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

3 participants