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

Sinric Pro With ESP8266 and OLED SSD1306 Display #385

Closed Tharos88 closed 2 weeks ago

Tharos88 commented 2 weeks ago

Hi!

I installed an OLED SSD1306 Display on my project, based on two relays and blinds. I want to show when the relays are activated or not on the display, but I didn't find the way in the source code how to retrieve the information which relay is ON and which relay is OFF.

What can I do to know the relays information in order to show when the relays are ON or OFF on the display?

Thank you!

sivar2311 commented 2 weeks ago

The library is stateless. You must keep the state yourself, e.g. in a global variable. Update the state in every function that changes the state (e.g. in the onPowerState callback, button handler, webhandler callback etc.).

kakopappa commented 2 weeks ago

FYI: If you set SinricPro.restoreDeviceStates(true); the server will invoke the callbacks with the last known state in the server

eg:

  SinricPro.restoreDeviceStates(true);
  SinricPro.begin(APP_KEY, APP_SECRET);

It may help you in case of power failures ect.

Tharos88 commented 2 weeks ago

The library is stateless. You must keep the state yourself, e.g. in a global variable. Update the state in every function that changes the state (e.g. in the onPowerState callback, button handler, webhandler callback etc.).

Sorry. Could you give me an example how I can to know what relay is On or OFF?

Thank you.

kakopappa commented 2 weeks ago

Here's an example. onPowerState callback stores the power state in myPowerState variable. You can use this variable to check whether on or off

#include <Arduino.h>
#if defined(ESP8266)
  #include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
  #include <WiFi.h>
#endif

#include "SinricPro.h"
#include "SinricProSwitch.h"

.......

bool myPowerState = false;

bool onPowerState(const String &deviceId, bool &state) {
  myPowerState = state; // state is stored in myPowerState   
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];

  // set callback function to device
  mySwitch.onPowerState(onPowerState);

  // setup SinricPro
  SinricPro.restoreDeviceStates(true); // restore the last known state from the server.
  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupSinricPro();
}

void handlePowerState() {
  if (myPowerState) {
     // Do something if On
   } else {
    // Do something if Off
   }
}

void loop() {
  handlePowerState();
  SinricPro.handle();
}
Tharos88 commented 2 weeks ago

Thank you for your help, @kakopappa

I was able to solve the problem with some IF statements.

Again, thank you so much.