// 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 == "5df78edf341b506fe7a307ee") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(device1, HIGH);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "5df78edf341b506fe7a307ee") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(device1, LOW);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
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
// 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");
}
what is the issue , how to make it work , the relay lights glow when it is first switched on , later no response is there when i ask alexa to turn it on via voice command.
hi
i have followed your example , my serial monitor shows proper output , but there is no action on the relay.
my relay pins are connected to the esp as shown in the picture
code used is as below
int device1 = 5; int device2 = 4; int device3 = 0; int device4 = 2;
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
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
define MyApiKey "281eac9f-f527-4e4f-b541-f9f7712a00ba" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
define MySSID "Axxxx" // TODO: Change to your Wifi network SSID
define MyWifiPassword "axxxx" // 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 == "5df78edf341b506fe7a307ee") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device1, HIGH); } else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}
void turnOff(String deviceId) { if (deviceId == "5df78edf341b506fe7a307ee") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device1, LOW); } else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); } 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
if ARDUINOJSON_VERSION_MAJOR == 5
endif
if ARDUINOJSON_VERSION_MAJOR == 6
endif
} }
void setup() { Serial.begin(74880);
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();
}
}
// If you want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino
what is the issue , how to make it work , the relay lights glow when it is first switched on , later no response is there when i ask alexa to turn it on via voice command.