Hello, I have an esp8266 connected with 4 relays and I am controlling it with Alexa and Sinric everything working fine with voice commands also but whenever I disconnect the power supply of Nodemcu and then connect it back all four relays turn on automatically.
I am very new to this stuff please help me. Attaching my code here.
// 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 == "5e85fc13ce60582b5f888cc9") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(0, LOW);
}
else if (deviceId == "5e85fc27ce60582b5f888ce3") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(2, LOW);
}
else if (deviceId == "5e85fc30ce60582b5f888cf1") // Device ID of third device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(14, LOW);
}
else if (deviceId == "5e85fc41ce60582b5f888d0b") // Device ID of forth device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(12, LOW);
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "5e85fc13ce60582b5f888cc9") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(0, HIGH);
}
else if (deviceId == "5e85fc27ce60582b5f888ce3") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(2, HIGH);
}
else if (deviceId == "5e85fc30ce60582b5f888cf1") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(14, HIGH);
}
else if (deviceId == "5e85fc41ce60582b5f888d0b") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(12, 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);
WiFiMulti.addAP(MySSID, MyWifiPassword);
Serial.println();
Serial.print("Connecting to Wifi: ");
Serial.println(MySSID);
// try again every 5000ms if connection has failed
webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(14, OUTPUT);
pinMode(12, OUTPUT);
}
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 are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.
// eg: setPowerStateOnServer("deviceid", "ON")
// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
Hello, I have an esp8266 connected with 4 relays and I am controlling it with Alexa and Sinric everything working fine with voice commands also but whenever I disconnect the power supply of Nodemcu and then connect it back all four relays turn on automatically. I am very new to this stuff please help me. Attaching my code here.
Version 0.3 - March 06 2018 Grensom Version */
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
include
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
///////////WiFi Setup/////////////
define MyApiKey "............." // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
define MySSID "7........." // TODO: Change to your Wifi network SSID
define MyWifiPassword "..........." // TODO: Change to your Wifi network password
/////////////////////////////////
define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void setPowerStateOnServer(String deviceId, String value); void setTargetTemperatureOnServer(String deviceId, String value, String scale);
// 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 == "5e85fc13ce60582b5f888cc9") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(0, LOW); } else if (deviceId == "5e85fc27ce60582b5f888ce3") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(2, LOW); } else if (deviceId == "5e85fc30ce60582b5f888cf1") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(14, LOW); } else if (deviceId == "5e85fc41ce60582b5f888d0b") // Device ID of forth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(12, LOW); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}
void turnOff(String deviceId) { if (deviceId == "5e85fc13ce60582b5f888cc9") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(0, HIGH); } else if (deviceId == "5e85fc27ce60582b5f888ce3") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(2, HIGH); } else if (deviceId == "5e85fc30ce60582b5f888cf1") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(14, HIGH); } else if (deviceId == "5e85fc41ce60582b5f888d0b") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(12, 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
} }
void setup() { Serial.begin(115200);
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 pinMode(0, OUTPUT); pinMode(2, OUTPUT); pinMode(14, OUTPUT); pinMode(12, OUTPUT); }
void loop() { webSocket.loop();
if(isConnected) { uint64_t now = millis();
}
}
// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server // so it will reflect on Alexa app. // eg: setPowerStateOnServer("deviceid", "ON")
// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
void setPowerStateOnServer(String deviceId, String value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["deviceId"] = deviceId; root["action"] = "setPowerState"; root["value"] = value; StreamString databuf; root.printTo(databuf);
webSocket.sendTXT(databuf); }
//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0")
// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.
void setTargetTemperatureOnServer(String deviceId, String value, String scale) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["action"] = "SetTargetTemperature"; root["deviceId"] = deviceId;
JsonObject& valueObj = root.createNestedObject("value"); JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint"); targetSetpoint["value"] = value; targetSetpoint["scale"] = scale;
StreamString databuf; root.printTo(databuf);
webSocket.sendTXT(databuf); }