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

onpowerstate #349

Closed dony2074 closed 7 months ago

dony2074 commented 7 months ago

i get this error when verify my code, to upload to the esp32 board

error: 'using SinricProTemperaturesensor = class SINRICPRO_3_0_0::SinricProTemperaturesensor' {aka 'class SINRICPRO_3_0_0::SinricProTemperaturesensor'} has no member named 'onPowerState'

this is my full code, sorry i just copy and paste the code from the zero code sinric pro

#ifdef ENABLE_DEBUG
   #define DEBUG_ESP_PORT Serial
   #define NODEBUG_WEBSOCKETS
   #define NDEBUG
#endif 

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

#include <OneWire.h>
#include <DallasTemperature.h>
#include "SinricProTemperaturesensor.h"
#include <Wire.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID         "WKWKWK"
#define WIFI_PASS         "12345678"
#define APP_KEY           ""
#define APP_SECRET        ""

#define SWITCH_ID_1       ""
#define RELAYPIN_1        12

#define SWITCH_ID_2       ""
#define RELAYPIN_2        13

#define TEMP_SENSOR_ID    ""
#define EVENT_WAIT_TIME   60000
#define BAUD_RATE         115200                

#define ONE_WIRE_BUS 

const int oneWireBus = 14;  
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

bool deviceIsOn;                              
float temperature;                            
float lastTemperature;                        
unsigned long lastEvent = (-EVENT_WAIT_TIME); 

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off");
  deviceIsOn = state; // turn on / off temperature sensor
  return true; // request handled properly
}

bool onPowerState1(const String &deviceId, bool &state) {
 Serial.printf("Device 1 turned %s", state?"on":"off");
 digitalWrite(RELAYPIN_1, state ? HIGH:LOW);
 return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
 Serial.printf("Device 2 turned %s", state?"on":"off");
 digitalWrite(RELAYPIN_2, state ? HIGH:LOW);
 return true; // request handled properly
}

void handleTemperaturesensor() {
  if (deviceIsOn == false) {
    Serial.print("Sensor is truned off. Do nothing!");
    return;
  }; 

  unsigned long actualMillis = millis();
  if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds

  Serial.print("Requesting DS18B20 temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures

  uint8_t count = sensors.getDS18Count();

  if(count <= 0) {
    Serial.printf("DS18B20 sensor not found!\r\n");  // print error message
    return;
  } 

  float temperature = sensors.getTempCByIndex(0);

  if (temperature == DEVICE_DISCONNECTED_C) { // reading failed... 
    Serial.printf("Reading DS18B20 failed!\r\n");  // print error message
    return; // try again next time
  } 

  if (temperature == lastTemperature) {
    Serial.printf("Temperature has not changed since last read. do nothing...!\r\n");
    return; // try again next time
  }

  SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];  // get temperaturesensor device
  bool success = mySensor.sendTemperatureEvent(temperature); // send event

  if (success) {  // if event was sent successfuly, print temperature to serial
    Serial.printf("Temperature: %2.1f Celsius\r\n", temperature);
  } else {  // if sending event failed, print error message
    Serial.printf("Something went wrong...could not send Event to server!\r\n");
  }

  lastTemperature = temperature;  // save actual temperature for next compare
  lastEvent = actualMillis;       // save actual time for next compare
}

// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

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

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }

  Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}

// setup function for SinricPro
void setupSinricPro() {
  // add devices and callbacks to SinricPro
  pinMode(RELAYPIN_1, OUTPUT);
  pinMode(RELAYPIN_2, OUTPUT);

  SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
  mySensor.onPowerState(onPowerState);

  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
  mySwitch1.onPowerState(onPowerState1);

  SinricProSwitch& mySwitch2 = SinricPro[SWITCH_ID_2];
  mySwitch2.onPowerState(onPowerState2);

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); 
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });

  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setupSensor() {
  // Start up the library
  sensors.begin();
}

// main setup function
void setup() {
  Serial.begin(115200); Serial.printf("\r\n\r\n");
  setupWiFi();
  setupSensor();
  setupSinricPro();
  sensors.begin();
}

void loop() {
  SinricPro.handle();
  sensors.requestTemperatures(); 
  handleTemperaturesensor();
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(1000);
}

thanks for help and the reply

sivar2311 commented 7 months ago

The server and the app no longer support onPowerState for sensors. For this reason, version 3.0.0 was released, which is not backwards compatible - see Changelog: "BREAKING CHANGE: Remove PowerStateController from Air Quality, Contact, Motion and Temperature sensor." and the examples for the temperature sensors.

kakopappa commented 7 months ago
  1. You can downgrade the sdk for 2.x

  2. Remove mySensor.onPowerState(onPowerState);

On Sun, 19 Nov 2023 at 3:39 AM sivar2311 @.***> wrote:

The server and the app no longer support onPowerState for sensors. For this reason, version 3.0.0 was released, which is not backwards compatible - see Changelog https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/changelog.md: "BREAKING CHANGE: Remove PowerStateController from Air Quality, Contact, Motion and Temperature sensor." and the examples for the temperature sensors https://github.com/sinricpro/esp8266-esp32-sdk/tree/master/examples/temperaturesensor .

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/349#issuecomment-1817629509, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZWEUPLESMUZKOXYXNDYFEMHXAVCNFSM6AAAAAA7RFEDQCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXGYZDSNJQHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

dony2074 commented 7 months ago
  1. You can downgrade the sdk for 2.x

  2. Remove mySensor.onPowerState(onPowerState);

On Sun, 19 Nov 2023 at 3:39 AM sivar2311 @.***> wrote:

The server and the app no longer support onPowerState for sensors. For this reason, version 3.0.0 was released, which is not backwards compatible - see Changelog https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/changelog.md: "BREAKING CHANGE: Remove PowerStateController from Air Quality, Contact, Motion and Temperature sensor." and the examples for the temperature sensors https://github.com/sinricpro/esp8266-esp32-sdk/tree/master/examples/temperaturesensor .

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/349#issuecomment-1817629509, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZWEUPLESMUZKOXYXNDYFEMHXAVCNFSM6AAAAAA7RFEDQCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJXGYZDSNJQHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProTemperaturesensor.h:10, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:10: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h: In member function 'virtual ArduinoJson::V6213PB2::DynamicJsonDocument SINRICPRO_2_11_1::SinricProDevice::prepareEvent(const char, const char)': c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:73:3: error: 'DEBUG_SINRIC' was not declared in this scope DEBUG_SINRIC("[SinricProDevice:prepareEvent()]: Device "%s" isn't configured correctly! The '%s' event will be ignored.\r\n", deviceId.c_str(), action); ^~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:73:3: note: the macro 'DEBUG_SINRIC' had not yet been defined In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProUDP.h:20, from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricPro.h:17, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:12: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDebug.h:20: note: it was later defined here

define DEBUG_SINRIC(...)

In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProTemperaturesensor.h:10, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:10: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h: In member function 'virtual bool SINRICPRO_2_11_1::SinricProDevice::sendEvent(ArduinoJson::V6213PB2::JsonDocument&)': c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:79:8: error: 'SinricPro' was not declared in this scope if (!SinricPro.isConnected()) { ^~~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:79:8: note: suggested alternative: 'SinricProClass' if (!SinricPro.isConnected()) { ^~~~~ SinricProClass c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:80:5: error: 'DEBUG_SINRIC' was not declared in this scope DEBUG_SINRIC("[SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server.\r\n"); ^~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:80:5: note: the macro 'DEBUG_SINRIC' had not yet been defined In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProUDP.h:20, from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricPro.h:17, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:12: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDebug.h:20: note: it was later defined here

define DEBUG_SINRIC(...)

exit status 1

Compilation error: exit status 1

i got this massage after downgrade my sdk

dony2074 commented 7 months ago

The server and the app no longer support onPowerState for sensors. For this reason, version 3.0.0 was released, which is not backwards compatible - see Changelog: "BREAKING CHANGE: Remove PowerStateController from Air Quality, Contact, Motion and Temperature sensor." and the examples for the temperature sensors.

In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProTemperaturesensor.h:10, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:10: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h: In member function 'virtual ArduinoJson::V6213PB2::DynamicJsonDocument SINRICPRO_2_11_1::SinricProDevice::prepareEvent(const char, const char)': c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:73:3: error: 'DEBUG_SINRIC' was not declared in this scope DEBUG_SINRIC("[SinricProDevice:prepareEvent()]: Device "%s" isn't configured correctly! The '%s' event will be ignored.\r\n", deviceId.c_str(), action); ^~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:73:3: note: the macro 'DEBUG_SINRIC' had not yet been defined In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProUDP.h:20, from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricPro.h:17, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:12: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDebug.h:20: note: it was later defined here

define DEBUG_SINRIC(...)

In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProTemperaturesensor.h:10, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:10: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h: In member function 'virtual bool SINRICPRO_2_11_1::SinricProDevice::sendEvent(ArduinoJson::V6213PB2::JsonDocument&)': c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:79:8: error: 'SinricPro' was not declared in this scope if (!SinricPro.isConnected()) { ^~~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:79:8: note: suggested alternative: 'SinricProClass' if (!SinricPro.isConnected()) { ^~~~~ SinricProClass c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:80:5: error: 'DEBUG_SINRIC' was not declared in this scope DEBUG_SINRIC("[SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server.\r\n"); ^~~~ c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDevice.h:80:5: note: the macro 'DEBUG_SINRIC' had not yet been defined In file included from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProUDP.h:20, from c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricPro.h:17, from C:\Users\Dony\Downloads\TA (Code)\onpowerstate\onpowerstate.ino:12: c:\Users\Dony\Documents\Arduino\libraries\SinricPro\src/SinricProDebug.h:20: note: it was later defined here

define DEBUG_SINRIC(...)

exit status 1

Compilation error: exit status 1

i got this massage after downgrade my sdk

sivar2311 commented 7 months ago

Your include order is wrong:

#include <OneWire.h>
#include <DallasTemperature.h>
#include "SinricProTemperaturesensor.h"
#include <Wire.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

You need to include SinricPro.h first, before including SinricPro devices like SinricProTemperaturesensor.h

Change your includes to this

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"
#include "SinricProTemperaturesensor.h"

and the code compiles fine.

Btw: This would also happen in 3.x.x and is not related to the downgrade.

dony2074 commented 7 months ago

Your include order is wrong:

#include <OneWire.h>
#include <DallasTemperature.h>
#include "SinricProTemperaturesensor.h"
#include <Wire.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

You need to include SinricPro.h first, before including SinricPro devices like SinricProTemperaturesensor.h

Change your includes to this

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"
#include "SinricProTemperaturesensor.h"

and the code compiles fine.

Btw: This would also happen in 3.x.x and is not related to the downgrade.

Thankyou now the code is work fine, but in the serial monitor its keep saying disconnected from sinricpro, i have assumption that maybe because the lack of electricity, or maybe because the code ?

sivar2311 commented 7 months ago

The code looks okay. The issue must be somewhere else. WiFi, WiFi-Router, Router Network Settings.... could be many reasons.

dony2074 commented 7 months ago

The code looks okay. The issue must be somewhere else. WiFi, WiFi-Router, Router Network Settings.... could be many reasons.

But if i use other code like just relay the code work fine and say connected to sinric pro, is there tips for combining code from 2 relays and temperature sensor ds18b20

sivar2311 commented 7 months ago

I took a deeper look at your code. I've found 2 things:

  1. You're calling sensors.begin() twice! (this shouldn't be critical but avoid this)
    • it is called in setupSensor() which gets called by setup()
    • it is also called in setup()
  2. You're calling delay(1000) in the loop() function (critical!)
    • this will block the whole system on every loop for 1 second and will break the connection!
    • better use the "blink without delay"-idiom
sivar2311 commented 7 months ago

Try this:

void printSensorReading() {
  const unsigned long millisToWait = 1000; 

  static unsigned long lastMillis;
  unsigned long currentMillis = millis();
  if (lastMillis && currentMillis - lastMillis < millisToWait ) return;
  lastMillis = currentMillis;

  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
}

void loop() {
  SinricPro.handle();
  sensors.requestTemperatures(); 
  handleTemperaturesensor();
  printSensorReading();
}
dony2074 commented 7 months ago

I took a deeper look at your code. I've found 2 things:

  1. You're calling sensors.begin() twice! (this shouldn't be critical but avoid this)
    • it is called in setupSensors() which gets called by setup()
    • it is also called in setup()
  2. You're calling delay(1000) in the loop() function (critical!)
    • this will block the whole system on every loop for 1 second and will break the connection!
    • better use the "blink without delay"-idiom

Thanks for your guidance, the code work fine now