Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

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

Open
lee3gomes opened this issue Aug 31, 2021 · 1 comment
Open

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

lee3gomes opened this issue Aug 31, 2021 · 1 comment

Comments

@lee3gomes
Copy link

lee3gomes commented Aug 31, 2021

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 <Arduino.h>
#include <ArduinoJson.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProLight.h"
#include <WiFiManager.h>
#include <DNSServer.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <StreamString.h>
#include <WiFiUdp.h>
#include <WebSocketsClient.h>
String header;

#include <AceButton.h> // 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
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants