Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
portale sinric pro
#2
(02-25-2023, 02:54 PM)pasqua Wrote: Buongiorno
Sto usando una scheda ESP8266 per comunicare con il cloud HiveMQ dove leggo i topic inviati da Virtuino IoT. 
Volevo capire se è possibile collegare la scheda anche al portale Sinric Pro in modo da rendere l'app compatibile con Alexa.

Ci sono riuscito ora con le routine di alexa posso impostare il dimmer

#ifdef ESP8266
#include <ESP8266WiFi.h>  // Pins for board ESP8266 Wemos-NodeMCU
#else
#include <WiFi.h> 
#endif

#include <PubSubClient.h>
#include <WiFiClientSecure.h>

//---- WiFi settings
const char* ssid = "xxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxxxx";

//---- MQTT Broker settings
const char* mqtt_server = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud"; // replace with your broker url
const char* mqtt_username = "xxxxxxxxxxx";
const char* mqtt_password = "xxxxxxxxxxxxxx";
const int mqtt_port =xxxx;

//sinric
#include "SinricPro.h"
#include "SinricProDimSwitch.h"

#define APP_KEY          "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf"      
#define APP_SECRET        "dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxd" 
#define DIMSWITCH_ID      "xxxxxxxxxxxxxxxxxxxxxxx"   
#define BAUD_RATE        9600                // Change baudrate to your need

WiFiClientSecure espClient;  // for no secure connection use WiFiClient instead of WiFiClientSecure
//WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;

#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];


int command2 =0;

const char* command2_topic="mytopic";

//==========================================
void setup_wifi() {
  delay(10);
  Serial.print("\nConnecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("\nWiFi connected\nIP address: ");
  Serial.println(WiFi.localIP());
}

//====================mqtt=================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";  // Create a random client ID
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("connected");

      client.subscribe(command2_topic);  // subscribe the topics here

    } else {

      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");  // Wait 5 seconds before retrying
      setup_wifi();
      delay(5000);
    }
  }
}

//================================================ setup
//================================================
void setup() {

 
  Serial.begin(9600);


     
  setup_wifi();
  //pinMode(BUILTIN_LED, OUTPUT);    // Initialize the BUILTIN_LED pin as an output



    espClient.setInsecure();

 
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

  setupSinricPro();
}

//================================================ loop
//================================================
void loop() {

  if (!client.connected()) reconnect();
  client.loop();

  //---- example: how to publish sensor values every 5 sec
  unsigned long now = millis();
    SinricPro.handle();

}

//======================================= 
// This void is called every time we have a message from the broker

void callback(char* topic, byte* payload, unsigned int length) {
  String incommingMessage = "";
  for (int i = 0; i < length; i++) incommingMessage+=(char)payload[i];
 
  Serial.println("Message arrived ["+String(topic)+"]"+incommingMessage);

  if( strcmp(topic,command2_topic) == 0){//mytopic


     
    if (incommingMessage.equals("80")) {
        Serial.println("HAI IMPOSTATO IL DIMMER AD 80 "); 
      } 
  } 
}



//======================================= publising as string
void publishMessage(const char* topic, String payload , boolean retained){
  if (client.publish(topic, payload.c_str(), true))
      Serial.println("Message publised ["+String(topic)+"]: "+payload);
}



// SINRIC PRO
struct {
  bool powerState = false;
  int powerLevel = 0;
} device_state;

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state?"on":"off");
  device_state.powerState = state;
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  device_state.powerLevel = powerLevel;
  Serial.printf("Device %s power level changed to %d\r\n", deviceId.c_str(), device_state.powerLevel);
  // PUBBLICA QUANTO RICEVUTO SU HIVEMQ
  delay(50);
 
  Serial.println(powerLevel);
  publishMessage(command2_topic,String(powerLevel),true);
  return true;
}

bool onAdjustPowerLevel(const String &deviceId, int &levelDelta) {
  device_state.powerLevel += levelDelta;
  Serial.printf("Device %s power level changed about %i to %d\r\n", deviceId.c_str(), levelDelta, device_state.powerLevel);
  levelDelta = device_state.powerLevel;
  return true;
}

void setupSinricPro() {
  SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];

  // set callback function to device
  myDimSwitch.onPowerState(onPowerState);
  myDimSwitch.onPowerLevel(onPowerLevel);
  myDimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);

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


Messages In This Thread
portale sinric pro - by pasqua - 02-25-2023, 02:54 PM
RE: portale sinric pro - by pasqua - 02-25-2023, 05:26 PM
RE: portale sinric pro - by iliaslamprou - 02-27-2023, 12:48 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)