kakopappa / sinric

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

Sinric not working with google home / google home switch #334

Open Ritesh7379 opened 4 years ago

Ritesh7379 commented 4 years ago

when i give command through google mini home / google home dashboard switch the ### relay connected with esp 8266 is not responding and below massage display in esp 8266 serial monitor but relay is not responding

..... WiFi connected. IP address: 10.0.0.18 [WSc] Service connected to sinric.com at url: / Waiting for commands from sinric.com ...

[WSc] Service connected to sinric.com at url: {"deviceId":"5d74d46bf1dce8012dfc5d33","action":"action.devices.commands.OnOff","value":{"on":false}}

relay is not responding

Waiting for commands from sinric.com ... [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d46bf1dce8012dfc5d33","action":"action.devices.commands.OnOff","value":{"on":true}}

relay is not responding

Waiting for commands from sinric.com ... [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d433f1dce8012dfc5d22","action":"action.devices.commands.OnOff","value":{"on":false}}

relay is not responding

Waiting for commands from sinric.com ... [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d433f1dce8012dfc5d22","action":"action.devices.commands.OnOff","value":{"on":true}}

relay is not responding

Waiting for commands from sinric.com ...

but when i touch sinric dash board switch button for on/off message display

[WSc] Service connected to sinric.com at url: {"deviceId":"5d74d433f1dce8012dfc5d22","action":"setPowerState","value":"OFF"}

relay is responding

Waiting for commands from sinric.com ... Turn off Device ID: 5d74d433f1dce8012dfc5d22 [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d433f1dce8012dfc5d22","action":"setPowerState","value":"ON"}

relay is responding

Waiting for commands from sinric.com ... Turn on device id: 5d74d433f1dce8012dfc5d22 [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d46bf1dce8012dfc5d33","action":"setPowerState","value":"OFF"}

relay is responding

Waiting for commands from sinric.com ... Turn off Device ID: 5d74d46bf1dce8012dfc5d33 [WSc] Service connected to sinric.com at url: {"deviceId":"5d74d46bf1dce8012dfc5d33","action":"setPowerState","value":"ON"}

relay is responding

Waiting for commands from sinric.com ... Turn on device id: 5d74d46bf1dce8012dfc5d33

relay is responding

please help me to resolve the same.

regards Ritesh

kakopappa commented 4 years ago

copy and paste the full code without the API Key

Ritesh7379 commented 4 years ago

Hi, thanks for support, i am using below code. same is working with Alexa but through Google home mini i am not able to operate any relay.

include

include

include

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)

include

int device_1=16; int device_2=5;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "xxxxxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard

define MySSID ".xxxxxx." // TODO: Change to your Wifi network SSID

define MyWifiPassword "xxxxxxxxxxxxx" // TODO: Change to your Wifi network password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false; // 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 == "5d74d46bf1dce8012dfc5d33") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId);

digitalWrite(device_1,LOW);

} else if (deviceId == "5d74d433f1dce8012dfc5d22") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2,LOW); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}

void turnOff(String deviceId) { if (deviceId == "5d74d46bf1dce8012dfc5d33") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1,HIGH); } else if (deviceId == "5d74d433f1dce8012dfc5d22") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2,HIGH); }

}

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] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n"); // Example payloads

    // For Switch or Light device types

// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff // {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}}

    // 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 == "SetTargetTemperature") {
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
        String value = json ["value"];

        if(action == "action.devices.commands.OnOff") { // Switch 

String value = json ["value"]["on"]; Serial.println(value);

if(value == "true") { 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;

} }

void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); pinMode(device_2, OUTPUT);

digitalWrite(device_1,HIGH); // Make it low if you want everything to go off digitalWrite(device_2,HIGH); // in case of a power cut

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 }

void loop() { 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");          
  }

}
}

kakopappa commented 4 years ago

There is a problem with if-else blocks it should be in the same level

  if (action == "setPowerState") { // Switch or Light
     ....
  }
  else if (action == "action.devices.commands.OnOff") { // Switch
      String value = json["value"]["on"];
      Serial.println(value);

      if (value == "true") {
        turnOn(deviceId);
      } else {
        turnOff(deviceId);
      }
  }
  else if (action == "SetTargetTemperature") {
     ....

  } else if (action == "test") {
    Serial.println("[WSc] received test command from sinric.com");
  }

On Thu, Sep 12, 2019 at 11:56 PM Ritesh7379 notifications@github.com wrote:

/ Version 0.4 - April 26 2019 /

include

include

include

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)

include

int device_1=16; int device_2=5; int device_3=4; int device_4=0;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "xxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API

Key. Your API Key is displayed on sinric.com dashboard

define MySSID ".xxxxxx" // TODO: Change to your Wifi network SSID

define MyWifiPassword "xxxxxxxxxxx" // TODO: Change to your Wifi network

password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

// 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 == "5xxxxxxxxxxxxxxxxxuu") // Device ID of first device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_1,LOW); } else if (deviceId == "55xxxxxxxxxxxxxxxxxyy") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2,LOW); } else if (deviceId == "55xxxxxxxxxxxxxxxxxii") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_3,LOW); } else if (deviceId == "5xxxxxxxxxxxxxxxxxkk") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_4,LOW); }

else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId); } }

void turnOff(String deviceId) { if (deviceId == "5xxxxxxxxxxxxxxxxxrr") // Device ID of first device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxyyuu") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxuuoruuri") // Device ID of third device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_3,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxuukdhk") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_4,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 == "SetTargetTemperature") {
    String deviceId = json ["deviceId"];
    String action = json ["action"];
    String value = json ["value"];
}
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;

} }

void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); pinMode(device_2, OUTPUT); pinMode(device_3, OUTPUT); pinMode(device_4, OUTPUT);

digitalWrite(device_1,HIGH); // Make it low if you want everything to go off digitalWrite(device_2,HIGH); // in case of a power cut digitalWrite(device_3,HIGH); digitalWrite(device_4,HIGH);

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 }

void loop() { 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"); }

} }

// If you want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/334?email_source=notifications&email_token=ABZAZZQCRRYK5GWY5R6WBSTQJJYE3A5CNFSM4IUTC2ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6SRT4Y#issuecomment-530913779, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZAZZTYZZPM7H6L2OU7BYTQJJYE3ANCNFSM4IUTC2ZA .

Ritesh7379 commented 4 years ago

Hi , very very thanks for swift reply, i have uploaded below code and everythings are working fine.Now relays are responding perfectly with Alexa & Google Home mini with voice commands. .....but there is small problem still persist.

when I give voice command to alexa " bulb on" alexa say "okey" and relay repond . this is happen in both condition on/off but, when I give voice command to Google mini home " bulb on" Google mini home say "sorry i didn't get any response" and relay reponds . this is happen in both condition on/off.

Regards

Ritesh

include

include

include

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)

include

int device_1=16; int device_2=5;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "xxxxxxxxxxx" // TODO: Change to your sinric API Key. Your

API Key is displayed on sinric.com dashboard

define MySSID "xxxxxxxx" // TODO: Change to your Wifi network SSID

define MyWifiPassword "xxxxxxxx" // TODO: Change to your Wifi network

password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false; // 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 == "5d74d46bf1dce8012dfc5d33") // Device ID of first device { Serial.print("Turn on device id: "); Serial.println(deviceId);

digitalWrite(device_1,LOW);

} else if (deviceId == "5d74d433f1dce8012dfc5d22") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2,LOW); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId); } }

void turnOff(String deviceId) { if (deviceId == "5d74d46bf1dce8012dfc5d33") // Device ID of first device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1,HIGH); } else if (deviceId == "5d74d433f1dce8012dfc5d22") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2,HIGH); }

}

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] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n"); // Example payloads

    // For Switch or Light device types

// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff // {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}}

    // 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

} else if (action == "action.devices.commands.OnOff") { // Switch String value = json["value"]["on"]; Serial.println(value);

if (value == "true") { turnOn(deviceId); } else { turnOff(deviceId); } } else if (action == "SetTargetTemperature") {

} else if (action == "test") { Serial.println("[WSc] received test command from sinric.com"); }

         if(action == "setPowerState") { // Switch or Light

        String value = json ["value"];

        if(value == "ON") {
            turnOn(deviceId);
        } else {
            turnOff(deviceId);
        }
    }
    else if (action == "SetTargetTemperature") {
        String deviceId = json ["deviceId"];
        String action = json ["action"];
        String value = json ["value"];

        if(action == "action.devices.commands.OnOff") { // Switch

String value = json ["value"]["on"]; Serial.println(value);

if(value == "true") { 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;

} }

void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); pinMode(device_2, OUTPUT);

digitalWrite(device_1,HIGH); // Make it low if you want everything to go off digitalWrite(device_2,HIGH); // in case of a power cut

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 }

void loop() { 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"); } } }

On Fri, Sep 13, 2019 at 7:29 AM Aruna Tennakoon notifications@github.com wrote:

There is a problem with if-else blocks it should be in the same level

if (action == "setPowerState") { // Switch or Light .... } else if (action == "action.devices.commands.OnOff") { // Switch String value = json["value"]["on"]; Serial.println(value);

if (value == "true") { turnOn(deviceId); } else { turnOff(deviceId); } } else if (action == "SetTargetTemperature") { ....

} else if (action == "test") { Serial.println("[WSc] received test command from sinric.com"); }

On Thu, Sep 12, 2019 at 11:56 PM Ritesh7379 notifications@github.com wrote:

/ Version 0.4 - April 26 2019 /

include

include

include

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries

include //

https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)

include

int device_1=16; int device_2=5; int device_3=4; int device_4=0;

ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;

define MyApiKey "xxxxxxxxxxxxxxxxxxx" // TODO: Change to your sinric API

Key. Your API Key is displayed on sinric.com dashboard

define MySSID ".xxxxxx" // TODO: Change to your Wifi network SSID

define MyWifiPassword "xxxxxxxxxxx" // TODO: Change to your Wifi network

password

define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0; bool isConnected = false;

// 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 == "5xxxxxxxxxxxxxxxxxuu") // Device ID of first device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_1,LOW); } else if (deviceId == "55xxxxxxxxxxxxxxxxxyy") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2,LOW); } else if (deviceId == "55xxxxxxxxxxxxxxxxxii") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_3,LOW); } else if (deviceId == "5xxxxxxxxxxxxxxxxxkk") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_4,LOW); }

else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId); } }

void turnOff(String deviceId) { if (deviceId == "5xxxxxxxxxxxxxxxxxrr") // Device ID of first device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxyyuu") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxuuoruuri") // Device ID of third device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_3,HIGH); } else if (deviceId == "5xxxxxxxxxxxxxxxxxuukdhk") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_4,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 == "SetTargetTemperature") { String deviceId = json ["deviceId"]; String action = json ["action"]; String value = json ["value"]; } 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;

} }

void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); pinMode(device_2, OUTPUT); pinMode(device_3, OUTPUT); pinMode(device_4, OUTPUT);

digitalWrite(device_1,HIGH); // Make it low if you want everything to go off digitalWrite(device_2,HIGH); // in case of a power cut digitalWrite(device_3,HIGH); digitalWrite(device_4,HIGH);

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 }

void loop() { 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"); }

} }

// If you want a push button:

https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/kakopappa/sinric/issues/334?email_source=notifications&email_token=ABZAZZQCRRYK5GWY5R6WBSTQJJYE3A5CNFSM4IUTC2ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6SRT4Y#issuecomment-530913779 , or mute the thread < https://github.com/notifications/unsubscribe-auth/ABZAZZTYZZPM7H6L2OU7BYTQJJYE3ANCNFSM4IUTC2ZA

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/334?email_source=notifications&email_token=AND7HG24QXD5ERJRXGNMBG3QJLXZTA5CNFSM4IUTC2ZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6TX6EA#issuecomment-531070736, or mute the thread https://github.com/notifications/unsubscribe-auth/AND7HG3U3VE4OQXJMSKFGJ3QJLXZTANCNFSM4IUTC2ZA .