sinric
sinric copied to clipboard
Webservice disconnected from sinric.com! Something wrong on my code
/* Version 0.1 - March 17 2018 */
#include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries #include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries #include <StreamString.h>
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
#define MyApiKey "myse" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard #define MySSID "myse" // TODO: Change to your Wifi network SSID #define MyWifiPassword "myse" // TODO: Change to your Wifi network password
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void turnOn(String deviceId) {
if (deviceId == "myse") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else if (deviceId == "myse") // 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 == "myse") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else if (deviceId == "myse") // 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 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}}
#if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject((char*)payload);
#endif
#if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument json(1024);
deserializeJson(json, (char*) payload);
#endif
String deviceId = json ["deviceId"];
String action = json ["action"];
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;
default: break;
} }
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, "/"); //"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");
}
}
}
Did I forget to fix anything?
Code looks good. Have you tried connecting via mobile hotspot?
On Thu, 30 Jul 2020 at 8:07 PM Phongsakorn Wisetthon < [email protected]> wrote:
`/* Version 0.1 - March 17 2018 */
#include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries #include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries #include <StreamString.h>
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
#define MyApiKey "myse" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard #define MySSID "myse" // TODO: Change to your Wifi network SSID #define MyWifiPassword "myse" // TODO: Change to your Wifi network password
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void turnOn(String deviceId) { if (deviceId == "myse") // Device ID of first device { Serial.print("Turn on device id: "); Serial.println(deviceId); } else if (deviceId == "myse") // 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 == "myse") // Device ID of first device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); } else if (deviceId == "myse") // 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 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}}#if ARDUINOJSON_VERSION_MAJOR == 5 DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject((char*)payload); #endif #if ARDUINOJSON_VERSION_MAJOR == 6 DynamicJsonDocument json(1024); deserializeJson(json, (char*) payload); #endif String deviceId = json ["deviceId"]; String action = json ["action"];
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; default: break;
} }
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, "/"); //"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"); }
} }`
Did I forget to fix anything?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/434, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZT73227PPNO2DIIA2DR6FWALANCNFSM4PNWUG6Q .
Code looks good. Have you tried connecting via mobile hotspot?
code is work via mobile hotspot how to fix it 😅🤣
I have AIS Fiber at home. Works fine.
On Thu, 30 Jul 2020 at 9:11 PM Phongsakorn Wisetthon < [email protected]> wrote:
Code looks good. Have you tried connecting via mobile hotspot?
code is work via mobile hotspot how to fix it 😅🤣
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/434#issuecomment-666388510, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZQRNJD25HCN7JTCA73R6F5QJANCNFSM4PNWUG6Q .
I have 3BB at my apartment. But Not Works T_T