kakopappa / sinric

Amazon Alexa Smart home skill / Google Home Action for ESP8266 / ESP32 / Arduino
https://sinric.com
284 stars 166 forks source link

Unable to update status of relay through switch on Sinric pro #489

Open lee3gomes opened 2 years ago

lee3gomes commented 2 years ago

Hey Guys, I am trying to update the status of the relay through the server but doesnt seem to be working. It worked with the old sinric server. Heres the code.

ifdef ENABLE_DEBUG

   #define DEBUG_ESP_PORT Serial
   #define NODEBUG_WEBSOCKETS
   #define NDEBUG

endif

include

include

ifdef ESP8266

   #include <ESP8266WiFi.h>

endif

ifdef ESP32

   #include <WiFi.h>

endif

include "SinricPro.h"

include "SinricProLight.h"

include

include

include

include

include

include

include

String header;

include // https://github.com/bxparks/AceButton

using namespace ace_button;

// Set web server port number to 80 WiFiServer server(80); WiFiManager wifiManager;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

define APP_KEY "42db20f2-f08195193ab2" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"

define APP_SECRET "10dea-9c89-327702c47897" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

define LIGHT_ID "612bb2608" // Should look like "5dc1564130xxxxxxxxxxxxxx"

define BAUD_RATE 9600 // Change baudrate to your need

define BAUD_RATE 115200 // Change baudrate to your need

ButtonConfig config1; AceButton button1(&config1); void handleEvent1(AceButton*, uint8_t, uint8_t);

const int pump=0; const int BUTTON1_PIN = 2;

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value); void setTargetTemperatureOnServer(String deviceId, String value, String scale);

bool onPowerState(const String &deviceId, bool &state) { if(deviceId=="612bbbc92c014831f8242608"){ Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off"); if(state){ digitalWrite(pump,LOW); Serial.println("PUMP 01 TURNED ON"); } else{ digitalWrite(pump,HIGH); } }

return true; // request handled properly

}

void setupSinricPro() { // get a new Light device from SinricPro SinricProLight &myLight1 = SinricPro["612bbbc92c014831f8242608"];

// set callback function to RED LIGHT myLight1.onPowerState(onPowerState);

// 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); }

// main setup function void setup() { Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); //wifiManager.resetSettings(); wifiManager.autoConnect("Wi-Pump01"); Serial.println("Connected.");

server.begin(); pinMode(BUTTON1_PIN, INPUT_PULLUP); pinMode(pump,OUTPUT); digitalWrite(pump,HIGH);

config1.setEventHandler(button1Handler); button1.init(BUTTON1_PIN); setupSinricPro();

}

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

void setPowerStateOnServer(String deviceId, String value) { DynamicJsonDocument jsonBuffer(2048); jsonBuffer["deviceId"] = deviceId; jsonBuffer["action"] = "setPowerState"; jsonBuffer["value"] = value; StreamString databuf; serializeJson(jsonBuffer, databuf);

webSocket.sendTXT(databuf); } void setTargetTemperatureOnServer(String deviceId, String value, String scale) { DynamicJsonDocument jsonBuffer(2048); jsonBuffer["action"] = "SetTargetTemperature"; jsonBuffer["deviceId"] = deviceId;

JsonObject valueObj = jsonBuffer.createNestedObject("value"); JsonObject targetSetpoint = valueObj.createNestedObject("targetSetpoint"); targetSetpoint["value"] = value; targetSetpoint["scale"] = scale;

StreamString databuf; serializeJson(jsonBuffer, databuf);

webSocket.sendTXT(databuf); } void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { Serial.println("EVENT1"); switch (eventType) { case AceButton::kEventPressed: Serial.println("kEventPressed"); setPowerStateOnServer("612bbbc92c014831f8242608", "OFF"); digitalWrite(0, HIGH); break;

} }

sivar2311 commented 2 years ago

It looks like you have mixed Sinric Classic code with SinricPro code. This will not work!

You don't have to worry about the WebSocket protocol, the SinricPro SDK does all that for you!

Take a look at the switch example. There you can see how a switch can send the state to the SinricPro server.

Also have a look at the documentation and the provided examples. Each device offers the possibility to send events to the server.

Please use the SinricPro repository for SinricPro related issue - Thanks in advance.