Help: Blynk, ifttt, Google Home, Alexa and sinric.com

Hi I would like to share my project with you … an added code for Sinric, with blynk and ifttt and Google home are already working. Give me a hand to see if I have it spelled correctly. Thank you

I should point out that it must work as a Button not as a Switch because it is mounted on a domestic system where there are relays for this I have set the time function in the google home command section (ifttt) and are working

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>  //per aggiornamenti OTA
#include <WiFiUdp.h>  //per aggiornamenti OTA
#include <ArduinoOTA.h>  //per aggiornamenti OTA
#include <WebSocketsClient.h> //per sinric
#include <ArduinoJson.h> //per sinric
#include <Arduino.h> //per sinric
#include <ESP8266WiFiMulti.h> //per sinric
#include <StreamString.h> //per sinric
#include <SimpleTimer.h>

SimpleTimer timer;
//inizio programmazione sinric

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes 

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

#define MyApiKey "643642exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define MySSID "xxxxxxxxxxxxxxx" // TODO: Change to your Wifi network SSID
#define MyWifiPassword "xxxxxxxxxxxxxx" // TODO: Change to your Wifi network password

#define luce_salotto "5d139fxxxxxxxxxxxxxxxxxx"  //TODO: Device ID of PRIMO device (R1 Luce Salotto)
#define luce_cucina_isola "5d1xxxxxxxxxxxxxxxx"  //TODO: Device ID of SECONDO device (R2 luce Luce Cucina Isola)
#define luce_botola "5d1468xxxxxxxxxxxxxxxxxx"  //TODO: Device ID of TERZO device (R3 Luce Botola)
#define luce_corridoio "5d1468xxxxxxxxxxxxxxxx"  //TODO: Device ID of QUARTO device (R4 Luce Corridoio)

const int relayPin1 = 5; // TODO: Change according to your board
const int relayPin2 = 4; // TODO: Change according to your board
const int relayPin3 = 14; // TODO: Change according to your board
const int relayPin4 = 12; // TODO: Change according to your board

// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
  if (deviceId == "5d139xxxxxxxxxxxxxxxxxxx") // ID luce salotto
  {  
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    
     digitalWrite(relayPin1, LOW); // turn on relay with voltage HIGH
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin1, HIGH);  // Set  Pin 5 HIGH
     });
     
  }
  else  if (deviceId == "5d14xxxxxxxxxxxxxxxxxxxxx") // ID luce_cucina_isola
  {  
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    
     digitalWrite(relayPin2, LOW); // turn on relay with voltage HIGH
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin2, HIGH);  // Set  Pin 5 HIGH
     });
  }  
  else  if (deviceId == "5d14684xxxxxxxxxxxxxxxxxxxx") // ID luce_botola
  {  
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    
     digitalWrite(relayPin3, LOW); // turn on relay with voltage HIGH
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin3, HIGH);  // Set  Pin 5 HIGH
     });
  }  
  else  if (deviceId == "5d146xxxxxxxxxxxxxxxxxxx") // ID luce_corridoio
  {  
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);
    
     digitalWrite(relayPin4, LOW); // turn on relay with voltage HIGH
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin1, HIGH);  // Set  Pin 5 HIGH
     });
  }  
   
  else {
    Serial.print("Turn on for unknown device id: ");
    Serial.println(deviceId);
  }     
}


void turnOff(String deviceId) {
   if (deviceId == "5d139fdxxxxxxxxxxxxxxxxxxxxxxxxx") // ID luce salotto
   {  
     Serial.print("Turn off Device ID: ");
     Serial.println(deviceId);
     
     digitalWrite(relayPin1, HIGH);  // turn off relay with voltage LOW
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin1, LOW);  // Set  Pin 5 HIGH
     });
   }
   else if (deviceId == "5d14xxxxxxxxxxxxxxxxxxxxxxxxxxxx") // ID luce_cucina_isola
   {  
     Serial.print("Turn off Device ID: ");
     Serial.println(relayPin2);
     
     digitalWrite(relayPin2, HIGH);  // turn off relay with voltage LOW
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin2, LOW);  // Set  Pin 5 HIGH
     });
   }
   else if (deviceId == "5d146xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") // ID luce_botola
   {  
     Serial.print("Turn off Device ID: ");
     Serial.println(relayPin3);
     
     digitalWrite(relayPin3, HIGH);  // turn off relay with voltage LOW
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin3, LOW);  // Set  Pin 5 HIGH
     });
   }
   else if (deviceId == "5dxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) // ID luce_corridoio
   {  
     Serial.print("Turn off Device ID: ");
     Serial.println(relayPin4);
     
     digitalWrite(relayPin4, HIGH);  // turn off relay with voltage LOW
      timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
     digitalWrite(relayPin4, LOW);  // Set  Pin 5 HIGH
     });
   }
   else {
     Serial.print("Turn off for unknown device id: ");
     Serial.println(deviceId);    
  }
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  switch(type) {
    case WStype_DISCONNECTED:
      isConnected = false;    
      Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
      break;
    case WStype_CONNECTED: {
      isConnected = true;
      Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
      Serial.printf("Waiting for commands from sinric.com ...\n");        
      }
      break;
    case WStype_TEXT: {
        Serial.printf("[WSc] get text: %s\n", payload);
        // Example payloads

        // For Switch or Light device types
        // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

        // For Light device type
        // Look at the light example in github
          
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject((char*)payload); 
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        
        if(action == "setPowerState") { // Switch or Light
            String value = json ["value"];
            if(value == "ON") {
                turnOn(deviceId);
            } else {
                turnOff(deviceId);
            }
        }
        else if (action == "test") {
            Serial.println("[WSc] received test command from sinric.com");
        }
      }
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      break;
  }
}










#define R1 5    //D1(PIN NODEMCU) = V1 (PIN VIRTUALE)= RELE' IN 1
#define R2 4    //D2(PIN NODEMCU) = V2 (PIN VIRTUALE)= RELE' IN 2
#define R3 14   //D5(PIN NODEMCU) = V3 (PIN VIRTUALE)= RELE' IN 3
#define R4 12   //D6(PIN NODEMCU) = V4 (PIN VIRTUALE)= RELE' IN 4

char auth[] = "bca78xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //nodemcu test
 
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";


//test web http://139.59.206.133/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/update/V1?value=1

//ifttt Esempio: http://139.59.206.133/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxupdate/V1
//Method: PUT
//Content Type: application/json
//Body: ["1"]


//INIZIO PROGRAMMAZIONE PER IFTTT GOOGLE
BLYNK_WRITE(V1)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(R1, LOW); // (D1 NODEMCU) GPIO 5 LOW (LUCE SOGGIORNO RELE IN1)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(R1, HIGH);  // Set  Pin 5 HIGH
          Blynk.virtualWrite(V1, HIGH);// Set  Virtual Pin 0 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE

}
}

BLYNK_WRITE(V2)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(R2, LOW); // (D2 NODEMCU) GPIO 4 LOW (LUCE ISOLA RELE IN2)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(R2, HIGH);  // Set  Pin 4 HIGH
          Blynk.virtualWrite(V2, HIGH);// Set  Virtual Pin 2 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V3)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(R3, LOW); // (D5 NODEMCU) is GPIO 14 (LUCE BOTOLA RELE IN3)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(R3, HIGH);  // Set  Pin 14 HIGH
          Blynk.virtualWrite(V3, HIGH);// Set  Virtual Pin 3 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V4)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(R4, LOW); // (D6 NODEMCU) is GPIO 13 (LUCE CORRIDOIO RELE IN4)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(R4, HIGH);  // Set  Pin 13 HIGH
          Blynk.virtualWrite(V4, HIGH);// Set  Virtual Pin 4 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}


//FINE PROGRAMMAZIONE PER IFTTT GOOGLE
void setup() {
  Serial.begin(115200);

//CODICE SINRIC
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  
  WiFiMulti.addAP(MySSID, MyWifiPassword);
  Serial.println();
  Serial.print("Connecting to Wifi: ");
  Serial.println(MySSID);
  // Waiting for Wifi connect
  while(WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  if(WiFiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.print("WiFi connected. ");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }

  // server address, port and URL
  webSocket.begin("iot.sinric.com", 80, "/");

  // event handler
  webSocket.onEvent(webSocketEvent);
  webSocket.setAuthorization("apikey", MyApiKey);
  
  // try again every 5000ms if connection has failed
  webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
 
//CODICE SINRIC  


  pinMode(R1, OUTPUT); //D1 is GPIO 5 (..........RELE' IN1) (RELAY BUTTON)
  digitalWrite(R1, HIGH); // Set GPIO 5 HIGH

  
  pinMode(R2, OUTPUT); //D2 is GPIO 4 (..........RELE' IN2) (RELAY BUTTON)
  digitalWrite(R2, HIGH); // Set GPIO 4 HIGH
  
  pinMode(R3, OUTPUT); //D5 is GPIO 14 (..........RELE' IN3) (RELAY BUTTON)
  digitalWrite(R3, HIGH); // Set GPIO 14 HIGH
 
  
  pinMode(R4, OUTPUT); //D6 is GPIO 12 (..........RELE' IN4) (RELAY BUTTON)
  digitalWrite(R4, HIGH); // Set GPIO 12 HIGH

  WiFi.begin(ssid, pass); //per aggiornamenti OTA
  Blynk.config(auth); //per aggiornamenti OTA
  Blynk.connect();; //per aggiornamenti OTA

  ArduinoOTA.setHostname("NodeMCU_LUCI_ZONA_GIORNO");  // per aggiornamenti OTA (nome identificativo dispositivo)
  ArduinoOTA.begin();  //per aggiornamenti OTA
  
  
 Blynk.begin(auth, ssid, pass);
  

} 
void loop() {
  timer.run();
  ArduinoOTA.handle();  //per aggiornamenti OTA
  Blynk.run();
//CODICE SINRIC 
  webSocket.loop();
  
  if(isConnected) {
      uint64_t now = millis();
      
      // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
      if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
          heartbeatTimestamp = now;
          webSocket.sendTXT("H"); 
      }
  }      
 //CODICE SINRIC  
}
1 Like

te funciono?