Open gabrieldalbani4 opened 4 years ago
@gabrieldalbani4, code looks fine.
cannot connect?
@kakopappa, no but through the serial monitor it says it is connected. When I click on or off in the Alexa app the relay doesn't do anything at all. Neither with this code nor blynks code I tried both just to make sure. I also tried a different relay and different ESP8266. SO I am not sure what's wrong.
This is exactly what is happening to me and I have been trying to resolve it for quite some time. The device shows as connected to the WiFi through the serial monitor, also it shows in the Alexa App and the Sinric Pro App but there is no real communication between the Sinric Server and the device. Is there any port forwarding that we need to provision in the router?
I am using the new code combined with my information, it used to work before the new update to us json 6. Did I do something wrong? It upload fine but intended functions don't work.
/ 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 = 03; int device_2 = 01; int device_3 = 16; int device_4 = 05; int device_5 = 04; int device_6 = 14; int device_7 = 12; int device_8 = 13;
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
define MyApiKey "****" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
define MySSID "*****" // 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;
// 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 == "5d530d48ea25201d63c93282") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_1, LOW); } else if (deviceId == "5d530d5bea25201d63c93287") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_2, LOW); } else if (deviceId == "5d530d6dea25201d63c9328f") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_3, LOW); } else if (deviceId == "5d530d93ea25201d63c9329d") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_4, LOW); } else if (deviceId == "5d530ad4ea25201d63c931bf") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_5, LOW); } else if (deviceId == "5d530ae6ea25201d63c931c7") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_6, LOW); } else if (deviceId == "5d530b0dea25201d63c931d3") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_7, LOW); } else if (deviceId == "5d530b83ea25201d63c931fb") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(device_8, LOW); } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}
void turnOff(String deviceId) { if (deviceId == "5d530d48ea25201d63c93282") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_1, HIGH); } else if (deviceId == "5d530d5bea25201d63c93287") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_2, HIGH); } else if (deviceId == "5d530d6dea25201d63c9328f") // Device ID of third device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_3, HIGH); } else if (deviceId == "5d530d93ea25201d63c9329d") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_4, HIGH); } else if (deviceId == "5d530ad4ea25201d63c931bf") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_5, HIGH); } else if (deviceId == "5d530ae6ea25201d63c931c7") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_6, HIGH); } else if (deviceId == "5d530b0dea25201d63c931d3") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_7, HIGH); } else if (deviceId == "5d530b83ea25201d63c931fb") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(device_8, 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
if ARDUINOJSON_VERSION_MAJOR == 5
endif
if ARDUINOJSON_VERSION_MAJOR == 6
endif
} }
void setup() { Serial.begin(115200); pinMode(device_1, OUTPUT); pinMode(device_2, OUTPUT); pinMode(device_3, OUTPUT); pinMode(device_4, OUTPUT); pinMode(device_5, OUTPUT); pinMode(device_6, OUTPUT); pinMode(device_7, OUTPUT); pinMode(device_8, 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 }
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) {
if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject();
endif
if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument root(1024);
endif
root["deviceId"] = deviceId; root["action"] = "setPowerState"; root["value"] = value; StreamString databuf;
if ARDUINOJSON_VERSION_MAJOR == 5
root.printTo(databuf);
endif
if ARDUINOJSON_VERSION_MAJOR == 6
serializeJson(root, databuf);
endif
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) {
if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject();
endif
if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument root(1024);
endif
root["action"] = "SetTargetTemperature"; root["deviceId"] = deviceId;
if ARDUINOJSON_VERSION_MAJOR == 5
JsonObject& valueObj = root.createNestedObject("value"); JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint");
endif
if ARDUINOJSON_VERSION_MAJOR == 6
JsonObject valueObj = root.createNestedObject("value"); JsonObject targetSetpoint = valueObj.createNestedObject("targetSetpoint");
endif
targetSetpoint["value"] = value; targetSetpoint["scale"] = scale;
StreamString databuf;
if ARDUINOJSON_VERSION_MAJOR == 5
root.printTo(databuf);
endif
if ARDUINOJSON_VERSION_MAJOR == 6
serializeJson(root, databuf);
endif
webSocket.sendTXT(databuf); }